root/trunk/lib/config.py

Revision 16, 5.0 kB (checked in by nerijusb, 1 year ago)

Transition to the wx namespace - opendict now works with wx 2.8

Line 
1 # OpenDict
2 # Copyright (c) 2003-2006 Martynas Jocius <martynas.jocius@idiles.com>
3 # Copyright (c) 2007 IDILES SYSTEMS, UAB <support@idiles.com>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your opinion) any later version.
9 #
10 # This program is distributed in the hope that will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MECHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more detals.
14 #
15 # You shoud have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307 USA
19 #
20 # Module: config.py
21
22 import os
23 import string
24 import codecs
25
26 from lib.logger import systemLog, debugLog, DEBUG, INFO, WARNING, ERROR
27 from lib.misc import numVersion
28 from lib import info
29 from lib import util
30 from lib import parser
31 from lib import xmltools
32
33
34 class ActiveDictConfig(object):
35     """Config file manager for activated dictionaries. Operates with
36     names of dictionaries only."""
37
38     def __init__(self):
39       self.filePath = os.path.join(info.LOCAL_HOME, "active.conf")
40
41       # If config file does not exist (this is the first time),
42       # set special attribute init=True to notify that
43       if not os.path.exists(self.filePath):
44           self.init = True
45       else:
46           self.init = False
47
48       self.dicts = []
49
50
51     def load(self):
52         """Load list of active dictionaries."""
53
54         try:
55             for line in open(self.filePath):
56                 name = line.strip()
57                 name = unicode(name, 'UTF-8')
58                 self.dicts.append(name)
59         except IOError, e:
60             pass
61
62
63     def save(self):
64         """Save list of active dictionaries."""
65
66         fd = open(self.filePath, 'w')
67         for d in self.dicts:
68             name = d.encode('UTF-8')
69             print >> fd, name
70         fd.close()
71
72
73     def enabled(self, name):
74         """Return True if this dictionary is enabled."""
75
76         if name in self.dicts:
77             return True
78
79         return False
80
81
82     def add(self, name):
83         """Add new dictionary to the list."""
84
85         if type(name) == str:
86             name = unicode(name, 'UTF-8')
87
88         if not name in self.dicts:
89             self.dicts.append(name)
90
91
92     def remove(self, name):
93         """Remove dictionary from the list."""
94
95         if type(name) == str:
96             name = unicode(name, 'UTF-8')
97
98         if name in self.dicts:
99             self.dicts.remove(name)
100
101
102
103 class Configuration:
104    """This class is used for reading and writing config file.
105    It also takes care of installing new plugins (but shouldn't)"""
106
107    def __init__(self):
108       """Initialize default values"""
109
110       self.activedict = ActiveDictConfig()
111       self.activedict.load()
112      
113       self.filePath = os.path.join(info.LOCAL_HOME, "opendict.xml")
114       self.props = {}
115
116       # TODO: Should not be here after removing register part from config
117       import wx
118       self.app = wx.GetApp()
119
120       #
121       # Default values
122       #
123       self.set('saveWindowSize', 'True')
124       self.set('saveWindowPos', 'True')
125       self.set('saveSashPos', 'True')
126
127       self.set('defaultDict', '')
128       self.set('windowWidth', '550')
129       self.set('windowHeight', '370')
130       self.set('windowPosX', '-1')
131       self.set('windowPosY', '-1')
132       self.set('sashPos', '160')
133
134       # Internal variables
135       self.window = None
136       self.ids = {}
137
138       self.plugMenuIds = 200
139       self.regMenuIds = 300
140       self.groupMenuIds = 400
141
142       self.set('encoding', 'UTF-8')
143       self.set('fontFace', 'Fixed')
144       self.set('fontSize', '10')
145      
146       self.set('dictServer', 'dict.org')
147       self.set('dictServerPort', '2628')
148       self.set('dict-server-encoding', 'UTF-8')
149
150       self.repository = \
151                'http://db.opendict.idiles.com/Data/opendict-add-ons.xml'
152
153
154    def get(self, name):
155       """Return property value"""
156
157       return self.props.get(name)
158
159
160    def set(self, name, value):
161       """Set property"""
162
163       self.props[name] = value
164
165
166
167    def load(self):
168       """Load configuration from file to memory"""
169
170       try:
171          if os.path.exists(self.filePath):
172             self.props.update(xmltools.parseMainConfig(self.filePath))
173       except Exception, e:
174          systemLog(ERROR, "Unable to read configuration file: %s" % e)
175
176       # Old configurations may still keep outdated entry, rewrite it
177       self.set('repository-list', self.repository)
178
179
180
181    def save(self):
182       """Write configuration to disk"""
183
184       doc = xmltools.generateMainConfig(self.props)
185       xmltools.writeConfig(doc, os.path.join(info.LOCAL_HOME,
186                                              self.filePath))
187
188
189
190    def checkDir(self, dir):
191       """Check if directory exists. Create one if not"""
192
193       raise "Deprecated"
194
195       if not os.path.exists(os.path.join(uhome, dir)):
196          os.mkdir(os.path.join(uhome, dir))
Note: See TracBrowser for help on using the browser.