source: src/mlx/config.py@ 147:7a297d30a0ce

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

Added option to hide the window when minimized

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