mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Unified prefs writing
This commit is contained in:
1 parent
957525393b
commit
4e9472c14a
8 files changed
+274
-69
No files matched your search
+68
-31
@@ -3,10 +3,19 @@
|
||||
A self-contained JSON file in the app's per-OS user-writable data dir (see
|
||||
``paths.app_data_dir``) so it survives a frozen/packaged build and works the same
|
||||
on every platform. Best-effort: any I/O error degrades to "no prefs".
|
||||
|
||||
**Every read-modify-write holds ``_LOCK`` and every write is atomic.** FastAPI
|
||||
runs ``PATCH /api/prefs`` on a thread pool, and the UI sends several at once on
|
||||
launch. Unlocked, with ``write_text`` truncating before it wrote, a reader could
|
||||
catch the file empty, take it for "no prefs" and write back only its own key —
|
||||
wiping everything else (measured: 249 of 300 trials of three concurrent patches).
|
||||
A file that exists but will not parse is moved aside before it is replaced, so
|
||||
it is never silently overwritten.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from . import paths
|
||||
@@ -16,6 +25,9 @@ from . import paths
|
||||
_PREFS_PATH = paths.app_data_dir() / "userprefs.json"
|
||||
_LEGACY_PREFS_PATH = Path(__file__).resolve().parent.parent / "userprefs.json"
|
||||
|
||||
# Reentrant: the helpers below read and write inside one critical section.
|
||||
_LOCK = threading.RLock()
|
||||
|
||||
|
||||
def _migrate_legacy() -> None:
|
||||
"""One-time: if the new file is absent but a legacy package-relative one
|
||||
@@ -27,29 +39,52 @@ def _migrate_legacy() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def load_prefs() -> dict:
|
||||
def _read(*, for_write: bool) -> dict:
|
||||
"""The stored prefs, or {} if there are none or they cannot be read.
|
||||
|
||||
With ``for_write``, a file that exists but is not a JSON object is renamed
|
||||
to ``userprefs.json.corrupt`` first, since the caller is about to replace it.
|
||||
"""
|
||||
try:
|
||||
_migrate_legacy()
|
||||
data = json.loads(_PREFS_PATH.read_text())
|
||||
return data if isinstance(data, dict) else {}
|
||||
except (OSError, ValueError):
|
||||
text = _PREFS_PATH.read_text()
|
||||
except OSError:
|
||||
return {}
|
||||
try:
|
||||
data = json.loads(text)
|
||||
if isinstance(data, dict):
|
||||
return data
|
||||
except ValueError:
|
||||
pass
|
||||
if for_write:
|
||||
try:
|
||||
_PREFS_PATH.replace(_PREFS_PATH.with_name(_PREFS_PATH.name + ".corrupt"))
|
||||
except OSError:
|
||||
pass
|
||||
return {}
|
||||
|
||||
|
||||
def load_prefs() -> dict:
|
||||
with _LOCK:
|
||||
return _read(for_write=False)
|
||||
|
||||
|
||||
def save_prefs(prefs: dict) -> None:
|
||||
try:
|
||||
_PREFS_PATH.write_text(json.dumps(prefs, indent=2))
|
||||
except OSError:
|
||||
pass # non-fatal — prefs are a convenience, not a requirement
|
||||
with _LOCK:
|
||||
try:
|
||||
paths.write_json(_PREFS_PATH, prefs)
|
||||
except OSError:
|
||||
pass # non-fatal — prefs are a convenience, not a requirement
|
||||
|
||||
|
||||
def update_prefs(patch: dict) -> dict:
|
||||
"""Shallow-merge `patch` into the stored prefs and persist. Returns the
|
||||
full updated prefs dict."""
|
||||
prefs = load_prefs()
|
||||
prefs.update(patch)
|
||||
save_prefs(prefs)
|
||||
return prefs
|
||||
with _LOCK:
|
||||
prefs = _read(for_write=True)
|
||||
prefs.update(patch)
|
||||
save_prefs(prefs)
|
||||
return prefs
|
||||
|
||||
|
||||
def get_path_mapping(collection_path: str) -> dict | None:
|
||||
@@ -69,16 +104,17 @@ def set_path_mapping(
|
||||
collection_path: str, from_prefix: str | None, to_prefix: str | None
|
||||
) -> None:
|
||||
"""Persist (or, when either prefix is blank, clear) a collection's mapping."""
|
||||
prefs = load_prefs()
|
||||
mappings = prefs.get("path_mappings")
|
||||
if not isinstance(mappings, dict):
|
||||
mappings = {}
|
||||
if from_prefix and to_prefix:
|
||||
mappings[collection_path] = {"from": from_prefix, "to": to_prefix}
|
||||
else:
|
||||
mappings.pop(collection_path, None)
|
||||
prefs["path_mappings"] = mappings
|
||||
save_prefs(prefs)
|
||||
with _LOCK:
|
||||
prefs = _read(for_write=True)
|
||||
mappings = prefs.get("path_mappings")
|
||||
if not isinstance(mappings, dict):
|
||||
mappings = {}
|
||||
if from_prefix and to_prefix:
|
||||
mappings[collection_path] = {"from": from_prefix, "to": to_prefix}
|
||||
else:
|
||||
mappings.pop(collection_path, None)
|
||||
prefs["path_mappings"] = mappings
|
||||
save_prefs(prefs)
|
||||
|
||||
|
||||
def get_last_collection(platform: str | None = None) -> str | None:
|
||||
@@ -105,12 +141,13 @@ def get_last_collection(platform: str | None = None) -> str | None:
|
||||
|
||||
def set_last_collection(path: str, platform: str | None = None) -> None:
|
||||
"""Record a library as the last opened, globally and for its platform."""
|
||||
prefs = load_prefs()
|
||||
prefs["last_collection"] = path
|
||||
if platform:
|
||||
by_platform = prefs.get("last_collection_by_platform")
|
||||
if not isinstance(by_platform, dict):
|
||||
by_platform = {}
|
||||
by_platform[platform] = path
|
||||
prefs["last_collection_by_platform"] = by_platform
|
||||
save_prefs(prefs)
|
||||
with _LOCK:
|
||||
prefs = _read(for_write=True)
|
||||
prefs["last_collection"] = path
|
||||
if platform:
|
||||
by_platform = prefs.get("last_collection_by_platform")
|
||||
if not isinstance(by_platform, dict):
|
||||
by_platform = {}
|
||||
by_platform[platform] = path
|
||||
prefs["last_collection_by_platform"] = by_platform
|
||||
save_prefs(prefs)
|
||||
Reference in new issue
Block a user