Files
lightgui/tools/port_registry_blocks.py
cnmcdee 05a85ddf8a Phase 4: named live-apply dispatch in registry.h; repair gen_registry.py
registry.h gains 8 Phase 4 IWidget virtuals (set_named_color/double/int/
bool/string, set_y_range, set_anchor, set_value_str), one SFINAE trait per
distinct Stock/Instrumentation setter, and Holder overrides with if-chains
over (prop -> setter). The named dispatch keeps the virtual count flat
while covering all 116 Phase 4 prop names.

gen_registry.py was stale: regenerating it deleted the hand-added Phase 2
colour virtuals and the whole Phase 3 block. tools/port_registry_blocks.py
ports those 60 virtuals/traits/overrides back into the template (verified:
regeneration is a byte-identical no-op) and inserts the Phase 4 blocks, so
registry.h is generated again, not hand-edited.
2026-11-30 14:07:19 +00:00

171 lines
5.8 KiB
Python

#!/usr/bin/env python3
"""Port the hand-added Phase 2/3 live-apply blocks from
widgets/common/registry.h into the widgets/tools/gen_registry.py template
(the generator went stale: regenerating it deleted those blocks), and
optionally insert the Phase 4 blocks from a file produced by
tools/gen_phase4_props.py registry mode.
Usage:
port_registry_blocks.py [--phase4 <blocks-file>]
Run from /opt/lightgui. Step 1 (no --phase4) must leave registry.h
byte-identical after regeneration; with --phase4 the only diff must be
the Phase 4 additions.
"""
import os
import re
import subprocess
import sys
ROOT = "/opt/lightgui"
REG = os.path.join(ROOT, "widgets", "common", "registry.h")
GEN = os.path.join(ROOT, "widgets", "tools", "gen_registry.py")
def double_braces(s):
return s.replace("{", "{{").replace("}", "}}")
def read(p):
with open(p) as f:
return f.read()
def write(p, s):
with open(p, "w") as f:
f.write(s)
# --- extraction from registry.h -------------------------------------------
TRAIT_RE = re.compile(
r"template <typename, typename = void>\n"
r"struct (has_\w+) : std::false_type \{\};\n"
r"template <typename T>\n"
r"struct \1<T, [^\n]*> : std::true_type \{\};\n")
def extract_virtuals(reg_text, gen_text):
lines = reg_text.split("\n")
out = []
for i, ln in enumerate(lines):
if re.match(r" virtual bool set_\w+\(.*\) \{ return false; \}$", ln):
# Carry a preceding section comment (e.g. the Phase 3 header)
# when it directly precedes an extra virtual.
if i > 0 and re.match(r" // ", lines[i - 1]):
if double_braces(lines[i - 1]) not in gen_text:
out.append(lines[i - 1])
out.append(ln)
return out
def extract_traits(reg_text):
out = []
for m in TRAIT_RE.finditer(reg_text):
out.append(m.group(0))
return out
def extract_overrides(reg_text):
lines = reg_text.split("\n")
out = []
i = 0
while i < len(lines):
m = re.match(r" bool (set_\w+)\(.*\) override \{$", lines[i])
if m:
depth = 0
j = i
while j < len(lines):
depth += lines[j].count("{") - lines[j].count("}")
if j > i and depth == 0:
break
j += 1
out.append("\n".join(lines[i:j + 1]) + "\n")
i = j + 1
else:
i += 1
return out
def main():
phase4_file = None
if len(sys.argv) == 3 and sys.argv[1] == "--phase4":
phase4_file = sys.argv[2]
elif len(sys.argv) != 1:
print("usage: port_registry_blocks.py [--phase4 <blocks-file>]")
return 2
reg_text = read(REG)
gen_text = read(GEN)
# --- Phase 2/3 extras: present in registry.h, missing from the generator.
virt = [v for v in extract_virtuals(reg_text, gen_text)
if double_braces(v) not in gen_text]
traits = []
for t in extract_traits(reg_text):
name = re.search(r"struct (has_\w+) :", t).group(1)
if ("struct %s :" % name) not in gen_text:
traits.append(t)
over = []
for o in extract_overrides(reg_text):
first = o.split("\n")[0]
if double_braces(first) not in gen_text:
over.append(o)
print("porting Phase 2/3 extras: %d virtuals, %d traits, %d overrides"
% (len(virt), len(traits), len(over)))
anchor_v = (' virtual bool set_checked_items(const std::string&) '
'{{ return false; }}\n')
assert anchor_v in gen_text, "virtual anchor not found"
gen_text = gen_text.replace(
anchor_v, anchor_v + "".join(double_braces(v) + "\n" for v in virt), 1)
anchor_t = "}} // namespace detail\n"
assert anchor_t in gen_text, "trait anchor not found"
gen_text = gen_text.replace(
anchor_t, "".join(double_braces(t) for t in traits) + anchor_t, 1)
anchor_o = " std::string get_text() override {{\n"
assert anchor_o in gen_text, "override anchor not found"
gen_text = gen_text.replace(
anchor_o, "".join(double_braces(o) for o in over) + anchor_o, 1)
# --- Phase 4 blocks (if supplied): insert at the same anchors, after
# the Phase 2/3 entries (i.e. directly before get_text / detail close).
if phase4_file:
blocks = read(phase4_file)
parts = {}
cur = None
for ln in blocks.split("\n"):
m = re.match(r"// ===== Phase 4 (\w+) ", ln)
if m:
cur = m.group(1)
parts[cur] = []
continue
if cur:
parts[cur].append(ln)
v4 = double_braces("\n".join(parts.get("IWidget", [])).strip("\n") + "\n")
t4 = double_braces("\n".join(parts.get("detail", [])).strip("\n") + "\n")
o4 = double_braces("\n".join(parts.get("Holder", [])).strip("\n") + "\n")
anchor_v2 = ' virtual std::string get_text() {{ return ""; }}\n'
assert anchor_v2 in gen_text, "phase4 virtual anchor not found"
gen_text = gen_text.replace(anchor_v2, v4 + anchor_v2, 1)
gen_text = gen_text.replace(anchor_t, t4 + anchor_t, 1)
gen_text = gen_text.replace(anchor_o, o4 + anchor_o, 1)
print("inserted Phase 4 blocks: %d/%d/%d lines"
% (len(v4.splitlines()), len(t4.splitlines()), len(o4.splitlines())))
write(GEN, gen_text)
r = subprocess.run(["python3", "widgets/tools/gen_registry.py"],
cwd=ROOT, capture_output=True, text=True)
print(r.stdout.strip())
if r.returncode != 0:
print(r.stderr)
return r.returncode
r = subprocess.run(["git", "diff", "--numstat", "widgets/common/registry.h"],
cwd=ROOT, capture_output=True, text=True)
print("registry.h diff (added/deleted lines):", r.stdout.strip())
return 0
if __name__ == "__main__":
sys.exit(main())