| 136 | raise |
| 137 | |
| 138 | def python_mapdef_code(varname, map, comments=1, precisions=(2, 4)): |
| 139 | |
| 140 | l = [] |
| 141 | append = l.append |
| 142 | if "IDENTITY" in map: |
| 143 | append("%s = codecs.make_identity_dict(range(%d))" % |
| 144 | (varname, map["IDENTITY"])) |
| 145 | append("%s.update({" % varname) |
| 146 | splits = 1 |
| 147 | del map["IDENTITY"] |
| 148 | identity = 1 |
| 149 | else: |
| 150 | append("%s = {" % varname) |
| 151 | splits = 0 |
| 152 | identity = 0 |
| 153 | |
| 154 | mappings = sorted(map.items()) |
| 155 | i = 0 |
| 156 | key_precision, value_precision = precisions |
| 157 | for mapkey, mapvalue in mappings: |
| 158 | mapcomment = '' |
| 159 | if isinstance(mapkey, tuple): |
| 160 | (mapkey, mapcomment) = mapkey |
| 161 | if isinstance(mapvalue, tuple): |
| 162 | (mapvalue, mapcomment) = mapvalue |
| 163 | if mapkey is None: |
| 164 | continue |
| 165 | if (identity and |
| 166 | mapkey == mapvalue and |
| 167 | mapkey < 256): |
| 168 | # No need to include identity mappings, since these |
| 169 | # are already set for the first 256 code points. |
| 170 | continue |
| 171 | key = hexrepr(mapkey, key_precision) |
| 172 | value = hexrepr(mapvalue, value_precision) |
| 173 | if mapcomment and comments: |
| 174 | append(' %s: %s,\t# %s' % (key, value, mapcomment)) |
| 175 | else: |
| 176 | append(' %s: %s,' % (key, value)) |
| 177 | i += 1 |
| 178 | if i == 4096: |
| 179 | # Split the definition into parts to that the Python |
| 180 | # parser doesn't dump core |
| 181 | if splits == 0: |
| 182 | append('}') |
| 183 | else: |
| 184 | append('})') |
| 185 | append('%s.update({' % varname) |
| 186 | i = 0 |
| 187 | splits = splits + 1 |
| 188 | if splits == 0: |
| 189 | append('}') |
| 190 | else: |
| 191 | append('})') |
| 192 | |
| 193 | return l |
| 194 | |
| 195 | def python_tabledef_code(varname, map, comments=1, key_precision=2): |