#!/usr/bin/env python3 """Generate a compact runtime index for the generated Python API dispatcher.""" from __future__ import annotations import hashlib import json from pathlib import Path ROOT = Path(__file__).resolve().parents[1] manifest_path = ROOT / "docs/p6/python-api-manifest.json" out = ROOT / "web/public/api/python-api-index.json" diagnostic_out = ROOT / "docs/p6/python-api-index.json" manifest = json.loads(manifest_path.read_text(encoding="utf-8")) rows = manifest["records"] fields = { "modules": sorted({row["module"] for row in rows}), "kinds": sorted({row["kind"] for row in rows}), "families": sorted({row["family"] for row in rows}), "tiers": sorted({row["tier"] for row in rows}), "dispositions": sorted({row["disposition"] for row in rows}), } lookup = {key: {value: index for index, value in enumerate(values)} for key, values in fields.items()} records = [] for row in rows: records.append([ row["id"], lookup["modules"][row["module"]], lookup["kinds"][row["kind"]], row["name"], lookup["families"][row["family"]], lookup["tiers"][row["tier"]], lookup["dispositions"][row["disposition"]], row.get("evidence", {}).get("path"), row.get("evidence", {}).get("line"), ]) index = { "schema": "freecad-web-python-api-runtime-index/v1", "source": {"manifest": "docs/p6/python-api-manifest.json", "manifestSha256": hashlib.sha256(manifest_path.read_bytes()).hexdigest()}, "fields": fields, "records": records, } out.parent.mkdir(parents=True, exist_ok=True) serialized = json.dumps(index, ensure_ascii=False, separators=(",", ":")) + "\n" out.write_text(serialized, encoding="utf-8") diagnostic_out.parent.mkdir(parents=True, exist_ok=True) diagnostic_out.write_text(serialized, encoding="utf-8") print(json.dumps({"output": str(out.relative_to(ROOT)), "diagnosticOutput": str(diagnostic_out.relative_to(ROOT)), "records": len(records), "bytes": out.stat().st_size, "sha256": hashlib.sha256(out.read_bytes()).hexdigest()}, ensure_ascii=False))