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