Skip to content

Add two new plugin events for recommendation and assign_items #5768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions beets/autotag/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@
for iidx, t in zip(assigned_item_idxs, tracks)
if iidx != -1
}

# allow plugins to modify the mapping
new_assign_list = plugins.send(
"assign_items", mapping=mapping, org_items=items, org_tracks=tracks
)

# get the first plugin provided mapping that is a dict and not empty
new_mapping = next(
(item for item in new_assign_list if isinstance(item, dict) and item),
None,
)
if new_mapping:
# if a plugin provided a mapping, use that instead of the default one
mapping = new_mapping

extra_items = list(set(items) - mapping.keys())
extra_items.sort(key=lambda i: (i.disc, i.track, i.title))
extra_tracks = list(set(tracks) - set(mapping.values()))
Expand Down Expand Up @@ -328,7 +343,7 @@
dist.add("unmatched_tracks", 1.0)

# Plugins.
dist.update(plugins.album_distance(items, album_info, mapping))

Check failure on line 346 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 1 to "album_distance" has incompatible type "Sequence[Item]"; expected "list[Item]"

return dist

Expand Down Expand Up @@ -390,6 +405,15 @@
rec = Recommendation.low
else:
# No conclusion. Return immediately. Can't be downgraded any further.
# but allow plugins to modify the recommendation
new_rec_list = plugins.send(
"recommendation", results=results, org_rec=Recommendation.none
)
# take the first recommendation from a plugin
if len(new_rec_list) > 0 and isinstance(
new_rec_list[0], Recommendation
):
return new_rec_list[0]
return Recommendation.none

# Downgrade to the max rec if it is lower than the current rec for an
Expand All @@ -411,6 +435,11 @@
)
rec = min(rec, max_rec)

# allow plugins to modify the recommendation
new_rec_list = plugins.send("recommendation", results=results, org_rec=rec)
# take the first recommendation from a plugin
if len(new_rec_list) > 0 and isinstance(new_rec_list[0], Recommendation):
rec = new_rec_list[0]
return rec


Expand Down Expand Up @@ -555,9 +584,9 @@

# Get the results from the data sources.
for matched_candidate in hooks.album_candidates(
items, search_artist, search_album, va_likely, extra_tags

Check failure on line 587 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 5 to "album_candidates" has incompatible type "Optional[dict[str, Any]]"; expected "dict[Any, Any]"
):
_add_candidate(items, candidates, matched_candidate)

Check failure on line 589 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 3 to "_add_candidate" has incompatible type "tuple[Any, ...]"; expected "AlbumInfo"

log.debug("Evaluating {0} candidates.", len(candidates))
# Sort and get the recommendation.
Expand Down Expand Up @@ -616,9 +645,9 @@
log.debug("Item search terms: {0} - {1}", search_artist, search_title)

# Get and evaluate candidate metadata.
for track_info in hooks.item_candidates(item, search_artist, search_title):

Check failure on line 648 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 3 to "item_candidates" has incompatible type "Optional[str]"; expected "str"

Check failure on line 648 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 2 to "item_candidates" has incompatible type "Optional[str]"; expected "str"
dist = track_distance(item, track_info, incl_artist=True)

Check failure on line 649 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 2 to "track_distance" has incompatible type "tuple[Any, ...]"; expected "TrackInfo"
candidates[track_info.track_id] = hooks.TrackMatch(dist, track_info)

Check failure on line 650 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

Argument 2 to "TrackMatch" has incompatible type "tuple[Any, ...]"; expected "TrackInfo"

Check failure on line 650 in beets/autotag/match.py

View workflow job for this annotation

GitHub Actions / Check types with mypy

"tuple[Any, ...]" has no attribute "track_id"

# Sort by distance and return with recommendation.
log.debug("Found {0} candidates.", len(candidates))
Expand Down
Loading