[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
|
---|
| 36 |
|
---|
[123] | 37 | self._language = ""
|
---|
[40] | 38 |
|
---|
[123] | 39 | self._modified = False
|
---|
[107] | 40 |
|
---|
[40] | 41 | @property
|
---|
[42] | 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
|
---|
[45] | 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
|
---|
[123] | 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
|
---|
[40] | 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 |
|
---|
[42] | 118 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 119 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 120 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 121 | "rememberPassword", False)
|
---|
[42] | 122 |
|
---|
[40] | 123 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 124 | self._updateURL = self._get(config, "update", "url",
|
---|
| 125 | Config.DEFAULT_UPDATE_URL)
|
---|
[107] | 126 |
|
---|
| 127 | self._language = self._get(config, "general", "language", "")
|
---|
| 128 |
|
---|
[40] | 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 |
|
---|
[42] | 138 | config.add_section("login")
|
---|
| 139 | config.set("login", "id", self._pilotID)
|
---|
| 140 | config.set("login", "password", self._password)
|
---|
[45] | 141 | config.set("login", "rememberPassword",
|
---|
| 142 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 143 |
|
---|
[40] | 144 | config.add_section("update")
|
---|
[45] | 145 | config.set("update", "auto",
|
---|
| 146 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 147 | config.set("update", "url", self._updateURL)
|
---|
| 148 |
|
---|
[107] | 149 | config.add_section("general")
|
---|
| 150 | if self._language:
|
---|
| 151 | config.set("general", "language", self._language)
|
---|
| 152 |
|
---|
[40] | 153 | try:
|
---|
[43] | 154 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 155 | 0600)
|
---|
| 156 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 157 | config.write(f)
|
---|
| 158 | self._modified = False
|
---|
| 159 | except Exception, e:
|
---|
[43] | 160 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 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
|
---|
[36] | 175 |
|
---|
[107] | 176 | def getLanguage(self):
|
---|
| 177 | """Get the language to be used."""
|
---|
[113] | 178 | import locale
|
---|
[107] | 179 | if self._language:
|
---|
[124] | 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()))
|
---|
[107] | 188 | return self._language
|
---|
| 189 | else:
|
---|
[113] | 190 | locale.setlocale(locale.LC_ALL, "")
|
---|
[107] | 191 | return locale.getdefaultlocale()[0]
|
---|
| 192 |
|
---|
[36] | 193 | #-------------------------------------------------------------------------------
|
---|