Skip to content

Commit

Permalink
refactor!: changed CandidateAlbum fields
Browse files Browse the repository at this point in the history
* `source_str` is now split into two fields: `plugin_source` and `source_id`. This is so in the future we can check against the `plugin_source` and apply different handling criteria per plugin e.g. import weight values.
* `sub_header_info` renamed to `disambigs`. The "sub-header" is specific to the cli, so it was renamed to be more generalized.
  • Loading branch information
jtpavlock committed Dec 20, 2022
1 parent cc659f5 commit 9cc69db
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
10 changes: 5 additions & 5 deletions moe/moe_import/import_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ def candidate_prompt(new_album: Album, candidates: list[CandidateAlbum]):
def _fmt_candidate_info(candidate: CandidateAlbum) -> str:
"""Formats a candidates info for the candidate prompt."""
sub_header_values = []
for str_field in ["media", "country", "label", "catalog_num"]:
if value := getattr(candidate.album, str_field):
for field in ["media", "country", "label", "catalog_num"]:
if value := getattr(candidate.album, field):
sub_header_values.append(value)
sub_header_values.extend(candidate.sub_header_info)
sub_header_values.extend(candidate.disambigs)
sub_header = " | ".join(sub_header_values)

return (
Expand All @@ -163,7 +163,7 @@ def _fmt_candidate_info(candidate: CandidateAlbum) -> str:
+ sub_header
+ "\n"
+ " " * (9 + len(candidate.match_value_pct))
+ candidate.source_str
+ f"{candidate.plugin_source.capitalize()}: {candidate.source_id}"
+ "\n"
)

Expand Down Expand Up @@ -273,7 +273,7 @@ def _fmt_album(new_album: Album, candidate: CandidateAlbum) -> Text:
return (
header_text.append_text(sub_header_text)
.append("\n")
.append(candidate.source_str)
.append(f"{candidate.plugin_source.capitalize()}: {candidate.source_id}")
.append("\n")
)

