Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""Lokal lagring: JSON-filer (bruger-præferencer, cookie-tekster) + IP-label-sync til NPMplus."""
|
|
import json
|
|
import subprocess
|
|
|
|
from config import DATA, IP_LABELS_LOCAL, IP_LABELS_REMOTE, SETTINGS_PATH, USERPREFS # noqa: F401
|
|
|
|
|
|
def load_json(path, default):
|
|
try:
|
|
return json.loads(path.read_text("utf-8"))
|
|
except Exception:
|
|
return default
|
|
|
|
|
|
def save_ip_labels(data: dict) -> str:
|
|
IP_LABELS_LOCAL.write_text(json.dumps(data, ensure_ascii=False, indent=2), "utf-8")
|
|
if IP_LABELS_REMOTE:
|
|
try:
|
|
subprocess.run(
|
|
["scp", "-o", "StrictHostKeyChecking=accept-new", "-o", "ConnectTimeout=8",
|
|
str(IP_LABELS_LOCAL), IP_LABELS_REMOTE],
|
|
check=True, capture_output=True, timeout=20)
|
|
return "gemt + synkroniseret til NPMplus"
|
|
except Exception as e:
|
|
return f"gemt lokalt, men sync til NPMplus fejlede: {e}"
|
|
return "gemt lokalt"
|
|
|
|
|
|
def get_prefs(username):
|
|
p = (load_json(USERPREFS, {}) or {}).get(username) or {}
|
|
return {"favorites": p.get("favorites") or [], "view": p.get("view") or "grouped"}
|
|
|
|
|
|
def set_prefs(username, **kw):
|
|
allp = load_json(USERPREFS, {}) or {}
|
|
cur = allp.get(username) or {}
|
|
cur.update(kw)
|
|
allp[username] = cur
|
|
USERPREFS.write_text(json.dumps(allp, ensure_ascii=False, indent=2), "utf-8")
|