mirror of
https://github.com/lllin000/PaperForge.git
synced 2026-07-22 17:00:23 +00:00
Atomic snapshots, canonical index mutation serialization, sync post-clean truth, plugin path config-awareness, embed stop signal honesty, full snapshot bootstrap, pf_ prefix unification, workflow command/lifecycle/path corrections, mechanical/cognitive route separation with unknown-command guard.
22 lines
664 B
Python
22 lines
664 B
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
def read_json(path: Path):
|
|
"""Read and parse a JSON file."""
|
|
return json.loads(path.read_text(encoding="utf-8"))
|
|
|
|
|
|
def write_json_atomic(path: Path, data) -> None:
|
|
"""Write JSON atomically using a temp file and replace."""
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = path.with_suffix(f"{path.suffix}.tmp")
|
|
tmp.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
tmp.replace(path)
|
|
|
|
|
|
def write_json(path: Path, data) -> None:
|
|
"""Write data as JSON, creating parent directories as needed."""
|
|
write_json_atomic(path, data)
|