| 193 | return l |
| 194 | |
| 195 | def python_tabledef_code(varname, map, comments=1, key_precision=2): |
| 196 | |
| 197 | l = [] |
| 198 | append = l.append |
| 199 | append('%s = (' % varname) |
| 200 | |
| 201 | # Analyze map and create table dict |
| 202 | mappings = sorted(map.items()) |
| 203 | table = {} |
| 204 | maxkey = 255 |
| 205 | if 'IDENTITY' in map: |
| 206 | for key in range(256): |
| 207 | table[key] = (key, '') |
| 208 | del map['IDENTITY'] |
| 209 | for mapkey, mapvalue in mappings: |
| 210 | mapcomment = '' |
| 211 | if isinstance(mapkey, tuple): |
| 212 | (mapkey, mapcomment) = mapkey |
| 213 | if isinstance(mapvalue, tuple): |
| 214 | (mapvalue, mapcomment) = mapvalue |
| 215 | if mapkey == MISSING_CODE: |
| 216 | continue |
| 217 | table[mapkey] = (mapvalue, mapcomment) |
| 218 | if mapkey > maxkey: |
| 219 | maxkey = mapkey |
| 220 | if maxkey > MAX_TABLE_SIZE: |
| 221 | # Table too large |
| 222 | return None |
| 223 | |
| 224 | # Create table code |
| 225 | maxchar = 0 |
| 226 | for key in range(maxkey + 1): |
| 227 | if key not in table: |
| 228 | mapvalue = MISSING_CODE |
| 229 | mapcomment = 'UNDEFINED' |
| 230 | else: |
| 231 | mapvalue, mapcomment = table[key] |
| 232 | if mapvalue == MISSING_CODE: |
| 233 | mapchar = UNI_UNDEFINED |
| 234 | else: |
| 235 | if isinstance(mapvalue, tuple): |
| 236 | # 1-n mappings not supported |
| 237 | return None |
| 238 | else: |
| 239 | mapchar = chr(mapvalue) |
| 240 | maxchar = max(maxchar, ord(mapchar)) |
| 241 | if mapcomment and comments: |
| 242 | append(' %a \t# %s -> %s' % (mapchar, |
| 243 | hexrepr(key, key_precision), |
| 244 | mapcomment)) |
| 245 | else: |
| 246 | append(' %a' % mapchar) |
| 247 | |
| 248 | if maxchar < 256: |
| 249 | append(' %a \t## Widen to UCS2 for optimization' % UNI_UNDEFINED) |
| 250 | append(')') |
| 251 | return l |
| 252 | |