source: src/mlx/config.py@ 45:0938977d6603

Last change on this file since 45:0938977d6603 was 45:0938977d6603, checked in by István Váradi <locvais@…>, 12 years ago

Added option to remember the password as well as mnemonics and tooltips

File size: 4.7 KB
Line 
1# Configuration and related stuff
2
3#-------------------------------------------------------------------------------
4
5import os
6import sys
7import ConfigParser
8
9#-------------------------------------------------------------------------------
10
11configPath = os.path.join(os.path.expanduser("~"),
12 "mlx.config" if os.name=="nt" else ".mlxrc")
13
14#-------------------------------------------------------------------------------
15
16class 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 self._rememberPassword = False
26
27 self._autoUpdate = True
28 self._updateURL = Config.DEFAULT_UPDATE_URL
29
30 self._modified = False
31
32 @property
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
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
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
97 self._pilotID = self._get(config, "login", "id", "")
98 self._password = self._get(config, "login", "password", "")
99 self._rememberPassword = self._getBoolean(config, "login",
100 "rememberPassword", False)
101
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
114 config.add_section("login")
115 config.set("login", "id", self._pilotID)
116 config.set("login", "password", self._password)
117 config.set("login", "rememberPassword",
118 "yes" if self._rememberPassword else "no")
119
120 config.add_section("update")
121 config.set("update", "auto",
122 "yes" if self._autoUpdate else "no")
123 config.set("update", "url", self._updateURL)
124
125 try:
126 fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
127 0600)
128 with os.fdopen(fd, "wt") as f:
129 config.write(f)
130 self._modified = False
131 except Exception, e:
132 print >> sys.stderr, "Failed to update config: " + str(e)
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
147
148#-------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.