blob: c72fb9e6e75aacaedfff55def32744b25e9103d3 [file] [log] [blame]
Just7842e561999-12-16 21:34:53 +00001
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +08002def _makeunicodes(f):
Just7842e561999-12-16 21:34:53 +00003 import re
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +08004 lines = iter(f.readlines())
jvr13325c62004-09-25 07:47:41 +00005 unicodes = {}
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +08006 for line in lines:
7 if not line: continue
8 num, name = line.split(';')[:2]
9 if name[0] == '<': continue # "<control>", etc.
jvr13325c62004-09-25 07:47:41 +000010 num = int(num, 16)
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080011 unicodes[num] = name
jvr13325c62004-09-25 07:47:41 +000012 return unicodes
Just7842e561999-12-16 21:34:53 +000013
14
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080015class _UnicodeCustom(object):
Just1c1d0592000-03-28 10:33:58 +000016
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080017 def __init__(self, f):
18 if isinstance(f, basestring):
19 f = open(f)
20 self.codes = _makeunicodes(f)
Just1c1d0592000-03-28 10:33:58 +000021
Just7842e561999-12-16 21:34:53 +000022 def __getitem__(self, charCode):
Just7842e561999-12-16 21:34:53 +000023 try:
24 return self.codes[charCode]
jvr13325c62004-09-25 07:47:41 +000025 except KeyError:
Just7842e561999-12-16 21:34:53 +000026 return "????"
27
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080028class _UnicodeBuiltin(object):
Just7842e561999-12-16 21:34:53 +000029
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080030 def __getitem__(self, charCode):
31 import unicodedata
32 try:
33 return unicodedata.name(unichr(charCode))
34 except ValueError:
35 return "????"
36
37Unicode = _UnicodeBuiltin()
38
39def setUnicodeData(f):
40 global Unicode
41 Unicode = _UnicodeCustom(f)