Files
lightgui_rust/tools/extract_events.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

201 lines
7.1 KiB
Python

#!/usr/bin/env python3
"""Extract the designer event/signal tables from src/events.h into data/events.json.
Sources (read-only, /opt/lightgui):
- src/events.h — EventKind enum (+ wiring notes in the header comment),
one EventDef table per widget type, events_for() dispatch,
no_events_note() for the types with no widget-specific signal
- src/model.h — kTypes: the 38 types the tables must cover
EventDef fields: signal (GTK signal name), handler (member handler
name), label (Events-pane label), kind, builtin (already wired by the
generator for other reasons — document only, do not reconnect),
lazarus (Lazarus Object Inspector name, e.g. "OnClick"; null when the
C++ default nullptr applies).
NOTE on the count: the survey/PORTING_PLAN.md says EventKind has 11
variants. The header itself defines 10 (Simple, SwitchPage,
RowActivated, RowSelected, BufferChanged, DeleteEvent, Draw,
ItemClicked, ItemActivate, CellChanged). This extractor records what
the header actually defines (10) and fails if that list changes.
Usage: python3 tools/extract_events.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,
cpp_string_concat, fail, parse_ktypes, provenance,
read_source, split_top_level, table_body, write_json)
EVENTS_H = "src/events.h"
GENERATOR = "python3 tools/extract_events.py"
def parse_event_kinds(text):
m = re.search(r"enum class EventKind \{(.*?)\};", text, re.S)
if not m:
fail("EventKind enum not found in %s" % EVENTS_H)
kinds = [k.strip() for k in m.group(1).split(",") if k.strip()]
return kinds
def parse_kind_notes(text):
"""Wiring note per kind, from the header comment at the top."""
notes = {}
for m in re.finditer(r"^//\s+(\w+)\s+—\s+(.*)$", text, re.M):
notes[m.group(1)] = m.group(2).strip()
return notes
def parse_event_entry(group, kinds):
fields = split_top_level(group)
if len(fields) < 4:
fail("malformed EventDef entry: {%s}" % group)
signal = cpp_string(fields[0])
handler = cpp_string(fields[1])
label = cpp_string(fields[2])
kind = fields[3].strip()
if "::" in kind:
kind = kind.split("::", 1)[1]
if kind not in kinds:
fail("unknown EventKind %r in entry for signal %r" % (kind, signal))
builtin = False
if len(fields) > 4:
if fields[4].strip() == "true":
builtin = True
elif fields[4].strip() == "false":
builtin = False
else:
fail("unexpected 5th field in EventDef for %r: %s"
% (signal, fields[4]))
lazarus = None
if len(fields) > 5:
f6 = fields[5].strip()
if f6 == "nullptr":
lazarus = None
else:
lazarus = cpp_string(f6)
if len(fields) > 6:
fail("unexpected trailing fields in EventDef for %r" % signal)
return {"signal": signal, "handler": handler, "label": label,
"kind": kind, "builtin": builtin, "lazarus": lazarus}
def parse_table(text, var_name, kinds):
body = table_body(
text,
r"static const std::vector<EventDef> %s = \{" % re.escape(var_name))
if body is None:
fail("EventDef table %r not found in %s" % (var_name, EVENTS_H))
return [parse_event_entry(g, kinds) for g in braced_groups(body)]
def parse_no_events_notes(text):
"""no_events_note(): type -> reason string (adjacent literals joined)."""
m = re.search(r"no_events_note\(const std::string& type\) \{(.*?)\n\}",
text, re.S)
if not m:
fail("no_events_note not found in %s" % EVENTS_H)
body = m.group(1)
notes = {}
for m in re.finditer(
r'if \(([^)]*)\)\s*return\s*((?:"(?:[^"\\]|\\.)*"\s*)+);', body):
cond = m.group(1)
types = re.findall(r'type == "([^"]+)"', cond)
if not types:
fail("unparsable no_events_note condition: %r" % cond)
note = cpp_string_concat(m.group(2))
for t in types:
if t in notes:
fail("type %r has two no-events notes" % t)
notes[t] = note
return notes
def build():
text = read_source(EVENTS_H)
kinds = parse_event_kinds(text)
kind_notes = parse_kind_notes(text)
for k in kinds:
if k not in kind_notes:
fail("EventKind %r has no wiring note in the header comment" % k)
# Scope to events_for(): the header also defines raw_events_for()
# with its own `raw` table (the same five raw events for every type),
# which is deliberately not part of the per-type tables, and the
# dispatch now tests the normalised key `t`, not `type`.
fn_body = table_body(
text,
r"inline const std::vector<EventDef>& events_for\(const std::string& type\) \{")
if fn_body is None:
fail("events_for not found in %s" % EVENTS_H)
tables = {}
for m in re.finditer(
r"static const std::vector<EventDef> (\w+) = \{", fn_body):
var = m.group(1)
if var in tables:
fail("duplicate EventDef table %r" % var)
tables[var] = parse_table(text, var, kinds)
dispatch = {}
for m in re.finditer(r'if \((\w+) == "([^"]+)"\) return (\w+);',
fn_body):
if m.group(2) in dispatch:
fail("type %r dispatched twice in events_for" % m.group(2))
dispatch[m.group(2)] = m.group(3)
for t, var in dispatch.items():
if var not in tables:
fail("events_for dispatches %r to missing table %r" % (t, var))
orphans = [v for v in tables if v not in dispatch.values()]
if orphans:
fail("EventDef tables never dispatched: %s" % orphans)
no_events = parse_no_events_notes(text)
ktypes = parse_ktypes()
keys = [t["key"] for t in ktypes]
with_events = [k for k in keys if k in dispatch]
without = [k for k in keys if k not in dispatch]
if sorted(without) != sorted(no_events):
fail("types without events (%s) != types with a no-events note (%s)"
% (sorted(without), sorted(no_events)))
for k in keys:
if k in dispatch and k in no_events:
fail("type %r has both events and a no-events note" % k)
types = {key: tables[dispatch[key]] for key in with_events}
return {
"_provenance": provenance([EVENTS_H, "src/model.h"], GENERATOR),
"event_kinds": kinds,
"event_kind_count": len(kinds),
"kind_notes": {k: kind_notes[k] for k in kinds},
"types": types,
"types_with_events": len(types),
"types_without_events": no_events,
"type_count": len(keys),
}
def main():
try:
data = build()
except ExtractError as e:
print("extract_events: ERROR: %s" % e, file=sys.stderr)
return 1
write_json(os.path.join(DATA_DIR, "events.json"), data)
n_events = sum(len(v) for v in data["types"].values())
print("events: %d kinds, %d types with events (%d events), "
"%d without"
% (data["event_kind_count"], data["types_with_events"],
n_events, len(data["types_without_events"])))
return 0
if __name__ == "__main__":
sys.exit(main())