forked from placebokkk/pyfst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand_tpl.py
38 lines (33 loc) · 1.09 KB
/
expand_tpl.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
import sys
import yaml
def generate_from_template(buffer, types):
generateds = []
for t in types['types']:
buffer_t = buffer
for k in t:
buffer_t = buffer_t.replace('{{' + k + '}}', t[k])
generateds.append(buffer_t)
return generateds
if __name__ == '__main__':
tpl_path = sys.argv[1]
yml_path = sys.argv[2]
out_path = sys.argv[3]
stream = open(yml_path, 'r')
types = yaml.safe_load(stream)
buffer = ''
is_in_template = False
with open(out_path, 'w') as out_f:
with open(tpl_path, 'r') as tpl:
for l in tpl.readlines():
if l.strip() == '{{#types}}':
is_in_template = True
buffer = ''
elif l.strip() == '{{/types}}':
generateds = generate_from_template(buffer, types)
for g in generateds:
out_f.write(g)
is_in_template = False
elif is_in_template:
buffer = buffer + l
else:
out_f.write(l)