AI Implementation feature(1082): Project Scaffold and Docker Packaging (#1)
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
def posted_path(data_dir: Path) -> Path:
|
||||
return Path(data_dir) / "posted.json"
|
||||
|
||||
|
||||
def load_posted(data_dir: Path) -> dict[str, Any]:
|
||||
"""Return the posted-state map, creating an empty one if missing."""
|
||||
path = posted_path(data_dir)
|
||||
if not path.exists():
|
||||
return {}
|
||||
try:
|
||||
with path.open("r", encoding="utf-8") as fh:
|
||||
data = json.load(fh)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
if not isinstance(data, dict):
|
||||
return {}
|
||||
return data
|
||||
|
||||
|
||||
def save_posted(data_dir: Path, state: dict[str, Any]) -> None:
|
||||
"""Persist the posted-state map atomically-ish."""
|
||||
path = posted_path(data_dir)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = path.with_suffix(path.suffix + ".tmp")
|
||||
with tmp.open("w", encoding="utf-8") as fh:
|
||||
json.dump(state, fh, sort_keys=True, indent=2)
|
||||
tmp.replace(path)
|
||||
Reference in New Issue
Block a user