Apply some cosmetic fixes to the output of the script.

Only include the decoding map if no table can be generated.
This commit is contained in:
Marc-André Lemburg 2005-10-25 11:53:33 +00:00
parent 982e8d671c
commit bd20ea55bc

View file

@ -15,12 +15,14 @@ lowercase with hyphens replaced by underscores.
The tool also writes marshalled versions of the mapping tables to the The tool also writes marshalled versions of the mapping tables to the
same location (with .mapping extension). same location (with .mapping extension).
Written by Marc-Andre Lemburg (mal@lemburg.com). Modified to generate Written by Marc-Andre Lemburg (mal@lemburg.com).
Unicode table maps for decoding.
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
(c) Copyright Guido van Rossum, 2000. (c) Copyright Guido van Rossum, 2000.
Table generation:
(c) Copyright Marc-Andre Lemburg, 2005. (c) Copyright Marc-Andre Lemburg, 2005.
Licensed to PSF under a Contributor Agreement.
"""#" """#"
@ -117,21 +119,22 @@ def readmap(filename):
return enc2uni return enc2uni
def hexrepr(t): def hexrepr(t, precision=4):
if t is None: if t is None:
return 'None' return 'None'
try: try:
len(t) len(t)
except: except:
return '0x%04x' % t return '0x%0*X' % (precision, t)
try: try:
return '(' + ', '.join(map(lambda t: '0x%04x' % t, t)) + ')' return '(' + ', '.join(['0x%0*X' % (precision, item)
for item in t]) + ')'
except TypeError, why: except TypeError, why:
print '* failed to convert %r: %s' % (t, why) print '* failed to convert %r: %s' % (t, why)
raise raise
def python_mapdef_code(varname, map, comments=1): def python_mapdef_code(varname, map, comments=1, precisions=(2, 4)):
l = [] l = []
append = l.append append = l.append
@ -150,6 +153,7 @@ def python_mapdef_code(varname, map, comments=1):
mappings = map.items() mappings = map.items()
mappings.sort() mappings.sort()
i = 0 i = 0
key_precision, value_precision = precisions
for mapkey, mapvalue in mappings: for mapkey, mapvalue in mappings:
mapcomment = '' mapcomment = ''
if isinstance(mapkey, tuple): if isinstance(mapkey, tuple):
@ -164,8 +168,8 @@ def python_mapdef_code(varname, map, comments=1):
# No need to include identity mappings, since these # No need to include identity mappings, since these
# are already set for the first 256 code points. # are already set for the first 256 code points.
continue continue
key = hexrepr(mapkey) key = hexrepr(mapkey, key_precision)
value = hexrepr(mapvalue) value = hexrepr(mapvalue, value_precision)
if mapcomment and comments: if mapcomment and comments:
append(' %s: %s,\t# %s' % (key, value, mapcomment)) append(' %s: %s,\t# %s' % (key, value, mapcomment))
else: else:
@ -188,7 +192,7 @@ def python_mapdef_code(varname, map, comments=1):
return l return l
def python_tabledef_code(varname, map, comments=1): def python_tabledef_code(varname, map, comments=1, key_precision=2):
l = [] l = []
append = l.append append = l.append
@ -236,7 +240,7 @@ def python_tabledef_code(varname, map, comments=1):
mapchar = unichr(mapvalue) mapchar = unichr(mapvalue)
if mapcomment and comments: if mapcomment and comments:
append(' %r\t# %s -> %s' % (mapchar, append(' %r\t# %s -> %s' % (mapchar,
hexrepr(key), hexrepr(key, key_precision),
mapcomment)) mapcomment))
else: else:
append(' %r' % mapchar) append(' %r' % mapchar)
@ -263,7 +267,8 @@ def codegen(name, map, comments=1):
encoding_map_code = python_mapdef_code( encoding_map_code = python_mapdef_code(
'encoding_map', 'encoding_map',
codecs.make_encoding_map(map), codecs.make_encoding_map(map),
comments=comments) comments=comments,
precisions=(4, 2))
l = [ l = [
'''\ '''\
@ -303,23 +308,29 @@ class StreamReader(Codec,codecs.StreamReader):
def getregentry(): def getregentry():
return (Codec().encode,Codec().decode,StreamReader,StreamWriter) return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
''')
# Add decoding table or map (with preference to the table)
if not decoding_table_code:
l.append('''
### Decoding Map ### Decoding Map
''') ''')
l.extend(decoding_map_code) l.extend(decoding_map_code)
else:
# Add optional decoding table
if decoding_table_code:
l.append(''' l.append('''
### Decoding Table ### Decoding Table
''') ''')
l.extend(decoding_table_code) l.extend(decoding_table_code)
# Add encoding map
l.append(''' l.append('''
### Encoding Map ### Encoding Map
''') ''')
l.extend(encoding_map_code) l.extend(encoding_map_code)
# Final new-line
l.append('\n')
return '\n'.join(l) return '\n'.join(l)
def pymap(name,map,pyfile,comments=1): def pymap(name,map,pyfile,comments=1):
@ -343,6 +354,8 @@ def convertdir(dir,prefix='',comments=1):
mapnames = os.listdir(dir) mapnames = os.listdir(dir)
for mapname in mapnames: for mapname in mapnames:
mappathname = os.path.join(dir, mapname) mappathname = os.path.join(dir, mapname)
if not os.path.isfile(mappathname):
continue
name = os.path.split(mapname)[1] name = os.path.split(mapname)[1]
name = name.replace('-','_') name = name.replace('-','_')
name = name.split('.')[0] name = name.split('.')[0]