[36] | 1 | # Configuration and related stuff
|
---|
[113] | 2 | # -*- encoding: utf-8 -*-
|
---|
[36] | 3 |
|
---|
| 4 | #-------------------------------------------------------------------------------
|
---|
| 5 |
|
---|
[40] | 6 | import os
|
---|
[43] | 7 | import sys
|
---|
[40] | 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 |
|
---|
[124] | 17 | if os.name=="nt":
|
---|
| 18 | _languageMap = { "en_GB" : "eng",
|
---|
| 19 | "hu_HU" : "hun" }
|
---|
| 20 |
|
---|
| 21 | #-------------------------------------------------------------------------------
|
---|
| 22 |
|
---|
[36] | 23 | class Config(object):
|
---|
| 24 | """Our configuration."""
|
---|
[40] | 25 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
| 26 |
|
---|
[36] | 27 | def __init__(self):
|
---|
| 28 | """Construct the configuration with default values."""
|
---|
[40] | 29 |
|
---|
[42] | 30 | self._pilotID = ""
|
---|
| 31 | self._password = ""
|
---|
[45] | 32 | self._rememberPassword = False
|
---|
[42] | 33 |
|
---|
[40] | 34 | self._autoUpdate = True
|
---|
| 35 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
[131] | 36 |
|
---|
[123] | 37 | self._language = ""
|
---|
[131] | 38 | self._flareTimeFromFS = False
|
---|
[40] | 39 |
|
---|
[123] | 40 | self._modified = False
|
---|
[107] | 41 |
|
---|
[40] | 42 | @property
|
---|
[42] | 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
|
---|
[45] | 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
|
---|
[123] | 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
|
---|
[131] | 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
|
---|
[40] | 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 |
|
---|
[42] | 133 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 134 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 135 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 136 | "rememberPassword", False)
|
---|
[42] | 137 |
|
---|
[40] | 138 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 139 | self._updateURL = self._get(config, "update", "url",
|
---|
| 140 | Config.DEFAULT_UPDATE_URL)
|
---|
[107] | 141 |
|
---|
| 142 | self._language = self._get(config, "general", "language", "")
|
---|
[131] | 143 | self._flareTimeFromFS = self._getBoolean(config, "general",
|
---|
| 144 | "flareTimeFromFS",
|
---|
| 145 | False)
|
---|
[107] | 146 |
|
---|
[40] | 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 |
|
---|
[42] | 156 | config.add_section("login")
|
---|
| 157 | config.set("login", "id", self._pilotID)
|
---|
| 158 | config.set("login", "password", self._password)
|
---|
[45] | 159 | config.set("login", "rememberPassword",
|
---|
| 160 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 161 |
|
---|
[40] | 162 | config.add_section("update")
|
---|
[45] | 163 | config.set("update", "auto",
|
---|
| 164 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 165 | config.set("update", "url", self._updateURL)
|
---|
| 166 |
|
---|
[107] | 167 | config.add_section("general")
|
---|
| 168 | if self._language:
|
---|
| 169 | config.set("general", "language", self._language)
|
---|
[131] | 170 | config.set("general", "flareTimeFromFS",
|
---|
| 171 | "yes" if self._flareTimeFromFS else "no")
|
---|
[107] | 172 |
|
---|
[40] | 173 | try:
|
---|
[43] | 174 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 175 | 0600)
|
---|
| 176 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 177 | config.write(f)
|
---|
| 178 | self._modified = False
|
---|
| 179 | except Exception, e:
|
---|
[43] | 180 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 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
|
---|
[36] | 195 |
|
---|
[107] | 196 | def getLanguage(self):
|
---|
| 197 | """Get the language to be used."""
|
---|
[113] | 198 | import locale
|
---|
[107] | 199 | if self._language:
|
---|
[124] | 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()))
|
---|
[107] | 208 | return self._language
|
---|
| 209 | else:
|
---|
[113] | 210 | locale.setlocale(locale.LC_ALL, "")
|
---|
[107] | 211 | return locale.getdefaultlocale()[0]
|
---|
| 212 |
|
---|
[36] | 213 | #-------------------------------------------------------------------------------
|
---|