(filename)
| 71 | return tuple(l) |
| 72 | |
| 73 | def readmap(filename): |
| 74 | |
| 75 | with open(filename) as f: |
| 76 | lines = f.readlines() |
| 77 | enc2uni = {} |
| 78 | identity = [] |
| 79 | unmapped = list(range(256)) |
| 80 | |
| 81 | # UTC mapping tables per convention don't include the identity |
| 82 | # mappings for code points 0x00 - 0x1F and 0x7F, unless these are |
| 83 | # explicitly mapped to different characters or undefined |
| 84 | for i in list(range(32)) + [127]: |
| 85 | identity.append(i) |
| 86 | unmapped.remove(i) |
| 87 | enc2uni[i] = (i, 'CONTROL CHARACTER') |
| 88 | |
| 89 | for line in lines: |
| 90 | line = line.strip() |
| 91 | if not line or line[0] == '#': |
| 92 | continue |
| 93 | m = mapRE.match(line) |
| 94 | if not m: |
| 95 | #print '* not matched: %s' % repr(line) |
| 96 | continue |
| 97 | enc,uni,comment = m.groups() |
| 98 | enc = parsecodes(enc) |
| 99 | uni = parsecodes(uni) |
| 100 | if comment is None: |
| 101 | comment = '' |
| 102 | else: |
| 103 | comment = comment[1:].strip() |
| 104 | if not isinstance(enc, tuple) and enc < 256: |
| 105 | if enc in unmapped: |
| 106 | unmapped.remove(enc) |
| 107 | if enc == uni: |
| 108 | identity.append(enc) |
| 109 | enc2uni[enc] = (uni,comment) |
| 110 | else: |
| 111 | enc2uni[enc] = (uni,comment) |
| 112 | |
| 113 | # If there are more identity-mapped entries than unmapped entries, |
| 114 | # it pays to generate an identity dictionary first, and add explicit |
| 115 | # mappings to None for the rest |
| 116 | if len(identity) >= len(unmapped): |
| 117 | for enc in unmapped: |
| 118 | enc2uni[enc] = (MISSING_CODE, "") |
| 119 | enc2uni['IDENTITY'] = 256 |
| 120 | |
| 121 | return enc2uni |
| 122 | |
| 123 | def hexrepr(t, precision=4): |
| 124 |
no test coverage detected
searching dependent graphs…