some fixes and improvements
This commit is contained in:
@@ -4,8 +4,11 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class GuiSessionState:
|
||||
@@ -32,13 +35,22 @@ class GuiSessionStateStore:
|
||||
if not self._path.exists():
|
||||
return GuiSessionState()
|
||||
|
||||
payload = json.loads(self._path.read_text(encoding="utf-8"))
|
||||
try:
|
||||
payload = json.loads(self._path.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError) as exc:
|
||||
# The state file is GUI-local cache — a corrupted file should not
|
||||
# prevent the app from starting. Reset to defaults and let the
|
||||
# next write overwrite it.
|
||||
logger.warning("Resetting unreadable GUI session-state %s: %s", self._path, exc)
|
||||
return GuiSessionState()
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError(f"GUI session-state root must be JSON object: {self._path}")
|
||||
logger.warning("Resetting GUI session-state with non-object root: %s", self._path)
|
||||
return GuiSessionState()
|
||||
|
||||
raw_path = payload.get("last_profile_path", "")
|
||||
if not isinstance(raw_path, str):
|
||||
raise ValueError("GUI session-state `last_profile_path` must be a string")
|
||||
logger.warning("Resetting GUI session-state with non-string last_profile_path: %s", self._path)
|
||||
return GuiSessionState()
|
||||
return GuiSessionState(last_profile_path=raw_path)
|
||||
|
||||
def write(self, state: GuiSessionState) -> Path:
|
||||
|
||||
Reference in New Issue
Block a user