Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 1 | """ Unicode Mapping Parser and Codec Generator. |
| 2 | |
| 3 | This script parses Unicode mapping files as available from the Unicode |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 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 | |
Fred Drake | bae57a8 | 2000-03-17 16:56:23 +0000 | [diff] [blame] | 15 | The tool also writes marshalled versions of the mapping tables to the |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 16 | same location (with .mapping extension). |
| 17 | |
| 18 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 19 | |
| 20 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 21 | (c) Copyright Guido van Rossum, 2000. |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 22 | |
| 23 | """#" |
| 24 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 25 | import re,os,time,marshal |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 26 | |
| 27 | # Create numeric tables or character based ones ? |
| 28 | numeric = 1 |
| 29 | |
| 30 | mapRE = re.compile('((?:0x[0-9a-fA-F]+\+?)+)' |
| 31 | '\s+' |
| 32 | '((?:(?:0x[0-9a-fA-Z]+|<[A-Za-z]+>)\+?)*)' |
| 33 | '\s*' |
| 34 | '(#.+)?') |
| 35 | |
| 36 | def parsecodes(codes, |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 37 | len=len, filter=filter,range=range): |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 38 | |
| 39 | """ Converts code combinations to either a single code integer |
| 40 | or a tuple of integers. |
| 41 | |
| 42 | meta-codes (in angular brackets, e.g. <LR> and <RL>) are |
| 43 | ignored. |
| 44 | |
| 45 | Empty codes or illegal ones are returned as None. |
| 46 | |
| 47 | """ |
| 48 | if not codes: |
| 49 | return None |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 50 | l = codes.split('+') |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 51 | if len(l) == 1: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 52 | return int(l[0],16) |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 53 | for i in range(len(l)): |
| 54 | try: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 55 | l[i] = int(l[i],16) |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 56 | except ValueError: |
| 57 | l[i] = None |
| 58 | l = filter(lambda x: x is not None, l) |
| 59 | if len(l) == 1: |
| 60 | return l[0] |
| 61 | else: |
| 62 | return tuple(l) |
| 63 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 64 | def readmap(filename): |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 65 | |
| 66 | f = open(filename,'r') |
| 67 | lines = f.readlines() |
| 68 | f.close() |
| 69 | enc2uni = {} |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 70 | identity = [] |
| 71 | unmapped = range(256) |
| 72 | for i in range(256): |
| 73 | unmapped[i] = i |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 74 | for line in lines: |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 75 | line = line.strip() |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 76 | if not line or line[0] == '#': |
| 77 | continue |
| 78 | m = mapRE.match(line) |
| 79 | if not m: |
| 80 | #print '* not matched: %s' % repr(line) |
| 81 | continue |
| 82 | enc,uni,comment = m.groups() |
| 83 | enc = parsecodes(enc) |
| 84 | uni = parsecodes(uni) |
| 85 | if not comment: |
| 86 | comment = '' |
| 87 | else: |
| 88 | comment = comment[1:] |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 89 | if enc < 256: |
| 90 | unmapped.remove(enc) |
| 91 | if enc == uni: |
| 92 | identity.append(enc) |
| 93 | else: |
| 94 | enc2uni[enc] = (uni,comment) |
| 95 | else: |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 96 | enc2uni[enc] = (uni,comment) |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 97 | # If there are more identity-mapped entries than unmapped entries, |
Walter Dörwald | 771bc37 | 2003-02-02 23:39:45 +0000 | [diff] [blame] | 98 | # it pays to generate an identity dictionary first, and add explicit |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 99 | # mappings to None for the rest |
| 100 | if len(identity)>=len(unmapped): |
| 101 | for enc in unmapped: |
| 102 | enc2uni[enc] = (None, "") |
| 103 | enc2uni['IDENTITY'] = 256 |
| 104 | |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 105 | return enc2uni |
| 106 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 107 | def hexrepr(t): |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 108 | |
| 109 | if t is None: |
| 110 | return 'None' |
| 111 | try: |
| 112 | len(t) |
| 113 | except: |
| 114 | return '0x%04x' % t |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 115 | return '(' + ', '.join(map(lambda t: '0x%04x' % t, t)) + ')' |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 116 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 117 | def unicoderepr(t): |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 118 | |
| 119 | if t is None: |
| 120 | return 'None' |
| 121 | if numeric: |
| 122 | return hexrepr(t) |
| 123 | else: |
| 124 | try: |
| 125 | len(t) |
| 126 | except: |
| 127 | return repr(unichr(t)) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 128 | return repr(''.join(map(unichr, t))) |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 129 | |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 130 | def keyrepr(t): |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 131 | |
| 132 | if t is None: |
| 133 | return 'None' |
| 134 | if numeric: |
| 135 | return hexrepr(t) |
| 136 | else: |
| 137 | try: |
| 138 | len(t) |
| 139 | except: |
| 140 | if t < 256: |
| 141 | return repr(chr(t)) |
| 142 | else: |
| 143 | return repr(unichr(t)) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 144 | return repr(''.join(map(chr, t))) |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 145 | |
| 146 | def codegen(name,map,comments=1): |
| 147 | |
| 148 | """ Returns Python source for the given map. |
| 149 | |
| 150 | Comments are included in the source, if comments is true (default). |
| 151 | |
| 152 | """ |
| 153 | l = [ |
| 154 | '''\ |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 155 | """ Python Character Mapping Codec generated from '%s' with gencodec.py. |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 157 | """#" |
| 158 | |
| 159 | import codecs |
| 160 | |
| 161 | ### Codec APIs |
| 162 | |
| 163 | class Codec(codecs.Codec): |
| 164 | |
| 165 | def encode(self,input,errors='strict'): |
| 166 | |
| 167 | return codecs.charmap_encode(input,errors,encoding_map) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 169 | def decode(self,input,errors='strict'): |
| 170 | |
| 171 | return codecs.charmap_decode(input,errors,decoding_map) |
| 172 | |
| 173 | class StreamWriter(Codec,codecs.StreamWriter): |
| 174 | pass |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 175 | |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 176 | class StreamReader(Codec,codecs.StreamReader): |
| 177 | pass |
| 178 | |
| 179 | ### encodings module API |
| 180 | |
| 181 | def getregentry(): |
| 182 | |
| 183 | return (Codec().encode,Codec().decode,StreamReader,StreamWriter) |
| 184 | |
| 185 | ### Decoding Map |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 186 | ''' % name, |
| 187 | ] |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 188 | |
| 189 | if map.has_key("IDENTITY"): |
| 190 | l.append("decoding_map = codecs.make_identity_dict(range(%d))" |
| 191 | % map["IDENTITY"]) |
| 192 | l.append("decoding_map.update({") |
| 193 | splits = 1 |
| 194 | del map["IDENTITY"] |
| 195 | else: |
| 196 | l.append("decoding_map = {") |
| 197 | splits = 0 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 199 | mappings = map.items() |
| 200 | mappings.sort() |
| 201 | append = l.append |
| 202 | i = 0 |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 203 | for e,value in mappings: |
| 204 | try: |
| 205 | (u,c) = value |
| 206 | except TypeError: |
| 207 | u = value |
| 208 | c = '' |
| 209 | key = keyrepr(e) |
| 210 | if c and comments: |
| 211 | append('\t%s: %s,\t# %s' % (key,unicoderepr(u),c)) |
| 212 | else: |
| 213 | append('\t%s: %s,' % (key,unicoderepr(u))) |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 214 | i += 1 |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 215 | if i == 4096: |
| 216 | # Split the definition into parts to that the Python |
| 217 | # parser doesn't dump core |
| 218 | if splits == 0: |
| 219 | append('}') |
| 220 | else: |
| 221 | append('})') |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 222 | append('decoding_map.update({') |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 223 | i = 0 |
| 224 | splits = splits + 1 |
| 225 | if splits == 0: |
| 226 | append('}') |
| 227 | else: |
| 228 | append('})') |
| 229 | append(''' |
| 230 | ### Encoding Map |
| 231 | |
Marc-André Lemburg | 716cf91 | 2001-05-16 09:41:45 +0000 | [diff] [blame] | 232 | encoding_map = codecs.make_encoding_map(decoding_map) |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 233 | ''') |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 234 | return '\n'.join(l) |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 235 | |
| 236 | def pymap(name,map,pyfile,comments=1): |
| 237 | |
| 238 | code = codegen(name,map,comments) |
| 239 | f = open(pyfile,'w') |
| 240 | f.write(code) |
| 241 | f.close() |
| 242 | |
| 243 | def marshalmap(name,map,marshalfile): |
| 244 | |
| 245 | d = {} |
| 246 | for e,(u,c) in map.items(): |
| 247 | d[e] = (u,c) |
| 248 | f = open(marshalfile,'wb') |
| 249 | marshal.dump(d,f) |
| 250 | f.close() |
| 251 | |
| 252 | def convertdir(dir,prefix='',comments=1): |
| 253 | |
| 254 | mapnames = os.listdir(dir) |
| 255 | for mapname in mapnames: |
| 256 | name = os.path.split(mapname)[1] |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 257 | name = name.replace('-','_') |
| 258 | name = name.split('.')[0] |
| 259 | name = name.lower() |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 260 | codefile = name + '.py' |
| 261 | marshalfile = name + '.mapping' |
| 262 | print 'converting %s to %s and %s' % (mapname, |
| 263 | prefix + codefile, |
| 264 | prefix + marshalfile) |
| 265 | try: |
| 266 | map = readmap(os.path.join(dir,mapname)) |
| 267 | if not map: |
| 268 | print '* map is empty; skipping' |
| 269 | else: |
| 270 | pymap(mapname, map, prefix + codefile,comments) |
| 271 | marshalmap(mapname, map, prefix + marshalfile) |
| 272 | except ValueError: |
| 273 | print '* conversion failed' |
| 274 | |
| 275 | def rewritepythondir(dir,prefix='',comments=1): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 277 | mapnames = os.listdir(dir) |
| 278 | for mapname in mapnames: |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 279 | if not mapname.endswith('.mapping'): |
Guido van Rossum | 34a7911 | 2000-03-10 22:36:57 +0000 | [diff] [blame] | 280 | continue |
| 281 | codefile = mapname[:-len('.mapping')] + '.py' |
| 282 | print 'converting %s to %s' % (mapname, |
| 283 | prefix + codefile) |
| 284 | try: |
| 285 | map = marshal.load(open(os.path.join(dir,mapname), |
| 286 | 'rb')) |
| 287 | if not map: |
| 288 | print '* map is empty; skipping' |
| 289 | else: |
| 290 | pymap(mapname, map, prefix + codefile,comments) |
| 291 | except ValueError, why: |
| 292 | print '* conversion failed: %s' % why |
| 293 | |
| 294 | if __name__ == '__main__': |
| 295 | |
| 296 | import sys |
| 297 | if 1: |
| 298 | apply(convertdir,tuple(sys.argv[1:])) |
| 299 | else: |
| 300 | apply(rewritepythondir,tuple(sys.argv[1:])) |