source: src/mlx/config.py@ 175:6db038066dec

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

Checklist saving/restoring and editing is implemented

File size: 22.5 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 Hotkey(object):
26 """A hotkey."""
27 def __init__(self, ctrl = False, shift = False, key = "0"):
28 """Construct the hotkey."""
29 self.ctrl = ctrl
30 self.shift = shift
31 self.key = key
32
33 def set(self, s):
34 """Set the hotkey from the given string."""
35 self.ctrl = "C" in s[:-1]
36 self.shift = "S" in s[:-1]
37 self.key = s[-1]
38
39 def __eq__(self, other):
40 """Check if the given hotkey is equal to the other one."""
41 return self.ctrl == other.ctrl and self.shift == other.shift and \
42 self.key == other.key
43
44 def __str__(self):
45 """Construct the hotkey to a string."""
46 s = ""
47 if self.ctrl: s += "C"
48 if self.shift: s += "S"
49 s += self.key
50 return s
51
52#-------------------------------------------------------------------------------
53
54class Checklist(object):
55 """A checklist for a certain aircraft type."""
56 # The name of the section of the checklists
57 SECTION="checklists"
58
59 @staticmethod
60 def fromConfig(config, aircraftType):
61 """Create a checklist for the given aircraft type from the given
62 config."""
63 baseName = "checklist." + const.icaoCodes[aircraftType] + "."
64 fileList = []
65 while True:
66 option = baseName + str(len(fileList))
67 if config.has_option(Checklist.SECTION, option):
68 fileList.append(config.get(Checklist.SECTION, option))
69 else:
70 break
71
72 return Checklist(fileList)
73
74 def __init__(self, fileList = None):
75 """Construct the check list with the given file list."""
76 self._fileList = [] if fileList is None else fileList[:]
77
78 def clone(self):
79 """Clone the checklist."""
80 return Checklist(self._fileList)
81
82 def toConfig(self, config, aircraftType):
83 """Add this checklist to the given config."""
84 baseName = "checklist." + const.icaoCodes[aircraftType] + "."
85 for index in range(0, len(self._fileList)):
86 option = baseName + str(index)
87 config.set(Checklist.SECTION, option,
88 self._fileList[index])
89
90 def __eq__(self, other):
91 """Determine if the checklist is equal to the given other one."""
92 return self._fileList == other._fileList
93
94 def __len__(self):
95 """Get the length of the file list."""
96 return len(self._fileList)
97
98 def __getitem__(self, index):
99 """Get the file with the given index."""
100 return self._fileList[index]
101
102 def __iter__(self):
103 """Iterate over the files."""
104 return iter(self._fileList)
105
106#-------------------------------------------------------------------------------
107
108class Config(object):
109 """Our configuration."""
110 DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
111
112 _messageTypesSection = "messageTypes"
113
114 def __init__(self):
115 """Construct the configuration with default values."""
116
117 self._pilotID = ""
118 self._password = ""
119 self._rememberPassword = False
120
121 self._language = ""
122 self._hideMinimizedWindow = True
123 self._onlineGateSystem = True
124 self._onlineACARS = True
125 self._flareTimeFromFS = False
126 self._syncFSTime = False
127
128 self._pirepDirectory = None
129
130 self._enableSounds = True
131
132 self._pilotControlsSounds = True
133 self._pilotHotkey = Hotkey(ctrl = True, shift = False, key = "0")
134
135 #self._approachCallOuts = False
136 self._speedbrakeAtTD = True
137
138 self._enableChecklists = False
139 self._checklistHotkey = Hotkey(ctrl = True, shift = True, key = "0")
140
141 self._autoUpdate = True
142 self._updateURL = Config.DEFAULT_UPDATE_URL
143
144 self._messageTypeLevels = {}
145
146 self._checklists = {}
147 for aircraftType in const.aircraftTypes:
148 self._checklists[aircraftType] = Checklist()
149
150 self._modified = False
151
152 @property
153 def pilotID(self):
154 """Get the pilot ID."""
155 return self._pilotID
156
157 @pilotID.setter
158 def pilotID(self, pilotID):
159 """Set the pilot ID."""
160 if pilotID!=self._pilotID:
161 self._pilotID = pilotID
162 self._modified = True
163
164 @property
165 def password(self):
166 """Get the password."""
167 return self._password
168
169 @password.setter
170 def password(self, password):
171 """Set the password."""
172 if password!=self._password:
173 self._password = password
174 self._modified = True
175
176 @property
177 def rememberPassword(self):
178 """Get if we should remember the password."""
179 return self._rememberPassword
180
181 @rememberPassword.setter
182 def rememberPassword(self, rememberPassword):
183 """Set if we should remember the password."""
184 if rememberPassword!=self._rememberPassword:
185 self._rememberPassword = rememberPassword
186 self._modified = True
187
188 @property
189 def language(self):
190 """Get the language to use."""
191 return self._language
192
193 @language.setter
194 def language(self, language):
195 """Set the language to use."""
196 if language!=self._language:
197 self._language = language
198 self._modified = True
199
200 @property
201 def hideMinimizedWindow(self):
202 """Get whether a minimized window should be hidden."""
203 return self._hideMinimizedWindow
204
205 @hideMinimizedWindow.setter
206 def hideMinimizedWindow(self, hideMinimizedWindow):
207 """Set whether a minimized window should be hidden."""
208 if hideMinimizedWindow!=self._hideMinimizedWindow:
209 self._hideMinimizedWindow = hideMinimizedWindow
210 self._modified = True
211
212 @property
213 def onlineGateSystem(self):
214 """Get whether the online gate system should be used."""
215 return self._onlineGateSystem
216
217 @onlineGateSystem.setter
218 def onlineGateSystem(self, onlineGateSystem):
219 """Set whether the online gate system should be used."""
220 if onlineGateSystem!=self._onlineGateSystem:
221 self._onlineGateSystem = onlineGateSystem
222 self._modified = True
223
224 @property
225 def onlineACARS(self):
226 """Get whether the online ACARS system should be used."""
227 return self._onlineACARS
228
229 @onlineACARS.setter
230 def onlineACARS(self, onlineACARS):
231 """Set whether the online ACARS system should be used."""
232 if onlineACARS!=self._onlineACARS:
233 self._onlineACARS = onlineACARS
234 self._modified = True
235
236 @property
237 def flareTimeFromFS(self):
238 """Get whether the flare time should be calculated from the time values
239 returned by the simulator."""
240 return self._flareTimeFromFS
241
242 @flareTimeFromFS.setter
243 def flareTimeFromFS(self, flareTimeFromFS):
244 """Set whether the flare time should be calculated from the time values
245 returned by the simulator."""
246 if flareTimeFromFS!=self._flareTimeFromFS:
247 self._flareTimeFromFS = flareTimeFromFS
248 self._modified = True
249
250 @property
251 def syncFSTime(self):
252 """Get whether the simulator's time should be synchronized with the
253 machine's clock."""
254 return self._syncFSTime
255
256 @syncFSTime.setter
257 def syncFSTime(self, syncFSTime):
258 """Set whether the simulator's time should be synchronized with the
259 machine's clock."""
260 if syncFSTime!=self._syncFSTime:
261 self._syncFSTime = syncFSTime
262 self._modified = True
263
264 @property
265 def pirepDirectory(self):
266 """Get the directory offered by default when saving a PIREP."""
267 return self._pirepDirectory
268
269 @pirepDirectory.setter
270 def pirepDirectory(self, pirepDirectory):
271 """Get the directory offered by default when saving a PIREP."""
272 if pirepDirectory!=self._pirepDirectory and \
273 (pirepDirectory!="" or self._pirepDirectory is not None):
274 self._pirepDirectory = None if pirepDirectory=="" \
275 else pirepDirectory
276 self._modified = True
277
278 def getMessageTypeLevel(self, messageType):
279 """Get the level for the given message type."""
280 return self._messageTypeLevels[messageType] \
281 if messageType in self._messageTypeLevels \
282 else const.MESSAGELEVEL_NONE
283
284 def isMessageTypeFS(self, messageType):
285 """Determine if the given message type is displayed in the
286 simulator."""
287 level = self.getMessageTypeLevel(messageType)
288 return level==const.MESSAGELEVEL_FS or \
289 level==const.MESSAGELEVEL_BOTH
290
291 def setMessageTypeLevel(self, messageType, level):
292 """Set the level of the given message type."""
293 if messageType not in self._messageTypeLevels or \
294 self._messageTypeLevels[messageType]!=level:
295 self._messageTypeLevels[messageType] = level
296 self._modified = True
297
298 @property
299 def enableSounds(self):
300 """Get whether background sounds are enabled."""
301 return self._enableSounds
302
303 @enableSounds.setter
304 def enableSounds(self, enableSounds):
305 """Set whether background sounds are enabled."""
306 if enableSounds!=self._enableSounds:
307 self._enableSounds = enableSounds
308 self._modified = True
309
310 @property
311 def pilotControlsSounds(self):
312 """Get whether the pilot controls the background sounds."""
313 return self._pilotControlsSounds
314
315 @pilotControlsSounds.setter
316 def pilotControlsSounds(self, pilotControlsSounds):
317 """Set whether the pilot controls the background sounds."""
318 if pilotControlsSounds!=self._pilotControlsSounds:
319 self._pilotControlsSounds = pilotControlsSounds
320 self._modified = True
321
322 @property
323 def pilotHotkey(self):
324 """Get the pilot's hotkey."""
325 return self._pilotHotkey
326
327 @pilotHotkey.setter
328 def pilotHotkey(self, pilotHotkey):
329 """Set the pilot's hotkey."""
330 if pilotHotkey!=self._pilotHotkey:
331 self._pilotHotkey = pilotHotkey
332 self._modified = True
333
334 # @property
335 # def approachCallOuts(self):
336 # """Get whether the approach callouts should be played."""
337 # return self._approachCallOuts
338
339 # @approachCallOuts.setter
340 # def approachCallOuts(self, approachCallOuts):
341 # """Set whether the approach callouts should be played."""
342 # if approachCallOuts!=self._approachCallOuts:
343 # self._approachCallOuts = approachCallOuts
344 # self._modified = True
345
346 @property
347 def speedbrakeAtTD(self):
348 """Get whether the speedbrake sounds should be played at touchdown."""
349 return self._speedbrakeAtTD
350
351 @speedbrakeAtTD.setter
352 def speedbrakeAtTD(self, speedbrakeAtTD):
353 """Set whether the speedbrake sounds should be played at touchdown."""
354 if speedbrakeAtTD!=self._speedbrakeAtTD:
355 self._speedbrakeAtTD = speedbrakeAtTD
356 self._modified = True
357
358 @property
359 def enableChecklists(self):
360 """Get whether aircraft-specific checklists should be played."""
361 return self._enableChecklists
362
363 @enableChecklists.setter
364 def enableChecklists(self, enableChecklists):
365 """Get whether aircraft-specific checklists should be played."""
366 if enableChecklists!=self._enableChecklists:
367 self._enableChecklists = enableChecklists
368 self._modified = True
369
370 @property
371 def checklistHotkey(self):
372 """Get the checklist hotkey."""
373 return self._checklistHotkey
374
375 @checklistHotkey.setter
376 def checklistHotkey(self, checklistHotkey):
377 """Set the checklist hotkey."""
378 if checklistHotkey!=self._checklistHotkey:
379 self._checklistHotkey = checklistHotkey
380 self._modified = True
381
382 @property
383 def autoUpdate(self):
384 """Get if an automatic update is needed."""
385 return self._autoUpdate
386
387 @autoUpdate.setter
388 def autoUpdate(self, autoUpdate):
389 """Set if an automatic update is needed."""
390 if autoUpdate!=self._autoUpdate:
391 self._autoUpdate = autoUpdate
392 self._modified = True
393
394 @property
395 def updateURL(self):
396 """Get the update URL."""
397 return self._updateURL
398
399 @updateURL.setter
400 def updateURL(self, updateURL):
401 """Set the update URL."""
402 if updateURL!=self._updateURL:
403 self._updateURL = updateURL
404 self._modified = True
405
406 def getChecklist(self, aircraftType):
407 """Get the checklist for the given aircraft type."""
408 return self._checklists[aircraftType]
409
410 def setChecklist(self, aircraftType, checklist):
411 """Set the checklist for the given aircraft type."""
412 if checklist!=self._checklists[aircraftType]:
413 self._checklists[aircraftType] = checklist.clone()
414 self._modified = True
415
416 def load(self):
417 """Load the configuration from its default location."""
418 config = ConfigParser.RawConfigParser()
419 config.read(configPath)
420
421 self._pilotID = self._get(config, "login", "id", "")
422 self._password = self._get(config, "login", "password", "")
423 self._rememberPassword = self._getBoolean(config, "login",
424 "rememberPassword", False)
425
426 self._language = self._get(config, "general", "language", "")
427 self._hideMinimizedWindow = self._getBoolean(config, "general",
428 "hideMinimizedWindow",
429 True)
430 self._onlineGateSystem = self._getBoolean(config, "general",
431 "onlineGateSystem",
432 True)
433 self._onlineACARS = self._getBoolean(config, "general",
434 "onlineACARS", True)
435 self._flareTimeFromFS = self._getBoolean(config, "general",
436 "flareTimeFromFS",
437 False)
438 self._syncFSTime = self._getBoolean(config, "general",
439 "syncFSTime",
440 False)
441 self._pirepDirectory = self._get(config, "general",
442 "pirepDirectory", None)
443
444 self._messageTypeLevels = {}
445 for messageType in const.messageTypes:
446 self._messageTypeLevels[messageType] = \
447 self._getMessageTypeLevel(config, messageType)
448
449 self._enableSounds = self._getBoolean(config, "sounds",
450 "enable", True)
451 self._pilotControlsSounds = self._getBoolean(config, "sounds",
452 "pilotControls", True)
453 self._pilotHotkey.set(self._get(config, "sounds",
454 "pilotHotkey", "C0"))
455 #self._approachCallOuts = self._getBoolean(config, "sounds",
456 # "approachCallOuts", False)
457 self._speedbrakeAtTD = self._getBoolean(config, "sounds",
458 "speedbrakeAtTD", True)
459
460 self._enableChecklists = self._getBoolean(config, "sounds",
461 "enableChecklists", False)
462 self._checklistHotkey.set(self._get(config, "sounds",
463 "checklistHotkey", "CS0"))
464
465 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
466 self._updateURL = self._get(config, "update", "url",
467 Config.DEFAULT_UPDATE_URL)
468
469 for aircraftType in const.aircraftTypes:
470 self._checklists[aircraftType] = \
471 Checklist.fromConfig(config, aircraftType)
472
473 self._modified = False
474
475 def save(self):
476 """Save the configuration file if it has been modified."""
477 if not self._modified:
478 return
479
480 config = ConfigParser.RawConfigParser()
481
482 config.add_section("login")
483 config.set("login", "id", self._pilotID)
484 config.set("login", "password", self._password)
485 config.set("login", "rememberPassword",
486 "yes" if self._rememberPassword else "no")
487
488 config.add_section("general")
489 if self._language:
490 config.set("general", "language", self._language)
491 config.set("general", "hideMinimizedWindow",
492 "yes" if self._hideMinimizedWindow else "no")
493 config.set("general", "onlineGateSystem",
494 "yes" if self._onlineGateSystem else "no")
495 config.set("general", "onlineACARS",
496 "yes" if self._onlineACARS else "no")
497 config.set("general", "flareTimeFromFS",
498 "yes" if self._flareTimeFromFS else "no")
499 config.set("general", "syncFSTime",
500 "yes" if self._syncFSTime else "no")
501
502 if self._pirepDirectory is not None:
503 config.set("general", "pirepDirectory", self._pirepDirectory)
504
505 config.add_section(Config._messageTypesSection)
506 for messageType in const.messageTypes:
507 if messageType in self._messageTypeLevels:
508 option = self._getMessageTypeLevelOptionName(messageType)
509 level = self._messageTypeLevels[messageType]
510 config.set(Config._messageTypesSection, option,
511 const.messageLevel2string(level))
512
513 config.add_section("sounds")
514 config.set("sounds", "enable",
515 "yes" if self._enableSounds else "no")
516 config.set("sounds", "pilotControls",
517 "yes" if self._pilotControlsSounds else "no")
518 config.set("sounds", "pilotHotkey", str(self._pilotHotkey))
519 #config.set("sounds", "approachCallOuts",
520 # "yes" if self._approachCallOuts else "no")
521 config.set("sounds", "speedbrakeAtTD",
522 "yes" if self._speedbrakeAtTD else "no")
523
524 config.set("sounds", "enableChecklists",
525 "yes" if self._enableChecklists else "no")
526 config.set("sounds", "checklistHotkey",
527 str(self._checklistHotkey))
528
529 config.add_section("update")
530 config.set("update", "auto",
531 "yes" if self._autoUpdate else "no")
532 config.set("update", "url", self._updateURL)
533
534 config.add_section(Checklist.SECTION)
535 for aircraftType in const.aircraftTypes:
536 self._checklists[aircraftType].toConfig(config, aircraftType)
537
538 try:
539 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
540 0600)
541 with os.fdopen(fd, "wt") as f:
542 config.write(f)
543 self._modified = False
544 except Exception, e:
545 print >> sys.stderr, "Failed to update config: " + str(e)
546
547 def _getBoolean(self, config, section, option, default):
548 """Get the given option as a boolean, if found in the given config,
549 otherwise the default."""
550 return config.getboolean(section, option) \
551 if config.has_option(section, option) \
552 else default
553
554 def _get(self, config, section, option, default):
555 """Get the given option as a string, if found in the given config,
556 otherwise the default."""
557 return config.get(section, option) \
558 if config.has_option(section, option) \
559 else default
560
561 def _getMessageTypeLevel(self, config, messageType):
562 """Get the message type level for the given message type."""
563 option = self._getMessageTypeLevelOptionName(messageType)
564 if config.has_option(Config._messageTypesSection, option):
565 value = config.get(Config._messageTypesSection, option)
566 return const.string2messageLevel(value)
567 elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
568 const.MESSAGETYPE_FAULT,
569 const.MESSAGETYPE_NOGO,
570 const.MESSAGETYPE_GATE_SYSTEM,
571 const.MESSAGETYPE_HELP]:
572 return const.MESSAGELEVEL_BOTH
573 else:
574 return const.MESSAGELEVEL_FS
575
576 def _getMessageTypeLevelOptionName(self, messageType):
577 """Get the option name for the given message type level."""
578 return const.messageType2string(messageType)
579
580 def setupLocale(self):
581 """Setup the locale based on the language set.
582
583 Return True if a specific language was set, False otherwise."""
584 import locale
585 if self._language:
586 print "Setting up locale for", self._language
587 os.environ["LANGUAGE"] = self._language
588 langAndEncoding = self._language + "." + locale.getpreferredencoding()
589 os.environ["LANG"] = langAndEncoding
590 os.environ["LC_MESSAGES"] = langAndEncoding
591 os.environ["LC_COLLATE"] = langAndEncoding
592 os.environ["LC_CTYPE"] = langAndEncoding
593 os.environ["LC_MONETARY"] = langAndEncoding
594 os.environ["LC_NUMERIC"] = langAndEncoding
595 os.environ["LC_TIME"] = langAndEncoding
596 return True
597 else:
598 return False
599
600 def getLanguage(self):
601 """Get the language to be used."""
602 import locale
603 if self._language:
604 if os.name=="nt":
605 if self._language in _languageMap:
606 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
607 else:
608 locale.setlocale(locale.LC_ALL, "")
609 else:
610 locale.setlocale(locale.LC_ALL, (self._language,
611 locale.getpreferredencoding()))
612 return self._language
613 else:
614 locale.setlocale(locale.LC_ALL, "")
615 return locale.getdefaultlocale()[0]
616
617#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.