PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/python/engine/PinYin/SpecialTable.py

http://scim-python.googlecode.com/
Python | 74 lines | 45 code | 5 blank | 24 comment | 13 complexity | d09777e2edfb7cda08149c350b43b804 MD5 | raw file
  1. # vim: set noet ts=4:
  2. #
  3. # scim-python
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this program; if not, write to the
  20. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21. # Boston, MA 02111-1307 USA
  22. #
  23. # $Id: $
  24. #
  25. import os.path
  26. import time
  27. class SpecialTable:
  28. _dict = None
  29. def __init__ (self):
  30. if SpecialTable._dict == None:
  31. self._load_table ()
  32. def _load_table (self):
  33. _dict = {}
  34. def parse_table (name, _dict):
  35. if not os.path.isfile (name):
  36. return
  37. for l in file (name):
  38. l = l.strip ()
  39. if l == "" or l[0] == "#":
  40. continue
  41. key, values = l.decode ("utf8").split (u"=")
  42. key = key.strip ()
  43. values = map (lambda x: x.decode ("utf8"), eval (values + u","))
  44. if key not in _dict:
  45. _dict[key] = []
  46. _dict[key] += list (values)
  47. name = os.path.join (os.path.dirname (__file__), "special_table")
  48. try:
  49. parse_table (name, _dict)
  50. except Exception, e:
  51. print e
  52. name = os.path.expanduser ("~/.scim/scim-python/pinyin/special_table")
  53. try:
  54. parse_table (name, _dict)
  55. except Exception, e:
  56. print e
  57. SpecialTable._dict = _dict
  58. def lookup (self, key):
  59. if key in SpecialTable._dict:
  60. return SpecialTable._dict[key][:]
  61. keys = SpecialTable._dict.keys ()
  62. keys = filter (lambda k: k.startswith(key), keys)
  63. if len (keys) == 1:
  64. return SpecialTable._dict.get (keys[0], [])[:]
  65. else:
  66. return []
  67. if __name__ == "__main__":
  68. SpecialTable ()._load_table ()
  69. print SpecialTable._dict