source: src/mlx/config.py@ 148:453ebaea9090

Last change on this file since 148:453ebaea9090 was 148:453ebaea9090, checked in by István Váradi <ivaradi@…>, 12 years ago

Implemented FS time synchronization

File size: 12.5 KB
Line 
1# Configuration and related stuff
2# -*- encoding: utf-8 -*-
3
4#-------------------------------------------------------------------------------
5
6import const
7
8import os
9import sys
10import ConfigParser
11
12#-------------------------------------------------------------------------------
13
14configPath = os.path.join(os.path.expanduser("~"),
15 "mlx.config" if os.name=="nt" else ".mlxrc")
16
17#-------------------------------------------------------------------------------
18
19if os.name=="nt":
20 _languageMap = { "en_GB" : "eng",
21 "hu_HU" : "hun" }
22
23#-------------------------------------------------------------------------------
24
25class Config(object):
26 """Our configuration."""
27 DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
28
29 _messageTypesSection = "messageTypes"
30
31 def __init__(self):
32 """Construct the configuration with default values."""
33
34 self._pilotID = ""
35 self._password = ""
36 self._rememberPassword = False
37
38 self._language = ""
39 self._hideMinimizedWindow = True
40 self._onlineGateSystem = True
41 self._onlineACARS = True
42 self._flareTimeFromFS = False
43 self._syncFSTime = False
44
45 self._autoUpdate = True
46 self._updateURL = Config.DEFAULT_UPDATE_URL
47
48 self._messageTypeLevels = {}
49
50 self._modified = False
51
52 @property
53 def pilotID(self):
54 """Get the pilot ID."""
55 return self._pilotID
56
57 @pilotID.setter
58 def pilotID(self, pilotID):
59 """Set the pilot ID."""
60 if pilotID!=self._pilotID:
61 self._pilotID = pilotID
62 self._modified = True
63
64 @property
65 def password(self):
66 """Get the password."""
67 return self._password
68
69 @password.setter
70 def password(self, password):
71 """Set the password."""
72 if password!=self._password:
73 self._password = password
74 self._modified = True
75
76 @property
77 def rememberPassword(self):
78 """Get if we should remember the password."""
79 return self._rememberPassword
80
81 @rememberPassword.setter
82 def rememberPassword(self, rememberPassword):
83 """Set if we should remember the password."""
84 if rememberPassword!=self._rememberPassword:
85 self._rememberPassword = rememberPassword
86 self._modified = True
87
88 @property
89 def language(self):
90 """Get the language to use."""
91 return self._language
92
93 @language.setter
94 def language(self, language):
95 """Set the language to use."""
96 if language!=self._language:
97 self._language = language
98 self._modified = True
99
100 @property
101 def hideMinimizedWindow(self):
102 """Get whether a minimized window should be hidden."""
103 return self._hideMinimizedWindow
104
105 @hideMinimizedWindow.setter
106 def hideMinimizedWindow(self, hideMinimizedWindow):
107 """Set whether a minimized window should be hidden."""
108 if hideMinimizedWindow!=self._hideMinimizedWindow:
109 self._hideMinimizedWindow = hideMinimizedWindow
110 self._modified = True
111
112 @property
113 def onlineGateSystem(self):
114 """Get whether the online gate system should be used."""
115 return self._onlineGateSystem
116
117 @onlineGateSystem.setter
118 def onlineGateSystem(self, onlineGateSystem):
119 """Set whether the online gate system should be used."""
120 if onlineGateSystem!=self._onlineGateSystem:
121 self._onlineGateSystem = onlineGateSystem
122 self._modified = True
123
124 @property
125 def onlineACARS(self):
126 """Get whether the online ACARS system should be used."""
127 return self._onlineACARS
128
129 @onlineACARS.setter
130 def onlineACARS(self, onlineACARS):
131 """Set whether the online ACARS system should be used."""
132 if onlineACARS!=self._onlineACARS:
133 self._onlineACARS = onlineACARS
134 self._modified = True
135
136 @property
137 def flareTimeFromFS(self):
138 """Get whether the flare time should be calculated from the time values
139 returned by the simulator."""
140 return self._flareTimeFromFS
141
142 @flareTimeFromFS.setter
143 def flareTimeFromFS(self, flareTimeFromFS):
144 """Set whether the flare time should be calculated from the time values
145 returned by the simulator."""
146 if flareTimeFromFS!=self._flareTimeFromFS:
147 self._flareTimeFromFS = flareTimeFromFS
148 self._modified = True
149
150 @property
151 def syncFSTime(self):
152 """Get whether the simulator's time should be synchronized with the
153 machine's clock."""
154 return self._syncFSTime
155
156 @syncFSTime.setter
157 def syncFSTime(self, syncFSTime):
158 """Set whether the simulator's time should be synchronized with the
159 machine's clock."""
160 if syncFSTime!=self._syncFSTime:
161 self._syncFSTime = syncFSTime
162 self._modified = True
163
164 def getMessageTypeLevel(self, messageType):
165 """Get the level for the given message type."""
166 return self._messageTypeLevels[messageType] \
167 if messageType in self._messageTypeLevels \
168 else const.MESSAGELEVEL_NONE
169
170 def isMessageTypeFS(self, messageType):
171 """Determine if the given message type is displayed in the
172 simulator."""
173 level = self.getMessageTypeLevel(messageType)
174 return level==const.MESSAGELEVEL_FS or \
175 level==const.MESSAGELEVEL_BOTH
176
177 def setMessageTypeLevel(self, messageType, level):
178 """Set the level of the given message type."""
179 if messageType not in self._messageTypeLevels or \
180 self._messageTypeLevels[messageType]!=level:
181 self._messageTypeLevels[messageType] = level
182 self._modified = True
183
184 @property
185 def autoUpdate(self):
186 """Get if an automatic update is needed."""
187 return self._autoUpdate
188
189 @autoUpdate.setter
190 def autoUpdate(self, autoUpdate):
191 """Set if an automatic update is needed."""
192 if autoUpdate!=self._autoUpdate:
193 self._autoUpdate = autoUpdate
194 self._modified = True
195
196 @property
197 def updateURL(self):
198 """Get the update URL."""
199 return self._updateURL
200
201 @updateURL.setter
202 def updateURL(self, updateURL):
203 """Set the update URL."""
204 if updateURL!=self._updateURL:
205 self._updateURL = updateURL
206 self._modified = True
207
208 def load(self):
209 """Load the configuration from its default location."""
210 config = ConfigParser.RawConfigParser()
211 config.read(configPath)
212
213 self._pilotID = self._get(config, "login", "id", "")
214 self._password = self._get(config, "login", "password", "")
215 self._rememberPassword = self._getBoolean(config, "login",
216 "rememberPassword", False)
217
218 self._language = self._get(config, "general", "language", "")
219 self._hideMinimizedWindow = self._getBoolean(config, "general",
220 "hideMinimizedWindow",
221 True)
222 self._onlineGateSystem = self._getBoolean(config, "general",
223 "onlineGateSystem",
224 True)
225 self._onlineACARS = self._getBoolean(config, "general",
226 "onlineACARS", True)
227 self._flareTimeFromFS = self._getBoolean(config, "general",
228 "flareTimeFromFS",
229 False)
230 self._syncFSTime = self._getBoolean(config, "general",
231 "syncFSTime",
232 False)
233
234 self._messageTypeLevels = {}
235 for messageType in const.messageTypes:
236 self._messageTypeLevels[messageType] = \
237 self._getMessageTypeLevel(config, messageType)
238
239 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
240 self._updateURL = self._get(config, "update", "url",
241 Config.DEFAULT_UPDATE_URL)
242
243 self._modified = False
244
245 def save(self):
246 """Save the configuration file if it has been modified."""
247 if not self._modified:
248 return
249
250 config = ConfigParser.RawConfigParser()
251
252 config.add_section("login")
253 config.set("login", "id", self._pilotID)
254 config.set("login", "password", self._password)
255 config.set("login", "rememberPassword",
256 "yes" if self._rememberPassword else "no")
257
258 config.add_section("general")
259 if self._language:
260 config.set("general", "language", self._language)
261 config.set("general", "hideMinimizedWindow",
262 "yes" if self._hideMinimizedWindow else "no")
263 config.set("general", "onlineGateSystem",
264 "yes" if self._onlineGateSystem else "no")
265 config.set("general", "onlineACARS",
266 "yes" if self._onlineACARS else "no")
267 config.set("general", "flareTimeFromFS",
268 "yes" if self._flareTimeFromFS else "no")
269 config.set("general", "syncFSTime",
270 "yes" if self._syncFSTime else "no")
271
272 config.add_section(Config._messageTypesSection)
273 for messageType in const.messageTypes:
274 if messageType in self._messageTypeLevels:
275 option = self._getMessageTypeLevelOptionName(messageType)
276 level = self._messageTypeLevels[messageType]
277 config.set(Config._messageTypesSection, option,
278 const.messageLevel2string(level))
279
280 config.add_section("update")
281 config.set("update", "auto",
282 "yes" if self._autoUpdate else "no")
283 config.set("update", "url", self._updateURL)
284
285 try:
286 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
287 0600)
288 with os.fdopen(fd, "wt") as f:
289 config.write(f)
290 self._modified = False
291 except Exception, e:
292 print >> sys.stderr, "Failed to update config: " + str(e)
293
294 def _getBoolean(self, config, section, option, default):
295 """Get the given option as a boolean, if found in the given config,
296 otherwise the default."""
297 return config.getboolean(section, option) \
298 if config.has_option(section, option) \
299 else default
300
301 def _get(self, config, section, option, default):
302 """Get the given option as a string, if found in the given config,
303 otherwise the default."""
304 return config.get(section, option) \
305 if config.has_option(section, option) \
306 else default
307
308 def _getMessageTypeLevel(self, config, messageType):
309 """Get the message type level for the given message type."""
310 option = self._getMessageTypeLevelOptionName(messageType)
311 if config.has_option(Config._messageTypesSection, option):
312 value = config.get(Config._messageTypesSection, option)
313 return const.string2messageLevel(value)
314 elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
315 const.MESSAGETYPE_FAULT,
316 const.MESSAGETYPE_NOGO,
317 const.MESSAGETYPE_GATE_SYSTEM,
318 const.MESSAGETYPE_HELP]:
319 return const.MESSAGELEVEL_BOTH
320 else:
321 return const.MESSAGELEVEL_FS
322
323 def _getMessageTypeLevelOptionName(self, messageType):
324 """Get the option name for the given message type level."""
325 return const.messageType2string(messageType)
326
327 def getLanguage(self):
328 """Get the language to be used."""
329 import locale
330 if self._language:
331 if os.name=="nt":
332 if self._language in _languageMap:
333 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
334 else:
335 locale.setlocale(locale.LC_ALL, "")
336 else:
337 locale.setlocale(locale.LC_ALL, (self._language,
338 locale.getpreferredencoding()))
339 return self._language
340 else:
341 locale.setlocale(locale.LC_ALL, "")
342 return locale.getdefaultlocale()[0]
343
344#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.