unicode.py 1.2 KB

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