1 | # Configuration and related stuff
|
---|
2 | # -*- encoding: utf-8 -*-
|
---|
3 |
|
---|
4 | #-------------------------------------------------------------------------------
|
---|
5 |
|
---|
6 | import const
|
---|
7 |
|
---|
8 | import os
|
---|
9 | import sys
|
---|
10 | import ConfigParser
|
---|
11 |
|
---|
12 | #-------------------------------------------------------------------------------
|
---|
13 |
|
---|
14 | configPath = os.path.join(os.path.expanduser("~"),
|
---|
15 | "mlx.config" if os.name=="nt" else ".mlxrc")
|
---|
16 |
|
---|
17 | #-------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | if os.name=="nt":
|
---|
20 | _languageMap = { "en_GB" : "eng",
|
---|
21 | "hu_HU" : "hun" }
|
---|
22 |
|
---|
23 | #-------------------------------------------------------------------------------
|
---|
24 |
|
---|
25 | class 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._hideMinimizedWindow = True
|
---|
40 | self._onlineGateSystem = True
|
---|
41 | self._onlineACARS = True
|
---|
42 | self._flareTimeFromFS = False
|
---|
43 | self._syncFSTime = False
|
---|
44 |
|
---|
45 | self._pirepDirectory = None
|
---|
46 |
|
---|
47 | self._autoUpdate = True
|
---|
48 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
49 |
|
---|
50 | self._messageTypeLevels = {}
|
---|
51 |
|
---|
52 | self._modified = False
|
---|
53 |
|
---|
54 | @property
|
---|
55 | def pilotID(self):
|
---|
56 | """Get the pilot ID."""
|
---|
57 | return self._pilotID
|
---|
58 |
|
---|
59 | @pilotID.setter
|
---|
60 | def pilotID(self, pilotID):
|
---|
61 | """Set the pilot ID."""
|
---|
62 | if pilotID!=self._pilotID:
|
---|
63 | self._pilotID = pilotID
|
---|
64 | self._modified = True
|
---|
65 |
|
---|
66 | @property
|
---|
67 | def password(self):
|
---|
68 | """Get the password."""
|
---|
69 | return self._password
|
---|
70 |
|
---|
71 | @password.setter
|
---|
72 | def password(self, password):
|
---|
73 | """Set the password."""
|
---|
74 | if password!=self._password:
|
---|
75 | self._password = password
|
---|
76 | self._modified = True
|
---|
77 |
|
---|
78 | @property
|
---|
79 | def rememberPassword(self):
|
---|
80 | """Get if we should remember the password."""
|
---|
81 | return self._rememberPassword
|
---|
82 |
|
---|
83 | @rememberPassword.setter
|
---|
84 | def rememberPassword(self, rememberPassword):
|
---|
85 | """Set if we should remember the password."""
|
---|
86 | if rememberPassword!=self._rememberPassword:
|
---|
87 | self._rememberPassword = rememberPassword
|
---|
88 | self._modified = True
|
---|
89 |
|
---|
90 | @property
|
---|
91 | def language(self):
|
---|
92 | """Get the language to use."""
|
---|
93 | return self._language
|
---|
94 |
|
---|
95 | @language.setter
|
---|
96 | def language(self, language):
|
---|
97 | """Set the language to use."""
|
---|
98 | if language!=self._language:
|
---|
99 | self._language = language
|
---|
100 | self._modified = True
|
---|
101 |
|
---|
102 | @property
|
---|
103 | def hideMinimizedWindow(self):
|
---|
104 | """Get whether a minimized window should be hidden."""
|
---|
105 | return self._hideMinimizedWindow
|
---|
106 |
|
---|
107 | @hideMinimizedWindow.setter
|
---|
108 | def hideMinimizedWindow(self, hideMinimizedWindow):
|
---|
109 | """Set whether a minimized window should be hidden."""
|
---|
110 | if hideMinimizedWindow!=self._hideMinimizedWindow:
|
---|
111 | self._hideMinimizedWindow = hideMinimizedWindow
|
---|
112 | self._modified = True
|
---|
113 |
|
---|
114 | @property
|
---|
115 | def onlineGateSystem(self):
|
---|
116 | """Get whether the online gate system should be used."""
|
---|
117 | return self._onlineGateSystem
|
---|
118 |
|
---|
119 | @onlineGateSystem.setter
|
---|
120 | def onlineGateSystem(self, onlineGateSystem):
|
---|
121 | """Set whether the online gate system should be used."""
|
---|
122 | if onlineGateSystem!=self._onlineGateSystem:
|
---|
123 | self._onlineGateSystem = onlineGateSystem
|
---|
124 | self._modified = True
|
---|
125 |
|
---|
126 | @property
|
---|
127 | def onlineACARS(self):
|
---|
128 | """Get whether the online ACARS system should be used."""
|
---|
129 | return self._onlineACARS
|
---|
130 |
|
---|
131 | @onlineACARS.setter
|
---|
132 | def onlineACARS(self, onlineACARS):
|
---|
133 | """Set whether the online ACARS system should be used."""
|
---|
134 | if onlineACARS!=self._onlineACARS:
|
---|
135 | self._onlineACARS = onlineACARS
|
---|
136 | self._modified = True
|
---|
137 |
|
---|
138 | @property
|
---|
139 | def flareTimeFromFS(self):
|
---|
140 | """Get whether the flare time should be calculated from the time values
|
---|
141 | returned by the simulator."""
|
---|
142 | return self._flareTimeFromFS
|
---|
143 |
|
---|
144 | @flareTimeFromFS.setter
|
---|
145 | def flareTimeFromFS(self, flareTimeFromFS):
|
---|
146 | """Set whether the flare time should be calculated from the time values
|
---|
147 | returned by the simulator."""
|
---|
148 | if flareTimeFromFS!=self._flareTimeFromFS:
|
---|
149 | self._flareTimeFromFS = flareTimeFromFS
|
---|
150 | self._modified = True
|
---|
151 |
|
---|
152 | @property
|
---|
153 | def syncFSTime(self):
|
---|
154 | """Get whether the simulator's time should be synchronized with the
|
---|
155 | machine's clock."""
|
---|
156 | return self._syncFSTime
|
---|
157 |
|
---|
158 | @syncFSTime.setter
|
---|
159 | def syncFSTime(self, syncFSTime):
|
---|
160 | """Set whether the simulator's time should be synchronized with the
|
---|
161 | machine's clock."""
|
---|
162 | if syncFSTime!=self._syncFSTime:
|
---|
163 | self._syncFSTime = syncFSTime
|
---|
164 | self._modified = True
|
---|
165 |
|
---|
166 | @property
|
---|
167 | def pirepDirectory(self):
|
---|
168 | """Get the directory offered by default when saving a PIREP."""
|
---|
169 | return self._pirepDirectory
|
---|
170 |
|
---|
171 | @pirepDirectory.setter
|
---|
172 | def pirepDirectory(self, pirepDirectory):
|
---|
173 | """Get the directory offered by default when saving a PIREP."""
|
---|
174 | if pirepDirectory!=self._pirepDirectory and \
|
---|
175 | (pirepDirectory!="" or self._pirepDirectory is not None):
|
---|
176 | self._pirepDirectory = None if pirepDirectory=="" \
|
---|
177 | else pirepDirectory
|
---|
178 | self._modified = True
|
---|
179 |
|
---|
180 | def getMessageTypeLevel(self, messageType):
|
---|
181 | """Get the level for the given message type."""
|
---|
182 | return self._messageTypeLevels[messageType] \
|
---|
183 | if messageType in self._messageTypeLevels \
|
---|
184 | else const.MESSAGELEVEL_NONE
|
---|
185 |
|
---|
186 | def isMessageTypeFS(self, messageType):
|
---|
187 | """Determine if the given message type is displayed in the
|
---|
188 | simulator."""
|
---|
189 | level = self.getMessageTypeLevel(messageType)
|
---|
190 | return level==const.MESSAGELEVEL_FS or \
|
---|
191 | level==const.MESSAGELEVEL_BOTH
|
---|
192 |
|
---|
193 | def setMessageTypeLevel(self, messageType, level):
|
---|
194 | """Set the level of the given message type."""
|
---|
195 | if messageType not in self._messageTypeLevels or \
|
---|
196 | self._messageTypeLevels[messageType]!=level:
|
---|
197 | self._messageTypeLevels[messageType] = level
|
---|
198 | self._modified = True
|
---|
199 |
|
---|
200 | @property
|
---|
201 | def autoUpdate(self):
|
---|
202 | """Get if an automatic update is needed."""
|
---|
203 | return self._autoUpdate
|
---|
204 |
|
---|
205 | @autoUpdate.setter
|
---|
206 | def autoUpdate(self, autoUpdate):
|
---|
207 | """Set if an automatic update is needed."""
|
---|
208 | if autoUpdate!=self._autoUpdate:
|
---|
209 | self._autoUpdate = autoUpdate
|
---|
210 | self._modified = True
|
---|
211 |
|
---|
212 | @property
|
---|
213 | def updateURL(self):
|
---|
214 | """Get the update URL."""
|
---|
215 | return self._updateURL
|
---|
216 |
|
---|
217 | @updateURL.setter
|
---|
218 | def updateURL(self, updateURL):
|
---|
219 | """Set the update URL."""
|
---|
220 | if updateURL!=self._updateURL:
|
---|
221 | self._updateURL = updateURL
|
---|
222 | self._modified = True
|
---|
223 |
|
---|
224 | def load(self):
|
---|
225 | """Load the configuration from its default location."""
|
---|
226 | config = ConfigParser.RawConfigParser()
|
---|
227 | config.read(configPath)
|
---|
228 |
|
---|
229 | self._pilotID = self._get(config, "login", "id", "")
|
---|
230 | self._password = self._get(config, "login", "password", "")
|
---|
231 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
232 | "rememberPassword", False)
|
---|
233 |
|
---|
234 | self._language = self._get(config, "general", "language", "")
|
---|
235 | self._hideMinimizedWindow = self._getBoolean(config, "general",
|
---|
236 | "hideMinimizedWindow",
|
---|
237 | True)
|
---|
238 | self._onlineGateSystem = self._getBoolean(config, "general",
|
---|
239 | "onlineGateSystem",
|
---|
240 | True)
|
---|
241 | self._onlineACARS = self._getBoolean(config, "general",
|
---|
242 | "onlineACARS", True)
|
---|
243 | self._flareTimeFromFS = self._getBoolean(config, "general",
|
---|
244 | "flareTimeFromFS",
|
---|
245 | False)
|
---|
246 | self._syncFSTime = self._getBoolean(config, "general",
|
---|
247 | "syncFSTime",
|
---|
248 | False)
|
---|
249 | self._pirepDirectory = self._get(config, "general",
|
---|
250 | "pirepDirectory", None)
|
---|
251 |
|
---|
252 | self._messageTypeLevels = {}
|
---|
253 | for messageType in const.messageTypes:
|
---|
254 | self._messageTypeLevels[messageType] = \
|
---|
255 | self._getMessageTypeLevel(config, messageType)
|
---|
256 |
|
---|
257 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
258 | self._updateURL = self._get(config, "update", "url",
|
---|
259 | Config.DEFAULT_UPDATE_URL)
|
---|
260 |
|
---|
261 | self._modified = False
|
---|
262 |
|
---|
263 | def save(self):
|
---|
264 | """Save the configuration file if it has been modified."""
|
---|
265 | if not self._modified:
|
---|
266 | return
|
---|
267 |
|
---|
268 | config = ConfigParser.RawConfigParser()
|
---|
269 |
|
---|
270 | config.add_section("login")
|
---|
271 | config.set("login", "id", self._pilotID)
|
---|
272 | config.set("login", "password", self._password)
|
---|
273 | config.set("login", "rememberPassword",
|
---|
274 | "yes" if self._rememberPassword else "no")
|
---|
275 |
|
---|
276 | config.add_section("general")
|
---|
277 | if self._language:
|
---|
278 | config.set("general", "language", self._language)
|
---|
279 | config.set("general", "hideMinimizedWindow",
|
---|
280 | "yes" if self._hideMinimizedWindow else "no")
|
---|
281 | config.set("general", "onlineGateSystem",
|
---|
282 | "yes" if self._onlineGateSystem else "no")
|
---|
283 | config.set("general", "onlineACARS",
|
---|
284 | "yes" if self._onlineACARS else "no")
|
---|
285 | config.set("general", "flareTimeFromFS",
|
---|
286 | "yes" if self._flareTimeFromFS else "no")
|
---|
287 | config.set("general", "syncFSTime",
|
---|
288 | "yes" if self._syncFSTime else "no")
|
---|
289 |
|
---|
290 | if self._pirepDirectory is not None:
|
---|
291 | config.set("general", "pirepDirectory", self._pirepDirectory)
|
---|
292 |
|
---|
293 | config.add_section(Config._messageTypesSection)
|
---|
294 | for messageType in const.messageTypes:
|
---|
295 | if messageType in self._messageTypeLevels:
|
---|
296 | option = self._getMessageTypeLevelOptionName(messageType)
|
---|
297 | level = self._messageTypeLevels[messageType]
|
---|
298 | config.set(Config._messageTypesSection, option,
|
---|
299 | const.messageLevel2string(level))
|
---|
300 |
|
---|
301 | config.add_section("update")
|
---|
302 | config.set("update", "auto",
|
---|
303 | "yes" if self._autoUpdate else "no")
|
---|
304 | config.set("update", "url", self._updateURL)
|
---|
305 |
|
---|
306 | try:
|
---|
307 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
308 | 0600)
|
---|
309 | with os.fdopen(fd, "wt") as f:
|
---|
310 | config.write(f)
|
---|
311 | self._modified = False
|
---|
312 | except Exception, e:
|
---|
313 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
314 |
|
---|
315 | def _getBoolean(self, config, section, option, default):
|
---|
316 | """Get the given option as a boolean, if found in the given config,
|
---|
317 | otherwise the default."""
|
---|
318 | return config.getboolean(section, option) \
|
---|
319 | if config.has_option(section, option) \
|
---|
320 | else default
|
---|
321 |
|
---|
322 | def _get(self, config, section, option, default):
|
---|
323 | """Get the given option as a string, if found in the given config,
|
---|
324 | otherwise the default."""
|
---|
325 | return config.get(section, option) \
|
---|
326 | if config.has_option(section, option) \
|
---|
327 | else default
|
---|
328 |
|
---|
329 | def _getMessageTypeLevel(self, config, messageType):
|
---|
330 | """Get the message type level for the given message type."""
|
---|
331 | option = self._getMessageTypeLevelOptionName(messageType)
|
---|
332 | if config.has_option(Config._messageTypesSection, option):
|
---|
333 | value = config.get(Config._messageTypesSection, option)
|
---|
334 | return const.string2messageLevel(value)
|
---|
335 | elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
|
---|
336 | const.MESSAGETYPE_FAULT,
|
---|
337 | const.MESSAGETYPE_NOGO,
|
---|
338 | const.MESSAGETYPE_GATE_SYSTEM,
|
---|
339 | const.MESSAGETYPE_HELP]:
|
---|
340 | return const.MESSAGELEVEL_BOTH
|
---|
341 | else:
|
---|
342 | return const.MESSAGELEVEL_FS
|
---|
343 |
|
---|
344 | def _getMessageTypeLevelOptionName(self, messageType):
|
---|
345 | """Get the option name for the given message type level."""
|
---|
346 | return const.messageType2string(messageType)
|
---|
347 |
|
---|
348 | def getLanguage(self):
|
---|
349 | """Get the language to be used."""
|
---|
350 | import locale
|
---|
351 | if self._language:
|
---|
352 | if os.name=="nt":
|
---|
353 | if self._language in _languageMap:
|
---|
354 | locale.setlocale(locale.LC_ALL, _languageMap[self._language])
|
---|
355 | else:
|
---|
356 | locale.setlocale(locale.LC_ALL, "")
|
---|
357 | else:
|
---|
358 | locale.setlocale(locale.LC_ALL, (self._language,
|
---|
359 | locale.getpreferredencoding()))
|
---|
360 | return self._language
|
---|
361 | else:
|
---|
362 | locale.setlocale(locale.LC_ALL, "")
|
---|
363 | return locale.getdefaultlocale()[0]
|
---|
364 |
|
---|
365 | #-------------------------------------------------------------------------------
|
---|