source: src/mlx/config.py@ 134:9ce031d5d4a9

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

Most of the remaining messages are implemented

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