Skip to content

Commit

Permalink
improve artist alias config type
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 25, 2023
1 parent 105bba7 commit 7552464
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,8 @@ max_proc = 4
# Artist aliases: Releases belonging to an alias will also appear in the main
# artists' releases.
artist_aliases = [
["main_artist", ["alias1", "alias2"]],
["Abakus", ["Cinnamon Chasers"]],
["tripleS", ["EVOLution", "LOVElution", "+(KR)ystal Eyes", "Acid Angel From Asia", "Acid Eyes"]],
{ artist = "Abakus", aliases = ["Cinnamon Chasers"] },
{ artist = "tripleS", aliases = ["EVOLution", "LOVElution", "+(KR)ystal Eyes", "Acid Angel From Asia", "Acid Eyes"] },
]
# Artists, genres, and labels to hide from the virtual filesystem navigation.
fuse_hide_artists = [ "xxx" ]
Expand Down
26 changes: 18 additions & 8 deletions rose/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class ConfigNotFoundError(RoseError):
pass


class ConfigDecodeError(RoseError):
pass


class MissingConfigKeyError(RoseError):
pass

Expand Down Expand Up @@ -59,6 +63,8 @@ def read(cls, config_path_override: Path | None = None) -> Config:
data = tomllib.loads(cfgtext)
except FileNotFoundError as e:
raise ConfigNotFoundError(f"Configuration file not found ({cfgpath})") from e
except tomllib.TOMLDecodeError as e:
raise ConfigDecodeError("Failed to decode configuration file: invalid TOML") from e

try:
music_source_dir = Path(data["music_source_dir"]).expanduser()
Expand Down Expand Up @@ -109,18 +115,22 @@ def read(cls, config_path_override: Path | None = None) -> Config:
artist_aliases_map: dict[str, list[str]] = defaultdict(list)
artist_aliases_parents_map: dict[str, list[str]] = defaultdict(list)
try:
for parent, subs in data.get("artist_aliases", []):
artist_aliases_map[parent] = subs
if not isinstance(subs, list):
raise ValueError(f"Aliases must be of type list[str]: got {type(subs)}")
for s in subs:
for entry in data.get("artist_aliases", []):
if not isinstance(entry["artist"], str):
raise ValueError(f"Artists must be of type str: got {type(entry['artist'])}")
artist_aliases_map[entry["artist"]] = entry["aliases"]
if not isinstance(entry["aliases"], list):
raise ValueError(
f"Aliases must be of type list[str]: got {type(entry['aliases'])}"
)
for s in entry["aliases"]:
if not isinstance(s, str):
raise ValueError(f"Each alias must be of type str: got {type(s)}")
artist_aliases_parents_map[s].append(parent)
except (ValueError, TypeError) as e:
artist_aliases_parents_map[s].append(entry["artist"])
except (ValueError, TypeError, KeyError) as e:
raise InvalidConfigValueError(
f"Invalid value for artist_aliases in configuration file ({cfgpath}): "
"must be a list of (parent: str, aliases: list[str]) tuples"
"must be a list of { artist = str, aliases = list[str] } records"
) from e

try:
Expand Down
27 changes: 10 additions & 17 deletions rose/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_config_full() -> None:
cache_dir = "{cache_dir}"
max_proc = 8
artist_aliases = [
["Abakus", ["Cinnamon Chasers"]],
["tripleS", ["EVOLution", "LOVElution", "+(KR)ystal Eyes", "Acid Angel From Asia", "Acid Eyes"]],
{{ artist = "Abakus", aliases = ["Cinnamon Chasers"] }},
{{ artist = "tripleS", aliases = ["EVOLution", "LOVElution", "+(KR)ystal Eyes", "Acid Angel From Asia", "Acid Eyes"] }},
]
fuse_hide_artists = [ "xxx" ]
fuse_hide_genres = [ "yyy" ]
Expand Down Expand Up @@ -153,44 +153,37 @@ def write(x: str) -> None:
Config.read(config_path_override=path)
assert (
str(excinfo.value)
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of (parent: str, aliases: list[str]) tuples" # noqa
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of {{ artist = str, aliases = list[str] }} records" # noqa
)
write(config + '\nartist_aliases = ["lalala"]')
with pytest.raises(InvalidConfigValueError) as excinfo:
Config.read(config_path_override=path)
assert (
str(excinfo.value)
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of (parent: str, aliases: list[str]) tuples" # noqa
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of {{ artist = str, aliases = list[str] }} records" # noqa
)
write(config + '\nartist_aliases = [["lalala"]]')
with pytest.raises(InvalidConfigValueError) as excinfo:
Config.read(config_path_override=path)
assert (
str(excinfo.value)
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of (parent: str, aliases: list[str]) tuples" # noqa
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of {{ artist = str, aliases = list[str] }} records" # noqa
)
write(config + '\nartist_aliases = [["lalala", "lalala"]]')
write(config + '\nartist_aliases = [{artist="lalala", aliases="lalala"}]')
with pytest.raises(InvalidConfigValueError) as excinfo:
Config.read(config_path_override=path)
assert (
str(excinfo.value)
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of (parent: str, aliases: list[str]) tuples" # noqa
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of {{ artist = str, aliases = list[str] }} records" # noqa
)
write(config + '\nartist_aliases = [["lalala", "lalala", "lalala"]]')
write(config + '\nartist_aliases = [{artist="lalala", aliases=[123]}]')
with pytest.raises(InvalidConfigValueError) as excinfo:
Config.read(config_path_override=path)
assert (
str(excinfo.value)
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of (parent: str, aliases: list[str]) tuples" # noqa
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of {{ artist = str, aliases = list[str] }} records" # noqa
)
write(config + '\nartist_aliases = [["lalala", [123]]]')
with pytest.raises(InvalidConfigValueError) as excinfo:
Config.read(config_path_override=path)
assert (
str(excinfo.value)
== f"Invalid value for artist_aliases in configuration file ({path}): must be a list of (parent: str, aliases: list[str]) tuples" # noqa
)
config += '\nartist_aliases = [["tripleS", ["EVOLution"]]]'
config += '\nartist_aliases = [{artist="tripleS", aliases=["EVOLution"]}]'

# fuse_hide_artists
write(config + '\nfuse_hide_artists = "lalala"')
Expand Down

0 comments on commit 7552464

Please sign in to comment.