source: src/mlx/config.py@ 295:7340a9356485

Last change on this file since 295:7340a9356485 was 273:066f7271e849, checked in by István Váradi <ivaradi@…>, 12 years ago

Added the playback of approach callouts

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