#!/usr/bin/env python3 """Export the JVMTM transaction-grade compliance matrix JSON to CSV.""" from __future__ import annotations import csv import json import sys from pathlib import Path FIELDNAMES = [ "control_id", "phase", "domain", "requirement", "validation_method", "blocking_level", "applies_to_rail", "source_audit_rows", "repo_evidence_artifacts", "validator_command", "failure_action", "high_value_override", "notes", ] def format_artifacts(artifacts: list[dict[str, str]]) -> str: return " | ".join(f'{artifact["artifact_type"]}:{artifact["ref"]}' for artifact in artifacts) def render_rows(matrix: dict) -> list[dict[str, str]]: rows: list[dict[str, str]] = [] for control in matrix["controls"]: rows.append( { "control_id": control["control_id"], "phase": control["phase"], "domain": control["domain"], "requirement": control["requirement"], "validation_method": control["validation_method"], "blocking_level": control["blocking_level"], "applies_to_rail": " | ".join(control["applies_to_rail"]), "source_audit_rows": " | ".join(control["source_audit_rows"]), "repo_evidence_artifacts": format_artifacts(control["repo_evidence_artifacts"]), "validator_command": control["validator_command"], "failure_action": control["failure_action"], "high_value_override": control["high_value_override"], "notes": control["notes"], } ) return rows def main() -> int: repo_root = Path(__file__).resolve().parents[2] matrix_path = ( Path(sys.argv[1]) if len(sys.argv) > 1 else repo_root / "config/jvmtm-regulatory-closure/transaction-compliance-matrix.json" ) csv_path = ( Path(sys.argv[2]) if len(sys.argv) > 2 else repo_root / "config/jvmtm-regulatory-closure/transaction-compliance-matrix.csv" ) matrix = json.loads(matrix_path.read_text(encoding="utf-8")) rows = render_rows(matrix) csv_path.parent.mkdir(parents=True, exist_ok=True) with csv_path.open("w", encoding="utf-8", newline="") as handle: writer = csv.DictWriter(handle, fieldnames=FIELDNAMES, lineterminator="\n") writer.writeheader() writer.writerows(rows) print(f"Wrote {csv_path} ({len(rows)} controls)") return 0 if __name__ == "__main__": raise SystemExit(main())