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