annotate Tools/scripts/gencodec.py @ 15164:8930ec3bc00a legacy-trunk

Added additional test cases for pulldom modifications.
author Lars Gustäbel <lars@gustaebel.de>
date Wed, 11 Oct 2000 22:36:00 +0000
parents ba80c9064491
children bf9bb0e3dd34
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
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 site (ftp.unicode.org) and creates Python codec modules from them. The
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 codecs use the standard character mapping codec to actually apply the
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 mapping.
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.
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
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 import string,re,os,time,marshal
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # Create numeric tables or character based ones ?
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 numeric = 1
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 mapRE = re.compile('((?:0x[0-9a-fA-F]+\+?)+)'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 '\s+'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 '((?:(?:0x[0-9a-fA-Z]+|<[A-Za-z]+>)\+?)*)'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 '\s*'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 '(#.+)?')
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 def parsecodes(codes,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 split=string.split,atoi=string.atoi,len=len,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 filter=filter,range=range):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 """ Converts code combinations to either a single code integer
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 or a tuple of integers.
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 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
44 ignored.
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 Empty codes or illegal ones are returned as None.
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 """
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if not codes:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 return None
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 l = split(codes,'+')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 if len(l) == 1:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 return atoi(l[0],16)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 for i in range(len(l)):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 l[i] = atoi(l[i],16)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 except ValueError:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 l[i] = None
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 l = filter(lambda x: x is not None, l)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if len(l) == 1:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 return l[0]
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return tuple(l)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def readmap(filename,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 strip=string.strip):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 f = open(filename,'r')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 lines = f.readlines()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 f.close()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 enc2uni = {}
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 for line in lines:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 line = strip(line)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 if not line or line[0] == '#':
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 continue
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 m = mapRE.match(line)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 if not m:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 #print '* not matched: %s' % repr(line)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 continue
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 enc,uni,comment = m.groups()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 enc = parsecodes(enc)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 uni = parsecodes(uni)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 if not comment:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 comment = ''
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 comment = comment[1:]
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if enc != uni:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 enc2uni[enc] = (uni,comment)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return enc2uni
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 def hexrepr(t,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 join=string.join):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if t is None:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 return 'None'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 len(t)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 except:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 return '0x%04x' % t
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 return '(' + join(map(lambda t: '0x%04x' % t, t),', ') + ')'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 def unicoderepr(t,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 join=string.join):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
107
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if t is None:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return 'None'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if numeric:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 return hexrepr(t)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 len(t)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 except:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return repr(unichr(t))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return repr(join(map(unichr, t),''))
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 def keyrepr(t,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 join=string.join):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 if t is None:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return 'None'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 if numeric:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return hexrepr(t)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 len(t)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 except:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 if t < 256:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return repr(chr(t))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return repr(unichr(t))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 return repr(join(map(chr, t),''))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 def codegen(name,map,comments=1):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 """ Returns Python source for the given map.
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 """
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 l = [
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 """ Python Character Mapping Codec generated from '%s'.
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 Written by Marc-Andre Lemburg (mal@lemburg.com).
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 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
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
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 import codecs
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 ### Codec APIs
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 class Codec(codecs.Codec):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
159
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 def encode(self,input,errors='strict'):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 return codecs.charmap_encode(input,errors,encoding_map)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 def decode(self,input,errors='strict'):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 return codecs.charmap_decode(input,errors,decoding_map)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 class StreamWriter(Codec,codecs.StreamWriter):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 pass
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 class StreamReader(Codec,codecs.StreamReader):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 pass
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
173
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 ### encodings module API
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 def getregentry():
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
177
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 return (Codec().encode,Codec().decode,StreamReader,StreamWriter)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 ### Decoding Map
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 decoding_map = {
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 ''' % name,
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 mappings = map.items()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 mappings.sort()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 append = l.append
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 i = 0
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 splits = 0
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 for e,value in mappings:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 (u,c) = value
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 except TypeError:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 u = value
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 c = ''
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 key = keyrepr(e)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 if c and comments:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 append('\t%s: %s,\t# %s' % (key,unicoderepr(u),c))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 append('\t%s: %s,' % (key,unicoderepr(u)))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 i = i + 1
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 if i == 4096:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 # Split the definition into parts to that the Python
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 # parser doesn't dump core
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 if splits == 0:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 append('}')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 append('})')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 append('map.update({')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 i = 0
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 splits = splits + 1
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 if splits == 0:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 append('}')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 append('})')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 append('''
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 ### Encoding Map
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 encoding_map = {}
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 for k,v in decoding_map.items():
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 encoding_map[v] = k
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 ''')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 return string.join(l,'\n')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 def pymap(name,map,pyfile,comments=1):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 code = codegen(name,map,comments)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 f = open(pyfile,'w')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 f.write(code)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 f.close()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 def marshalmap(name,map,marshalfile):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 d = {}
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 for e,(u,c) in map.items():
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 d[e] = (u,c)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 f = open(marshalfile,'wb')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 marshal.dump(d,f)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 f.close()
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 def convertdir(dir,prefix='',comments=1):
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 mapnames = os.listdir(dir)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 for mapname in mapnames:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 name = os.path.split(mapname)[1]
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 name = string.replace(name,'-','_')
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 name = string.split(name, '.')[0]
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 name = string.lower(name)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 codefile = name + '.py'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 marshalfile = name + '.mapping'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 print 'converting %s to %s and %s' % (mapname,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 prefix + codefile,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 prefix + marshalfile)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 map = readmap(os.path.join(dir,mapname))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 if not map:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 print '* map is empty; skipping'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 pymap(mapname, map, prefix + codefile,comments)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 marshalmap(mapname, map, prefix + marshalfile)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 except ValueError:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 print '* conversion failed'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 def rewritepythondir(dir,prefix='',comments=1):
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 mapnames = os.listdir(dir)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 for mapname in mapnames:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 if mapname[-len('.mapping'):] != '.mapping':
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 continue
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 codefile = mapname[:-len('.mapping')] + '.py'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 print 'converting %s to %s' % (mapname,
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 prefix + codefile)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 try:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 map = marshal.load(open(os.path.join(dir,mapname),
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 'rb'))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 if not map:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 print '* map is empty; skipping'
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 pymap(mapname, map, prefix + codefile,comments)
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 except ValueError, why:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 print '* conversion failed: %s' % why
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 if __name__ == '__main__':
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 import sys
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 if 1:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 apply(convertdir,tuple(sys.argv[1:]))
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 else:
5305f9b01fc7 Marc-Andre Lemburg: added
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 apply(rewritepythondir,tuple(sys.argv[1:]))