Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 1 | """ Unicode Mapping Parser and Codec Generator. |
| 2 | |
| 3 | This script parses Unicode mapping files as available from the Unicode |
| 4 | site (ftp://ftp.unicode.org/Public/MAPPINGS/) and creates Python codec |
| 5 | modules from them. The codecs use the standard character mapping codec |
| 6 | to actually apply the mapping. |
| 7 | |
| 8 | Synopsis: gencodec.py dir codec_prefix |
| 9 | |
| 10 | All files in dir are scanned and those producing non-empty mappings |
| 11 | will be written to <codec_prefix><mapname>.py with <mapname> being the |
| 12 | first part of the map's filename ('a' in a.b.c.txt) converted to |
| 13 | lowercase with hyphens replaced by underscores. |
| 14 | |
| 15 | The tool also writes marshalled versions of the mapping tables to the |
| 16 | same location (with .mapping extension). |
| 17 | |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 18 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 19 | |
| 20 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 21 | (c) Copyright Guido van Rossum, 2000. |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 22 | |
| 23 | Table generation: |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 24 | (c) Copyright Marc-Andre Lemburg, 2005. |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 25 | Licensed to PSF under a Contributor Agreement. |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 26 | |
| 27 | """#" |
| 28 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 29 | import re, os, marshal, codecs |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 30 | |
| 31 | # Maximum allowed size of charmap tables |
| 32 | MAX_TABLE_SIZE = 8192 |
| 33 | |
| 34 | # Standard undefined Unicode code point |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 35 | UNI_UNDEFINED = chr(0xFFFE) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 36 | |
| 37 | mapRE = re.compile('((?:0x[0-9a-fA-F]+\+?)+)' |
| 38 | '\s+' |
| 39 | '((?:(?:0x[0-9a-fA-Z]+|<[A-Za-z]+>)\+?)*)' |
| 40 | '\s*' |
| 41 | '(#.+)?') |
| 42 | |
| 43 | def parsecodes(codes, |
| 44 | len=len, filter=filter,range=range): |
| 45 | |
| 46 | """ Converts code combinations to either a single code integer |
| 47 | or a tuple of integers. |
| 48 | |
| 49 | meta-codes (in angular brackets, e.g. <LR> and <RL>) are |
| 50 | ignored. |
| 51 | |
| 52 | Empty codes or illegal ones are returned as None. |
| 53 | |
| 54 | """ |
| 55 | if not codes: |
| 56 | return None |
| 57 | l = codes.split('+') |
| 58 | if len(l) == 1: |
| 59 | return int(l[0],16) |
| 60 | for i in range(len(l)): |
| 61 | try: |
| 62 | l[i] = int(l[i],16) |
| 63 | except ValueError: |
| 64 | l[i] = None |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 65 | l = [x for x in l if x is not None] |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 66 | if len(l) == 1: |
| 67 | return l[0] |
| 68 | else: |
| 69 | return tuple(l) |
| 70 | |
| 71 | def readmap(filename): |
| 72 | |
| 73 | f = open(filename,'r') |
| 74 | lines = f.readlines() |
| 75 | f.close() |
| 76 | enc2uni = {} |
| 77 | identity = [] |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 78 | unmapped = list(range(256)) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 79 | |
| 80 | # UTC mapping tables per convention don't include the identity |
| 81 | # mappings for code points 0x00 - 0x1F and 0x7F, unless these are |
| 82 | # explicitly mapped to different characters or undefined |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 83 | for i in list(range(32)) + [127]: |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 84 | identity.append(i) |
| 85 | unmapped.remove(i) |
| 86 | enc2uni[i] = (i, 'CONTROL CHARACTER') |
| 87 | |
| 88 | for line in lines: |
| 89 | line = line.strip() |
| 90 | if not line or line[0] == '#': |
| 91 | continue |
| 92 | m = mapRE.match(line) |
| 93 | if not m: |
| 94 | #print '* not matched: %s' % repr(line) |
| 95 | continue |
| 96 | enc,uni,comment = m.groups() |
| 97 | enc = parsecodes(enc) |
| 98 | uni = parsecodes(uni) |
| 99 | if comment is None: |
| 100 | comment = '' |
| 101 | else: |
| 102 | comment = comment[1:].strip() |
| 103 | if enc < 256: |
| 104 | if enc in unmapped: |
| 105 | unmapped.remove(enc) |
| 106 | if enc == uni: |
| 107 | identity.append(enc) |
| 108 | enc2uni[enc] = (uni,comment) |
| 109 | else: |
| 110 | enc2uni[enc] = (uni,comment) |
| 111 | |
| 112 | # If there are more identity-mapped entries than unmapped entries, |
| 113 | # it pays to generate an identity dictionary first, and add explicit |
| 114 | # mappings to None for the rest |
| 115 | if len(identity) >= len(unmapped): |
| 116 | for enc in unmapped: |
| 117 | enc2uni[enc] = (None, "") |
| 118 | enc2uni['IDENTITY'] = 256 |
| 119 | |
| 120 | return enc2uni |
| 121 | |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 122 | def hexrepr(t, precision=4): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 123 | |
| 124 | if t is None: |
| 125 | return 'None' |
| 126 | try: |
| 127 | len(t) |
| 128 | except: |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 129 | return '0x%0*X' % (precision, t) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 130 | try: |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 131 | return '(' + ', '.join(['0x%0*X' % (precision, item) |
| 132 | for item in t]) + ')' |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 133 | except TypeError as why: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 134 | print('* failed to convert %r: %s' % (t, why)) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 135 | raise |
| 136 | |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 137 | def python_mapdef_code(varname, map, comments=1, precisions=(2, 4)): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 138 | |
| 139 | l = [] |
| 140 | append = l.append |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 141 | if "IDENTITY" in map: |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 142 | append("%s = codecs.make_identity_dict(range(%d))" % |
| 143 | (varname, map["IDENTITY"])) |
| 144 | append("%s.update({" % varname) |
| 145 | splits = 1 |
| 146 | del map["IDENTITY"] |
| 147 | identity = 1 |
| 148 | else: |
| 149 | append("%s = {" % varname) |
| 150 | splits = 0 |
| 151 | identity = 0 |
| 152 | |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 153 | mappings = sorted(map.items()) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 154 | i = 0 |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 155 | key_precision, value_precision = precisions |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 156 | for mapkey, mapvalue in mappings: |
| 157 | mapcomment = '' |
| 158 | if isinstance(mapkey, tuple): |
| 159 | (mapkey, mapcomment) = mapkey |
| 160 | if isinstance(mapvalue, tuple): |
| 161 | (mapvalue, mapcomment) = mapvalue |
| 162 | if mapkey is None: |
| 163 | continue |
| 164 | if (identity and |
| 165 | mapkey == mapvalue and |
| 166 | mapkey < 256): |
| 167 | # No need to include identity mappings, since these |
| 168 | # are already set for the first 256 code points. |
| 169 | continue |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 170 | key = hexrepr(mapkey, key_precision) |
| 171 | value = hexrepr(mapvalue, value_precision) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 172 | if mapcomment and comments: |
| 173 | append(' %s: %s,\t# %s' % (key, value, mapcomment)) |
| 174 | else: |
| 175 | append(' %s: %s,' % (key, value)) |
| 176 | i += 1 |
| 177 | if i == 4096: |
| 178 | # Split the definition into parts to that the Python |
| 179 | # parser doesn't dump core |
| 180 | if splits == 0: |
| 181 | append('}') |
| 182 | else: |
| 183 | append('})') |
| 184 | append('%s.update({' % varname) |
| 185 | i = 0 |
| 186 | splits = splits + 1 |
| 187 | if splits == 0: |
| 188 | append('}') |
| 189 | else: |
| 190 | append('})') |
| 191 | |
| 192 | return l |
| 193 | |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 194 | def python_tabledef_code(varname, map, comments=1, key_precision=2): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 195 | |
| 196 | l = [] |
| 197 | append = l.append |
| 198 | append('%s = (' % varname) |
| 199 | |
| 200 | # Analyze map and create table dict |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 201 | mappings = sorted(map.items()) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 202 | table = {} |
| 203 | maxkey = 0 |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 204 | if 'IDENTITY' in map: |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 205 | for key in range(256): |
| 206 | table[key] = (key, '') |
| 207 | maxkey = 255 |
| 208 | del map['IDENTITY'] |
| 209 | for mapkey, mapvalue in mappings: |
| 210 | mapcomment = '' |
| 211 | if isinstance(mapkey, tuple): |
| 212 | (mapkey, mapcomment) = mapkey |
| 213 | if isinstance(mapvalue, tuple): |
| 214 | (mapvalue, mapcomment) = mapvalue |
| 215 | if mapkey is None: |
| 216 | continue |
| 217 | table[mapkey] = (mapvalue, mapcomment) |
| 218 | if mapkey > maxkey: |
| 219 | maxkey = mapkey |
| 220 | if maxkey > MAX_TABLE_SIZE: |
| 221 | # Table too large |
| 222 | return None |
| 223 | |
| 224 | # Create table code |
| 225 | for key in range(maxkey + 1): |
| 226 | if key not in table: |
| 227 | mapvalue = None |
| 228 | mapcomment = 'UNDEFINED' |
| 229 | else: |
| 230 | mapvalue, mapcomment = table[key] |
| 231 | if mapvalue is None: |
| 232 | mapchar = UNI_UNDEFINED |
| 233 | else: |
| 234 | if isinstance(mapvalue, tuple): |
| 235 | # 1-n mappings not supported |
| 236 | return None |
| 237 | else: |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 238 | mapchar = chr(mapvalue) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 239 | if mapcomment and comments: |
| 240 | append(' %r\t# %s -> %s' % (mapchar, |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 241 | hexrepr(key, key_precision), |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 242 | mapcomment)) |
| 243 | else: |
| 244 | append(' %r' % mapchar) |
| 245 | |
| 246 | append(')') |
| 247 | return l |
| 248 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 249 | def codegen(name, map, encodingname, comments=1): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 250 | |
| 251 | """ Returns Python source for the given map. |
| 252 | |
| 253 | Comments are included in the source, if comments is true (default). |
| 254 | |
| 255 | """ |
| 256 | # Generate code |
| 257 | decoding_map_code = python_mapdef_code( |
| 258 | 'decoding_map', |
| 259 | map, |
| 260 | comments=comments) |
| 261 | decoding_table_code = python_tabledef_code( |
| 262 | 'decoding_table', |
| 263 | map, |
| 264 | comments=comments) |
| 265 | encoding_map_code = python_mapdef_code( |
| 266 | 'encoding_map', |
| 267 | codecs.make_encoding_map(map), |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 268 | comments=comments, |
| 269 | precisions=(4, 2)) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 270 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 271 | if decoding_table_code: |
| 272 | suffix = 'table' |
| 273 | else: |
| 274 | suffix = 'map' |
| 275 | |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 276 | l = [ |
| 277 | '''\ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 278 | """ Python Character Mapping Codec %s generated from '%s' with gencodec.py. |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 279 | |
| 280 | """#" |
| 281 | |
| 282 | import codecs |
| 283 | |
| 284 | ### Codec APIs |
| 285 | |
| 286 | class Codec(codecs.Codec): |
| 287 | |
| 288 | def encode(self,input,errors='strict'): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 289 | return codecs.charmap_encode(input,errors,encoding_%s) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 290 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 291 | def decode(self,input,errors='strict'): |
| 292 | return codecs.charmap_decode(input,errors,decoding_%s) |
| 293 | ''' % (encodingname, name, suffix, suffix)] |
| 294 | l.append('''\ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 295 | class IncrementalEncoder(codecs.IncrementalEncoder): |
| 296 | def encode(self, input, final=False): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 297 | return codecs.charmap_encode(input,self.errors,encoding_%s)[0] |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 298 | |
| 299 | class IncrementalDecoder(codecs.IncrementalDecoder): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 300 | def decode(self, input, final=False): |
| 301 | return codecs.charmap_decode(input,self.errors,decoding_%s)[0]''' % |
| 302 | (suffix, suffix)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 303 | |
| 304 | l.append(''' |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 305 | class StreamWriter(Codec,codecs.StreamWriter): |
| 306 | pass |
| 307 | |
| 308 | class StreamReader(Codec,codecs.StreamReader): |
| 309 | pass |
| 310 | |
| 311 | ### encodings module API |
| 312 | |
| 313 | def getregentry(): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 314 | return codecs.CodecInfo( |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 315 | name=%r, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 316 | encode=Codec().encode, |
| 317 | decode=Codec().decode, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 318 | incrementalencoder=IncrementalEncoder, |
| 319 | incrementaldecoder=IncrementalDecoder, |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 320 | streamreader=StreamReader, |
| 321 | streamwriter=StreamWriter, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 322 | ) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 323 | ''' % encodingname.replace('_', '-')) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 324 | |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 325 | # Add decoding table or map (with preference to the table) |
| 326 | if not decoding_table_code: |
| 327 | l.append(''' |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 328 | ### Decoding Map |
| 329 | ''') |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 330 | l.extend(decoding_map_code) |
| 331 | else: |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 332 | l.append(''' |
| 333 | ### Decoding Table |
| 334 | ''') |
| 335 | l.extend(decoding_table_code) |
| 336 | |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 337 | # Add encoding map |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 338 | if decoding_table_code: |
| 339 | l.append(''' |
| 340 | ### Encoding table |
| 341 | encoding_table=codecs.charmap_build(decoding_table) |
| 342 | ''') |
| 343 | else: |
| 344 | l.append(''' |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 345 | ### Encoding Map |
| 346 | ''') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 347 | l.extend(encoding_map_code) |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 348 | |
| 349 | # Final new-line |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | l.append('') |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 351 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 352 | return '\n'.join(l).expandtabs() |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 353 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 354 | def pymap(name,map,pyfile,encodingname,comments=1): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 355 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 356 | code = codegen(name,map,encodingname,comments) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 357 | f = open(pyfile,'w') |
| 358 | f.write(code) |
| 359 | f.close() |
| 360 | |
| 361 | def marshalmap(name,map,marshalfile): |
| 362 | |
| 363 | d = {} |
| 364 | for e,(u,c) in map.items(): |
| 365 | d[e] = (u,c) |
| 366 | f = open(marshalfile,'wb') |
| 367 | marshal.dump(d,f) |
| 368 | f.close() |
| 369 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 370 | def convertdir(dir, dirprefix='', nameprefix='', comments=1): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 371 | |
| 372 | mapnames = os.listdir(dir) |
| 373 | for mapname in mapnames: |
| 374 | mappathname = os.path.join(dir, mapname) |
Marc-André Lemburg | bd20ea5 | 2005-10-25 11:53:33 +0000 | [diff] [blame] | 375 | if not os.path.isfile(mappathname): |
| 376 | continue |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 377 | name = os.path.split(mapname)[1] |
| 378 | name = name.replace('-','_') |
| 379 | name = name.split('.')[0] |
| 380 | name = name.lower() |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 381 | name = nameprefix + name |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 382 | codefile = name + '.py' |
| 383 | marshalfile = name + '.mapping' |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 384 | print('converting %s to %s and %s' % (mapname, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 385 | dirprefix + codefile, |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 386 | dirprefix + marshalfile)) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 387 | try: |
| 388 | map = readmap(os.path.join(dir,mapname)) |
| 389 | if not map: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 390 | print('* map is empty; skipping') |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 391 | else: |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 392 | pymap(mappathname, map, dirprefix + codefile,name,comments) |
| 393 | marshalmap(mappathname, map, dirprefix + marshalfile) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 394 | except ValueError as why: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 395 | print('* conversion failed: %s' % why) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 396 | raise |
| 397 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 398 | def rewritepythondir(dir, dirprefix='', comments=1): |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 399 | |
| 400 | mapnames = os.listdir(dir) |
| 401 | for mapname in mapnames: |
| 402 | if not mapname.endswith('.mapping'): |
| 403 | continue |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 404 | name = mapname[:-len('.mapping')] |
| 405 | codefile = name + '.py' |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 406 | print('converting %s to %s' % (mapname, |
| 407 | dirprefix + codefile)) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 408 | try: |
| 409 | map = marshal.load(open(os.path.join(dir,mapname), |
| 410 | 'rb')) |
| 411 | if not map: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 412 | print('* map is empty; skipping') |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 413 | else: |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 414 | pymap(mapname, map, dirprefix + codefile,name,comments) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 415 | except ValueError as why: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 416 | print('* conversion failed: %s' % why) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 417 | |
| 418 | if __name__ == '__main__': |
| 419 | |
| 420 | import sys |
| 421 | if 1: |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 422 | convertdir(*sys.argv[1:]) |
Marc-André Lemburg | c5694c8 | 2005-10-21 13:45:17 +0000 | [diff] [blame] | 423 | else: |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 424 | rewritepythondir(*sys.argv[1:]) |