Files
lightgui_rust/tools/extract_props.py
T
Muse 9e6f882575 Regen data tables from lightgui e474894: kTypes 36->38, EventDef lazarus
- tools/extract_palette.py: kTypes expectation 36->38 (ToggleButton and
  CheckListBox added in C); palette total 598->600
- tools/extract_events.py: parse 6th EventDef field `lazarus` (Flag 10
  Lazarus Object Inspector name); scope table/dispatch parsing to
  events_for() (raw_events_for() has its own `raw` table, deliberately
  excluded); dispatch now tests the normalised key `t`, not `type`
- data/*.json regenerated via `make regen`: registry default sizes per
  the C rule (incl. the 15 monitor widgets' hand-written sizes),
  palette Misc/X order per C xframe_palette.h, props 37 types / 270
  props / 25 enum tables / 35 common props, events 29 types with
  lazarus names; indicators provenance head only
- crates/xframe-common: palette asserts 598->600, LightGUI 36->38
- crates/xframe-widgets: *_gen.rs regenerated; gen_data.py opt_f now
  emits float literals for Option<f64> lo/hi (pre-existing latent bug:
  running it on the old data also emitted Some(0)); stale eb0b5fc /
  547 / 9 / 35 / 27 counts updated to e474894 / 562 / 25 / 37 / 29

make check: all 5 tables OK. cargo build warning-free. cargo test
--workspace --no-fail-fast: all suites green except 5 pre-existing
environmental failures, verified identical on HEAD: lightgui-debug
console (37) + gtk (7) need /usr/bin/gdb (not installed on dallas),
lightgui-support font/theme need a display (gtk::init fails), and
lightgui-gen fixtures is stale vs the Task 4 event routing in gen.rs.
2026-12-05 19:20:00 +00:00

196 lines
7.0 KiB
Python

#!/usr/bin/env python3
"""Extract the designer property tables from src/props.h into data/props.json.
Sources (read-only, /opt/lightgui):
- src/props.h — PropKind enum, enum value tables, one PropDef table
per widget type, common_props(), props_for() dispatch
- src/model.h — kTypes: the 38 types the tables must cover
PropDef fields: name, kind, default (canonical string), lo/hi (spin
range, meaningful only for Int/Double), enum_labels / enum_values
(Enum only). canonicalize() in main.cpp enforces lo/hi — the Rust
port must keep the same tables and the same clamping, so lo/hi are
preserved exactly, including the negative and fractional bounds.
One type has no table by design: Canvas gets common props only
(props_for returns `none` for it). Every other kTypes key must have
exactly one table.
Usage: python3 tools/extract_props.py
"""
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common import (DATA_DIR, ExtractError, braced_groups, cpp_string,
fail, parse_ktypes, parse_number, provenance,
read_source, split_top_level, table_body, write_json)
PROPS_H = "src/props.h"
GENERATOR = "python3 tools/extract_props.py"
PROP_KINDS = {"Str", "Bool", "Int", "Double", "Color", "Enum"}
def parse_prop_kinds(text):
m = re.search(r"enum class PropKind \{(.*?)\};", text, re.S)
if not m:
fail("PropKind enum not found in %s" % PROPS_H)
kinds = [k.strip() for k in m.group(1).split(",") if k.strip()]
if set(kinds) != PROP_KINDS:
fail("PropKind variants changed: %s" % kinds)
return kinds
def parse_enum_tables(text):
"""inline const int kXxxValues[] = {0, 1, 2}; // comment labels"""
tables = {}
for m in re.finditer(
r"inline const int (k\w+Values)\[\] = \{([^}]*)\};[ \t]*(//.*)?",
text):
values = [int(x) for x in m.group(2).split(",") if x.strip()]
comment = (m.group(3) or "").lstrip("/").strip()
tables[m.group(1)] = {"values": values, "comment": comment}
return tables
def parse_prop_entry(group, enum_tables):
fields = split_top_level(group)
if len(fields) < 3:
fail("malformed PropDef entry: {%s}" % group)
name = cpp_string(fields[0])
kind = fields[1].strip()
if "::" in kind:
kind = kind.split("::", 1)[1]
if kind not in PROP_KINDS:
fail("unknown PropKind %r in entry for %r" % (kind, name))
default = cpp_string(fields[2])
entry = {"name": name, "kind": kind, "default": default}
rest = fields[3:]
if kind in ("Int", "Double"):
if len(rest) < 2:
fail("Int/Double prop %r lacks lo/hi" % name)
entry["lo"] = parse_number(rest[0])
entry["hi"] = parse_number(rest[1])
rest = rest[2:]
elif kind == "Enum":
# Two placeholder 0,0 fields, then labels, then the values table.
if len(rest) < 4:
fail("Enum prop %r lacks labels/values" % name)
entry["enum_labels"] = cpp_string(rest[2]).split("|")
values_name = rest[3].strip()
if values_name not in enum_tables:
fail("Enum prop %r references unknown table %s"
% (name, values_name))
entry["enum_values_table"] = values_name
entry["enum_values"] = enum_tables[values_name]["values"]
if len(entry["enum_labels"]) != len(entry["enum_values"]):
fail("Enum prop %r: %d labels but %d values"
% (name, len(entry["enum_labels"]),
len(entry["enum_values"])))
rest = rest[4:]
if rest:
fail("unexpected trailing fields in PropDef entry for %r: %s"
% (name, rest))
return entry
def parse_table(text, var_name):
body = table_body(
text,
r"static const std::vector<PropDef> %s = \{" % re.escape(var_name))
if body is None:
fail("PropDef table %r not found in %s" % (var_name, PROPS_H))
return [parse_prop_entry(g, ENUM_TABLES)
for g in braced_groups(body)]
ENUM_TABLES = {}
def build():
global ENUM_TABLES
text = read_source(PROPS_H)
kinds = parse_prop_kinds(text)
ENUM_TABLES = parse_enum_tables(text)
if not ENUM_TABLES:
fail("no enum value tables parsed from %s" % PROPS_H)
# Per-type tables: every `static const std::vector<PropDef> X = {`.
tables = {}
for m in re.finditer(
r"static const std::vector<PropDef> (\w+) = \{", text):
var = m.group(1)
if var == "v":
continue # common_props() table, parsed separately below
if var in tables:
fail("duplicate PropDef table %r" % var)
tables[var] = parse_table(text, var)
# common_props(): the `v` table inside common_props().
m = re.search(r"common_props\(\) \{\n\s*static const std::vector<PropDef> v = \{",
text)
if not m:
fail("common_props table not found in %s" % PROPS_H)
body = table_body(
text, r"common_props\(\) \{\n\s*static const std::vector<PropDef> v = \{")
common = [parse_prop_entry(g, ENUM_TABLES) for g in braced_groups(body)]
# Dispatch: if (type == "X") return <table>;
dispatch = {}
for m in re.finditer(r'if \(type == "([^"]+)"\) return (\w+);', text):
if m.group(1) in dispatch:
fail("type %r dispatched twice in props_for" % m.group(1))
dispatch[m.group(1)] = m.group(2)
for t, var in dispatch.items():
if var not in tables:
fail("props_for dispatches %r to missing table %r" % (t, var))
orphans = [v for v in tables if v not in dispatch.values()]
if orphans:
fail("PropDef tables never dispatched: %s" % orphans)
# Coverage: every kTypes key has a table except Canvas (by design).
ktypes = parse_ktypes()
keys = [t["key"] for t in ktypes]
no_table = [k for k in keys if k not in dispatch]
if no_table != ["Canvas"]:
fail("types without a props table (expected only Canvas): %s"
% no_table)
types = {}
for key in keys:
if key in dispatch:
types[key] = tables[dispatch[key]]
return {
"_provenance": provenance([PROPS_H, "src/model.h"], GENERATOR),
"prop_kinds": kinds,
"enum_tables": ENUM_TABLES,
"common_props": common,
"types": types,
"types_with_table": len(types),
"types_without_table": no_table,
"type_count": len(keys),
}
def main():
try:
data = build()
except ExtractError as e:
print("extract_props: ERROR: %s" % e, file=sys.stderr)
return 1
write_json(os.path.join(DATA_DIR, "props.json"), data)
n_props = sum(len(v) for v in data["types"].values())
print("props: %d kinds, %d enum tables, %d types with tables "
"(%d props), %d without (%s)"
% (len(data["prop_kinds"]), len(data["enum_tables"]),
data["types_with_table"], n_props,
len(data["types_without_table"]),
",".join(data["types_without_table"])))
return 0
if __name__ == "__main__":
sys.exit(main())