diff --git a/rose/artiststr/__init__.py b/rose/artiststr/__init__.py index 108c453..75a93b8 100644 --- a/rose/artiststr/__init__.py +++ b/rose/artiststr/__init__.py @@ -1,17 +1,17 @@ import re -from dataclasses import dataclass +from dataclasses import dataclass, field TAG_SPLITTER_REGEX = re.compile(r" \\\\ | / |; ?| vs\. ") @dataclass class Artists: - main: list[str] - guest: list[str] - remixer: list[str] - producer: list[str] - composer: list[str] - djmixer: list[str] + main: list[str] = field(default_factory=list) + guest: list[str] = field(default_factory=list) + remixer: list[str] = field(default_factory=list) + producer: list[str] = field(default_factory=list) + composer: list[str] = field(default_factory=list) + djmixer: list[str] = field(default_factory=list) def parse_artist_string( @@ -39,7 +39,7 @@ def _split_tag(t: str | None) -> list[str]: dj, main = re.split(r" ?pres. ", main, maxsplit=1) li_dj.extend(_split_tag(dj)) if main and " performed by " in main: - composer, main = re.split(r" ?performed by. ", main, maxsplit=1) + composer, main = re.split(r" ?performed by ", main, maxsplit=1) li_composer.extend(_split_tag(composer)) if main: li_main.extend(_split_tag(main)) @@ -57,7 +57,7 @@ def _split_tag(t: str | None) -> list[str]: def format_artist_string(a: Artists, genres: list[str]) -> str: r = ";".join(a.producer + a.main + a.remixer) if a.composer and "Classical" in genres: - r = ";".join(a.composer) + " performed by. " + r + r = ";".join(a.composer) + " performed by " + r if a.djmixer: r = ";".join(a.djmixer) + " pres. " + r if a.guest: diff --git a/rose/artiststr/__test__.py b/rose/artiststr/__test__.py new file mode 100644 index 0000000..149828d --- /dev/null +++ b/rose/artiststr/__test__.py @@ -0,0 +1,35 @@ +from rose.artiststr import Artists, format_artist_string, parse_artist_string + + +def test_parse_artist_string() -> None: + assert parse_artist_string("A;B feat. C;D") == Artists( + main=["A", "B"], + guest=["C", "D"], + ) + assert parse_artist_string("A pres. C;D") == Artists( + djmixer=["A"], + main=["C", "D"], + ) + assert parse_artist_string("A performed by C;D") == Artists( + composer=["A"], + main=["C", "D"], + ) + assert parse_artist_string("A pres. B;C feat. D;E") == Artists( + djmixer=["A"], + main=["B", "C"], + guest=["D", "E"], + ) + + +def test_format_artist_string() -> None: + assert format_artist_string(Artists(main=["A", "B"], guest=["C", "D"]), []) == "A;B feat. C;D" + assert format_artist_string(Artists(djmixer=["A"], main=["C", "D"]), []) == "A pres. C;D" + assert format_artist_string(Artists(composer=["A"], main=["C", "D"]), []) == "C;D" + assert ( + format_artist_string(Artists(composer=["A"], main=["C", "D"]), ["Classical"]) + == "A performed by C;D" + ) + assert ( + format_artist_string(Artists(djmixer=["A"], main=["B", "C"], guest=["D", "E"]), []) + == "A pres. B;C feat. D;E" + )