108 lines
3.1 KiB
Python

import json
from jinja2 import Environment, FileSystemLoader
import sys
import os
with open('render-params.json') as f:
GLOBAL_CONFIG = json.load(f)
def extract_param_names(mc):
result = []
def helper_extract(widget):
if 'childs' in widget:
r = []
for child in widget['childs']:
r += helper_extract(child)
return r
elif 'name' in widget:
match widget['widget']:
case 'select': return [{"name": widget['name'], "initValue": widget['values'][0]['value']}]
case 'checkbox': return [{"name": widget['name'], "initValue": 'false'}]
case 'number': return [{"name": widget['name'], "initValue": widget['min'] if widget['min'] else '0'}]
case 'modulation-modcod': return [{"name": widget['name'] + "Modulation", "initValue": '"QPSK"'}]
case 'modulation-speed': return [{"name": widget['name'] + "Speed", "initValue": '"1/4"'}]
case 'watch': return []
return [{"name": widget['name'], "initValue": 'null'}]
return []
for cat in mc['params']:
ws = []
for w in mc['params'][cat]:
ws += helper_extract(w)
# ws.sort(key=lambda k: k['name'])
result.append({
"group": cat,
"params": ws
})
return result
def add_submit_widgets(params):
def find_submit(w):
if w['widget'] == 'submit':
return True
if 'childs' in w:
for c in w['childs']:
if find_submit(c):
return True
return False
for group in params:
wid_found = False
for wid in params[group]:
if find_submit(wid):
wid_found = True
break
if wid_found:
continue
params[group].append({"widget": "submit"})
def extract_param_groups(mc):
return [k for k in mc['params']]
def build_modem_env(modem):
if modem not in GLOBAL_CONFIG['modem_types']:
raise RuntimeError(f"Modem '{modem}' is not exist in config!")
mc = GLOBAL_CONFIG['modem_types'][modem]
add_submit_widgets(mc['params'])
return {
"modem": modem,
"modem_name": mc['modem_name'],
"header_tabs": mc['tabs'],
"tab_names_array": [t['name'] for t in mc['tabs']],
"params": mc["params"],
"dangerousParamGroups": mc["dangerousParamGroups"] if 'dangerousParamGroups' in mc else {},
"paramGroups": extract_param_names(mc),
"paramGroupsList": extract_param_groups(mc),
}
def render_modem(modem):
loader = FileSystemLoader('template')
env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
template = env.get_template('main.html')
context = build_modem_env(modem)
with open(f"main-{modem}.html", "w") as f:
f.write(template.render(context))
if __name__ == '__main__':
for mt in GLOBAL_CONFIG['modem_types']:
print(f'Generating {mt} modem...')
render_modem(mt)
os.system(f'cp -u main-{mt}.html ../static')