32 lines
998 B
Python
32 lines
998 B
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
class StateStore:
|
|
"""Small JSON store for UI preferences; native runtime state is not stored here."""
|
|
|
|
def __init__(self) -> None:
|
|
storage_root = os.getenv("FLET_APP_STORAGE_DATA")
|
|
root = Path(
|
|
os.getenv(
|
|
"VPNAPP_STATE_DIR",
|
|
storage_root or str(Path.home() / ".vpnapp"),
|
|
)
|
|
)
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
self.path = root / "ui-state.json"
|
|
|
|
def load(self) -> dict[str, Any]:
|
|
try:
|
|
return json.loads(self.path.read_text(encoding="utf-8"))
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
|
return {}
|
|
|
|
def save(self, value: dict[str, Any]) -> None:
|
|
temporary = self.path.with_suffix(".tmp")
|
|
temporary.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
temporary.replace(self.path)
|