Menú d'opcions: ocultar no descarregats i comprovar actualitzacions, amb persistència
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
"""Preferencias persistentes en settings.json, junto al ejecutable."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
from .paths import base_dir
|
||||
|
||||
SETTINGS_NAME = "settings.json"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Settings:
|
||||
hide_not_downloaded: bool = False
|
||||
updates_pending: list[str] = field(default_factory=list) # ids con update pendiente
|
||||
|
||||
|
||||
def settings_path() -> Path:
|
||||
return base_dir() / SETTINGS_NAME
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
path = settings_path()
|
||||
if not path.exists():
|
||||
return Settings()
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return Settings()
|
||||
return Settings(
|
||||
hide_not_downloaded=bool(data.get("hide_not_downloaded", False)),
|
||||
updates_pending=list(data.get("updates_pending", [])),
|
||||
)
|
||||
|
||||
|
||||
def save_settings(settings: Settings) -> None:
|
||||
try:
|
||||
settings_path().write_text(
|
||||
json.dumps(asdict(settings), ensure_ascii=False, indent=2),
|
||||
encoding="utf-8",
|
||||
)
|
||||
except OSError:
|
||||
pass
|
||||
Reference in New Issue
Block a user