# scripts/lib/local_state.py """Read/write .me-hub/local-state.json — per-machine, gitignored.""" from __future__ import annotations import json from datetime import datetime, timezone from pathlib import Path from typing import Optional from lib.paths import repo_root def _default_local_state_path() -> Path: return repo_root() / ".me-hub" / "local-state.json" _LOCAL_STATE_PATH_FN = _default_local_state_path SCHEMA_VERSION = 1 def write(data: dict) -> None: """Write the local-state file. Adds schema_version.""" path = _LOCAL_STATE_PATH_FN() path.parent.mkdir(parents=True, exist_ok=True) payload = {"schema_version": SCHEMA_VERSION, **data} path.write_text(json.dumps(payload, indent=2, sort_keys=True), encoding="utf-8") def read() -> Optional[dict]: """Return the parsed local-state dict, or None if the file is missing.""" path = _LOCAL_STATE_PATH_FN() if not path.is_file(): return None return json.loads(path.read_text(encoding="utf-8")) def update_last_refresh(state_commit: str) -> None: """Stamp the current UTC time and the source-state commit we synced against.""" cur = read() or {} cur["last_local_refresh_utc"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") cur["last_seen_state_commit"] = state_commit write(cur)