#!/usr/bin/env python3 """Generate lightgui/props_phase6.py from the C Phase 4 tables. Sources (read, never modified): /opt/lightgui/tools/gen_phase4_props.py -- the single source of truth the C work used: it parses the Stock*/Inst* widget headers under /opt/lightgui/widgets/components and derives one property table per widget (103 widgets, 1008 props, 116 distinct names, 0 missing defaults). /opt/lightgui_python/lightgui/xframe/palette.py -- the 547 palette kinds; the C class name maps to a palette kind through the palette's own registry-name table (mostly stripping the "Stock"/"Inst" prefix, but StockPriceLabel -> Price and InstRotarySelector -> Rotary, so the palette is the authority, not the prefix rule). The generated file keeps the C tables verbatim (internal prop names, kinds, defaults, ranges) and adds: * a Lazarus display name per prop (mechanical CamelCase of the internal name, e.g. up-color -> UpColor, anchor-1-bar -> Anchor1Bar), matching the style of the Phase 2 tables, * a model_only flag: only "symbol" (the ticker symbol stored on chart widgets; in C symbol_ is settable/readable but never drawn, so it is stored in the model and emitted by the generator as a comment). Every other Phase 6 prop is honoured by the draw code (colours, ranges, levels, values, anchors, view window, alarm bands, brick-size, bins, labels, ...) and live-applies. * registration via props.register_custom_table() on import, so the Inspector and the generator pick the tables up with no other change (props.py's register_custom_table() is the Phase 6 hook). StockChart up/down defaults are the corrected values from the C tables: up-color rgb(0,150,0), down-color rgb(200,30,30). Re-run: python3 tools/gen_props_phase6.py (from the repo root) """ import sys from pathlib import Path REPO = Path(__file__).resolve().parent.parent C_TOOLS = Path("/opt/lightgui/tools") WIDGETS_DIR = Path("/opt/lightgui/widgets/components") sys.path.insert(0, str(C_TOOLS)) import gen_phase4_props as c_gen # noqa: E402 # Palette kind -> C class name is the reverse of this strip; verified # against palette.py at generation time (must be exactly the 37 Stock # + 66 Instrumentation kinds, no duplicates, no strays). MODEL_ONLY = frozenset({"symbol"}) def _display_name(prop): """Mechanical Lazarus display name: up-color -> UpColor, anchor-1-bar -> Anchor1Bar, y-min -> YMin.""" parts = prop.replace("-", " ").split() return "".join(p[:1].upper() + p[1:] for p in parts) def _kind_of(class_name): return _REGISTRY_KIND[class_name] def _parse_palette_kinds(): """Registry name -> palette kind for the Stock/Inst entries, parsed statically from palette.py (no package import: importing the palette would import core, which imports the file being generated). Most names map by stripping the Stock/Inst prefix, but not all (StockPriceLabel -> Price, InstRotarySelector -> Rotary), so the palette table is the authority.""" import re palette_text = (REPO / "lightgui" / "xframe" / "palette.py") \ .read_text(encoding="utf-8") out = {} for match in re.finditer( r"PaletteEntry\(\d+, '((?:Stock|Inst)\w+)', '\w+', " r"'(?:Stock Market|Instrumentation)', \d+, \d+, \d+, " r"'\w+', '([^']+)'\)", palette_text): out[match.group(1)] = match.group(2) return out def _parse_all_palette_kinds(): """Registry name -> palette kind for ALL 547 entries, parsed statically (see _parse_palette_kinds). Used to find the shadowed names: entries outside the Stock/Inst families whose kind is a Phase 6 kind (kinds are shared across families).""" import re palette_text = (REPO / "lightgui" / "xframe" / "palette.py") \ .read_text(encoding="utf-8") out = {} for match in re.finditer( r"PaletteEntry\(\d+, '(\w+)', '\w+', '[^']+', \d+, \d+, " r"\d+, '\w+', '([^']+)'\)", palette_text): out[match.group(1)] = match.group(2) return out _REGISTRY_KIND = _parse_palette_kinds() def main(): widgets = c_gen.parse_widgets(str(WIDGETS_DIR)) assert len(widgets) == 103, "expected 103 widgets, got %d" \ % len(widgets) tables = {} total = 0 for class_name in sorted(widgets): props, missing = c_gen.props_for_widget(widgets, class_name) assert not missing, "missing defaults for %s: %s" \ % (class_name, missing) kind = _kind_of(class_name) assert kind not in tables, "duplicate kind %r" % kind tables[kind] = (class_name, props) total += len(props) assert total == 1008, "expected 1008 props, got %d" % total distinct = {p[0] for _c, props in tables.values() for p in props} assert len(distinct) == 116, "expected 116 distinct names, got %d" \ % len(distinct) # Cross-check against the palette: exactly the Stock + Inst kinds. palette_kinds = set(_REGISTRY_KIND.values()) assert palette_kinds == set(tables), \ "palette/generator kind mismatch: %s" % ( palette_kinds ^ set(tables),) # Shadowed names: entries outside the Stock/Inst families whose # kind is a Phase 6 kind. props._resolve_kind() maps a registry # name to its kind, so without an explicit table these would # inherit the Phase 6 table (IndVWAP would get StockVWAP's # props; TArrow would get StockArrow's). They keep the generic # set only, as in Phase 2 -- register them with empty tables so # _resolve_kind() keeps the registry name. all_kinds = _parse_all_palette_kinds() shadowed = sorted(name for name, kind in all_kinds.items() if kind in tables and name not in _REGISTRY_KIND) assert shadowed, "expected shadowed names, found none" assert not (set(shadowed) & set(tables)), \ "shadowed name collides with a Phase 6 kind: %s" % shadowed lines = [] lines.append('"""Property tables for the Stock (37) + Instrumentation') lines.append('(66) families -- Phase 6.') lines.append('') lines.append('GENERATED -- do not edit by hand. Run') lines.append('tools/gen_props_phase6.py to regenerate from') lines.append('/opt/lightgui/tools/gen_phase4_props.py (the C tables,') lines.append('canonical) and lightgui/xframe/palette.py.') lines.append('') lines.append('103 widgets, 1008 props, 116 distinct prop names.') lines.append('Generic set first (from lightgui.props.props_for), then') lines.append('each widget\'s own props below (register_custom_table).') lines.append('StockChart up/down defaults are the corrected values') lines.append('up-color rgb(0,150,0) / down-color rgb(200,30,30).') lines.append('Model-only: "symbol" only -- the ticker symbol stored on') lines.append('chart widgets; in C symbol_ is never drawn, so it is') lines.append('stored in the model and emitted by the generator as a') lines.append('comment. Every other prop here is honoured by the draw') lines.append('code and live-applies.') lines.append('Shadowed registry names (empty tables, generic set') lines.append('only): entries outside the Stock/Inst families whose') lines.append('palette kind is a Phase 6 kind -- kinds are shared') lines.append('across families, so without an explicit table') lines.append('props._resolve_kind() would map them onto the Phase 6') lines.append('table (IndVWAP onto StockVWAP, TArrow onto') lines.append('StockArrow).') lines.append('"""') lines.append('') lines.append('from lightgui.props import PropDef, register_custom_table') lines.append('') lines.append('') lines.append('# C class name -> palette kind, for reference.') lines.append('PHASE6_CLASSES = {') for kind in sorted(tables): class_name, _props = tables[kind] lines.append(' %r: %r,' % (kind, class_name)) lines.append('}') lines.append('') lines.append('# Registry names outside the Stock/Inst families whose') lines.append('# kind is a Phase 6 kind; generic set only (see above).') lines.append('SHADOWED = (') for name in shadowed: lines.append(' %r,' % name) lines.append(')') lines.append('') lines.append('TABLES = {') for kind in sorted(tables): _class_name, props = tables[kind] lines.append(' %r: (' % kind) for (prop, pkind, lo, hi, default, _setter) in props: lo_s = "0" if lo is None else repr(lo) hi_s = "0" if hi is None else repr(hi) model_only = "True" if prop in MODEL_ONLY else "False" lines.append( ' PropDef(%r, %r, %r, %s, %s, (), (), %r, %s),' % (prop, pkind, default, lo_s, hi_s, _display_name(prop), model_only)) lines.append(' ),') lines.append('}') lines.append('') lines.append('') lines.append('for _kind, _props in TABLES.items():') lines.append(' register_custom_table(_kind, _props)') lines.append('for _name in SHADOWED:') lines.append(' register_custom_table(_name, ())') lines.append('') out = REPO / "lightgui" / "props_phase6.py" out.write_text("\n".join(lines), encoding="utf-8") print("wrote %s: %d kinds, %d props, %d distinct names, %d shadowed" % (out, len(tables), total, len(distinct), len(shadowed))) if __name__ == "__main__": main()