Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 1 | |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 2 | def _makeunicodes(f): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 3 | import re |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 4 | lines = iter(f.readlines()) |
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 5 | unicodes = {} |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 6 | for line in lines: |
7 | if not line: continue | ||||
8 | num, name = line.split(';')[:2] | ||||
9 | if name[0] == '<': continue # "<control>", etc. | ||||
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 10 | num = int(num, 16) |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 11 | unicodes[num] = name |
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 12 | return unicodes |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 13 | |
14 | |||||
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 15 | class _UnicodeCustom(object): |
Just | 1c1d059 | 2000-03-28 10:33:58 +0000 | [diff] [blame] | 16 | |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 17 | def __init__(self, f): |
18 | if isinstance(f, basestring): | ||||
19 | f = open(f) | ||||
20 | self.codes = _makeunicodes(f) | ||||
Just | 1c1d059 | 2000-03-28 10:33:58 +0000 | [diff] [blame] | 21 | |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 22 | def __getitem__(self, charCode): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 23 | try: |
24 | return self.codes[charCode] | ||||
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 25 | except KeyError: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 26 | return "????" |
27 | |||||
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 28 | class _UnicodeBuiltin(object): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 29 | |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 30 | def __getitem__(self, charCode): |
31 | import unicodedata | ||||
32 | try: | ||||
33 | return unicodedata.name(unichr(charCode)) | ||||
34 | except ValueError: | ||||
35 | return "????" | ||||
36 | |||||
37 | Unicode = _UnicodeBuiltin() | ||||
38 | |||||
39 | def setUnicodeData(f): | ||||
40 | global Unicode | ||||
41 | Unicode = _UnicodeCustom(f) |