1515The tool also writes marshalled versions of the mapping tables to the
1616same location (with .mapping extension).
1717
18- Written by Marc-Andre Lemburg (mal@lemburg.com). Modified to generate
19- Unicode table maps for decoding.
18+ Written by Marc-Andre Lemburg (mal@lemburg.com).
2019
2120(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
2221(c) Copyright Guido van Rossum, 2000.
22+
23+ Table generation:
2324(c) Copyright Marc-Andre Lemburg, 2005.
25+ Licensed to PSF under a Contributor Agreement.
2426
2527""" #"
2628
@@ -117,21 +119,22 @@ def readmap(filename):
117119
118120 return enc2uni
119121
120- def hexrepr (t ):
122+ def hexrepr (t , precision = 4 ):
121123
122124 if t is None :
123125 return 'None'
124126 try :
125127 len (t )
126128 except :
127- return '0x%04x ' % t
129+ return '0x%0*X ' % ( precision , t )
128130 try :
129- return '(' + ', ' .join (map (lambda t : '0x%04x' % t , t )) + ')'
131+ return '(' + ', ' .join (['0x%0*X' % (precision , item )
132+ for item in t ]) + ')'
130133 except TypeError , why :
131134 print '* failed to convert %r: %s' % (t , why )
132135 raise
133136
134- def python_mapdef_code (varname , map , comments = 1 ):
137+ def python_mapdef_code (varname , map , comments = 1 , precisions = ( 2 , 4 ) ):
135138
136139 l = []
137140 append = l .append
@@ -150,6 +153,7 @@ def python_mapdef_code(varname, map, comments=1):
150153 mappings = map .items ()
151154 mappings .sort ()
152155 i = 0
156+ key_precision , value_precision = precisions
153157 for mapkey , mapvalue in mappings :
154158 mapcomment = ''
155159 if isinstance (mapkey , tuple ):
@@ -164,8 +168,8 @@ def python_mapdef_code(varname, map, comments=1):
164168 # No need to include identity mappings, since these
165169 # are already set for the first 256 code points.
166170 continue
167- key = hexrepr (mapkey )
168- value = hexrepr (mapvalue )
171+ key = hexrepr (mapkey , key_precision )
172+ value = hexrepr (mapvalue , value_precision )
169173 if mapcomment and comments :
170174 append (' %s: %s,\t # %s' % (key , value , mapcomment ))
171175 else :
@@ -188,7 +192,7 @@ def python_mapdef_code(varname, map, comments=1):
188192
189193 return l
190194
191- def python_tabledef_code (varname , map , comments = 1 ):
195+ def python_tabledef_code (varname , map , comments = 1 , key_precision = 2 ):
192196
193197 l = []
194198 append = l .append
@@ -236,7 +240,7 @@ def python_tabledef_code(varname, map, comments=1):
236240 mapchar = unichr (mapvalue )
237241 if mapcomment and comments :
238242 append (' %r\t # %s -> %s' % (mapchar ,
239- hexrepr (key ),
243+ hexrepr (key , key_precision ),
240244 mapcomment ))
241245 else :
242246 append (' %r' % mapchar )
@@ -263,7 +267,8 @@ def codegen(name, map, comments=1):
263267 encoding_map_code = python_mapdef_code (
264268 'encoding_map' ,
265269 codecs .make_encoding_map (map ),
266- comments = comments )
270+ comments = comments ,
271+ precisions = (4 , 2 ))
267272
268273 l = [
269274 '''\
@@ -303,22 +308,28 @@ class StreamReader(Codec,codecs.StreamReader):
303308def getregentry():
304309
305310 return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
311+ ''' )
306312
313+ # Add decoding table or map (with preference to the table)
314+ if not decoding_table_code :
315+ l .append ('''
307316### Decoding Map
308317''' )
309- l .extend (decoding_map_code )
310-
311- # Add optional decoding table
312- if decoding_table_code :
318+ l .extend (decoding_map_code )
319+ else :
313320 l .append ('''
314321### Decoding Table
315322''' )
316323 l .extend (decoding_table_code )
317324
325+ # Add encoding map
318326 l .append ('''
319327### Encoding Map
320328''' )
321329 l .extend (encoding_map_code )
330+
331+ # Final new-line
332+ l .append ('\n ' )
322333
323334 return '\n ' .join (l )
324335
@@ -343,6 +354,8 @@ def convertdir(dir,prefix='',comments=1):
343354 mapnames = os .listdir (dir )
344355 for mapname in mapnames :
345356 mappathname = os .path .join (dir , mapname )
357+ if not os .path .isfile (mappathname ):
358+ continue
346359 name = os .path .split (mapname )[1 ]
347360 name = name .replace ('-' ,'_' )
348361 name = name .split ('.' )[0 ]
0 commit comments