1 | # Configuration and related stuff
|
---|
2 | # -*- encoding: utf-8 -*-
|
---|
3 |
|
---|
4 | #-------------------------------------------------------------------------------
|
---|
5 |
|
---|
6 | import os
|
---|
7 | import sys
|
---|
8 | import ConfigParser
|
---|
9 |
|
---|
10 | #-------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | configPath = os.path.join(os.path.expanduser("~"),
|
---|
13 | "mlx.config" if os.name=="nt" else ".mlxrc")
|
---|
14 |
|
---|
15 | #-------------------------------------------------------------------------------
|
---|
16 |
|
---|
17 | if os.name=="nt":
|
---|
18 | _languageMap = { "en_GB" : "eng",
|
---|
19 | "hu_HU" : "hun" }
|
---|
20 |
|
---|
21 | #-------------------------------------------------------------------------------
|
---|
22 |
|
---|
23 | class Config(object):
|
---|
24 | """Our configuration."""
|
---|
25 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
26 |
|
---|
27 | def __init__(self):
|
---|
28 | """Construct the configuration with default values."""
|
---|
29 |
|
---|
30 | self._pilotID = ""
|
---|
31 | self._password = ""
|
---|
32 | self._rememberPassword = False
|
---|
33 |
|
---|
34 | self._autoUpdate = True
|
---|
35 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
36 |
|
---|
37 | self._language = ""
|
---|
38 | self._flareTimeFromFS = False
|
---|
39 |
|
---|
40 | self._modified = False
|
---|
41 |
|
---|
42 | @property
|
---|
43 | def pilotID(self):
|
---|
44 | """Get the pilot ID."""
|
---|
45 | return self._pilotID
|
---|
46 |
|
---|
47 | @pilotID.setter
|
---|
48 | def pilotID(self, pilotID):
|
---|
49 | """Set the pilot ID."""
|
---|
50 | if pilotID!=self._pilotID:
|
---|
51 | self._pilotID = pilotID
|
---|
52 | self._modified = True
|
---|
53 |
|
---|
54 | @property
|
---|
55 | def password(self):
|
---|
56 | """Get the password."""
|
---|
57 | return self._password
|
---|
58 |
|
---|
59 | @password.setter
|
---|
60 | def password(self, password):
|
---|
61 | """Set the password."""
|
---|
62 | if password!=self._password:
|
---|
63 | self._password = password
|
---|
64 | self._modified = True
|
---|
65 |
|
---|
66 | @property
|
---|
67 | def rememberPassword(self):
|
---|
68 | """Get if we should remember the password."""
|
---|
69 | return self._rememberPassword
|
---|
70 |
|
---|
71 | @rememberPassword.setter
|
---|
72 | def rememberPassword(self, rememberPassword):
|
---|
73 | """Set if we should remember the password."""
|
---|
74 | if rememberPassword!=self._rememberPassword:
|
---|
75 | self._rememberPassword = rememberPassword
|
---|
76 | self._modified = True
|
---|
77 |
|
---|
78 | @property
|
---|
79 | def language(self):
|
---|
80 | """Get the language to use."""
|
---|
81 | return self._language
|
---|
82 |
|
---|
83 | @language.setter
|
---|
84 | def language(self, language):
|
---|
85 | """Set the language to use."""
|
---|
86 | if language!=self._language:
|
---|
87 | self._language = language
|
---|
88 | self._modified = True
|
---|
89 |
|
---|
90 | @property
|
---|
91 | def flareTimeFromFS(self):
|
---|
92 | """Get whether the flare time should be calculated from the time values
|
---|
93 | returned by the simulator."""
|
---|
94 | return self._flareTimeFromFS
|
---|
95 |
|
---|
96 | @flareTimeFromFS.setter
|
---|
97 | def flareTimeFromFS(self, flareTimeFromFS):
|
---|
98 | """Set whether the flare time should be calculated from the time values
|
---|
99 | returned by the simulator."""
|
---|
100 | if flareTimeFromFS!=self._flareTimeFromFS:
|
---|
101 | self._flareTimeFromFS = flareTimeFromFS
|
---|
102 | self._modified = True
|
---|
103 |
|
---|
104 | @property
|
---|
105 | def autoUpdate(self):
|
---|
106 | """Get if an automatic update is needed."""
|
---|
107 | return self._autoUpdate
|
---|
108 |
|
---|
109 | @autoUpdate.setter
|
---|
110 | def autoUpdate(self, autoUpdate):
|
---|
111 | """Set if an automatic update is needed."""
|
---|
112 | if autoUpdate!=self._autoUpdate:
|
---|
113 | self._autoUpdate = autoUpdate
|
---|
114 | self._modified = True
|
---|
115 |
|
---|
116 | @property
|
---|
117 | def updateURL(self):
|
---|
118 | """Get the update URL."""
|
---|
119 | return self._updateURL
|
---|
120 |
|
---|
121 | @updateURL.setter
|
---|
122 | def updateURL(self, updateURL):
|
---|
123 | """Set the update URL."""
|
---|
124 | if updateURL!=self._updateURL:
|
---|
125 | self._updateURL = updateURL
|
---|
126 | self._modified = True
|
---|
127 |
|
---|
128 | def load(self):
|
---|
129 | """Load the configuration from its default location."""
|
---|
130 | config = ConfigParser.RawConfigParser()
|
---|
131 | config.read(configPath)
|
---|
132 |
|
---|
133 | self._pilotID = self._get(config, "login", "id", "")
|
---|
134 | self._password = self._get(config, "login", "password", "")
|
---|
135 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
136 | "rememberPassword", False)
|
---|
137 |
|
---|
138 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
139 | self._updateURL = self._get(config, "update", "url",
|
---|
140 | Config.DEFAULT_UPDATE_URL)
|
---|
141 |
|
---|
142 | self._language = self._get(config, "general", "language", "")
|
---|
143 | self._flareTimeFromFS = self._getBoolean(config, "general",
|
---|
144 | "flareTimeFromFS",
|
---|
145 | False)
|
---|
146 |
|
---|
147 | self._modified = False
|
---|
148 |
|
---|
149 | def save(self):
|
---|
150 | """Save the configuration file if it has been modified."""
|
---|
151 | if not self._modified:
|
---|
152 | return
|
---|
153 |
|
---|
154 | config = ConfigParser.RawConfigParser()
|
---|
155 |
|
---|
156 | config.add_section("login")
|
---|
157 | config.set("login", "id", self._pilotID)
|
---|
158 | config.set("login", "password", self._password)
|
---|
159 | config.set("login", "rememberPassword",
|
---|
160 | "yes" if self._rememberPassword else "no")
|
---|
161 |
|
---|
162 | config.add_section("update")
|
---|
163 | config.set("update", "auto",
|
---|
164 | "yes" if self._autoUpdate else "no")
|
---|
165 | config.set("update", "url", self._updateURL)
|
---|
166 |
|
---|
167 | config.add_section("general")
|
---|
168 | if self._language:
|
---|
169 | config.set("general", "language", self._language)
|
---|
170 | config.set("general", "flareTimeFromFS",
|
---|
171 | "yes" if self._flareTimeFromFS else "no")
|
---|
172 |
|
---|
173 | try:
|
---|
174 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
175 | 0600)
|
---|
176 | with os.fdopen(fd, "wt") as f:
|
---|
177 | config.write(f)
|
---|
178 | self._modified = False
|
---|
179 | except Exception, e:
|
---|
180 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
181 |
|
---|
182 | def _getBoolean(self, config, section, option, default):
|
---|
183 | """Get the given option as a boolean, if found in the given config,
|
---|
184 | otherwise the default."""
|
---|
185 | return config.getboolean(section, option) \
|
---|
186 | if config.has_option(section, option) \
|
---|
187 | else default
|
---|
188 |
|
---|
189 | def _get(self, config, section, option, default):
|
---|
190 | """Get the given option as a string, if found in the given config,
|
---|
191 | otherwise the default."""
|
---|
192 | return config.get(section, option) \
|
---|
193 | if config.has_option(section, option) \
|
---|
194 | else default
|
---|
195 |
|
---|
196 | def getLanguage(self):
|
---|
197 | """Get the language to be used."""
|
---|
198 | import locale
|
---|
199 | if self._language:
|
---|
200 | if os.name=="nt":
|
---|
201 | if self._language in _languageMap:
|
---|
202 | locale.setlocale(locale.LC_ALL, _languageMap[self._language])
|
---|
203 | else:
|
---|
204 | locale.setlocale(locale.LC_ALL, "")
|
---|
205 | else:
|
---|
206 | locale.setlocale(locale.LC_ALL, (self._language,
|
---|
207 | locale.getpreferredencoding()))
|
---|
208 | return self._language
|
---|
209 | else:
|
---|
210 | locale.setlocale(locale.LC_ALL, "")
|
---|
211 | return locale.getdefaultlocale()[0]
|
---|
212 |
|
---|
213 | #-------------------------------------------------------------------------------
|
---|