source: src/mlx/config.py@ 136:6d206b573dee

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

Added option to enable/disable the online gate system

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