-
Notifications
You must be signed in to change notification settings - Fork 0
/
configparser.py
310 lines (235 loc) · 7.93 KB
/
configparser.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- coding: utf-8 -*-
from twisted.python.usage import Options
from xml.dom.minidom import parse as XMLParser
from xml.parsers.expat import ExpatError
from sys import exit, stderr
from re import match
def raiseMatch(re, data):
if match(re, data) is None:
raise AssertionError, data + ' did not match ' + re
return True
class ConfigParser(Options):
__cfoverride = ' (Overrides configuration file data)'
optParameters = [
['bootstrap', 'b', None, 'Bootstrap node' + __cfoverride],
['config-file', 'c', None, 'Node configuration file'],
]
winds = [ ]
node = { }
overlay = { }
components = [ ]
def __init__(self):
Options.__init__(self)
self['winds'] = ConfigParser.winds
self['node'] = ConfigParser.node
self['overlay'] = ConfigParser.overlay
self['components'] = ConfigParser.components
def subElementsParser(self, container, elementName, requiredAttributes=[]):
errors = [ ]
cfName = str(container.nodeName)
if cfName not in self:
setattr(ConfigParser, cfName, [ ])
self[cfName] = getattr(ConfigParser, cfName)
for e in container.getElementsByTagName(elementName):
d = { }
for k in e.attributes.keys():
d[str(k)] = str(e.getAttribute(k))
for k,t,f in requiredAttributes:
try:
assert d[k] is not None, (
'Required attribute %s is missing' % (k))
if f is not None:
f(d[k])
if t is not None:
d[k] = t(d[k])
# continue expression implicit here =)
except KeyError as e:
errors.append('Required attribute %s is empty' % (k))
except AssertionError as e:
errors.append(str(e))
if len(errors) == 0:
self[cfName].append(d)
if len(errors) > 0:
raise RuntimeError, str.join('\n', errors)
def elementParser(self, container, requiredAttributes):
errors = [ ]
nodeName = str(container.nodeName)
if nodeName not in self:
setattr(ConfigParser, nodeName, { })
self[nodeName] = getattr(ConfigParser, nodeName)
for k in container.attributes.keys():
self[nodeName][str(k)] = str(container.getAttribute(k))
for k,t,f in requiredAttributes:
try:
assert self[nodeName][k] is not None, (
'Required attribute %s is missing' % (k))
if f is not None:
f(self[nodeName][k])
if t is not None:
self[nodeName][k] = t(self[nodeName][k])
except KeyError as e:
errors.append('Required attribute %s is empty' % (k))
except AssertionError as e:
errors.append(str(e))
if len(errors) > 0:
raise RuntimeError, str.join('\n', errors)
def opt_node(self, symbol):
symbol = symbol.split(',')
try:
self['node']['name'], self['node']['domain'] = symbol[0].split('@')
except Exception as e:
raise RuntimeError, 'Error parsing node options\n\t' + str(e)
for r in symbol[1:]:
t = r.split(':')
self['node'][t[0]] = str.join(':', t[1:])
opt_n = opt_node
def nodeParser(self, node):
for k in node.attributes.keys():
self['node'][str(k)] = str(node.getAttribute(k))
if 'name' not in self['node'] or 'domain' not in self['node']:
raise RuntimeError, 'Missing node information'
# address,port,kind,protocol,module,class,extra0:extraData0
def opt_winds (self, symbol):
symbol = symbol.split(',')
wind = None
try:
wind = {
'address': symbol[0],
'ports': (int(symbol[1]),),
'kind': symbol[2],
'protocol': symbol[3],
'module': symbol[4],
'class': symbol[5],
}
except IndexError:
raise RuntimeError, 'Missing argument in wind CLI tuple'
for r in symbol[6:]:
k = r.split(':')
wind[k[0]] = str.join(':', k[1:])
self['winds'].append(wind)
opt_w = opt_winds
def windsParser(self, winds):
requiredAttributes = [
('address', None, None),
('kind', None, None),
('module', None, None),
('class', None, None),
('ports', lambda s: tuple(map(int, s.split(','))), None),
('protocol', None, None),
]
self.subElementsParser(winds, 'wind', requiredAttributes)
def opt_overlay(self, symbol):
symbol = map(str, symbol.split(','))
try:
self['overlay']['module'], self['overlay']['class'] = symbol[:2]
raiseMatch('[_a-zA-Z]+(\.[_a-zA-Z]+)*', self['overlay']['module'])
raiseMatch('[_a-zA-Z]+', self['overlay']['class'])
except ValueError:
raise RuntimeError, (
'Error reading overlay configuration from CLI')
except AssertionError as e:
raise RuntimeError, str(e)
for r in symbol[2:]:
k = r.split(':')
self['overlay'][k[0]] = str.join(':', k[1:])
opt_o = opt_overlay
def overlayParser(self, overlay):
requiredAttributes = [
('module',
None, lambda s: raiseMatch('[_a-zA-Z]+(\.[_a-zA-Z]+)*', s)),
('class',
None, lambda s: raiseMatch('[_a-zA-Z]+', s)),
]
self.elementParser(overlay, requiredAttributes)
def componentsParser(self, components):
requiredAttributes = [
('name', None, None),
('class', None, None)
]
self.subElementsParser(components, 'component', requiredAttributes)
def bootstrapParser(self, bootstrap):
# TODO: validation of these fields
requiredAttributes = [
('address', None, None),
('port', lambda p: int(p), None),
('kind', None, None),
('protocol', None, None),
]
self.subElementsParser(bootstrap, 'introducer', requiredAttributes)
def postOptions(self):
cdata = None
if 'config-file' in self and self['config-file'] != None:
try:
cdata = XMLParser(self['config-file'])
except ExpatError as ee:
raise RuntimeError, (
'Error while parsing configuration file\n' + str(ee))
mustHaveFields = [{
'nm': 'winds',
'prsr': self.windsParser,
},{
'nm': 'overlay',
'prsr': self.overlayParser,
},{
'nm': 'node',
'prsr': self.nodeParser,
}]
optionalFields = [{
'nm': 'bootstrap',
'prsr': self.bootstrapParser,
'dval': [ ]
},{
'nm': 'managementComponents',
'prsr': self.componentsParser,
'dval': [ ]
}]
for f in mustHaveFields:
if len(self[f['nm']]) is 0 and cdata is not None:
try:
f['prsr'](cdata.getElementsByTagName(f['nm'])[0])
except RuntimeError as e:
raise RuntimeError, (
'While parsing %s\n\t%s' % (f['nm'], str(e)))
elif len(self[f['nm']]) is 0 and cdata is None:
raise RuntimeError, (
'%s configuration file section\'s or CLI flag not found\n'
'Review your setup and try again.' % (f['nm']))
for f in optionalFields:
if f['nm'] not in self or self[f['nm']] is None:
self[f['nm']] = f['dval']
if cdata is None:
continue
try:
f['prsr'](cdata.getElementsByTagName(f['nm'])[0])
except IndexError as ie:
print ' > %s not in configuration file' % (f['nm'])
except Exception as e:
print >> stderr, (
' * While parsing a optional parameter %s\n * %s' % (
f['nm'], str(e)))
if __name__ == '__main__':
'''CLI Test case:
python configparser.py \
-w 127.0.0.1,8001,IPv4/UDP,default,puretcp,PureTCPProto \
-w 127.0.0.1,8002,IPv4/UDP,default,pureudp,PureUDPProto \
-o cyclon,Cyclon,cacheSize:40 -n alderan@inf.ufrgs.br
'''
'''Configuration file test case:
<?xml version="1.0" ?>
<ManP2P-ng subject="configuration">
<node name="Alderan" domain="redes.inf.ufrgs.br"/>
<winds>
<wind address="127.0.0.1" kind="IPv4/UDP" module="pureudp" class="PureUDPProto" ports="8001,8002" protocol="default"/>
<wind address="127.0.0.1" kind="IPv4/TCP" module="puretcp" class="PureTCPProto" ports="8001,8002" protocol="default"/>
</winds>
<overlay cacheSize="40" module="cyclon" class="Cyclon"/>
<bootstrap>
<introducer address="127.0.0.1" port="8004" kind="IPv4/UDP" protocol="default"/>
<introducer address="127.0.0.1" port="8005" kind="IPv4/TCP" protocol="default"/>
</bootstrap>
</ManP2P-ng>
'''
a = ConfigParser()
b = ConfigParser()
ConfigParser().parseOptions()
print ConfigParser()['node']['name'], ConfigParser()['node']['domain']