annotate Tools/scripts/gencodec.py @ 36392:2012c548ce69 2.4

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