-
Notifications
You must be signed in to change notification settings - Fork 20
/
regen.py
executable file
·151 lines (123 loc) · 4.99 KB
/
regen.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
#!/usr/bin/python3
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
def write_autogenerated(out):
out.write('{%comment%}\nAUTOGENERATED FILE, please run ./regen.py to regenerate it\n{%endcomment%}\n\n')
def write_status_tag(out, name):
out.write('{%% include_cached status.liquid status=%s comment=%s-comment %%}' % (name, name))
def is_intop(v):
return 'intop' in v and v['intop']
def filter_intop(data):
return {k: v for k, v in data.items() if is_intop(v)}.items()
def filter_nop(data):
return data.items()
def filter_dict(data, filter_fn):
return {k: v for k, v in data.items() if filter_fn(v)}.items()
def write_header_int(out, data, filter_fn):
for (k, v) in filter_fn(data):
out.write('<th>%s</th>\n' % (v['name']))
def write_header(out, data, filter_fn):
out.write('<th>Platform</th>\n')
write_header_int(out, data, filter_fn)
out.write('<th>Platform</th>\n')
def write_header_nested(out, data, filter_fn):
out.write('<th rowspan="2">Platform</th>\n')
for (k, v) in filter_fn(data):
if 'items' in v and (intops := len(filter_fn(v['items']))):
out.write('<th colspan="%d">%s</th>\n' % (intops,v['name']))
else:
out.write('<th rowspan="2">%s</th>\n' % (v['name']))
out.write('<th rowspan="2">Platform</th>\n')
out.write('</tr>\n')
out.write('<tr>\n')
for (k, v) in data.items():
if not 'items' in v:
continue
if 'items' in v:
write_header_int(out, v['items'], filter_fn)
def write_layout(out, data, prefix, wrap=True):
for (k, v) in data.items():
if not wrap:
wrap = True
out.write(' ')
else:
out.write('<tr>')
out.write('<th>%s</th>' % v['name'])
write_status_tag(out, '%s-%s' % (prefix, k))
out.write('<td>{{ %s-%s-comment }}</td>\n' % (prefix, k))
out.write('</tr>\n')
def write_layout_nested(out, data, prefix):
for (k, v) in data.items():
if 'items' in v:
out.write('<th rowspan="%d">%s</th>\n' % (len(v['items']), v['name']))
write_layout(out, v['items'], prefix + '-' + k, wrap = False)
else:
out.write('<tr><th colspan="2">%s</th>' % (v['name']))
write_status_tag(out, '%s-%s' % (prefix, k))
out.write('<td>{{ %s-%s-comment }}</td>\n' % (prefix, k))
out.write('</tr>\n')
def write_status(out, data, prefix, filter_fn):
for (k, v) in filter_fn(data):
if 'items' in v and len(filter_fn(v['items'])):
write_status(out, v['items'], prefix + '-' + k, filter_fn)
else:
write_status_tag(out, '%s-%s' % (prefix, k))
out.write('\n')
def generate_template(data, prefix, check_fn):
for (k, v) in data.items():
if 'items' not in v:
yield '%s-%s' % (prefix, k)
if 'items' in v and len(filter_dict(v['items'], check_fn)) == 0 and check_fn(v):
yield '%s-%s' % (prefix, k)
if 'items' in v:
yield from generate_template(v['items'], prefix + '-' + k, check_fn)
def handle_soc_pmic(data, kind, prefix):
has_nested = False
for (k, v) in data.items():
if 'items' in v:
has_nested = True
break
with open('_includes/index_%s_header.liquid' % kind, 'w') as out:
write_autogenerated(out)
out.write('<tr>\n')
if has_nested:
write_header_nested(out, data, filter_intop)
else:
write_header(out, data, filter_intop)
out.write('</tr>\n')
with open('_includes/index_%s_status.liquid' % kind, 'w') as out:
write_autogenerated(out)
write_status(out, data, 'd.' + prefix, filter_intop)
with open('_includes/full_%s_header.liquid' % kind, 'w') as out:
write_autogenerated(out)
out.write('<tr>\n')
if has_nested:
write_header_nested(out, data, filter_nop)
else:
write_header(out, data, filter_nop)
out.write('</tr>\n')
with open('_includes/full_%s_status.liquid' % kind, 'w') as out:
write_autogenerated(out)
write_status(out, data, 'd.' + prefix, filter_nop)
with open('_includes/layout_%s.liquid' % kind, 'w') as out:
write_autogenerated(out)
if has_nested:
write_layout_nested(out, data, 'page.' + prefix)
else:
write_layout(out, data, 'page.' + prefix)
with open('_%s.template' % kind, 'w') as out:
out.write('---\n')
out.write('name: ???\n')
out.write('layout: %s\n' % kind)
out.write(': N/A\n'.join(sorted(generate_template(data, prefix, is_intop))))
out.write(': N/A\n')
out.write('---\n')
with open('soc.yaml', "r") as file:
data = load(file, Loader=Loader)
handle_soc_pmic(data, 'soc', 'status')
with open('pmic.yaml', "r") as file:
data = load(file, Loader=Loader)
handle_soc_pmic(data, 'pmic', 'pmic')