-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_config.py
73 lines (58 loc) · 1.73 KB
/
test_config.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
import mfi
import os
import json
from collections import defaultdict
def save_config():
test_mpower = mfi.MPower(os.environ['TESTMPOWER'], os.environ['TESTUSER'],
os.environ['TESTPASS'])
test_config = test_mpower.get_cfg()
with open('mpower.cfg', 'r+') as f:
f.write(test_config)
def load_config():
with open('mpower.cfg', 'r+') as f:
test_config = f.read()
return test_config
config_text = load_config()
def parse_config(conf):
def Tree():
return defaultdict(Tree)
data = Tree()
for line in conf.splitlines():
if not line:
continue
path, val = line.split('=')
fields = path.split('.')
prop = fields.pop()
obj = data
for f in fields:
if f.isdigit():
items = obj.setdefault('items', [])
idx = int(f) - 1
while len(items) < idx + 1:
items.append(Tree())
obj = items[idx]
else:
obj = obj[f]
obj[prop] = val
return data
def output_conf(item, items=None, lines=None):
if items is None:
items = []
if lines is None:
lines = []
if isinstance(item, defaultdict):
for key, value in item.items():
items.append(key)
return output_conf(value, items, lines)
elif isinstance(item, list):
for value in item:
items.append(1)
return output_conf(value, items, lines)
else:
items.append('=')
items.append(item)
lines.append(items)
print(lines)
return output_conf(item, items, lines)
config = mfi.UbntConfig(config_text)
print(config.get_config_dump())