Expand Down
17 changes: 9 additions & 8 deletions moe/moe_import/import_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ class CandidateAlbum:
Attributes:
album (Album): The candidate album.
disambigs (list[str]): Any additional source-specific values that may be used to
disambiguate or identify the candidate from others.
match_value (float): 0 to 1 scale of how well the candidate album matches with
the album being imported.
source_str (str): A string identifying the release and it's source
e.g. 'Musicbrainz: 1234'. This will be displayed as the last line in the
candidate prompt.
sub_header_info (list[str]): List of any additional info to include in the
candidate sub-header. The following fields are already included:
['media', 'country', 'label']
match_value_pct (str): ``match_value`` as a percentage.
plugin_source (str): String identifying the plugin this candidate came from e.g
"musicbrainz".
source_id (str): A unique string identifying the release within the source e.g.
musicbrainz' release id.
"""

album: MetaAlbum
match_value: float
source_str: str
sub_header_info: list[str] = field(default_factory=list)
plugin_source: str
source_id: str
disambigs: list[str] = field(default_factory=list)

@property
def match_value_pct(self) -> str:
Expand Down
6 changes: 5 additions & 1 deletion tests/add/test_add_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class AddCLIPlugin:
@moe.hookimpl
def get_candidates(album: Album) -> list[CandidateAlbum]:
"""Return a fake candidate for testing."""
return [CandidateAlbum(album=album, match_value=1, source_str="add plugin")]
return [
CandidateAlbum(
album=album, match_value=1, plugin_source="add plugin", source_id="1"
)
]


@pytest.mark.usefixtures("_tmp_add_config")
Expand Down
38 changes: 30 additions & 8 deletions tests/import/test_import_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def test_multi_disc_album(self, tmp_config, tmp_session):
tmp_config("default_plugins = ['cli', 'import']", tmp_db=True)
album = album_factory(num_discs=2)
candidate = CandidateAlbum(
album=album_factory(num_discs=2), match_value=1, source_str="Tests"
album=album_factory(num_discs=2),
match_value=1,
plugin_source="Tests",
source_id="1",
)

mock_choice = PromptChoice("mock", "m", moe_import.import_cli._apply_changes)
Expand Down Expand Up @@ -70,7 +73,10 @@ def test_add_import_prompt_choice(self, tmp_config):
"""Plugins can add prompt choices to the import prompt."""
album = album_factory()
candidate = CandidateAlbum(
album=album_factory(), match_value=1, source_str="tests"
album=album_factory(),
match_value=1,
plugin_source="Tests",
source_id="1",
)
album.title = "not ImportPlugin"
tmp_config(
Expand Down Expand Up @@ -152,7 +158,10 @@ def test_apply_tracks(self):
"""
album = album_factory()
candidate = CandidateAlbum(
album=album_factory(), match_value=1, source_str="test"
album=album_factory(),
match_value=1,
plugin_source="tests",
source_id="1",
)
missing_track = track_factory(
album=candidate.album, track_num=len(album.tracks) + 1
Expand Down Expand Up @@ -185,7 +194,10 @@ def test_apply_diff_get_track(self):
"""
album = album_factory(num_tracks=0)
candidate = CandidateAlbum(
album=album_factory(num_tracks=0), match_value=1, source_str="tests"
album=album_factory(num_tracks=0),
match_value=1,
plugin_source="tests",
source_id="1",
)
track_factory(album=album, track_num=1, title="old track 1")
track_factory(album=candidate.album, track_num=1, title="new track 1")
Expand Down Expand Up @@ -224,7 +236,10 @@ def test_apply_extras(self):
"""`apply` prompt choice should keep any extras."""
album = album_factory()
candidate = CandidateAlbum(
album=album_factory(), match_value=1, source_str="tests"
album=album_factory(),
match_value=1,
plugin_source="tests",
source_id="1",
)
new_extra = extra_factory(album=album)

Expand All @@ -242,7 +257,10 @@ def test_apply_fields(self):
"""Fields get applied onto the old album."""
album = album_factory()
candidate = CandidateAlbum(
album=album_factory(title="new title"), match_value=1, source_str="tests"
album=album_factory(title="new title"),
match_value=1,
plugin_source="tests",
source_id="1",
)
assert album.title != candidate.album.title

Expand Down Expand Up @@ -272,7 +290,10 @@ def test_abort(self):
"""The `abort` prompt choice should raise an AbortImport error."""
album = album_factory()
candidate = CandidateAlbum(
album=album_factory(), match_value=1, source_str="tests"
album=album_factory(),
match_value=1,
plugin_source="tests",
source_id="1",
)

mock_choice = PromptChoice("mock", "m", moe_import.import_cli._abort_changes)
Expand Down Expand Up @@ -328,7 +349,8 @@ def test_full_diff_album(self):
label="me",
),
match_value=1,
source_str="tests",
plugin_source="tests",
source_id="1",
)
album.tracks[0].title = "really really long old title"

Expand Down
8 changes: 6 additions & 2 deletions tests/import/test_import_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def get_candidates(album: Album):
"""Changes the album title."""
album.title = "candidate title"

return [CandidateAlbum(album=album, match_value=1, source_str="hook")]
return [
CandidateAlbum(album, match_value=1, plugin_source="hook", source_id="1")
]

@staticmethod
@moe.hookimpl
Expand Down Expand Up @@ -59,7 +61,9 @@ def test_process_candidates(self, tmp_config):
config.CONFIG.pm.hook.process_candidates(
new_album=album,
candidates=[
CandidateAlbum(album=new_album, match_value=1, source_str="tests")
CandidateAlbum(
album=new_album, match_value=1, plugin_source="tests", source_id="1"
)
],
)

Expand Down

0 comments on commit 9cc69db

Please sign in to comment.