1 | import re
|
---|
2 |
|
---|
3 | from ....builtins import *
|
---|
4 |
|
---|
5 | from ...exceptions import *
|
---|
6 | from ...expressions import *
|
---|
7 | from ...streams import *
|
---|
8 |
|
---|
9 | ##
|
---|
10 | ## bpgetconfig has one record per-stream
|
---|
11 | ##
|
---|
12 | def stream(stream, format='bpgetconfig -X'):
|
---|
13 |
|
---|
14 | if format in ['bpgetconfig -X', 'bpgetconfig -L']:
|
---|
15 | return SingleRecordStream(stream)
|
---|
16 | else:
|
---|
17 | raise ParseError, 'Unknown format %s' % (format)
|
---|
18 |
|
---|
19 |
|
---|
20 | ##
|
---|
21 | ## Parse a bpgetconfig record
|
---|
22 | ##
|
---|
23 | ## bpgetconfig -X
|
---|
24 | ## bpgetconfig -L
|
---|
25 | ##
|
---|
26 | def parse(record, format='bpgetconfig -X', version=None, tz=None):
|
---|
27 |
|
---|
28 | config = ExtendedDict()
|
---|
29 |
|
---|
30 | config['servers'] = []
|
---|
31 | config['excludes'] = []
|
---|
32 |
|
---|
33 | if format in ['bpgetconfig -X', 'bpgetconfig -L']:
|
---|
34 |
|
---|
35 | try:
|
---|
36 |
|
---|
37 | for line in record:
|
---|
38 | pair = line.split(' = ') # pairs are delimited with =
|
---|
39 | key = pair[0]
|
---|
40 | if len(pair) == 2: # check if value exists
|
---|
41 | value = pair[1]
|
---|
42 | if re_integer.match(value): # convert to integer if possible
|
---|
43 | value = int(value)
|
---|
44 | else:
|
---|
45 | value = None # use None for no value
|
---|
46 | config[key] = value # store under the original key name
|
---|
47 | key = key.lower() # lowercase the key name
|
---|
48 | key = key.replace(' ', '_') # replace spaces with underscores
|
---|
49 | key = key.replace('/', '_') # replace slashes with underscores
|
---|
50 | config[key] = value # store under the new key name
|
---|
51 |
|
---|
52 | if key == 'server':
|
---|
53 | config.servers.append(value)
|
---|
54 |
|
---|
55 | if key == 'exclude':
|
---|
56 | config.excludes.append(value)
|
---|
57 |
|
---|
58 | return config
|
---|
59 |
|
---|
60 | except Exception, e:
|
---|
61 |
|
---|
62 | raise ParseError, e
|
---|
63 |
|
---|
64 | else:
|
---|
65 |
|
---|
66 | raise ParseError, 'Unknown format %s' % (format)
|
---|