1import os, string 2 3codecs = { 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 15TEMPLATE = string.Template("""\ 16# 17# $encoding.py: Python Unicode Codec for $ENCODING 18# 19# Written by Hye-Shik Chang <perky@FreeBSD.org> 20# 21 22import _codecs_$owner, codecs 23import _multibytecodec as mbc 24 25codec = _codecs_$owner.getcodec('$encoding') 26 27class Codec(codecs.Codec): 28 encode = codec.encode 29 decode = codec.decode 30 31class IncrementalEncoder(mbc.MultibyteIncrementalEncoder, 32 codecs.IncrementalEncoder): 33 codec = codec 34 35class IncrementalDecoder(mbc.MultibyteIncrementalDecoder, 36 codecs.IncrementalDecoder): 37 codec = codec 38 39class StreamReader(Codec, mbc.MultibyteStreamReader, codecs.StreamReader): 40 codec = codec 41 42class StreamWriter(Codec, mbc.MultibyteStreamWriter, codecs.StreamWriter): 43 codec = codec 44 45def 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 57def gencodecs(prefix): 58 for loc, encodings in codecs.items(): 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 with open(codecpath, 'w') as f: 65 f.write(code) 66 67if __name__ == '__main__': 68 import sys 69 gencodecs(sys.argv[1]) 70