From 4b600e1c0cb736132629e3469b27a504366e446b Mon Sep 17 00:00:00 2001 From: cwilvx Date: Mon, 9 Jun 2025 18:22:39 +0300 Subject: [PATCH] Refactor artistSplitIgnoreList to load from files + improving configuration management. + Update JSON serialization for sets in settings. --- swingmusic/api/settings.py | 8 +++- swingmusic/config.py | 56 ++++++++++++++++++------- swingmusic/data/artist_split_ignore.txt | 8 ++++ 3 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 swingmusic/data/artist_split_ignore.txt diff --git a/swingmusic/api/settings.py b/swingmusic/api/settings.py index 29b93c75..90358fd0 100644 --- a/swingmusic/api/settings.py +++ b/swingmusic/api/settings.py @@ -95,10 +95,16 @@ def get_all_settings(): Get all settings """ config = asdict(UserConfig()) + + # Convert sets to lists for JSON serialization + for key, value in config.items(): + if isinstance(value, set): + config[key] = sorted(list(value)) + config["plugins"] = [p for p in PluginTable.get_all()] config["version"] = Info.SWINGMUSIC_APP_VERSION - # hide lastfmSessionKeys for other users + # only return lastfmSessionKey for the current user current_user = get_current_userid() config["lastfmSessionKey"] = config["lastfmSessionKeys"].get(str(current_user), "") del config["lastfmSessionKeys"] diff --git a/swingmusic/config.py b/swingmusic/config.py index 0c153c6a..1f21af75 100644 --- a/swingmusic/config.py +++ b/swingmusic/config.py @@ -1,12 +1,43 @@ from dataclasses import dataclass, asdict, field import json -import os +from pathlib import Path from typing import Any from .settings import Paths # TODO: Publish this on PyPi +def load_artist_ignore_list_from_file(filepath: Path) -> set[str]: + """ + Loads artist names from a text file. + Returns an empty set if the file doesn't exist. + """ + try: + return { + line.strip() for line in filepath.read_text().splitlines() if line.strip() + } + except FileNotFoundError: + return set() + + +def load_default_artist_ignore_list() -> set[str]: + """ + Loads the default artist ignore list from the text file. + Returns an empty set if the file doesn't exist. + """ + default_file = Path(__file__).parent / "data" / "artist_split_ignore.txt" + return load_artist_ignore_list_from_file(default_file) + + +def load_user_artist_ignore_list() -> set[str]: + """ + Loads the user-defined artist ignore list from the config directory. + Returns an empty set if the file doesn't exist. + """ + user_file = Path(Paths.get_config_file_path()).parent / "artist_split_ignore.txt" + return load_artist_ignore_list_from_file(user_file) + + @dataclass class UserConfig: _config_path: str = "" @@ -23,17 +54,9 @@ class UserConfig: excludeDirs: list[str] = field(default_factory=list) artistSeparators: set[str] = field(default_factory=lambda: {";", "/"}) artistSplitIgnoreList: set[str] = field( - default_factory=lambda: { - "AC/DC", - "Bob marley & the wailers", - "Crosby, Stills, Nash & Young", - "Smith & Thell", - "Peter, Paul & Mary", - "Simon & Garfunkel", - "Judy & Mary", - "Belle & Sebastian", - "Florence & The Machine", - } + default_factory=lambda: load_default_artist_ignore_list().union( + load_user_artist_ignore_list() + ) ) genreSeparators: set[str] = field(default_factory=lambda: {"/", ";", "&"}) @@ -75,9 +98,10 @@ class UserConfig: # loop through the config file and set the values for key, value in config.items(): if key == "artistSplitIgnoreList": - # Merge with default values instead of overwriting - default_values = self.artistSplitIgnoreList - setattr(self, key, default_values.union(value)) + # Merge with default values and user file values instead of overwriting + default_values = load_default_artist_ignore_list() + user_values = load_user_artist_ignore_list() + setattr(self, key, default_values.union(user_values).union(value)) else: setattr(self, key, value) @@ -90,7 +114,7 @@ class UserConfig: if it doesn't exist """ # if not exists, create the config file - if not os.path.exists(self._config_path): + if not Path(self._config_path).exists(): self.write_to_file(asdict(self)) def load_config(self, path: str) -> dict[str, Any]: diff --git a/swingmusic/data/artist_split_ignore.txt b/swingmusic/data/artist_split_ignore.txt new file mode 100644 index 00000000..ed860cf4 --- /dev/null +++ b/swingmusic/data/artist_split_ignore.txt @@ -0,0 +1,8 @@ +AC/DC +Bob marley & the wailers +Crosby, Stills, Nash & Young +Smith & Thell +Peter, Paul & Mary +Simon & Garfunkel +Judy & Mary +Florence & The Machine \ No newline at end of file