[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 |
|
---|
[36] | 17 | class Config(object):
|
---|
| 18 | """Our configuration."""
|
---|
[40] | 19 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
| 20 |
|
---|
[36] | 21 | def __init__(self):
|
---|
| 22 | """Construct the configuration with default values."""
|
---|
[40] | 23 |
|
---|
[42] | 24 | self._pilotID = ""
|
---|
| 25 | self._password = ""
|
---|
[45] | 26 | self._rememberPassword = False
|
---|
[42] | 27 |
|
---|
[40] | 28 | self._autoUpdate = True
|
---|
| 29 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
| 30 |
|
---|
[123] | 31 | self._language = ""
|
---|
[40] | 32 |
|
---|
[123] | 33 | self._modified = False
|
---|
[107] | 34 |
|
---|
[40] | 35 | @property
|
---|
[42] | 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
|
---|
[45] | 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
|
---|
[123] | 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
|
---|
[40] | 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 |
|
---|
[42] | 112 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 113 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 114 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 115 | "rememberPassword", False)
|
---|
[42] | 116 |
|
---|
[40] | 117 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 118 | self._updateURL = self._get(config, "update", "url",
|
---|
| 119 | Config.DEFAULT_UPDATE_URL)
|
---|
[107] | 120 |
|
---|
| 121 | self._language = self._get(config, "general", "language", "")
|
---|
| 122 |
|
---|
[40] | 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 |
|
---|
[42] | 132 | config.add_section("login")
|
---|
| 133 | config.set("login", "id", self._pilotID)
|
---|
| 134 | config.set("login", "password", self._password)
|
---|
[45] | 135 | config.set("login", "rememberPassword",
|
---|
| 136 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 137 |
|
---|
[40] | 138 | config.add_section("update")
|
---|
[45] | 139 | config.set("update", "auto",
|
---|
| 140 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 141 | config.set("update", "url", self._updateURL)
|
---|
| 142 |
|
---|
[107] | 143 | config.add_section("general")
|
---|
| 144 | if self._language:
|
---|
| 145 | config.set("general", "language", self._language)
|
---|
| 146 |
|
---|
[40] | 147 | try:
|
---|
[43] | 148 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 149 | 0600)
|
---|
| 150 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 151 | config.write(f)
|
---|
| 152 | self._modified = False
|
---|
| 153 | except Exception, e:
|
---|
[43] | 154 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 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
|
---|
[36] | 169 |
|
---|
[107] | 170 | def getLanguage(self):
|
---|
| 171 | """Get the language to be used."""
|
---|
[113] | 172 | import locale
|
---|
[107] | 173 | if self._language:
|
---|
[123] | 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 |
|
---|
[113] | 180 | locale.setlocale(locale.LC_ALL, (self._language,
|
---|
| 181 | locale.getpreferredencoding()))
|
---|
[107] | 182 | return self._language
|
---|
| 183 | else:
|
---|
[113] | 184 | locale.setlocale(locale.LC_ALL, "")
|
---|
[107] | 185 | return locale.getdefaultlocale()[0]
|
---|
| 186 |
|
---|
[36] | 187 | #-------------------------------------------------------------------------------
|
---|