1 | n/a | import os, string |
---|
2 | n/a | |
---|
3 | n/a | codecs = { |
---|
4 | n/a | 'cn': ('gb2312', 'gbk', 'gb18030', 'hz'), |
---|
5 | n/a | 'tw': ('big5', 'cp950'), |
---|
6 | n/a | 'hk': ('big5hkscs',), |
---|
7 | n/a | 'jp': ('cp932', 'shift_jis', 'euc_jp', 'euc_jisx0213', 'shift_jisx0213', |
---|
8 | n/a | 'euc_jis_2004', 'shift_jis_2004'), |
---|
9 | n/a | 'kr': ('cp949', 'euc_kr', 'johab'), |
---|
10 | n/a | 'iso2022': ('iso2022_jp', 'iso2022_jp_1', 'iso2022_jp_2', |
---|
11 | n/a | 'iso2022_jp_2004', 'iso2022_jp_3', 'iso2022_jp_ext', |
---|
12 | n/a | 'iso2022_kr'), |
---|
13 | n/a | } |
---|
14 | n/a | |
---|
15 | n/a | TEMPLATE = string.Template("""\ |
---|
16 | n/a | # |
---|
17 | n/a | # $encoding.py: Python Unicode Codec for $ENCODING |
---|
18 | n/a | # |
---|
19 | n/a | # Written by Hye-Shik Chang <perky@FreeBSD.org> |
---|
20 | n/a | # |
---|
21 | n/a | |
---|
22 | n/a | import _codecs_$owner, codecs |
---|
23 | n/a | import _multibytecodec as mbc |
---|
24 | n/a | |
---|
25 | n/a | codec = _codecs_$owner.getcodec('$encoding') |
---|
26 | n/a | |
---|
27 | n/a | class Codec(codecs.Codec): |
---|
28 | n/a | encode = codec.encode |
---|
29 | n/a | decode = codec.decode |
---|
30 | n/a | |
---|
31 | n/a | class IncrementalEncoder(mbc.MultibyteIncrementalEncoder, |
---|
32 | n/a | codecs.IncrementalEncoder): |
---|
33 | n/a | codec = codec |
---|
34 | n/a | |
---|
35 | n/a | class IncrementalDecoder(mbc.MultibyteIncrementalDecoder, |
---|
36 | n/a | codecs.IncrementalDecoder): |
---|
37 | n/a | codec = codec |
---|
38 | n/a | |
---|
39 | n/a | class StreamReader(Codec, mbc.MultibyteStreamReader, codecs.StreamReader): |
---|
40 | n/a | codec = codec |
---|
41 | n/a | |
---|
42 | n/a | class StreamWriter(Codec, mbc.MultibyteStreamWriter, codecs.StreamWriter): |
---|
43 | n/a | codec = codec |
---|
44 | n/a | |
---|
45 | n/a | def getregentry(): |
---|
46 | n/a | return codecs.CodecInfo( |
---|
47 | n/a | name='$encoding', |
---|
48 | n/a | encode=Codec().encode, |
---|
49 | n/a | decode=Codec().decode, |
---|
50 | n/a | incrementalencoder=IncrementalEncoder, |
---|
51 | n/a | incrementaldecoder=IncrementalDecoder, |
---|
52 | n/a | streamreader=StreamReader, |
---|
53 | n/a | streamwriter=StreamWriter, |
---|
54 | n/a | ) |
---|
55 | n/a | """) |
---|
56 | n/a | |
---|
57 | n/a | def gencodecs(prefix): |
---|
58 | n/a | for loc, encodings in codecs.items(): |
---|
59 | n/a | for enc in encodings: |
---|
60 | n/a | code = TEMPLATE.substitute(ENCODING=enc.upper(), |
---|
61 | n/a | encoding=enc.lower(), |
---|
62 | n/a | owner=loc) |
---|
63 | n/a | codecpath = os.path.join(prefix, enc + '.py') |
---|
64 | n/a | open(codecpath, 'w').write(code) |
---|
65 | n/a | |
---|
66 | n/a | if __name__ == '__main__': |
---|
67 | n/a | import sys |
---|
68 | n/a | gencodecs(sys.argv[1]) |
---|