source: src/mlx/config.py@ 265:441bc4537a91

Last change on this file since 265:441bc4537a91 was 264:6fbadb22b2c3, checked in by István Váradi <ivaradi@…>, 12 years ago

Started implementing the approach callout editor

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 print "Updating approach callouts"
587 self._approachCallouts[aircraftType] = approachCallouts.clone()
588 self._modified = True
589
590 def load(self):
591 """Load the configuration from its default location."""
592 config = ConfigParser.RawConfigParser()
593 config.read(configPath)
594
595 self._pilotID = self._get(config, "login", "id", "")
596 self._password = self._get(config, "login", "password", "")
597 self._rememberPassword = self._getBoolean(config, "login",
598 "rememberPassword", False)
599
600 self._language = self._get(config, "general", "language", "")
601
602 self._hideMinimizedWindow = self._getBoolean(config, "general",
603 "hideMinimizedWindow",
604 True)
605 self._quitOnClose = self._getBoolean(config, "general",
606 "quitOnClose", False)
607
608 self._onlineGateSystem = self._getBoolean(config, "general",
609 "onlineGateSystem",
610 True)
611 self._onlineACARS = self._getBoolean(config, "general",
612 "onlineACARS", True)
613 self._flareTimeFromFS = self._getBoolean(config, "general",
614 "flareTimeFromFS",
615 False)
616 self._syncFSTime = self._getBoolean(config, "general",
617 "syncFSTime",
618 False)
619 self._usingFS2Crew = self._getBoolean(config, "general",
620 "usingFS2Crew",
621 False)
622 self._iasSmoothingLength = int(self._get(config, "general",
623 "iasSmoothingLength",
624 -2))
625 self._vsSmoothingLength = int(self._get(config, "general",
626 "vsSmoothingLength",
627 -2))
628 self._pirepDirectory = self._get(config, "general",
629 "pirepDirectory", None)
630
631 self._messageTypeLevels = {}
632 for messageType in const.messageTypes:
633 self._messageTypeLevels[messageType] = \
634 self._getMessageTypeLevel(config, messageType)
635
636 self._enableSounds = self._getBoolean(config, "sounds",
637 "enable", True)
638 self._pilotControlsSounds = self._getBoolean(config, "sounds",
639 "pilotControls", True)
640 self._pilotHotkey.set(self._get(config, "sounds",
641 "pilotHotkey", "C0"))
642 self._enableApproachCallOuts = \
643 self._getBoolean(config, "sounds", "enableApproachCallOuts", False)
644 self._speedbrakeAtTD = self._getBoolean(config, "sounds",
645 "speedbrakeAtTD", True)
646
647 self._enableChecklists = self._getBoolean(config, "sounds",
648 "enableChecklists", False)
649 self._checklistHotkey.set(self._get(config, "sounds",
650 "checklistHotkey", "CS0"))
651
652 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
653 self._updateURL = self._get(config, "update", "url",
654 Config.DEFAULT_UPDATE_URL)
655
656 for aircraftType in const.aircraftTypes:
657 self._checklists[aircraftType] = \
658 Checklist.fromConfig(config, aircraftType)
659 self._approachCallouts[aircraftType] = \
660 ApproachCallouts.fromConfig(config, aircraftType)
661
662 self._modified = False
663
664 def save(self):
665 """Save the configuration file if it has been modified."""
666 if not self._modified:
667 return
668
669 config = ConfigParser.RawConfigParser()
670
671 config.add_section("login")
672 config.set("login", "id", self._pilotID)
673 config.set("login", "password", self._password)
674 config.set("login", "rememberPassword",
675 "yes" if self._rememberPassword else "no")
676
677 config.add_section("general")
678 if self._language:
679 config.set("general", "language", self._language)
680 config.set("general", "hideMinimizedWindow",
681 "yes" if self._hideMinimizedWindow else "no")
682 config.set("general", "quitOnClose",
683 "yes" if self._quitOnClose else "no")
684 config.set("general", "onlineGateSystem",
685 "yes" if self._onlineGateSystem else "no")
686 config.set("general", "onlineACARS",
687 "yes" if self._onlineACARS else "no")
688 config.set("general", "flareTimeFromFS",
689 "yes" if self._flareTimeFromFS else "no")
690 config.set("general", "syncFSTime",
691 "yes" if self._syncFSTime else "no")
692 config.set("general", "usingFS2Crew",
693 "yes" if self._usingFS2Crew else "no")
694 config.set("general", "iasSmoothingLength",
695 str(self._iasSmoothingLength))
696 config.set("general", "vsSmoothingLength",
697 str(self._vsSmoothingLength))
698
699 if self._pirepDirectory is not None:
700 config.set("general", "pirepDirectory", self._pirepDirectory)
701
702 config.add_section(Config._messageTypesSection)
703 for messageType in const.messageTypes:
704 if messageType in self._messageTypeLevels:
705 option = self._getMessageTypeLevelOptionName(messageType)
706 level = self._messageTypeLevels[messageType]
707 config.set(Config._messageTypesSection, option,
708 const.messageLevel2string(level))
709
710 config.add_section("sounds")
711 config.set("sounds", "enable",
712 "yes" if self._enableSounds else "no")
713 config.set("sounds", "pilotControls",
714 "yes" if self._pilotControlsSounds else "no")
715 config.set("sounds", "pilotHotkey", str(self._pilotHotkey))
716 #config.set("sounds", "approachCallOuts",
717 # "yes" if self._approachCallOuts else "no")
718 config.set("sounds", "speedbrakeAtTD",
719 "yes" if self._speedbrakeAtTD else "no")
720
721 config.set("sounds", "enableChecklists",
722 "yes" if self._enableChecklists else "no")
723 config.set("sounds", "checklistHotkey",
724 str(self._checklistHotkey))
725
726 config.add_section("update")
727 config.set("update", "auto",
728 "yes" if self._autoUpdate else "no")
729 config.set("update", "url", self._updateURL)
730
731 config.add_section(Checklist.SECTION)
732 config.add_section(ApproachCallouts.SECTION)
733 for aircraftType in const.aircraftTypes:
734 self._checklists[aircraftType].toConfig(config, aircraftType)
735 self._approachCallouts[aircraftType].toConfig(config, aircraftType)
736
737 try:
738 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
739 0600)
740 with os.fdopen(fd, "wt") as f:
741 config.write(f)
742 self._modified = False
743 except Exception, e:
744 print >> sys.stderr, "Failed to update config: " + str(e)
745
746 def _getBoolean(self, config, section, option, default):
747 """Get the given option as a boolean, if found in the given config,
748 otherwise the default."""
749 return config.getboolean(section, option) \
750 if config.has_option(section, option) \
751 else default
752
753 def _get(self, config, section, option, default):
754 """Get the given option as a string, if found in the given config,
755 otherwise the default."""
756 return config.get(section, option) \
757 if config.has_option(section, option) \
758 else default
759
760 def _getMessageTypeLevel(self, config, messageType):
761 """Get the message type level for the given message type."""
762 option = self._getMessageTypeLevelOptionName(messageType)
763 if config.has_option(Config._messageTypesSection, option):
764 value = config.get(Config._messageTypesSection, option)
765 return const.string2messageLevel(value)
766 elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
767 const.MESSAGETYPE_FAULT,
768 const.MESSAGETYPE_NOGO,
769 const.MESSAGETYPE_GATE_SYSTEM,
770 const.MESSAGETYPE_HELP]:
771 return const.MESSAGELEVEL_BOTH
772 else:
773 return const.MESSAGELEVEL_FS
774
775 def _getMessageTypeLevelOptionName(self, messageType):
776 """Get the option name for the given message type level."""
777 return const.messageType2string(messageType)
778
779 def setupLocale(self):
780 """Setup the locale based on the language set.
781
782 Return True if a specific language was set, False otherwise."""
783 import locale
784 if self._language:
785 print "Setting up locale for", self._language
786 os.environ["LANGUAGE"] = self._language
787 langAndEncoding = self._language + "." + locale.getpreferredencoding()
788 os.environ["LANG"] = langAndEncoding
789 os.environ["LC_MESSAGES"] = langAndEncoding
790 os.environ["LC_COLLATE"] = langAndEncoding
791 os.environ["LC_CTYPE"] = langAndEncoding
792 os.environ["LC_MONETARY"] = langAndEncoding
793 os.environ["LC_NUMERIC"] = langAndEncoding
794 os.environ["LC_TIME"] = langAndEncoding
795 return True
796 else:
797 return False
798
799 def getLanguage(self):
800 """Get the language to be used."""
801 import locale
802 if self._language:
803 if os.name=="nt":
804 if self._language in _languageMap:
805 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
806 else:
807 locale.setlocale(locale.LC_ALL, "")
808 else:
809 locale.setlocale(locale.LC_ALL, (self._language,
810 locale.getpreferredencoding()))
811 return self._language
812 else:
813 locale.setlocale(locale.LC_ALL, "")
814 return locale.getdefaultlocale()[0]
815
816#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.