[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 |
|
---|
| 32 | @property
|
---|
[42] | 33 | def pilotID(self):
|
---|
| 34 | """Get the pilot ID."""
|
---|
| 35 | return self._pilotID
|
---|
| 36 |
|
---|
| 37 | @pilotID.setter
|
---|
| 38 | def pilotID(self, pilotID):
|
---|
| 39 | """Set the pilot ID."""
|
---|
| 40 | if pilotID!=self._pilotID:
|
---|
| 41 | self._pilotID = pilotID
|
---|
| 42 | self._modified = True
|
---|
| 43 |
|
---|
| 44 | @property
|
---|
| 45 | def password(self):
|
---|
| 46 | """Get the password."""
|
---|
| 47 | return self._password
|
---|
| 48 |
|
---|
| 49 | @password.setter
|
---|
| 50 | def password(self, password):
|
---|
| 51 | """Set the password."""
|
---|
| 52 | if password!=self._password:
|
---|
| 53 | self._password = password
|
---|
| 54 | self._modified = True
|
---|
| 55 |
|
---|
| 56 | @property
|
---|
[45] | 57 | def rememberPassword(self):
|
---|
| 58 | """Get if we should remember the password."""
|
---|
| 59 | return self._rememberPassword
|
---|
| 60 |
|
---|
| 61 | @rememberPassword.setter
|
---|
| 62 | def rememberPassword(self, rememberPassword):
|
---|
| 63 | """Set if we should remember the password."""
|
---|
| 64 | if rememberPassword!=self._rememberPassword:
|
---|
| 65 | self._rememberPassword = rememberPassword
|
---|
| 66 | self._modified = True
|
---|
| 67 |
|
---|
| 68 | @property
|
---|
[40] | 69 | def autoUpdate(self):
|
---|
| 70 | """Get if an automatic update is needed."""
|
---|
| 71 | return self._autoUpdate
|
---|
| 72 |
|
---|
| 73 | @autoUpdate.setter
|
---|
| 74 | def autoUpdate(self, autoUpdate):
|
---|
| 75 | """Set if an automatic update is needed."""
|
---|
| 76 | if autoUpdate!=self._autoUpdate:
|
---|
| 77 | self._autoUpdate = autoUpdate
|
---|
| 78 | self._modified = True
|
---|
| 79 |
|
---|
| 80 | @property
|
---|
| 81 | def updateURL(self):
|
---|
| 82 | """Get the update URL."""
|
---|
| 83 | return self._updateURL
|
---|
| 84 |
|
---|
| 85 | @updateURL.setter
|
---|
| 86 | def updateURL(self, updateURL):
|
---|
| 87 | """Set the update URL."""
|
---|
| 88 | if updateURL!=self._updateURL:
|
---|
| 89 | self._updateURL = updateURL
|
---|
| 90 | self._modified = True
|
---|
| 91 |
|
---|
| 92 | def load(self):
|
---|
| 93 | """Load the configuration from its default location."""
|
---|
| 94 | config = ConfigParser.RawConfigParser()
|
---|
| 95 | config.read(configPath)
|
---|
| 96 |
|
---|
[42] | 97 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 98 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 99 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 100 | "rememberPassword", False)
|
---|
[42] | 101 |
|
---|
[40] | 102 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 103 | self._updateURL = self._get(config, "update", "url",
|
---|
| 104 | Config.DEFAULT_UPDATE_URL)
|
---|
| 105 | self._modified = False
|
---|
| 106 |
|
---|
| 107 | def save(self):
|
---|
| 108 | """Save the configuration file if it has been modified."""
|
---|
| 109 | if not self._modified:
|
---|
| 110 | return
|
---|
| 111 |
|
---|
| 112 | config = ConfigParser.RawConfigParser()
|
---|
| 113 |
|
---|
[42] | 114 | config.add_section("login")
|
---|
| 115 | config.set("login", "id", self._pilotID)
|
---|
| 116 | config.set("login", "password", self._password)
|
---|
[45] | 117 | config.set("login", "rememberPassword",
|
---|
| 118 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 119 |
|
---|
[40] | 120 | config.add_section("update")
|
---|
[45] | 121 | config.set("update", "auto",
|
---|
| 122 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 123 | config.set("update", "url", self._updateURL)
|
---|
| 124 |
|
---|
| 125 | try:
|
---|
[43] | 126 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 127 | 0600)
|
---|
| 128 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 129 | config.write(f)
|
---|
| 130 | self._modified = False
|
---|
| 131 | except Exception, e:
|
---|
[43] | 132 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 133 |
|
---|
| 134 | def _getBoolean(self, config, section, option, default):
|
---|
| 135 | """Get the given option as a boolean, if found in the given config,
|
---|
| 136 | otherwise the default."""
|
---|
| 137 | return config.getboolean(section, option) \
|
---|
| 138 | if config.has_option(section, option) \
|
---|
| 139 | else default
|
---|
| 140 |
|
---|
| 141 | def _get(self, config, section, option, default):
|
---|
| 142 | """Get the given option as a string, if found in the given config,
|
---|
| 143 | otherwise the default."""
|
---|
| 144 | return config.get(section, option) \
|
---|
| 145 | if config.has_option(section, option) \
|
---|
| 146 | else default
|
---|
[36] | 147 |
|
---|
| 148 | #-------------------------------------------------------------------------------
|
---|