PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 2ms 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#
25import os.path
26import time
27
28class SpecialTable:
29	_dict = None
30	def __init__ (self):
31		if SpecialTable._dict == None:
32			self._load_table ()
33
34	def _load_table (self):
35		_dict = {}
36		def parse_table (name, _dict):
37			if not os.path.isfile (name):
38				return
39			for l in file (name):
40				l = l.strip ()
41				if l == "" or l[0] == "#":
42					continue
43				key, values = l.decode ("utf8").split (u"=")
44				key = key.strip ()
45				values = map (lambda x: x.decode ("utf8"), eval (values + u","))
46				if key not in _dict:
47					_dict[key] = []
48				_dict[key] += list (values)
49		name = os.path.join (os.path.dirname (__file__), "special_table")
50		try:
51			parse_table (name, _dict)
52		except Exception, e:
53			print e
54		name = os.path.expanduser ("~/.scim/scim-python/pinyin/special_table")
55		try:
56			parse_table (name, _dict)
57		except Exception, e:
58			print e
59		SpecialTable._dict = _dict
60
61	def lookup (self, key):
62		if key in SpecialTable._dict:
63			return  SpecialTable._dict[key][:]
64		keys = SpecialTable._dict.keys ()
65		keys = filter (lambda k: k.startswith(key), keys)
66		if len (keys) == 1:
67			return SpecialTable._dict.get (keys[0], [])[:]
68		else:
69			return []
70
71if __name__ == "__main__":
72	SpecialTable ()._load_table ()
73	print SpecialTable._dict
74