1 | # Configuration and related stuff
|
---|
2 |
|
---|
3 | #-------------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | import os
|
---|
6 | import ConfigParser
|
---|
7 |
|
---|
8 | #-------------------------------------------------------------------------------
|
---|
9 |
|
---|
10 | configPath = os.path.join(os.path.expanduser("~"),
|
---|
11 | "mlx.config" if os.name=="nt" else ".mlxrc")
|
---|
12 |
|
---|
13 | #-------------------------------------------------------------------------------
|
---|
14 |
|
---|
15 | class Config(object):
|
---|
16 | """Our configuration."""
|
---|
17 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
18 |
|
---|
19 | def __init__(self):
|
---|
20 | """Construct the configuration with default values."""
|
---|
21 |
|
---|
22 | self._autoUpdate = True
|
---|
23 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
24 |
|
---|
25 | self._modified = False
|
---|
26 |
|
---|
27 | @property
|
---|
28 | def autoUpdate(self):
|
---|
29 | """Get if an automatic update is needed."""
|
---|
30 | return self._autoUpdate
|
---|
31 |
|
---|
32 | @autoUpdate.setter
|
---|
33 | def autoUpdate(self, autoUpdate):
|
---|
34 | """Set if an automatic update is needed."""
|
---|
35 | if autoUpdate!=self._autoUpdate:
|
---|
36 | self._autoUpdate = autoUpdate
|
---|
37 | self._modified = True
|
---|
38 |
|
---|
39 | @property
|
---|
40 | def updateURL(self):
|
---|
41 | """Get the update URL."""
|
---|
42 | return self._updateURL
|
---|
43 |
|
---|
44 | @updateURL.setter
|
---|
45 | def updateURL(self, updateURL):
|
---|
46 | """Set the update URL."""
|
---|
47 | if updateURL!=self._updateURL:
|
---|
48 | self._updateURL = updateURL
|
---|
49 | self._modified = True
|
---|
50 |
|
---|
51 | def load(self):
|
---|
52 | """Load the configuration from its default location."""
|
---|
53 | config = ConfigParser.RawConfigParser()
|
---|
54 | config.read(configPath)
|
---|
55 |
|
---|
56 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
57 | self._updateURL = self._get(config, "update", "url",
|
---|
58 | Config.DEFAULT_UPDATE_URL)
|
---|
59 | self._modified = False
|
---|
60 |
|
---|
61 | def save(self):
|
---|
62 | """Save the configuration file if it has been modified."""
|
---|
63 | if not self._modified:
|
---|
64 | return
|
---|
65 |
|
---|
66 | config = ConfigParser.RawConfigParser()
|
---|
67 |
|
---|
68 | config.add_section("update")
|
---|
69 | config.set("update", "auto", self._autoUpdate)
|
---|
70 | config.set("update", "url", self._updateURL)
|
---|
71 |
|
---|
72 | try:
|
---|
73 | with open(configPath, "wt") as f:
|
---|
74 | config.write(f)
|
---|
75 | self._modified = False
|
---|
76 | except Exception, e:
|
---|
77 | print >> sys.stderr("Failed to update config: " + str(e))
|
---|
78 |
|
---|
79 | def _getBoolean(self, config, section, option, default):
|
---|
80 | """Get the given option as a boolean, if found in the given config,
|
---|
81 | otherwise the default."""
|
---|
82 | return config.getboolean(section, option) \
|
---|
83 | if config.has_option(section, option) \
|
---|
84 | else default
|
---|
85 |
|
---|
86 | def _get(self, config, section, option, default):
|
---|
87 | """Get the given option as a string, if found in the given config,
|
---|
88 | otherwise the default."""
|
---|
89 | return config.get(section, option) \
|
---|
90 | if config.has_option(section, option) \
|
---|
91 | else default
|
---|
92 |
|
---|
93 | #-------------------------------------------------------------------------------
|
---|