source: src/mlx/config.py@ 267:a4e5f3f529d7

Last change on this file since 267:a4e5f3f529d7 was 266:b80521b41013, checked in by István Váradi <ivaradi@…>, 12 years ago

The approach callout editor works

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