Files
HVideoTool/hvideotool/settings_store.py
T

100 lines
2.9 KiB
Python

"""Persist a handful of user choices to ~/HVideoTool/settings.json.
For the project-based tool this file holds the **defaults for new projects** (the
detector, model path, overlay threshold, restore engine) plus app-level state: the
last opened project, the recent-projects list, and the last directory used in file
dialogs. Per-project settings live in each project's ``project.json``, not here.
"""
from __future__ import annotations
import json
from pathlib import Path
from .config import AppConfig
_PATH = Path.home() / "HVideoTool" / "settings.json"
def _read() -> dict:
try:
return json.loads(_PATH.read_text(encoding="utf-8"))
except (OSError, ValueError):
return {}
def _write(data: dict) -> None:
_PATH.parent.mkdir(parents=True, exist_ok=True)
_PATH.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
def apply(config: AppConfig) -> None:
"""Overlay persisted settings onto ``config`` (mutates it in place)."""
data = _read()
if data.get("detector"):
config.detector = data["detector"]
if isinstance(data.get("detector_models"), list):
config.detector_models = [str(m) for m in data["detector_models"]]
if "threshold" in data:
config.default_threshold = float(data["threshold"])
if data.get("restorer"):
config.restorer = data["restorer"]
for key in ("dm_dir", "dm_model", "dm_gpu"):
if key in data:
setattr(config, key, data[key])
if "dm_feed_restored" in data:
config.dm_feed_restored = bool(data["dm_feed_restored"])
def save(config: AppConfig) -> None:
"""Persist the configurable settings, preserving other keys (e.g. last_dir)."""
data = _read()
data.update(
detector=config.detector,
detector_models=list(config.detector_models),
threshold=config.default_threshold,
restorer=config.restorer,
dm_dir=config.dm_dir,
dm_model=config.dm_model,
dm_gpu=config.dm_gpu,
dm_feed_restored=config.dm_feed_restored,
)
_write(data)
def last_dir() -> str | None:
return _read().get("last_dir")
def set_last_dir(path: str) -> None:
data = _read()
data["last_dir"] = path
_write(data)
def last_project() -> str | None:
return _read().get("last_project")
def set_last_project(path: str) -> None:
data = _read()
data["last_project"] = path
_write(data)
_RECENTS_CAP = 10
def recent_projects() -> list[str]:
recents = _read().get("recent_projects", [])
return [p for p in recents if isinstance(p, str)]
def add_recent_project(path: str) -> None:
"""Push ``path`` to the front of the recent-projects list (deduped, capped)."""
data = _read()
recents = [p for p in data.get("recent_projects", []) if isinstance(p, str) and p != path]
recents.insert(0, path)
data["recent_projects"] = recents[:_RECENTS_CAP]
_write(data)