source: src/mlx/config.py@ 133:dcbe33497899

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

The general message sending works and the most important messages are sent

File size: 9.2 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 = ""
39 self._flareTimeFromFS = False
40
[40]41 self._autoUpdate = True
42 self._updateURL = Config.DEFAULT_UPDATE_URL
[132]43
44 self._messageTypeLevels = {}
[131]45
[123]46 self._modified = False
[107]47
[40]48 @property
[42]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
[45]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
[123]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
[131]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
[132]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
[131]123 @property
[40]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
[42]152 self._pilotID = self._get(config, "login", "id", "")
153 self._password = self._get(config, "login", "password", "")
[45]154 self._rememberPassword = self._getBoolean(config, "login",
155 "rememberPassword", False)
[42]156
[132]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
[40]167 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
168 self._updateURL = self._get(config, "update", "url",
169 Config.DEFAULT_UPDATE_URL)
[107]170
[40]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
[42]180 config.add_section("login")
181 config.set("login", "id", self._pilotID)
182 config.set("login", "password", self._password)
[45]183 config.set("login", "rememberPassword",
184 "yes" if self._rememberPassword else "no")
[42]185
[132]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
[40]200 config.add_section("update")
[45]201 config.set("update", "auto",
202 "yes" if self._autoUpdate else "no")
[40]203 config.set("update", "url", self._updateURL)
204
205 try:
[43]206 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
207 0600)
208 with os.fdopen(fd, "wt") as f:
[40]209 config.write(f)
210 self._modified = False
211 except Exception, e:
[43]212 print >> sys.stderr, "Failed to update config: " + str(e)
[40]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
[36]227
[132]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)
[133]234 elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
235 const.MESSAGETYPE_FAULT,
236 const.MESSAGETYPE_NOGO,
237 const.MESSAGETYPE_GATE_SYSTEM,
238 const.MESSAGETYPE_HELP]:
239 return const.MESSAGELEVEL_BOTH
[132]240 else:
[133]241 return const.MESSAGELEVEL_FS
[132]242
243 def _getMessageTypeLevelOptionName(self, messageType):
244 """Get the option name for the given message type level."""
245 return const.messageType2string(messageType)
246
[107]247 def getLanguage(self):
248 """Get the language to be used."""
[113]249 import locale
[107]250 if self._language:
[124]251 if os.name=="nt":
252 if self._language in _languageMap:
253 locale.setlocale(locale.LC_ALL, _languageMap[self._language])
254 else:
255 locale.setlocale(locale.LC_ALL, "")
256 else:
257 locale.setlocale(locale.LC_ALL, (self._language,
258 locale.getpreferredencoding()))
[107]259 return self._language
260 else:
[113]261 locale.setlocale(locale.LC_ALL, "")
[107]262 return locale.getdefaultlocale()[0]
263
[36]264#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.