source: src/mlx/config.py@ 107:35310bf5309c

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

Added support for internationalization and translated most of the flight wizard into Hungarian

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