source: src/mlx/config.py@ 123:3b181cd0ab99

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

The Preferences dialog works

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