b63a796907
Centralize read_json/write_json_atomic used by pipeline meta I/O and worker processed.json bookkeeping. Preserves corrupt-file warning in load_processed(). Co-authored-by: Cursor <cursoragent@cursor.com>
26 lines
762 B
Python
26 lines
762 B
Python
"""Small shared I/O helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
def read_json(path: Path, *, default: Any = None) -> Any:
|
|
"""Read JSON from *path*, returning *default* when missing or corrupt."""
|
|
if not path.exists():
|
|
return default
|
|
try:
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
except (OSError, ValueError):
|
|
return default
|
|
|
|
|
|
def write_json_atomic(path: Path, data: Any) -> None:
|
|
"""Write JSON via a temp file and atomic replace."""
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_suffix(path.suffix + ".tmp")
|
|
tmp.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
tmp.replace(path)
|