source: src/mlx/config.py@ 128:e14fcd9d9215

Last change on this file since 128:e14fcd9d9215 was 124:6633722f7f86, checked in by István Váradi <ivaradi@…>, 13 years ago

The buttons are now explicitly defined for message dialogs to avoid language problems

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