35 lines
2.2 KiB
Python
35 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate a deduplicated runtime index for format import/export records."""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
source_path = ROOT / "docs/format-api-inventory.json"
|
|
out = ROOT / "web/public/api/format-runtime-index.json"
|
|
source = json.loads(source_path.read_text(encoding="utf-8"))
|
|
unique: dict[str, dict[str, object]] = {}
|
|
for row in source["formats"]:
|
|
direction = str(row["direction"]).lower()
|
|
module = str(row["module"])
|
|
kind = "extension" if "extension" in row else "class-evidence"
|
|
name = str(row.get("extension") or row.get("class") or "").lower() if kind == "extension" else str(row.get("class") or "")
|
|
prefix = "fmt" if kind == "extension" else "fmt-class"
|
|
key = f"{prefix}:{module}:{name}:{direction}"
|
|
record = unique.setdefault(key, {"id": key, "kind": kind, "name": name, "direction": direction, "module": module, "evidenceCount": 0})
|
|
record["evidenceCount"] = int(record["evidenceCount"]) + 1
|
|
rows = sorted(unique.values(), key=lambda row: str(row["id"]))
|
|
fields = {
|
|
"modules": sorted({str(row["module"]) for row in rows}),
|
|
"directions": sorted({str(row["direction"]) for row in rows}),
|
|
"kinds": sorted({str(row["kind"]) for row in rows}),
|
|
}
|
|
lookup = {key: {value: index for index, value in enumerate(values)} for key, values in fields.items()}
|
|
records = [[row["id"], lookup["modules"][row["module"]], lookup["kinds"][row["kind"]], row["name"], lookup["directions"][row["direction"]], row["evidenceCount"]] for row in rows]
|
|
index = {"schema": "freecad-web-format-runtime-index/v1", "source": {"inventory": "docs/format-api-inventory.json", "inventoryRecords": len(source["formats"]), "inventorySha256": hashlib.sha256(source_path.read_bytes()).hexdigest()}, "fields": fields, "records": records}
|
|
out.parent.mkdir(parents=True, exist_ok=True)
|
|
out.write_text(json.dumps(index, ensure_ascii=False, separators=(",", ":")) + "\n", encoding="utf-8")
|
|
print(json.dumps({"output": str(out.relative_to(ROOT)), "records": len(records), "bytes": out.stat().st_size, "sha256": hashlib.sha256(out.read_bytes()).hexdigest()}, ensure_ascii=False))
|