1*e1fe3e4aSElliott Hughesdef _makeunicodes(f): 2*e1fe3e4aSElliott Hughes lines = iter(f.readlines()) 3*e1fe3e4aSElliott Hughes unicodes = {} 4*e1fe3e4aSElliott Hughes for line in lines: 5*e1fe3e4aSElliott Hughes if not line: 6*e1fe3e4aSElliott Hughes continue 7*e1fe3e4aSElliott Hughes num, name = line.split(";")[:2] 8*e1fe3e4aSElliott Hughes if name[0] == "<": 9*e1fe3e4aSElliott Hughes continue # "<control>", etc. 10*e1fe3e4aSElliott Hughes num = int(num, 16) 11*e1fe3e4aSElliott Hughes unicodes[num] = name 12*e1fe3e4aSElliott Hughes return unicodes 13*e1fe3e4aSElliott Hughes 14*e1fe3e4aSElliott Hughes 15*e1fe3e4aSElliott Hughesclass _UnicodeCustom(object): 16*e1fe3e4aSElliott Hughes def __init__(self, f): 17*e1fe3e4aSElliott Hughes if isinstance(f, str): 18*e1fe3e4aSElliott Hughes with open(f) as fd: 19*e1fe3e4aSElliott Hughes codes = _makeunicodes(fd) 20*e1fe3e4aSElliott Hughes else: 21*e1fe3e4aSElliott Hughes codes = _makeunicodes(f) 22*e1fe3e4aSElliott Hughes self.codes = codes 23*e1fe3e4aSElliott Hughes 24*e1fe3e4aSElliott Hughes def __getitem__(self, charCode): 25*e1fe3e4aSElliott Hughes try: 26*e1fe3e4aSElliott Hughes return self.codes[charCode] 27*e1fe3e4aSElliott Hughes except KeyError: 28*e1fe3e4aSElliott Hughes return "????" 29*e1fe3e4aSElliott Hughes 30*e1fe3e4aSElliott Hughes 31*e1fe3e4aSElliott Hughesclass _UnicodeBuiltin(object): 32*e1fe3e4aSElliott Hughes def __getitem__(self, charCode): 33*e1fe3e4aSElliott Hughes try: 34*e1fe3e4aSElliott Hughes # use unicodedata backport to python2, if available: 35*e1fe3e4aSElliott Hughes # https://github.com/mikekap/unicodedata2 36*e1fe3e4aSElliott Hughes import unicodedata2 as unicodedata 37*e1fe3e4aSElliott Hughes except ImportError: 38*e1fe3e4aSElliott Hughes import unicodedata 39*e1fe3e4aSElliott Hughes try: 40*e1fe3e4aSElliott Hughes return unicodedata.name(chr(charCode)) 41*e1fe3e4aSElliott Hughes except ValueError: 42*e1fe3e4aSElliott Hughes return "????" 43*e1fe3e4aSElliott Hughes 44*e1fe3e4aSElliott Hughes 45*e1fe3e4aSElliott HughesUnicode = _UnicodeBuiltin() 46*e1fe3e4aSElliott Hughes 47*e1fe3e4aSElliott Hughes 48*e1fe3e4aSElliott Hughesdef setUnicodeData(f): 49*e1fe3e4aSElliott Hughes global Unicode 50*e1fe3e4aSElliott Hughes Unicode = _UnicodeCustom(f) 51