52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
SAMPLES = ROOT / "samples"
|
|
INDEX = ROOT / "index" / "catalog.json"
|
|
|
|
|
|
def flatten_keys(value: Any) -> set[str]:
|
|
if isinstance(value, dict):
|
|
return set(value) | set().union(*(flatten_keys(item) for item in value.values()))
|
|
if isinstance(value, list):
|
|
return set().union(*(flatten_keys(item) for item in value)) if value else set()
|
|
return set()
|
|
|
|
|
|
def build_index() -> dict[str, Any]:
|
|
records: list[dict[str, Any]] = []
|
|
for source in sorted(SAMPLES.glob("*/model.cdsl.json")):
|
|
cdsl = json.loads(source.read_text(encoding="utf-8"))
|
|
profiles = [
|
|
str(sketch.get("profile", {}).get("type"))
|
|
for sketch in cdsl.get("geometry", {}).get("sketches", [])
|
|
if sketch.get("profile", {}).get("type")
|
|
]
|
|
features = [str(feature.get("atomic_id")) for feature in cdsl.get("features", [])]
|
|
parameter_names = sorted(flatten_keys(cdsl.get("geometry", {})) | flatten_keys(cdsl.get("features", [])))
|
|
part_id = str(cdsl.get("part_id") or source.parent.name)
|
|
source_name = str(cdsl.get("meta", {}).get("source") or part_id)
|
|
records.append({
|
|
"part_id": part_id,
|
|
"source_name": source_name,
|
|
"profiles": profiles,
|
|
"features": features,
|
|
"parameters": parameter_names,
|
|
"summary": f"{part_id}: {', '.join(profiles)}; {', '.join(features)}",
|
|
})
|
|
payload = {"schema_version": "1.0", "samples": records}
|
|
INDEX.parent.mkdir(parents=True, exist_ok=True)
|
|
INDEX.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
return payload
|
|
|
|
|
|
if __name__ == "__main__":
|
|
result = build_index()
|
|
print(json.dumps({"samples": len(result["samples"])}, ensure_ascii=False))
|