#!/usr/bin/env python3 """Regenerate lossless CSV viewer projections and synchronize the data catalog.""" from __future__ import annotations import csv,hashlib,json from pathlib import Path ROOT=Path(__file__).resolve().parents[1] def main(): dest=ROOT/"hub_data";dest.mkdir(exist_ok=True) for name in ["v3_components_m6","v3_visibility_certificates"]: records=json.loads((ROOT/"data"/(name+".json")).read_text(encoding="utf-8")) rows=[] for r in records: row={"record_json":json.dumps(r,ensure_ascii=False,separators=(",",":")),"channels":6} if name=="v3_components_m6": row.update(component_id=r["type"]+"-"+"-".join(map(str,r["labels_zero_based"])), kind=r["type"],dimension=r["dimension"], labels_zero_based_json=json.dumps(r["labels_zero_based"],separators=(",",":")), equation_count=len(r["linear_equations_ascending_edges"])) else: row.update(stratum=r["stratum"],blocks=3,visible=r["visible"], normalized_response_exact=str(r["normalized_response"]), motif_json=json.dumps(r["motif"],separators=(",",":"))) rows.append(row) p=dest/(name+".csv") with p.open("w",newline="",encoding="utf-8") as f: w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows) print(name,len(rows)) cp=ROOT/"data_catalog.json" catalog=json.loads(cp.read_text(encoding="utf-8")) for item in catalog["configurations"]: p=ROOT/item["path"] with p.open(newline="",encoding="utf-8") as f: r=csv.DictReader(f);rows=list(r) item.update(columns=r.fieldnames,rows=len(rows),sha256=hashlib.sha256(p.read_bytes()).hexdigest()) catalog["total_rows"]=sum(x["rows"] for x in catalog["configurations"]) cp.write_text(json.dumps(catalog,indent=2,ensure_ascii=False)+"\n",encoding="utf-8") return 0 if __name__=="__main__":raise SystemExit(main())