source: src/mlx/config.py@ 113:a3c4d9205538

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

Minor internationalization fixes

File size: 5.4 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._modified = False
32
33 self._language = ""
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 autoUpdate(self):
73 """Get if an automatic update is needed."""
74 return self._autoUpdate
75
76 @autoUpdate.setter
77 def autoUpdate(self, autoUpdate):
78 """Set if an automatic update is needed."""
79 if autoUpdate!=self._autoUpdate:
80 self._autoUpdate = autoUpdate
81 self._modified = True
82
83 @property
84 def updateURL(self):
85 """Get the update URL."""
86 return self._updateURL
87
88 @updateURL.setter
89 def updateURL(self, updateURL):
90 """Set the update URL."""
91 if updateURL!=self._updateURL:
92 self._updateURL = updateURL
93 self._modified = True
94
95 def load(self):
96 """Load the configuration from its default location."""
97 config = ConfigParser.RawConfigParser()
98 config.read(configPath)
99
100 self._pilotID = self._get(config, "login", "id", "")
101 self._password = self._get(config, "login", "password", "")
102 self._rememberPassword = self._getBoolean(config, "login",
103 "rememberPassword", False)
104
105 self._autoUpdate = self._getBoolean(config, "update", "auto", True)
106 self._updateURL = self._get(config, "update", "url",
107 Config.DEFAULT_UPDATE_URL)
108
109 self._language = self._get(config, "general", "language", "")
110
111 self._modified = False
112
113 def save(self):
114 """Save the configuration file if it has been modified."""
115 if not self._modified:
116 return
117
118 config = ConfigParser.RawConfigParser()
119
120 config.add_section("login")
121 config.set("login", "id", self._pilotID)
122 config.set("login", "password", self._password)
123 config.set("login", "rememberPassword",
124 "yes" if self._rememberPassword else "no")
125
126 config.add_section("update")
127 config.set("update", "auto",
128 "yes" if self._autoUpdate else "no")
129 config.set("update", "url", self._updateURL)
130
131 config.add_section("general")
132 if self._language:
133 config.set("general", "language", self._language)
134
135 try:
136 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
137 0600)
138 with os.fdopen(fd, "wt") as f:
139 config.write(f)
140 self._modified = False
141 except Exception, e:
142 print >> sys.stderr, "Failed to update config: " + str(e)
143
144 def _getBoolean(self, config, section, option, default):
145 """Get the given option as a boolean, if found in the given config,
146 otherwise the default."""
147 return config.getboolean(section, option) \
148 if config.has_option(section, option) \
149 else default
150
151 def _get(self, config, section, option, default):
152 """Get the given option as a string, if found in the given config,
153 otherwise the default."""
154 return config.get(section, option) \
155 if config.has_option(section, option) \
156 else default
157
158 def getLanguage(self):
159 """Get the language to be used."""
160 import locale
161 if self._language:
162 locale.setlocale(locale.LC_ALL, (self._language,
163 locale.getpreferredencoding()))
164 return self._language
165 else:
166 locale.setlocale(locale.LC_ALL, "")
167 return locale.getdefaultlocale()[0]
168
169#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.