[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 |
|
---|
| 31 | self._modified = False
|
---|
| 32 |
|
---|
[107] | 33 | self._language = ""
|
---|
| 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
|
---|
[40] | 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 |
|
---|
[42] | 100 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 101 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 102 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 103 | "rememberPassword", False)
|
---|
[42] | 104 |
|
---|
[40] | 105 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 106 | self._updateURL = self._get(config, "update", "url",
|
---|
| 107 | Config.DEFAULT_UPDATE_URL)
|
---|
[107] | 108 |
|
---|
| 109 | self._language = self._get(config, "general", "language", "")
|
---|
| 110 |
|
---|
[40] | 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 |
|
---|
[42] | 120 | config.add_section("login")
|
---|
| 121 | config.set("login", "id", self._pilotID)
|
---|
| 122 | config.set("login", "password", self._password)
|
---|
[45] | 123 | config.set("login", "rememberPassword",
|
---|
| 124 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 125 |
|
---|
[40] | 126 | config.add_section("update")
|
---|
[45] | 127 | config.set("update", "auto",
|
---|
| 128 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 129 | config.set("update", "url", self._updateURL)
|
---|
| 130 |
|
---|
[107] | 131 | config.add_section("general")
|
---|
| 132 | if self._language:
|
---|
| 133 | config.set("general", "language", self._language)
|
---|
| 134 |
|
---|
[40] | 135 | try:
|
---|
[43] | 136 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 137 | 0600)
|
---|
| 138 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 139 | config.write(f)
|
---|
| 140 | self._modified = False
|
---|
| 141 | except Exception, e:
|
---|
[43] | 142 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 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
|
---|
[36] | 157 |
|
---|
[107] | 158 | def getLanguage(self):
|
---|
| 159 | """Get the language to be used."""
|
---|
[113] | 160 | import locale
|
---|
[107] | 161 | if self._language:
|
---|
[113] | 162 | locale.setlocale(locale.LC_ALL, (self._language,
|
---|
| 163 | locale.getpreferredencoding()))
|
---|
[107] | 164 | return self._language
|
---|
| 165 | else:
|
---|
[113] | 166 | locale.setlocale(locale.LC_ALL, "")
|
---|
[107] | 167 | return locale.getdefaultlocale()[0]
|
---|
| 168 |
|
---|
[36] | 169 | #-------------------------------------------------------------------------------
|
---|