source: src/mlx/config.py@ 309:a712392e1211

Last change on this file since 309:a712392e1211 was 298:24c67ec5cdca, checked in by István Váradi <ivaradi@…>, 12 years ago

Documented the non-GUI modules

File size: 30.6 KB
Line 
1# -*- encoding: utf-8 -*-
2
3import const
4
5import os
6import sys
7import ConfigParser
8
9## @package mlx.config
10#
11# The handling of the configuration.
12#
13# The \ref Config class contains the main configuration and is capable of
14# loading and saving the configuration. It contains getters and setters for the
15# configuration options.
16#
17# Some parts of the configuration are not simple data items, like strings or
18# booleans, but more complicated data. These have their own class, like \ref
19# ApproachCallouts or \ref Checklist.
20
21#-------------------------------------------------------------------------------
22
23configPath = os.path.join(os.path.expanduser("~"),
24 "mlx.config" if os.name=="nt" else ".mlxrc")
25
26#-------------------------------------------------------------------------------
27
28if os.name=="nt":
29 _languageMap = { "en_GB" : "eng",
30 "hu_HU" : "hun" }
31
32#-------------------------------------------------------------------------------
33
34class Hotkey(object):
35 """A hotkey."""
36 def __init__(self, ctrl = False, shift = False, key = "0"):
37 """Construct the hotkey."""
38 self.ctrl = ctrl
39 self.shift = shift
40 self.key = key
41
42 def set(self, s):
43 """Set the hotkey from the given string."""
44 self.ctrl = "C" in s[:-1]
45 self.shift = "S" in s[:-1]
46 self.key = s[-1]
47
48 def __eq__(self, other):
49 """Check if the given hotkey is equal to the other one."""
50 return self.ctrl == other.ctrl and self.shift == other.shift and \
51 self.key == other.key
52
53 def __ne__(self, other):
54 """Check if the given hotkey is not equal to the other one."""
55 return not self==other
56
57 def __str__(self):
58 """Construct the hotkey to a string."""
59 s = ""
60 if self.ctrl: s += "C"
61 if self.shift: s += "S"
62 s += self.key
63 return s
64
65#-------------------------------------------------------------------------------
66
67class Checklist(object):
68 """A checklist for a certain aircraft type."""
69 # The name of the section of the checklists
70 SECTION="checklists"
71
72 @staticmethod
73 def fromConfig(config, aircraftType):
74 """Create a checklist for the given aircraft type from the given
75 config."""
76 baseName = "checklist." + const.icaoCodes[aircraftType] + "."
77 fileList = []
78 while True:
79 option = baseName + str(len(fileList))
80 if config.has_option(Checklist.SECTION, option):
81 fileList.append(config.get(Checklist.SECTION, option))
82 else:
83 break
84
85 return Checklist(fileList)
86
87 def __init__(self, fileList = None):
88 """Construct the check list with the given file list."""
89 self._fileList = [] if fileList is None else fileList[:]
90
91 def clone(self):
92 """Clone the checklist."""
93 return Checklist(self._fileList)
94
95 def toConfig(self, config, aircraftType):
96 """Add this checklist to the given config."""
97 baseName = "checklist." + const.icaoCodes[aircraftType] + "."
98 for index in range(0, len(self._fileList)):
99 option = baseName + str(index)
100 config.set(Checklist.SECTION, option,
101 self._fileList[index])
102
103 def __eq__(self, other):
104 """Determine if the checklist is equal to the given other one."""
105 return self._fileList == other._fileList
106
107 def __ne__(self, other):
108 """Determine if the checklist is not equal to the given other one."""
109 return not self==other
110
111 def __len__(self):
112 """Get the length of the file list."""
113 return len(self._fileList)
114
115 def __getitem__(self, index):
116 """Get the file with the given index."""
117 return self._fileList[index]
118
119 def __iter__(self):
120 """Iterate over the files."""
121 return iter(self._fileList)
122
123#-------------------------------------------------------------------------------
124
125class ApproachCallouts(object):
126 """The approach callouts for a certain aircraft type."""
127 # The name of the section of the approach callouts
128 SECTION="callouts"
129
130 @staticmethod
131 def fromConfig(config, aircraftType):
132 """Create a checklist for the given aircraft type from the given
133 config."""
134 baseName = "callouts." + const.icaoCodes[aircraftType] + "."
135 mapping = {}
136 while True:
137 option = baseName + str(len(mapping))
138 if config.has_option(ApproachCallouts.SECTION, option):
139 value = config.get(ApproachCallouts.SECTION, option)
140 (altitude, path) = value.split(",")
141 altitude = int(altitude.strip())
142 path = path.strip()
143 mapping[altitude] = path
144 else:
145 break
146
147 return ApproachCallouts(mapping)
148
149 def __init__(self, mapping = None):
150 """Construct the check list with the given mapping of altitudes to
151 files."""
152 self._mapping = {} if mapping is None else mapping.copy()
153
154 def clone(self):
155 """Clone the callout information."""
156 return ApproachCallouts(self._mapping)
157
158 def toConfig(self, config, aircraftType):
159 """Add this checklist to the given config."""
160 baseName = "callouts." + const.icaoCodes[aircraftType] + "."
161 index = 0
162 for (altitude, path) in self._mapping.iteritems():
163 option = baseName + str(index)
164 config.set(ApproachCallouts.SECTION, option,
165 "%d, %s" % (altitude, path))
166 index += 1
167
168 def getAltitudes(self, descending = True):
169 """Get the altitudes in decreasing order by default."""
170 altitudes = self._mapping.keys()
171 altitudes.sort(reverse = descending)
172 return altitudes
173
174 def __nonzero__(self):
175 """Return if there is anything in the mapping."""
176 return not not self._mapping
177
178 def __eq__(self, other):
179 """Determine if the approach callout mapping is equal to the given
180 other one."""
181 return self._mapping == other._mapping
182
183 def __ne__(self, other):
184 """Determine if the approach callout mapping is not equal to the given
185 other one."""
186 return not self==other
187
188 def __len__(self):
189 """Get the number of elements in the mapping."""
190 return len(self._mapping)
191
192 def __getitem__(self, altitude):
193 """Get the file that is associated with the given altitude.
194
195 If no such file found, return None."""
196 return self._mapping[altitude] if altitude in self._mapping else None
197
198 def __iter__(self):
199 """Iterate over the pairs of altitudes and paths in decreasing order of
200 the altitude."""
201 altitudes = self.getAltitudes()
202
203 for altitude in altitudes:
204 yield (altitude, self._mapping[altitude])
205
206#-------------------------------------------------------------------------------
207
208class Config(object):
209 """Our configuration."""
210 DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
211
212 _messageTypesSection = "messageTypes"
213
214 def __init__(self):
215 """Construct the configuration with default values."""
216
217 self._pilotID = ""
218 self._password = ""
219 self._rememberPassword = False
220
221 self._language = ""
222 self._hideMinimizedWindow = True
223 self._quitOnClose = False
224 self._onlineGateSystem = True
225 self._onlineACARS = True
226 self._flareTimeFromFS = False
227 self._syncFSTime = False
228 self._usingFS2Crew = False
229 self._iasSmoothingLength = -2
230 self._vsSmoothingLength = -2
231
232 self._pirepDirectory = None
233
234 self._enableSounds = True
235
236 self._pilotControlsSounds = True
237 self._pilotHotkey = Hotkey(ctrl = True, shift = False, key = "0")
238
239 self._enableApproachCallouts = False
240 self._speedbrakeAtTD = True
241
242 self._enableChecklists = False
243 self._checklistHotkey = Hotkey(ctrl = True, shift = True, key = "0")
244
245 self._autoUpdate = True
246 self._updateURL = Config.DEFAULT_UPDATE_URL
247
248 self._messageTypeLevels = {}
249
250 self._checklists = {}
251 self._approachCallouts = {}
252 for aircraftType in const.aircraftTypes:
253 self._checklists[aircraftType] = Checklist()
254 self._approachCallouts[aircraftType] = ApproachCallouts()
255
256 self._modified = False
257
258 @property
259 def pilotID(self):
260 """Get the pilot ID."""
261 return self._pilotID
262
263 @pilotID.setter
264 def pilotID(self, pilotID):
265 """Set the pilot ID."""
266 if pilotID!=self._pilotID:
267 self._pilotID = pilotID
268 self._modified = True
269
270 @property
271 def password(self):
272 """Get the password."""
273 return self._password
274
275 @password.setter
276 def password(self, password):
277 """Set the password."""
278 if password!=self._password:
279 self._password = password
280 self._modified = True
281
282 @property
283 def rememberPassword(self):
284 """Get if we should remember the password."""
285 return self._rememberPassword
286
287 @rememberPassword.setter
288 def rememberPassword(self, rememberPassword):
289 """Set if we should remember the password."""
290 if rememberPassword!=self._rememberPassword:
291 self._rememberPassword = rememberPassword
292 self._modified = True
293
294 @property
295 def language(self):
296 """Get the language to use."""
297 return self._language
298
299 @language.setter
300 def language(self, language):
301 """Set the language to use."""
302 if language!=self._language:
303 self._language = language
304 self._modified = True
305
306 @property
307 def hideMinimizedWindow(self):
308 """Get whether a minimized window should be hidden."""
309 return self._hideMinimizedWindow
310
311 @hideMinimizedWindow.setter
312 def hideMinimizedWindow(self, hideMinimizedWindow):
313 """Set whether a minimized window should be hidden."""
314 if hideMinimizedWindow!=self._hideMinimizedWindow:
315 self._hideMinimizedWindow = hideMinimizedWindow
316 self._modified = True
317
318 @property
319 def quitOnClose(self):
320 """Get whether the application should quit when the close button is
321 clicked."""
322 return self._quitOnClose
323
324 @quitOnClose.setter
325 def quitOnClose(self, quitOnClose):
326 """Set whether the application should quit when the close button is
327 clicked."""
328 if quitOnClose!=self._quitOnClose:
329 self._quitOnClose = quitOnClose
330 self._modified = True
331
332 @property
333 def onlineGateSystem(self):
334 """Get whether the online gate system should be used."""
335 return self._onlineGateSystem
336
337 @onlineGateSystem.setter
338 def onlineGateSystem(self, onlineGateSystem):
339 """Set whether the online gate system should be used."""
340 if onlineGateSystem!=self._onlineGateSystem:
341 self._onlineGateSystem = onlineGateSystem
342 self._modified = True
343
344 @property
345 def onlineACARS(self):
346 """Get whether the online ACARS system should be used."""
347 return self._onlineACARS
348
349 @onlineACARS.setter
350 def onlineACARS(self, onlineACARS):
351 """Set whether the online ACARS system should be used."""
352 if onlineACARS!=self._onlineACARS:
353 self._onlineACARS = onlineACARS
354 self._modified = True
355
356 @property
357 def flareTimeFromFS(self):
358 """Get whether the flare time should be calculated from the time values
359 returned by the simulator."""
360 return self._flareTimeFromFS
361
362 @flareTimeFromFS.setter
363 def flareTimeFromFS(self, flareTimeFromFS):
364 """Set whether the flare time should be calculated from the time values
365 returned by the simulator."""
366 if flareTimeFromFS!=self._flareTimeFromFS:
367 self._flareTimeFromFS = flareTimeFromFS
368 self._modified = True
369
370 @property
371 def syncFSTime(self):
372 """Get whether the simulator's time should be synchronized with the
373 machine's clock."""
374 return self._syncFSTime
375
376 @syncFSTime.setter
377 def syncFSTime(self, syncFSTime):
378 """Set whether the simulator's time should be synchronized with the
379 machine's clock."""
380 if syncFSTime!=self._syncFSTime:
381 self._syncFSTime = syncFSTime
382 self._modified = True
383
384 @property
385 def usingFS2Crew(self):
386 """Get whether the FS2Crew addon is being used."""
387 return self._usingFS2Crew
388
389 @usingFS2Crew.setter
390 def usingFS2Crew(self, usingFS2Crew):
391 """Set whether the FS2Crew addon is being used."""
392 if usingFS2Crew!=self._usingFS2Crew:
393 self._usingFS2Crew = usingFS2Crew
394 self._modified = True
395
396 @property
397 def iasSmoothingLength(self):
398 """Get the number of samples over which the IAS is averaged for the
399 smoothed IAS calculation. It may be negative, in which case smoothing
400 is disabled, but we nevertheless store the number of seconds in case it
401 may become useful later."""
402 return self._iasSmoothingLength
403
404 @property
405 def realIASSmoothingLength(self):
406 """Get the real smoothing length of IAS."""
407 return max(self._iasSmoothingLength, 1)
408
409 @iasSmoothingLength.setter
410 def iasSmoothingLength(self, iasSmoothingLength):
411 """Set the number of samples over which the IAS is averaged for the
412 smoothed IAS calculation."""
413 if iasSmoothingLength!=self._iasSmoothingLength:
414 self._iasSmoothingLength = iasSmoothingLength
415 self._modified = True
416
417 @property
418 def vsSmoothingLength(self):
419 """Get the number of samples over which the VS is averaged for the
420 smoothed VS calculation. It may be negative, in which case smoothing
421 is disabled, but we nevertheless store the number of seconds in case it
422 may become useful later."""
423 return self._vsSmoothingLength
424
425 @property
426 def realVSSmoothingLength(self):
427 """Get the real smoothing length of VS."""
428 return max(self._vsSmoothingLength, 1)
429
430 @vsSmoothingLength.setter
431 def vsSmoothingLength(self, vsSmoothingLength):
432 """Set the number of samples over which the VS is averaged for the
433 smoothed VS calculation."""
434 if vsSmoothingLength!=self._vsSmoothingLength:
435 self._vsSmoothingLength = vsSmoothingLength
436 self._modified = True
437
438 @property
439 def pirepDirectory(self):
440 """Get the directory offered by default when saving a PIREP."""
441 return self._pirepDirectory
442
443 @pirepDirectory.setter
444 def pirepDirectory(self, pirepDirectory):
445 """Get the directory offered by default when saving a PIREP."""
446 if pirepDirectory!=self._pirepDirectory and \
447 (pirepDirectory!="" or self._pirepDirectory is not None):
448 self._pirepDirectory = None if pirepDirectory=="" \
449 else pirepDirectory
450 self._modified = True
451
452 def getMessageTypeLevel(self, messageType):
453 """Get the level for the given message type."""
454 return self._messageTypeLevels[messageType] \
455 if messageType in self._messageTypeLevels \
456 else const.MESSAGELEVEL_NONE
457
458 def isMessageTypeFS(self, messageType):
459 """Determine if the given message type is displayed in the
460 simulator."""
461 level = self.getMessageTypeLevel(messageType)
462 return level==const.MESSAGELEVEL_FS or \
463 level==const.MESSAGELEVEL_BOTH
464
465 def setMessageTypeLevel(self, messageType, level):
466 """Set the level of the given message type."""
467 if messageType not in self._messageTypeLevels or \
468 self._messageTypeLevels[messageType]!=level:
469 self._messageTypeLevels[messageType] = level
470 self._modified = True
471
472 @property
473 def enableSounds(self):
474 """Get whether background sounds are enabled."""
475 return self._enableSounds
476
477 @enableSounds.setter
478 def enableSounds(self, enableSounds):
479 """Set whether background sounds are enabled."""
480 if enableSounds!=self._enableSounds:
481 self._enableSounds = enableSounds
482 self._modified = True
483
484 @property
485 def pilotControlsSounds(self):
486 """Get whether the pilot controls the background sounds."""
487 return self._pilotControlsSounds
488
489 @pilotControlsSounds.setter
490 def pilotControlsSounds(self, pilotControlsSounds):
491 """Set whether the pilot controls the background sounds."""
492 if pilotControlsSounds!=self._pilotControlsSounds:
493 self._pilotControlsSounds = pilotControlsSounds
494 self._modified = True
495
496 @property
497 def pilotHotkey(self):
498 """Get the pilot's hotkey."""
499 return self._pilotHotkey
500
501 @pilotHotkey.setter
502 def pilotHotkey(self, pilotHotkey):
503 """Set the pilot's hotkey."""
504 if pilotHotkey!=self._pilotHotkey:
505 self._pilotHotkey = pilotHotkey
506 self._modified = True
507
508 @property
509 def enableApproachCallouts(self):
510 """Get whether the approach callouts should be played."""
511 return self._enableApproachCallouts
512
513 @enableApproachCallouts.setter
514 def enableApproachCallouts(self, enableApproachCallouts):
515 """Set whether the approach callouts should be played."""
516 if enableApproachCallouts!=self._enableApproachCallouts:
517 self._enableApproachCallouts = enableApproachCallouts
518 self._modified = True
519
520 @property
521 def speedbrakeAtTD(self):
522 """Get whether the speedbrake sounds should be played at touchdown."""
523 return self._speedbrakeAtTD
524
525 @speedbrakeAtTD.setter
526 def speedbrakeAtTD(self, speedbrakeAtTD):
527 """Set whether the speedbrake sounds should be played at touchdown."""
528 if speedbrakeAtTD!=self._speedbrakeAtTD:
529 self._speedbrakeAtTD = speedbrakeAtTD
530 self._modified = True
531
532 @property
533 def enableChecklists(self):
534 """Get whether aircraft-specific checklists should be played."""
535 return self._enableChecklists
536
537 @enableChecklists.setter
538 def enableChecklists(self, enableChecklists):
539 """Get whether aircraft-specific checklists should be played."""
540 if enableChecklists!=self._enableChecklists:
541 self._enableChecklists = enableChecklists
542 self._modified = True
543
544 @property
545 def checklistHotkey(self):
546 """Get the checklist hotkey."""
547 return self._checklistHotkey
548
549 @checklistHotkey.setter
550 def checklistHotkey(self, checklistHotkey):
551 """Set the checklist hotkey."""
552 if checklistHotkey!=self._checklistHotkey:
553 self._checklistHotkey = checklistHotkey
554 self._modified = True
555
556 @property
557 def autoUpdate(self):
558 """Get if an automatic update is needed."""
559 return self._autoUpdate
560
561 @autoUpdate.setter
562 def autoUpdate(self, autoUpdate):
563 """Set if an automatic update is needed."""
564 if autoUpdate!=self._autoUpdate:
565 self._autoUpdate = autoUpdate
566 self._modified = True
567
568 @property
569 def updateURL(self):
570 """Get the update URL."""
571 return self._updateURL
572
573 @updateURL.setter
574 def updateURL(self, updateURL):
575 """Set the update URL."""
576 if updateURL!=self._updateURL:
577 self._updateURL = updateURL
578 self._modified = True
579
580 def getChecklist(self, aircraftType):
581 """Get the checklist for the given aircraft type."""
582 return self._checklists[aircraftType]
583
584 def setChecklist(self, aircraftType, checklist):
585 """Set the checklist for the given aircraft type."""
586 if checklist!=self._checklists[aircraftType]:
587 self._checklists[aircraftType] = checklist.clone()
588 self._modified = True
589
590 def getApproachCallouts(self, aircraftType):
591 """Get the approach callouts for the given aircraft type."""
592 return self._approachCallouts[aircraftType]
593
594 def setApproachCallouts(self, aircraftType, approachCallouts):
595 """Set the approach callouts for the given aircraft type."""
596 if not approachCallouts==self._approachCallouts[aircraftType]:
597 self._approachCallouts[aircraftType] = approachCallouts.clone()
598 self._modified = True
599
600 def load(self):
601 """Load the configuration from its default location."""
602 config = ConfigParser.RawConfigParser()
603 config.read(configPath)
604
605 self._pilotID = self._get(config, "login", "id", "")
606 self._password = self._get(config, "login", "password", "")
607 self._rememberPassword = self._getBoolean(config, "login",
608 "rememberPassword", False)
609
610 self._language = self._get(config, "general", "language", "")
611
612 self._hideMinimizedWindow = self._getBoolean(config, "general",
613 "hideMinimizedWindow",
614 True)
615 self._quitOnClose = self._getBoolean(config, "general",
616 "quitOnClose", False)
617
618 self._onlineGateSystem = self._getBoolean(config, "general",
619 "onlineGateSystem",
620 True)
621 self._onlineACARS = self._getBoolean(config, "general",
622 "onlineACARS", True)
623 self._flareTimeFromFS = self._getBoolean(config, "general",
624 "flareTimeFromFS",
625 False)
626 self._syncFSTime = self._getBoolean(config, "general",
627 "syncFSTime",
628 False)
629 self._usingFS2Crew = self._getBoolean(config, "general",
630 "usingFS2Crew",
631 False)
632 self._iasSmoothingLength = int(self._get(config, "general",
633 "iasSmoothingLength",
634 -2))
635 self._vsSmoothingLength = int(self._get(config, "general",
636 "vsSmoothingLength",
637 -2))
638 self._pirepDirectory = self._get(config, "general",
639 "pirepDirectory", None)
640
641 self._messageTypeLevels = {}
642 for messageType in const.messageTypes:
643 self._messageTypeLevels[messageType] = \
644 self._getMessageTypeLevel(config, messageType)
645
646 self._enableSounds = self._getBoolean(config, "sounds",
647 "enable", True)
648 self._pilotControlsSounds = self._getBoolean(config, "sounds",
649 "pilotControls", True)
650 self._pilotHotkey.set(self._get(config, "sounds",
651 "pilotHotkey", "C0"))
652 self._enableApproachCallouts = \
653 self._getBoolean(config, "sounds", "enableApproachCallouts", False)
654 self._speedbrakeAtTD = self._getBoolean(config, "sounds",
655 "speedbrakeAtTD", True)
656
657 self._enableChecklists = self._getBoolean(config, "sounds",
658 "enableChecklists", False)
659 self._checklistHotkey.set(self._get(config, "sounds",
660 "checklistHotkey", "CS0"))
661
662 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
663 self._updateURL = self._get(config, "update", "url",
664 Config.DEFAULT_UPDATE_URL)
665
666 for aircraftType in const.aircraftTypes:
667 self._checklists[aircraftType] = \
668 Checklist.fromConfig(config, aircraftType)
669 self._approachCallouts[aircraftType] = \
670 ApproachCallouts.fromConfig(config, aircraftType)
671
672 self._modified = False
673
674 def save(self):
675 """Save the configuration file if it has been modified."""
676 if not self._modified:
677 return
678
679 config = ConfigParser.RawConfigParser()
680
681 config.add_section("login")
682 config.set("login", "id", self._pilotID)
683 config.set("login", "password", self._password)
684 config.set("login", "rememberPassword",
685 "yes" if self._rememberPassword else "no")
686
687 config.add_section("general")
688 if self._language:
689 config.set("general", "language", self._language)
690 config.set("general", "hideMinimizedWindow",
691 "yes" if self._hideMinimizedWindow else "no")
692 config.set("general", "quitOnClose",
693 "yes" if self._quitOnClose else "no")
694 config.set("general", "onlineGateSystem",
695 "yes" if self._onlineGateSystem else "no")
696 config.set("general", "onlineACARS",
697 "yes" if self._onlineACARS else "no")
698 config.set("general", "flareTimeFromFS",
699 "yes" if self._flareTimeFromFS else "no")
700 config.set("general", "syncFSTime",
701 "yes" if self._syncFSTime else "no")
702 config.set("general", "usingFS2Crew",
703 "yes" if self._usingFS2Crew else "no")
704 config.set("general", "iasSmoothingLength",
705 str(self._iasSmoothingLength))
706 config.set("general", "vsSmoothingLength",
707 str(self._vsSmoothingLength))
708
709 if self._pirepDirectory is not None:
710 config.set("general", "pirepDirectory", self._pirepDirectory)
711
712 config.add_section(Config._messageTypesSection)
713 for messageType in const.messageTypes:
714 if messageType in self._messageTypeLevels:
715 option = self._getMessageTypeLevelOptionName(messageType)
716 level = self._messageTypeLevels[messageType]
717 config.set(Config._messageTypesSection, option,
718 const.messageLevel2string(level))
719
720 config.add_section("sounds")
721 config.set("sounds", "enable",
722 "yes" if self._enableSounds else "no")
723 config.set("sounds", "pilotControls",
724 "yes" if self._pilotControlsSounds else "no")
725 config.set("sounds", "pilotHotkey", str(self._pilotHotkey))
726 config.set("sounds", "enableApproachCallouts",
727 "yes" if self._enableApproachCallouts else "no")
728 config.set("sounds", "speedbrakeAtTD",
729 "yes" if self._speedbrakeAtTD else "no")
730
731 config.set("sounds", "enableChecklists",
732 "yes" if self._enableChecklists else "no")
733 config.set("sounds", "checklistHotkey",
734 str(self._checklistHotkey))
735
736 config.add_section("update")
737 config.set("update", "auto",
738 "yes" if self._autoUpdate else "no")
739 config.set("update", "url", self._updateURL)
740
741 config.add_section(Checklist.SECTION)
742 config.add_section(ApproachCallouts.SECTION)
743 for aircraftType in const.aircraftTypes:
744 self._checklists[aircraftType].toConfig(config, aircraftType)
745 self._approachCallouts[aircraftType].toConfig(config, aircraftType)
746
747 try:
748 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
749 0600)
750 with os.fdopen(fd, "wt") as f:
751 config.write(f)
752 self._modified = False
753 except Exception, e:
754 print >> sys.stderr, "Failed to update config: " + str(e)
755
756 def _getBoolean(self, config, section, option, default):
757 """Get the given option as a boolean, if found in the given config,
758 otherwise the default."""
759 return config.getboolean(section, option) \
760 if config.has_option(section, option) \
761 else default
762
763 def _get(self, config, section, option, default):
764 """Get the given option as a string, if found in the given config,
765 otherwise the default."""
766 return config.get(section, option) \
767 if config.has_option(section, option) \
768 else default
769
770 def _getMessageTypeLevel(self, config, messageType):
771 """Get the message type level for the given message type."""
772 option = self._getMessageTypeLevelOptionName(messageType)
773 if config.has_option(Config._messageTypesSection, option):
774 value = config.get(Config._messageTypesSection, option)
775 return const.string2messageLevel(value)
776 elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
777 const.MESSAGETYPE_FAULT,
778 const.MESSAGETYPE_NOGO,
779 const.MESSAGETYPE_GATE_SYSTEM,
780 const.MESSAGETYPE_HELP]:
781 return const.MESSAGELEVEL_BOTH
782 else:
783 return const.MESSAGELEVEL_FS
784
785 def _getMessageTypeLevelOptionName(self, messageType):
786 """Get the option name for the given message type level."""
787 return const.messageType2string(messageType)
788
789 def setupLocale(self):
790 """Setup the locale based on the language set.
791
792 Return True if a specific language was set, False otherwise."""
793 import locale
794 if self._language:
795 print "Setting up locale for", self._language
796 os.environ["LANGUAGE"] = self._language
797 langAndEncoding = self._language + "." + locale.getpreferredencoding()
798 os.environ["LANG"] = langAndEncoding
799 os.environ["LC_MESSAGES"] = langAndEncoding
800 os.environ["LC_COLLATE"] = langAndEncoding
801 os.environ["LC_CTYPE"] = langAndEncoding
802 os.environ["LC_MONETARY"] = langAndEncoding
803 os.environ["LC_NUMERIC"] = langAndEncoding
804 os.environ["LC_TIME"] = langAndEncoding
805 return True
806 else:
807 return False
808
809 def getLanguage(self):
810 """Get the language to be used."""
811 import locale
812 if self._language:
813 if os.name=="nt":
814 if self._language in _languageMap:
815 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
816 else:
817 locale.setlocale(locale.LC_ALL, "")
818 else:
819 locale.setlocale(locale.LC_ALL, (self._language,
820 locale.getpreferredencoding()))
821 return self._language
822 else:
823 locale.setlocale(locale.LC_ALL, "")
824 return locale.getdefaultlocale()[0]
825
826#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.