""" Unicode Mapping Parser and Codec Generator.This script parses Unicode mapping files as available from the Unicodesite (ftp://ftp.unicode.org/Public/MAPPINGS/) and creates Python codecmodules from them. The codecs use the standard character mapping codecto actually apply the mapping.Synopsis: gencodec.py dir codec_prefixAll files in dir are scanned and those producing non-empty mappingswill be written to <codec_prefix><mapname>.py with <mapname> being thefirst part of the map's filename ('a' in a.b.c.txt) converted tolowercase with hyphens replaced by underscores.The tool also writes marshalled versions of the mapping tables to thesame location (with .mapping extension).Written by Marc-Andre Lemburg (mal@lemburg.com).(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.(c) Copyright Guido van Rossum, 2000.Table generation:(c) Copyright Marc-Andre Lemburg, 2005.Licensed to PSF under a Contributor Agreement."""#"import re, os, marshal, codecs# Maximum allowed size of charmap tablesMAX_TABLE_SIZE = 8192# Standard undefined Unicode code pointUNI_UNDEFINED = chr(0xFFFE)# Placeholder for a missing code pointMISSING_CODE = -1mapRE = re.compile(r'((?:0x[0-9a-fA-F]+\+?)+)'r'\s+'r'((?:(?:0x[0-9a-fA-Z]+|<[A-Za-z]+>)\+?)*)'r'\s*'r'(#.+)?')def parsecodes(codes, len=len, range=range):""" Converts code combinations to either a single code integeror a tuple of integers.meta-codes (in angular brackets, e.g. <LR> and <RL>) areignored.Empty codes or illegal ones are returned as None."""if not codes:return MISSING_CODEl = codes.split('+')if len(l) == 1:return int(l[0],16)for i in range(len(l)):try:l[i] = int(l[i],16)except ValueError:l[i] = MISSING_CODEl = [x for x in l if x != MISSING_CODE]if len(l) == 1:return l[0]else:return tuple(l)def readmap(filename):with open(filename) as f:lines = f.readlines()enc2uni = {}identity = []unmapped = list(range(256))# UTC mapping tables per convention don't include the identity# mappings for code points 0x00 - 0x1F and 0x7F, unless these are# explicitly mapped to different characters or undefinedfor i in list(range(32)) + [127]:identity.append(i)unmapped.remove(i)enc2uni[i] = (i, 'CONTROL CHARACTER')for line in lines:line = line.strip()if not line or line[0] == '#':continuem = mapRE.match(line)if not m:#print '* not matched: %s' % repr(line)continueenc,uni,comment = m.groups()enc = parsecodes(enc)uni = parsecodes(uni)if comment is None:comment = ''else:comment = comment[1:].strip()if not isinstance(enc, tuple) and enc < 256:if enc in unmapped:unmapped.remove(enc)if enc == uni:identity.append(enc)enc2uni[enc] = (uni,comment)else:enc2uni[enc] = (uni,comment)# If there are more identity-mapped entries than unmapped entries,# it pays to generate an identity dictionary first, and add explicit# mappings to None for the restif len(identity) >= len(unmapped):for enc in unmapped:enc2uni[enc] = (MISSING_CODE, "")enc2uni['IDENTITY'] = 256return enc2unidef hexrepr(t, precision=4):if t is None:return 'None'try:len(t)except TypeError:return '0x%0*X' % (precision, t)try:return '(' + ', '.join(['0x%0*X' % (precision, item)for item in t]) + ')'except TypeError as why:print('* failed to convert %r: %s' % (t, why))raisedef python_mapdef_code(varname, map, comments=1, precisions=(2, 4)):l = []append = l.appendif "IDENTITY" in map:append("%s = codecs.make_identity_dict(range(%d))" %(varname, map["IDENTITY"]))append("%s.update({" % varname)splits = 1del map["IDENTITY"]identity = 1else:append("%s = {" % varname)splits = 0identity = 0mappings = sorted(map.items())i = 0key_precision, value_precision = precisionsfor mapkey, mapvalue in mappings:mapcomment = ''if isinstance(mapkey, tuple):(mapkey, mapcomment) = mapkeyif isinstance(mapvalue, tuple):(mapvalue, mapcomment) = mapvalueif mapkey is None:continueif (identity andmapkey == mapvalue andmapkey < 256):# No need to include identity mappings, since these# are already set for the first 256 code points.continuekey = hexrepr(mapkey, key_precision)value = hexrepr(mapvalue, value_precision)if mapcomment and comments:append(' %s: %s,\t# %s' % (key, value, mapcomment))else:append(' %s: %s,' % (key, value))i += 1if i == 4096:# Split the definition into parts to that the Python# parser doesn't dump coreif splits == 0:append('}')else:append('})')append('%s.update({' % varname)i = 0splits = splits + 1if splits == 0:append('}')else:append('})')return ldef python_tabledef_code(varname, map, comments=1, key_precision=2):l = []append = l.appendappend('%s = (' % varname)# Analyze map and create table dictmappings = sorted(map.items())table = {}maxkey = 255if 'IDENTITY' in map:for key in range(256):table[key] = (key, '')del map['IDENTITY']for mapkey, mapvalue in mappings:mapcomment = ''if isinstance(mapkey, tuple):(mapkey, mapcomment) = mapkeyif isinstance(mapvalue, tuple):(mapvalue, mapcomment) = mapvalueif mapkey == MISSING_CODE:continuetable[mapkey] = (mapvalue, mapcomment)if mapkey > maxkey:maxkey = mapkeyif maxkey > MAX_TABLE_SIZE:# Table too largereturn None# Create table codemaxchar = 0for key in range(maxkey + 1):if key not in table:mapvalue = MISSING_CODEmapcomment = 'UNDEFINED'else:mapvalue, mapcomment = table[key]if mapvalue == MISSING_CODE:mapchar = UNI_UNDEFINEDelse:if isinstance(mapvalue, tuple):# 1-n mappings not supportedreturn Noneelse:mapchar = chr(mapvalue)maxchar = max(maxchar, ord(mapchar))if mapcomment and comments:append(' %a \t# %s -> %s' % (mapchar,hexrepr(key, key_precision),mapcomment))else:append(' %a' % mapchar)if maxchar < 256:append(' %a \t## Widen to UCS2 for optimization' % UNI_UNDEFINED)append(')')return ldef codegen(name, map, encodingname, comments=1):""" Returns Python source for the given map.Comments are included in the source, if comments is true (default)."""# Generate codedecoding_map_code = python_mapdef_code('decoding_map',map,comments=comments)decoding_table_code = python_tabledef_code('decoding_table',map,comments=comments)encoding_map_code = python_mapdef_code('encoding_map',codecs.make_encoding_map(map),comments=comments,precisions=(4, 2))if decoding_table_code:suffix = 'table'else:suffix = 'map'l = ['''\""" Python Character Mapping Codec %s generated from '%s' with gencodec.py."""#"import codecs### Codec APIsclass Codec(codecs.Codec):def encode(self, input, errors='strict'):return codecs.charmap_encode(input, errors, encoding_%s)def decode(self, input, errors='strict'):return codecs.charmap_decode(input, errors, decoding_%s)''' % (encodingname, name, suffix, suffix)]l.append('''\class IncrementalEncoder(codecs.IncrementalEncoder):def encode(self, input, final=False):return codecs.charmap_encode(input, self.errors, encoding_%s)[0]class IncrementalDecoder(codecs.IncrementalDecoder):def decode(self, input, final=False):return codecs.charmap_decode(input, self.errors, decoding_%s)[0]''' %(suffix, suffix))l.append('''class StreamWriter(Codec, codecs.StreamWriter):passclass StreamReader(Codec, codecs.StreamReader):pass### encodings module APIdef getregentry():return codecs.CodecInfo(name=%r,encode=Codec().encode,decode=Codec().decode,incrementalencoder=IncrementalEncoder,incrementaldecoder=IncrementalDecoder,streamreader=StreamReader,streamwriter=StreamWriter,)''' % encodingname.replace('_', '-'))# Add decoding table or map (with preference to the table)if not decoding_table_code:l.append('''### Decoding Map''')l.extend(decoding_map_code)else:l.append('''### Decoding Table''')l.extend(decoding_table_code)# Add encoding mapif decoding_table_code:l.append('''### Encoding tableencoding_table = codecs.charmap_build(decoding_table)''')else:l.append('''### Encoding Map''')l.extend(encoding_map_code)# Final new-linel.append('')return '\n'.join(l).expandtabs()def pymap(name,map,pyfile,encodingname,comments=1):code = codegen(name,map,encodingname,comments)with open(pyfile,'w') as f:f.write(code)def marshalmap(name,map,marshalfile):d = {}for e,(u,c) in map.items():d[e] = (u,c)with open(marshalfile,'wb') as f:marshal.dump(d,f)def convertdir(dir, dirprefix='', nameprefix='', comments=1):mapnames = os.listdir(dir)for mapname in mapnames:mappathname = os.path.join(dir, mapname)if not os.path.isfile(mappathname):continuename = os.path.split(mapname)[1]name = name.replace('-','_')name = name.split('.')[0]name = name.lower()name = nameprefix + namecodefile = name + '.py'marshalfile = name + '.mapping'print('converting %s to %s and %s' % (mapname,dirprefix + codefile,dirprefix + marshalfile))try:map = readmap(os.path.join(dir,mapname))if not map:print('* map is empty; skipping')else:pymap(mappathname, map, dirprefix + codefile,name,comments)marshalmap(mappathname, map, dirprefix + marshalfile)except ValueError as why:print('* conversion failed: %s' % why)raisedef rewritepythondir(dir, dirprefix='', comments=1):mapnames = os.listdir(dir)for mapname in mapnames:if not mapname.endswith('.mapping'):continuename = mapname[:-len('.mapping')]codefile = name + '.py'print('converting %s to %s' % (mapname,dirprefix + codefile))try:with open(os.path.join(dir, mapname), 'rb') as f:map = marshal.load(f)if not map:print('* map is empty; skipping')else:pymap(mapname, map, dirprefix + codefile,name,comments)except ValueError as why:print('* conversion failed: %s' % why)if __name__ == '__main__':import sysif 1:convertdir(*sys.argv[1:])else:rewritepythondir(*sys.argv[1:])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。