Spaces:
Paused
Paused
File size: 1,968 Bytes
e8829d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """JSON persistence for vitis runs under runs/vitis/ (root settable for tests)."""
from __future__ import annotations
import json
import secrets
from pathlib import Path
from typing import Dict, List, Optional
_ROOT = Path("runs/vitis")
def set_vitis_root(path: str) -> None:
global _ROOT
_ROOT = Path(path)
def vitis_root() -> Path:
return _ROOT
def is_default_root() -> bool:
"""True when persisting to the real runs/vitis dir (not a test tmpdir)."""
return _ROOT == Path("runs/vitis")
def new_id(prefix: str) -> str:
return f"{prefix}-{secrets.token_hex(5)}"
def save_run(kind: str, run_id: str, payload: Dict) -> Path:
d = _ROOT / kind
d.mkdir(parents=True, exist_ok=True)
path = d / f"{run_id}.json"
path.write_text(json.dumps(payload, indent=1, default=_json_default))
(d / "latest.json").write_text(
json.dumps(payload, indent=1, default=_json_default))
return path
def load_run(kind: str, run_id: str) -> Optional[Dict]:
path = _ROOT / kind / f"{run_id}.json"
if not path.exists():
return None
return json.loads(path.read_text())
def list_runs(kind: str) -> List[Dict]:
d = _ROOT / kind
if not d.exists():
return []
out = []
for p in sorted(d.glob(f"{kind[:3]}*-*.json")):
try:
data = json.loads(p.read_text())
out.append({"run_id": data.get("run_id", p.stem),
"created_at": data.get("created_at"),
"summary": data.get("summary", {})})
except (json.JSONDecodeError, OSError):
continue
return out
def _json_default(o):
import numpy as np
if isinstance(o, (np.integer,)):
return int(o)
if isinstance(o, (np.floating,)):
return float(o)
if isinstance(o, np.ndarray):
return o.tolist()
if isinstance(o, (np.bool_,)):
return bool(o)
raise TypeError(f"Not JSON serializable: {type(o)}")
|