47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import json
|
|
import os
|
|
from .security import get_safe_path, STATE_DIR
|
|
|
|
def _load_json(filename):
|
|
try:
|
|
path = get_safe_path(STATE_DIR, filename)
|
|
if os.path.exists(path):
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except Exception:
|
|
pass
|
|
return {}
|
|
|
|
def _save_json(filename, data):
|
|
os.makedirs(os.path.realpath(STATE_DIR), exist_ok=True)
|
|
path = get_safe_path(STATE_DIR, filename)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, indent=2)
|
|
|
|
# --- Staging Cache (Planner) ---
|
|
def load_staging(project_name):
|
|
return _load_json(f"{project_name}_staging.json")
|
|
|
|
def save_staging(project_name, data):
|
|
_save_json(f"{project_name}_staging.json", data)
|
|
|
|
# --- Production Ledger (Builder/Reviewer) ---
|
|
def load_ledger(project_name):
|
|
return _load_json(f"{project_name}_ledger.json")
|
|
|
|
def save_ledger(project_name, data):
|
|
_save_json(f"{project_name}_ledger.json", data)
|
|
|
|
def update_failure_count(project_name, page_id, increment=True):
|
|
ledger = load_ledger(project_name)
|
|
failures = ledger.get("failure_counts", {})
|
|
current = failures.get(page_id, 0)
|
|
|
|
if increment:
|
|
failures[page_id] = current + 1
|
|
else:
|
|
failures[page_id] = 0
|
|
|
|
ledger["failure_counts"] = failures
|
|
save_ledger(project_name, ledger)
|
|
return failures[page_id] |