[36] | 1 | # Configuration and related stuff
|
---|
| 2 |
|
---|
| 3 | #-------------------------------------------------------------------------------
|
---|
| 4 |
|
---|
[40] | 5 | import os
|
---|
[43] | 6 | import sys
|
---|
[40] | 7 | import ConfigParser
|
---|
| 8 |
|
---|
| 9 | #-------------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | configPath = os.path.join(os.path.expanduser("~"),
|
---|
| 12 | "mlx.config" if os.name=="nt" else ".mlxrc")
|
---|
| 13 |
|
---|
| 14 | #-------------------------------------------------------------------------------
|
---|
| 15 |
|
---|
[36] | 16 | class Config(object):
|
---|
| 17 | """Our configuration."""
|
---|
[40] | 18 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
| 19 |
|
---|
[36] | 20 | def __init__(self):
|
---|
| 21 | """Construct the configuration with default values."""
|
---|
[40] | 22 |
|
---|
[42] | 23 | self._pilotID = ""
|
---|
| 24 | self._password = ""
|
---|
[45] | 25 | self._rememberPassword = False
|
---|
[42] | 26 |
|
---|
[40] | 27 | self._autoUpdate = True
|
---|
| 28 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
| 29 |
|
---|
| 30 | self._modified = False
|
---|
| 31 |
|
---|
[107] | 32 | self._language = ""
|
---|
| 33 |
|
---|
[40] | 34 | @property
|
---|
[42] | 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
|
---|
[45] | 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
|
---|
[40] | 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 |
|
---|
[42] | 99 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 100 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 101 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 102 | "rememberPassword", False)
|
---|
[42] | 103 |
|
---|
[40] | 104 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 105 | self._updateURL = self._get(config, "update", "url",
|
---|
| 106 | Config.DEFAULT_UPDATE_URL)
|
---|
[107] | 107 |
|
---|
| 108 | self._language = self._get(config, "general", "language", "")
|
---|
| 109 |
|
---|
[40] | 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 |
|
---|
[42] | 119 | config.add_section("login")
|
---|
| 120 | config.set("login", "id", self._pilotID)
|
---|
| 121 | config.set("login", "password", self._password)
|
---|
[45] | 122 | config.set("login", "rememberPassword",
|
---|
| 123 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 124 |
|
---|
[40] | 125 | config.add_section("update")
|
---|
[45] | 126 | config.set("update", "auto",
|
---|
| 127 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 128 | config.set("update", "url", self._updateURL)
|
---|
| 129 |
|
---|
[107] | 130 | config.add_section("general")
|
---|
| 131 | if self._language:
|
---|
| 132 | config.set("general", "language", self._language)
|
---|
| 133 |
|
---|
[40] | 134 | try:
|
---|
[43] | 135 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 136 | 0600)
|
---|
| 137 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 138 | config.write(f)
|
---|
| 139 | self._modified = False
|
---|
| 140 | except Exception, e:
|
---|
[43] | 141 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 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
|
---|
[36] | 156 |
|
---|
[107] | 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 |
|
---|
[36] | 165 | #-------------------------------------------------------------------------------
|
---|