-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
216 lines (165 loc) · 4.95 KB
/
config.py
File metadata and controls
216 lines (165 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
"""
File for dealing with the configuration of wxPi.py.
"""
import os
import logging
import threading
from ConfigParser import SafeConfigParser, NoSectionError
from led import GPIOLED as LED
__version__ = '0.2'
__all__ = ['CONFIG_FILE', 'loadConfig', 'initLEDs', 'saveConfig', '__version__', '__all__']
# Logger instance
confLogger = logging.getLogger('__main__')
# Files
## Base path for the various files needed/generated by wxPi.py
_BASE_PATH = os.path.dirname(os.path.abspath(__file__))
## Wunderground Configuration
CONFIG_FILE = os.path.join(_BASE_PATH, 'wxPi.config')
class LockingConfigParser(SafeConfigParser):
"""
Sub-class of ConfigParser.SafeConfigParser that wraps the get, set, and
write methods with a semaphore to ensure that only one get/set/read/write
happens at a time. The sub-class also adds asDict and fromDict methods
to make it easier to tie the configuration into webforms.
"""
_lock = threading.Semaphore()
def get(self, *args, **kwds):
"""
Locked get() method.
"""
#self._lock.acquire()
value = SafeConfigParser.get(self, *args, **kwds)
#self._lock.release()
return value
def getint(self, *args, **kwds):
"""
Locked getint() method.
"""
value = SafeConfigParser.getint(self, *args, **kwds)
return int(value)
def getfloat(self, *args, **kwds):
"""
Locked getfloat() method.
"""
value = SafeConfigParser.getfloat(self, *args, **kwds)
return float(value)
def getbool(self, *args, **kwds):
"""
Locked getbool() method.
"""
value = SafeConfigParser.get(self, *args, **kwds)
if value.lower() in ('true', 'enabled', 'on', 'yes'):
value = True
else:
value = False
return value
def set(self, *args, **kwds):
"""
Locked set() method.
"""
#self._lock.acquire()
SafeConfigParser.set(self, *args, **kwds)
#self._lock.release()
def read(self, *args, **kwds):
"""
Locked read() method.
"""
SafeConfigParser.read(self, *args, **kwds)
def write(self, *args, **kwds):
"""
Locked write() method.
"""
SafeConfigParser.write(self, *args, **kwds)
def asDict(self):
"""
Return the configuration as a dictionary with keys structured as
section-option.
"""
configDict = {}
for section in self.sections():
for keyword,value in self.items(section):
configDict['%s-%s' % (section.lower(), keyword)] = value
# Done
return configDict
def fromDict(self, configDict):
"""
Given a dictionary created by asDict(), update the configuration
as needed.
"""
# Loop over the pairs in the dictionary
for key,value in configDict.iteritems():
try:
section, keyword = key.split('-', 1)
section = section.capitalize()
self.set(section, keyword, value)
except Exception, e:
print str(e)
pass
# Done
return True
def loadConfig(filename):
"""
Read in the configuration file and return a dictionary of the
parameters.
"""
# Initial configuration file
config = LockingConfigParser()
## Dummy WUnderground PWS information
## 1) ID - PWS ID
## 2) Password - PWS upload password
config.add_section('Account')
config.set('Account', 'id', 'Your_Id_Here')
config.set('Account', 'password', 'Your_Password_Here')
## Dummy station information
## 1) elevation - Station elevation in meters
## 2) duration - Duration, in seconds, to record data for
## 3) radioPin - GPIO pin that the radio is connected to
## 4) enableBMP085 - enable reading a BMP085/BMP180 sensor over I2C
## 5) includeIndoor - Whether or not to include indoor data
config.add_section('Station')
config.set('Station', 'elevation', '0.0')
config.set('Station', 'duration', '60.0')
config.set('Station', 'radiopin', '18')
config.set('Station', 'enablebmp085', 'True')
config.set('Station', 'includeindoor', 'False')
## Dummy LED information
## 1) redPin - GPIO pin that a red LED is attached to
## 2) yellowPin - GPIO pin that a yellow LED is attached to
## 3) greenPin - GPIO pin that a green LED is attached to
config.add_section('LED')
config.set('LED', 'redpin', '27')
config.set('LED', 'yellowpin', '17')
config.set('LED', 'greenpin', '4')
# Try to read in the actual configuration file
try:
config.read(filename)
confLogger.info('Loaded configuration from \'%s\'', os.path.basename(filename))
except:
pass
# Done
return config
def initLEDs(config):
"""
Given a LockingConfigParser configuration instance, create a dictionary of
GPIOLED instances to control the various LEDs.
"""
# Create the GPIOLED instances
leds = {}
for color in ('red', 'yellow', 'green'):
try:
pin = config.get('LED', '%spin' % color)
leds[color] = LED(pin)
except NoSectionError:
leds[color] = LED(-1)
# Done
return leds
def saveConfig(filename, config):
"""
Given a filename and a LockingConfigParser, write the configuration to
disk.
"""
fh = open(filename, 'w')
config.write(fh)
fh.close()
confLogger.info('Saved configuration to \'%s\'', os.path.basename(filename))