-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_parser.py
85 lines (49 loc) · 1.96 KB
/
template_parser.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
import json
import uuid
from utils import Tree
def template_parser(template_file):
template = Tree()
with open(template_file, 'r') as lines:
for line in lines:
line = line.rstrip()
if not line.strip(): # ignore empty lines
continue
if line == '# Global config items beginning ignore':
context = 'glb_beg_ign'
template[context] = []
elif line == '# Global config items subset or exact ignore':
context = 'glb_sub_ign'
template[context] = []
elif line == '# Global config items beginning with':
context = 'glb_beg_with'
template[context] = []
elif line == '# Global config items':
context = 'glb_cfg'
template[context] = []
elif line == '# Grouped optional config items':
key = uuid.uuid4().hex
context = 'grp_opt'
template['grp_opt'][key] = []
elif line == '# Global hierarchical config items':
context = 'glb_hier'
hierarc_cfg = []
template[context] = []
if '#' in line:
continue
if context == 'glb_hier':
if line == '!':
template['glb_hier'].append(hierarc_cfg)
hierarc_cfg = []
else:
hierarc_cfg.append(line)
elif context == 'grp_opt':
template['grp_opt'][key].append(line)
else:
template[context].append(line)
return template
def main():
template = template_parser('test-template.txt')
with open('template.json', 'w') as f:
json.dump(template, f, indent=4)
if __name__ == '__main__':
main()