source: src/mlx/config.py@ 42:f23f648550e9

Last change on this file since 42:f23f648550e9 was 42:f23f648550e9, checked in by István Váradi <ivaradi@…>, 12 years ago

Started the flight wizard and it is now possible to log in

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