from __future__ import annotations from dataclasses import dataclass from datetime import datetime import json from pathlib import Path import tomllib from typing import Any @dataclass(frozen=True) class RuntimePaths: root_dir: Path analysis_dir: Path output_root: Path run_dir: Path def load_config(root_dir: Path) -> dict[str, Any]: config_path = root_dir / "config.toml" with config_path.open("rb") as f: return tomllib.load(f) def init_runtime_paths(analysis_dir: Path, run_name: str | None = None) -> RuntimePaths: root_dir = analysis_dir.parent output_root = root_dir / "analysis_output" output_root.mkdir(parents=True, exist_ok=True) safe_run_name = run_name or datetime.now().strftime("run_%Y%m%d_%H%M%S") run_dir = output_root / safe_run_name run_dir.mkdir(parents=True, exist_ok=True) return RuntimePaths( root_dir=root_dir, analysis_dir=analysis_dir, output_root=output_root, run_dir=run_dir, ) def backend_dir(paths: RuntimePaths, backend: str) -> Path: out = paths.run_dir / backend out.mkdir(parents=True, exist_ok=True) return out def write_json(path: Path, payload: dict[str, Any]) -> None: path.parent.mkdir(parents=True, exist_ok=True) with path.open("w", encoding="utf-8") as f: json.dump(payload, f, indent=2)