source: src/mlx/config.py@ 132:92f78dc5b965

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

Added support for enabling/disabling message types

File size: 8.9 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 setMessageTypeLevel(self, messageType, level):
117 """Set the level of the given message type."""
118 if messageType not in self._messageTypeLevels or \
119 self._messageTypeLevels[messageType]!=level:
120 self._messageTypeLevels[messageType] = level
121 self._modified = True
122
123 @property
124 def autoUpdate(self):
125 """Get if an automatic update is needed."""
126 return self._autoUpdate
127
128 @autoUpdate.setter
129 def autoUpdate(self, autoUpdate):
130 """Set if an automatic update is needed."""
131 if autoUpdate!=self._autoUpdate:
132 self._autoUpdate = autoUpdate
133 self._modified = True
134
135 @property
136 def updateURL(self):
137 """Get the update URL."""
138 return self._updateURL
139
140 @updateURL.setter
141 def updateURL(self, updateURL):
142 """Set the update URL."""
143 if updateURL!=self._updateURL:
144 self._updateURL = updateURL
145 self._modified = True
146
147 def load(self):
148 """Load the configuration from its default location."""
149 config = ConfigParser.RawConfigParser()
150 config.read(configPath)
151
152 self._pilotID = self._get(config, "login", "id", "")
153 self._password = self._get(config, "login", "password", "")
154 self._rememberPassword = self._getBoolean(config, "login",
155 "rememberPassword", False)
156
157 self._language = self._get(config, "general", "language", "")
158 self._flareTimeFromFS = self._getBoolean(config, "general",
159 "flareTimeFromFS",
160 False)
161
162 self._messageTypeLevels = {}
163 for messageType in const.messageTypes:
164 self._messageTypeLevels[messageType] = \
165 self._getMessageTypeLevel(config, messageType)
166
167 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
168 self._updateURL = self._get(config, "update", "url",
169 Config.DEFAULT_UPDATE_URL)
170
171 self._modified = False
172
173 def save(self):
174 """Save the configuration file if it has been modified."""
175 if not self._modified:
176 return
177
178 config = ConfigParser.RawConfigParser()
179
180 config.add_section("login")
181 config.set("login", "id", self._pilotID)
182 config.set("login", "password", self._password)
183 config.set("login", "rememberPassword",
184 "yes" if self._rememberPassword else "no")
185
186 config.add_section("general")
187 if self._language:
188 config.set("general", "language", self._language)
189 config.set("general", "flareTimeFromFS",
190 "yes" if self._flareTimeFromFS else "no")
191
192 config.add_section(Config._messageTypesSection)
193 for messageType in const.messageTypes:
194 if messageType in self._messageTypeLevels:
195 option = self._getMessageTypeLevelOptionName(messageType)
196 level = self._messageTypeLevels[messageType]
197 config.set(Config._messageTypesSection, option,
198 const.messageLevel2string(level))
199
200 config.add_section("update")
201 config.set("update", "auto",
202 "yes" if self._autoUpdate else "no")
203 config.set("update", "url", self._updateURL)
204
205 try:
206 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
207 0600)
208 with os.fdopen(fd, "wt") as f:
209 config.write(f)
210 self._modified = False
211 except Exception, e:
212 print >> sys.stderr, "Failed to update config: " + str(e)
213
214 def _getBoolean(self, config, section, option, default):
215 """Get the given option as a boolean, if found in the given config,
216 otherwise the default."""
217 return config.getboolean(section, option) \
218 if config.has_option(section, option) \
219 else default
220
221 def _get(self, config, section, option, default):
222 """Get the given option as a string, if found in the given config,
223 otherwise the default."""
224 return config.get(section, option) \
225 if config.has_option(section, option) \
226 else default
227
228 def _getMessageTypeLevel(self, config, messageType):
229 """Get the message type level for the given message type."""
230 option = self._getMessageTypeLevelOptionName(messageType)
231 if config.has_option(Config._messageTypesSection, option):
232 value = config.get(Config._messageTypesSection, option)
233 return const.string2messageLevel(value)
234 else:
235 return const.MESSAGELEVEL_NONE
236
237 def _getMessageTypeLevelOptionName(self, messageType):
238 """Get the option name for the given message type level."""
239 return const.messageType2string(messageType)
240
241 def getLanguage(self):
242 """Get the language to be used."""
243 import locale
244 if self._language:
245 if os.name=="nt":
246 if self._language in _languageMap:
247 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
248 else:
249 locale.setlocale(locale.LC_ALL, "")
250 else:
251 locale.setlocale(locale.LC_ALL, (self._language,
252 locale.getpreferredencoding()))
253 return self._language
254 else:
255 locale.setlocale(locale.LC_ALL, "")
256 return locale.getdefaultlocale()[0]
257
258#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.