"""Regenerate component templates from a project in which every component type was freshly placed. Usage: python tools/make_templates.py project.HMI PAGE_ENTRY out.json "source description" For each component type the *last* instance on the page is used (fresh Editor defaults when the components were placed in toolbox order onto an existing page). """ import json import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) from nxhmi.container import HMIFile # noqa: E402 from nxhmi.page import Page # noqa: E402 KINDS = {121: "page", 116: "text", 55: "scrolling_text", 54: "number", 59: "xfloat", 98: "button", 106: "progress_bar", 112: "picture", 113: "crop", 109: "hotspot", 5: "touch_cap", 122: "gauge", 0: "waveform", 1: "slider", 51: "timer", 52: "variable", 53: "dual_state_button", 56: "checkbox", 57: "radio", 58: "qrcode", 2: "gmov", 3: "video", 4: "audio", 60: "ex_picture", 61: "combobox", 62: "sl_text", 63: "file_stream", 65: "file_browser", 66: "data_record", 67: "switch", 68: "text_select"} def main(hmi, entry, out, source): page = Page.parse(HMIFile.load(hmi).read(entry)) types = {} for c in reversed(page.components): t = c.get("type") if str(t) in types: continue types[str(t)] = {"kind": KINDS.get(t, "type%d" % t), "prefix": "page" if t == 121 else c.name.rstrip("0123456789"), "att": [[a.name, *a.decoded()] for a in c.attrs()], "events": [g.name for g in c.groups if g.name != "att"]} with open(out, "w", encoding="utf-8") as f: json.dump({"source": source, "types": dict(sorted(types.items(), key=lambda kv: int(kv[0])))}, f, indent=1) print("%d types -> %s" % (len(types), out)) if __name__ == "__main__": main(*sys.argv[1:5])