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