Import existing LinTunes project
Snapshot of the existing codebase before working through the TASKS.md backlog. Real library data (data/) and the iTunes import fixture (itunes-test-library/) are gitignored. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import copy
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtCore import QObject, pyqtSignal
|
||||
|
||||
|
||||
DEFAULTS = {
|
||||
"ui_scale": "medium", # small | medium | large
|
||||
"highlight": "blue", # see theme.HIGHLIGHT_COLORS
|
||||
"now_playing_font": None, # None=prefer Century Gothic, ""=use app
|
||||
# default, "Family"=user-chosen substitute
|
||||
"lastfm": {
|
||||
"api_key": "",
|
||||
"api_secret": "",
|
||||
"username": "",
|
||||
"session_key": "",
|
||||
"scrobble_enabled": False,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class Preferences(QObject):
|
||||
"""App preferences persisted to <data_dir>/preferences.json (so they ride
|
||||
the same syncthing share as the library)."""
|
||||
|
||||
changed = pyqtSignal()
|
||||
|
||||
def __init__(self, data_dir: Path, parent=None):
|
||||
super().__init__(parent)
|
||||
self._path = Path(data_dir) / "preferences.json"
|
||||
self._data = copy.deepcopy(DEFAULTS)
|
||||
if self._path.exists():
|
||||
try:
|
||||
with open(self._path, "r", encoding="utf-8") as f:
|
||||
stored = json.load(f)
|
||||
_deep_merge(self._data, stored)
|
||||
except (ValueError, OSError):
|
||||
pass
|
||||
|
||||
def get(self, key: str, default=None):
|
||||
return self._data.get(key, default)
|
||||
|
||||
def set(self, key: str, value):
|
||||
if self._data.get(key) == value:
|
||||
return
|
||||
self._data[key] = value
|
||||
self.save()
|
||||
self.changed.emit()
|
||||
|
||||
def update_lastfm(self, **fields):
|
||||
self._data["lastfm"].update(fields)
|
||||
self.save()
|
||||
self.changed.emit()
|
||||
|
||||
@property
|
||||
def lastfm(self) -> dict:
|
||||
return self._data["lastfm"]
|
||||
|
||||
def save(self):
|
||||
self._path.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp = self._path.with_suffix(".json.tmp")
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(self._data, f, indent=2)
|
||||
tmp.replace(self._path)
|
||||
|
||||
|
||||
def _deep_merge(base: dict, override: dict):
|
||||
for key, value in override.items():
|
||||
if isinstance(value, dict) and isinstance(base.get(key), dict):
|
||||
_deep_merge(base[key], value)
|
||||
else:
|
||||
base[key] = value
|
||||
Reference in New Issue
Block a user