source: src/mlx/config.py@ 137:f7489252b322

Last change on this file since 137:f7489252b322 was 136:6d206b573dee, checked in by István Váradi <ivaradi@…>, 13 years ago

Added option to enable/disable the online gate system

File size: 10.3 KB
Line 
1# Configuration and related stuff
2# -*- encoding: utf-8 -*-
3
4#-------------------------------------------------------------------------------
5
6import const
7
8import os
9import sys
10import ConfigParser
11
12#-------------------------------------------------------------------------------
13
14configPath = os.path.join(os.path.expanduser("~"),
15 "mlx.config" if os.name=="nt" else ".mlxrc")
16
17#-------------------------------------------------------------------------------
18
19if os.name=="nt":
20 _languageMap = { "en_GB" : "eng",
21 "hu_HU" : "hun" }
22
23#-------------------------------------------------------------------------------
24
25class Config(object):
26 """Our configuration."""
27 DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
28
29 _messageTypesSection = "messageTypes"
30
31 def __init__(self):
32 """Construct the configuration with default values."""
33
34 self._pilotID = ""
35 self._password = ""
36 self._rememberPassword = False
37
38 self._language = ""
39 self._onlineGateSystem = True
40 self._flareTimeFromFS = False
41
42 self._autoUpdate = True
43 self._updateURL = Config.DEFAULT_UPDATE_URL
44
45 self._messageTypeLevels = {}
46
47 self._modified = False
48
49 @property
50 def pilotID(self):
51 """Get the pilot ID."""
52 return self._pilotID
53
54 @pilotID.setter
55 def pilotID(self, pilotID):
56 """Set the pilot ID."""
57 if pilotID!=self._pilotID:
58 self._pilotID = pilotID
59 self._modified = True
60
61 @property
62 def password(self):
63 """Get the password."""
64 return self._password
65
66 @password.setter
67 def password(self, password):
68 """Set the password."""
69 if password!=self._password:
70 self._password = password
71 self._modified = True
72
73 @property
74 def rememberPassword(self):
75 """Get if we should remember the password."""
76 return self._rememberPassword
77
78 @rememberPassword.setter
79 def rememberPassword(self, rememberPassword):
80 """Set if we should remember the password."""
81 if rememberPassword!=self._rememberPassword:
82 self._rememberPassword = rememberPassword
83 self._modified = True
84
85 @property
86 def language(self):
87 """Get the language to use."""
88 return self._language
89
90 @language.setter
91 def language(self, language):
92 """Set the language to use."""
93 if language!=self._language:
94 self._language = language
95 self._modified = True
96
97 @property
98 def onlineGateSystem(self):
99 """Get whether the online gate system should be used."""
100 return self._onlineGateSystem
101
102 @onlineGateSystem.setter
103 def onlineGateSystem(self, onlineGateSystem):
104 """Set whether the online gate system should be used."""
105 if onlineGateSystem!=self._onlineGateSystem:
106 self._onlineGateSystem = onlineGateSystem
107 self._modified = True
108
109 @property
110 def flareTimeFromFS(self):
111 """Get whether the flare time should be calculated from the time values
112 returned by the simulator."""
113 return self._flareTimeFromFS
114
115 @flareTimeFromFS.setter
116 def flareTimeFromFS(self, flareTimeFromFS):
117 """Set whether the flare time should be calculated from the time values
118 returned by the simulator."""
119 if flareTimeFromFS!=self._flareTimeFromFS:
120 self._flareTimeFromFS = flareTimeFromFS
121 self._modified = True
122
123 def getMessageTypeLevel(self, messageType):
124 """Get the level for the given message type."""
125 return self._messageTypeLevels[messageType] \
126 if messageType in self._messageTypeLevels \
127 else const.MESSAGELEVEL_NONE
128
129 def isMessageTypeFS(self, messageType):
130 """Determine if the given message type is displayed in the
131 simulator."""
132 level = self.getMessageTypeLevel(messageType)
133 return level==const.MESSAGELEVEL_FS or \
134 level==const.MESSAGELEVEL_BOTH
135
136 def setMessageTypeLevel(self, messageType, level):
137 """Set the level of the given message type."""
138 if messageType not in self._messageTypeLevels or \
139 self._messageTypeLevels[messageType]!=level:
140 self._messageTypeLevels[messageType] = level
141 self._modified = True
142
143 @property
144 def autoUpdate(self):
145 """Get if an automatic update is needed."""
146 return self._autoUpdate
147
148 @autoUpdate.setter
149 def autoUpdate(self, autoUpdate):
150 """Set if an automatic update is needed."""
151 if autoUpdate!=self._autoUpdate:
152 self._autoUpdate = autoUpdate
153 self._modified = True
154
155 @property
156 def updateURL(self):
157 """Get the update URL."""
158 return self._updateURL
159
160 @updateURL.setter
161 def updateURL(self, updateURL):
162 """Set the update URL."""
163 if updateURL!=self._updateURL:
164 self._updateURL = updateURL
165 self._modified = True
166
167 def load(self):
168 """Load the configuration from its default location."""
169 config = ConfigParser.RawConfigParser()
170 config.read(configPath)
171
172 self._pilotID = self._get(config, "login", "id", "")
173 self._password = self._get(config, "login", "password", "")
174 self._rememberPassword = self._getBoolean(config, "login",
175 "rememberPassword", False)
176
177 self._language = self._get(config, "general", "language", "")
178 self._onlineGateSystem = self._getBoolean(config, "general",
179 "onlineGateSystem",
180 True)
181 self._flareTimeFromFS = self._getBoolean(config, "general",
182 "flareTimeFromFS",
183 False)
184
185 self._messageTypeLevels = {}
186 for messageType in const.messageTypes:
187 self._messageTypeLevels[messageType] = \
188 self._getMessageTypeLevel(config, messageType)
189
190 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
191 self._updateURL = self._get(config, "update", "url",
192 Config.DEFAULT_UPDATE_URL)
193
194 self._modified = False
195
196 def save(self):
197 """Save the configuration file if it has been modified."""
198 if not self._modified:
199 return
200
201 config = ConfigParser.RawConfigParser()
202
203 config.add_section("login")
204 config.set("login", "id", self._pilotID)
205 config.set("login", "password", self._password)
206 config.set("login", "rememberPassword",
207 "yes" if self._rememberPassword else "no")
208
209 config.add_section("general")
210 if self._language:
211 config.set("general", "language", self._language)
212 config.set("general", "onlineGateSystem",
213 "yes" if self._onlineGateSystem else "no")
214 config.set("general", "flareTimeFromFS",
215 "yes" if self._flareTimeFromFS else "no")
216
217 config.add_section(Config._messageTypesSection)
218 for messageType in const.messageTypes:
219 if messageType in self._messageTypeLevels:
220 option = self._getMessageTypeLevelOptionName(messageType)
221 level = self._messageTypeLevels[messageType]
222 config.set(Config._messageTypesSection, option,
223 const.messageLevel2string(level))
224
225 config.add_section("update")
226 config.set("update", "auto",
227 "yes" if self._autoUpdate else "no")
228 config.set("update", "url", self._updateURL)
229
230 try:
231 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
232 0600)
233 with os.fdopen(fd, "wt") as f:
234 config.write(f)
235 self._modified = False
236 except Exception, e:
237 print >> sys.stderr, "Failed to update config: " + str(e)
238
239 def _getBoolean(self, config, section, option, default):
240 """Get the given option as a boolean, if found in the given config,
241 otherwise the default."""
242 return config.getboolean(section, option) \
243 if config.has_option(section, option) \
244 else default
245
246 def _get(self, config, section, option, default):
247 """Get the given option as a string, if found in the given config,
248 otherwise the default."""
249 return config.get(section, option) \
250 if config.has_option(section, option) \
251 else default
252
253 def _getMessageTypeLevel(self, config, messageType):
254 """Get the message type level for the given message type."""
255 option = self._getMessageTypeLevelOptionName(messageType)
256 if config.has_option(Config._messageTypesSection, option):
257 value = config.get(Config._messageTypesSection, option)
258 return const.string2messageLevel(value)
259 elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
260 const.MESSAGETYPE_FAULT,
261 const.MESSAGETYPE_NOGO,
262 const.MESSAGETYPE_GATE_SYSTEM,
263 const.MESSAGETYPE_HELP]:
264 return const.MESSAGELEVEL_BOTH
265 else:
266 return const.MESSAGELEVEL_FS
267
268 def _getMessageTypeLevelOptionName(self, messageType):
269 """Get the option name for the given message type level."""
270 return const.messageType2string(messageType)
271
272 def getLanguage(self):
273 """Get the language to be used."""
274 import locale
275 if self._language:
276 if os.name=="nt":
277 if self._language in _languageMap:
278 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
279 else:
280 locale.setlocale(locale.LC_ALL, "")
281 else:
282 locale.setlocale(locale.LC_ALL, (self._language,
283 locale.getpreferredencoding()))
284 return self._language
285 else:
286 locale.setlocale(locale.LC_ALL, "")
287 return locale.getdefaultlocale()[0]
288
289#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.