1 | # Configuration and related stuff
|
---|
2 |
|
---|
3 | #-------------------------------------------------------------------------------
|
---|
4 |
|
---|
5 | import os
|
---|
6 | import sys
|
---|
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 |
|
---|
16 | class Config(object):
|
---|
17 | """Our configuration."""
|
---|
18 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
19 |
|
---|
20 | def __init__(self):
|
---|
21 | """Construct the configuration with default values."""
|
---|
22 |
|
---|
23 | self._pilotID = ""
|
---|
24 | self._password = ""
|
---|
25 |
|
---|
26 | self._autoUpdate = True
|
---|
27 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
28 |
|
---|
29 | self._modified = False
|
---|
30 |
|
---|
31 | @property
|
---|
32 | def pilotID(self):
|
---|
33 | """Get the pilot ID."""
|
---|
34 | return self._pilotID
|
---|
35 |
|
---|
36 | @pilotID.setter
|
---|
37 | def pilotID(self, pilotID):
|
---|
38 | """Set the pilot ID."""
|
---|
39 | if pilotID!=self._pilotID:
|
---|
40 | self._pilotID = pilotID
|
---|
41 | self._modified = True
|
---|
42 |
|
---|
43 | @property
|
---|
44 | def password(self):
|
---|
45 | """Get the password."""
|
---|
46 | return self._password
|
---|
47 |
|
---|
48 | @password.setter
|
---|
49 | def password(self, password):
|
---|
50 | """Set the password."""
|
---|
51 | if password!=self._password:
|
---|
52 | self._password = password
|
---|
53 | self._modified = True
|
---|
54 |
|
---|
55 | @property
|
---|
56 | def autoUpdate(self):
|
---|
57 | """Get if an automatic update is needed."""
|
---|
58 | return self._autoUpdate
|
---|
59 |
|
---|
60 | @autoUpdate.setter
|
---|
61 | def autoUpdate(self, autoUpdate):
|
---|
62 | """Set if an automatic update is needed."""
|
---|
63 | if autoUpdate!=self._autoUpdate:
|
---|
64 | self._autoUpdate = autoUpdate
|
---|
65 | self._modified = True
|
---|
66 |
|
---|
67 | @property
|
---|
68 | def updateURL(self):
|
---|
69 | """Get the update URL."""
|
---|
70 | return self._updateURL
|
---|
71 |
|
---|
72 | @updateURL.setter
|
---|
73 | def updateURL(self, updateURL):
|
---|
74 | """Set the update URL."""
|
---|
75 | if updateURL!=self._updateURL:
|
---|
76 | self._updateURL = updateURL
|
---|
77 | self._modified = True
|
---|
78 |
|
---|
79 | def load(self):
|
---|
80 | """Load the configuration from its default location."""
|
---|
81 | config = ConfigParser.RawConfigParser()
|
---|
82 | config.read(configPath)
|
---|
83 |
|
---|
84 | self._pilotID = self._get(config, "login", "id", "")
|
---|
85 | self._password = self._get(config, "login", "password", "")
|
---|
86 |
|
---|
87 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
88 | self._updateURL = self._get(config, "update", "url",
|
---|
89 | Config.DEFAULT_UPDATE_URL)
|
---|
90 | self._modified = False
|
---|
91 |
|
---|
92 | def save(self):
|
---|
93 | """Save the configuration file if it has been modified."""
|
---|
94 | if not self._modified:
|
---|
95 | return
|
---|
96 |
|
---|
97 | config = ConfigParser.RawConfigParser()
|
---|
98 |
|
---|
99 | config.add_section("login")
|
---|
100 | config.set("login", "id", self._pilotID)
|
---|
101 | config.set("login", "password", self._password)
|
---|
102 |
|
---|
103 | config.add_section("update")
|
---|
104 | config.set("update", "auto", self._autoUpdate)
|
---|
105 | config.set("update", "url", self._updateURL)
|
---|
106 |
|
---|
107 | try:
|
---|
108 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
109 | 0600)
|
---|
110 | with os.fdopen(fd, "wt") as f:
|
---|
111 | config.write(f)
|
---|
112 | self._modified = False
|
---|
113 | except Exception, e:
|
---|
114 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
115 |
|
---|
116 | def _getBoolean(self, config, section, option, default):
|
---|
117 | """Get the given option as a boolean, if found in the given config,
|
---|
118 | otherwise the default."""
|
---|
119 | return config.getboolean(section, option) \
|
---|
120 | if config.has_option(section, option) \
|
---|
121 | else default
|
---|
122 |
|
---|
123 | def _get(self, config, section, option, default):
|
---|
124 | """Get the given option as a string, if found in the given config,
|
---|
125 | otherwise the default."""
|
---|
126 | return config.get(section, option) \
|
---|
127 | if config.has_option(section, option) \
|
---|
128 | else default
|
---|
129 |
|
---|
130 | #-------------------------------------------------------------------------------
|
---|