Data, not hand-written code: tools/extract_registry.py reads widgets/common/registry.h and cross-checks it three ways - the on-disk widgets/components/ dirs, widgets/tools/sections.py, and the default_size() rule from gen_registry.py - failing on any drift. Re-derived counts (lightgui eb0b5fc): 547 widgets, 11 sections; families Gtk 102, Ind 250, Inst 66, Stock 37, T 90, X 2. The survey Stock 38 / Inst 67 counted the shared non-widget headers StockData.h / InstData.h; XGraph/XSpreadsheet form a sixth family X (2) the survey family table omitted. Includes the extraction harness: tools/common.py, extract_all.py, check.py (make check re-derives tables from the C++ sources and fails on drift), tools/README.md, and the top-level Makefile.
212 lines
7.8 KiB
Python
212 lines
7.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract the XFrame widget registry from the C++ sources into data/registry.json.
|
|
|
|
Sources (read-only, /opt/lightgui):
|
|
- widgets/common/registry.h — canonical names, section index, default sizes
|
|
- widgets/components/ — on-disk widget dirs (cross-check)
|
|
- widgets/tools/sections.py — section assignment + accent colours
|
|
- widgets/tools/gen_registry.py — default_size() rule (cross-check)
|
|
|
|
The registry is the single source of truth for the palette and layout
|
|
apps: names are in sorted order and the index is the identity the
|
|
palette -> layout selection file carries. This extractor derives the
|
|
same numbers three independent ways (registry.h arrays, the component
|
|
directories, sections.py + the default_size rule) and fails if they
|
|
disagree — a stale registry.h must never silently ship.
|
|
|
|
Usage: python3 tools/extract_registry.py
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from common import (DATA_DIR, LIGHTGUI_SRC, ExtractError, fail, provenance,
|
|
read_source, write_json)
|
|
|
|
REGISTRY_H = "widgets/common/registry.h"
|
|
GENERATOR = "python3 tools/extract_registry.py"
|
|
|
|
# Family by name prefix. Order matters: check the long prefixes first.
|
|
# Gtk* — GTK-named controls re-implemented as hand-drawn X11 widgets
|
|
# Ind* — stock-market indicators (each a widget + a formula)
|
|
# Stock*— stock charts / drawing tools
|
|
# Inst* — instrumentation (gauges, lamps, LEDs, tanks, switches, ...)
|
|
# T* — Lazarus/Delphi-style controls
|
|
# X* — XFrame-native extras (XGraph, XSpreadsheet)
|
|
FAMILY_PREFIXES = ("Gtk", "Ind", "Stock", "Inst")
|
|
|
|
|
|
def family_of(name):
|
|
for p in FAMILY_PREFIXES:
|
|
if name.startswith(p):
|
|
return p
|
|
if name.startswith("T"):
|
|
return "T"
|
|
if name.startswith("X"):
|
|
return "X"
|
|
fail("widget %r matches no known family prefix" % name)
|
|
|
|
|
|
def is_widget_dir(name):
|
|
d = os.path.join(LIGHTGUI_SRC, "widgets", "components", name)
|
|
return (os.path.isdir(d)
|
|
and os.path.isfile(os.path.join(d, name + ".h")))
|
|
|
|
|
|
def component_names():
|
|
cdir = os.path.join(LIGHTGUI_SRC, "widgets", "components")
|
|
return sorted(n for n in os.listdir(cdir) if is_widget_dir(n))
|
|
|
|
|
|
def parse_registry_h():
|
|
text = read_source(REGISTRY_H)
|
|
m = re.search(r"kWidgetCount\s*=\s*(\d+)", text)
|
|
declared = int(m.group(1)) if m else None
|
|
m = re.search(r"kWidgetNames\[kWidgetCount\] = \{(.*?)\};", text, re.S)
|
|
if not m:
|
|
fail("kWidgetNames not found in %s" % REGISTRY_H)
|
|
names = re.findall(r'"([^"]+)"', m.group(1))
|
|
m = re.search(r"kSectionNames\[kSectionCount\] = \{(.*?)\};", text, re.S)
|
|
if not m:
|
|
fail("kSectionNames not found in %s" % REGISTRY_H)
|
|
sections = re.findall(r'"([^"]+)"', m.group(1))
|
|
m = re.search(r"kWidgetSection\[kWidgetCount\] = \{(.*?)\};", text, re.S)
|
|
if not m:
|
|
fail("kWidgetSection not found in %s" % REGISTRY_H)
|
|
wsec = [int(x) for x in re.findall(r"\d+", m.group(1))]
|
|
m = re.search(r"kWidgetDefaultW\[kWidgetCount\] = \{([^}]*)\};", text)
|
|
if not m:
|
|
fail("kWidgetDefaultW not found in %s" % REGISTRY_H)
|
|
defw = [int(x) for x in m.group(1).split(",") if x.strip()]
|
|
m = re.search(r"kWidgetDefaultH\[kWidgetCount\] = \{([^}]*)\};", text)
|
|
if not m:
|
|
fail("kWidgetDefaultH not found in %s" % REGISTRY_H)
|
|
defh = [int(x) for x in m.group(1).split(",") if x.strip()]
|
|
return declared, names, sections, wsec, defw, defh
|
|
|
|
|
|
def load_sections_module():
|
|
"""Import the C repo's own sections.py + gen_registry.py (read-only).
|
|
|
|
This reuses the authoritative section assignment and default-size
|
|
rule instead of duplicating them by hand.
|
|
"""
|
|
tools_dir = os.path.join(LIGHTGUI_SRC, "widgets", "tools")
|
|
if tools_dir not in sys.path:
|
|
sys.path.insert(0, tools_dir)
|
|
import sections as secmod # noqa: E402
|
|
import gen_registry as genmod # noqa: E402
|
|
return secmod, genmod
|
|
|
|
|
|
def build():
|
|
declared, names, section_names, wsec, defw, defh = parse_registry_h()
|
|
n = len(names)
|
|
if declared != n:
|
|
fail("kWidgetCount=%s but kWidgetNames has %d entries"
|
|
% (declared, n))
|
|
if not (len(wsec) == len(defw) == len(defh) == n):
|
|
fail("registry arrays disagree: names=%d sections=%d W=%d H=%d"
|
|
% (n, len(wsec), len(defw), len(defh)))
|
|
if len(set(names)) != n:
|
|
fail("duplicate widget names in registry.h")
|
|
if names != sorted(names):
|
|
fail("kWidgetNames is not in sorted order")
|
|
|
|
# Cross-check 1: the on-disk component directories.
|
|
comps = component_names()
|
|
if comps != names:
|
|
only_disk = [c for c in comps if c not in set(names)]
|
|
only_reg = [c for c in names if c not in set(comps)]
|
|
fail("registry.h != components/ on disk: only-on-disk=%s only-in-registry=%s"
|
|
% (only_disk[:10], only_reg[:10]))
|
|
for name in names:
|
|
hdr = os.path.join(LIGHTGUI_SRC, "widgets", "components",
|
|
name, name + ".h")
|
|
if not os.path.isfile(hdr):
|
|
fail("missing header for %s: %s" % (name, hdr))
|
|
|
|
# Cross-check 2: section assignment from the C repo's sections.py.
|
|
secmod, genmod = load_sections_module()
|
|
errs = secmod.validate(names)
|
|
if errs:
|
|
fail("sections.py validation failed: %s" % errs[:10])
|
|
if list(secmod.SECTIONS) != section_names:
|
|
fail("section order differs: sections.py=%s registry.h=%s"
|
|
% (secmod.SECTIONS, section_names))
|
|
for i, name in enumerate(names):
|
|
want = secmod.SECTIONS.index(secmod.section_of(name))
|
|
if wsec[i] != want:
|
|
fail("section mismatch for %s: registry=%d sections.py=%d (%s)"
|
|
% (name, wsec[i], want, secmod.section_of(name)))
|
|
|
|
# Cross-check 3: default sizes from the C repo's default_size() rule.
|
|
for i, name in enumerate(names):
|
|
w, h = genmod.default_size(name)
|
|
if (w, h) != (defw[i], defh[i]):
|
|
fail("default size mismatch for %s: registry=%dx%d rule=%dx%d"
|
|
% (name, defw[i], defh[i], w, h))
|
|
|
|
families = {}
|
|
widgets = []
|
|
for i, name in enumerate(names):
|
|
fam = family_of(name)
|
|
families[fam] = families.get(fam, 0) + 1
|
|
widgets.append({
|
|
"index": i,
|
|
"name": name,
|
|
"family": fam,
|
|
"section": section_names[wsec[i]],
|
|
"section_index": wsec[i],
|
|
"default_w": defw[i],
|
|
"default_h": defh[i],
|
|
"header": "widgets/components/%s/%s.h" % (name, name),
|
|
})
|
|
|
|
sections = []
|
|
for si, sname in enumerate(section_names):
|
|
count = sum(1 for w in widgets if w["section_index"] == si)
|
|
color = secmod.SECTION_COLORS.get(sname)
|
|
sections.append({
|
|
"index": si,
|
|
"name": sname,
|
|
"color_rgb": list(color) if color else None,
|
|
"count": count,
|
|
})
|
|
|
|
data = {
|
|
"_provenance": provenance(
|
|
[REGISTRY_H, "widgets/tools/sections.py",
|
|
"widgets/tools/gen_registry.py",
|
|
"widgets/tools/widget_count.py"],
|
|
GENERATOR),
|
|
"widget_count": n,
|
|
"section_count": len(section_names),
|
|
"sections": sections,
|
|
"families": dict(sorted(families.items())),
|
|
"widgets": widgets,
|
|
}
|
|
if sum(families.values()) != n:
|
|
fail("family counts sum to %d, expected %d"
|
|
% (sum(families.values()), n))
|
|
return data
|
|
|
|
|
|
def main():
|
|
try:
|
|
data = build()
|
|
except ExtractError as e:
|
|
print("extract_registry: ERROR: %s" % e, file=sys.stderr)
|
|
return 1
|
|
write_json(os.path.join(DATA_DIR, "registry.json"), data)
|
|
fams = ", ".join("%s %d" % kv for kv in sorted(data["families"].items()))
|
|
print("registry: %d widgets (%s), %d sections"
|
|
% (data["widget_count"], fams, data["section_count"]))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|