-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Handle "join keyword" in MetadataSourcePlugin.get_artist()
#4515
Conversation
Some example outputs while importing: Old behaviour:
New behaviour:
Another example of new behaviour:
|
Ready for a final review. |
Excited for this PR! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!! This is looking good overall—please see one suggestion within.
beets/plugins.py
Outdated
else: | ||
artist = ', '.join(artist_names).replace(' ,', ',') or None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have this wrong, but would it work to move this "fallback" up into the loop? That is, the loop could look like this:
if join_key and artist.get(join_key, None):
...
else:
name += ', '
I'm honestly not 100% sure where the replace
call should go… it's a little mysterious to me why a ,
would appear in the string in the first place.
The reason I suggest this is that it could deal better with a situation where some artists have a join_key
value and others don't (when there are more than 2 artists). In that case, you'd probably want to use a comma for pairs of artists that would otherwise not have another joiner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the suggestion. Well, I think I managed to massively simplify this, which I think makes this method much more readable. It works well with Discogs as the metadata source, but some more testing reveals that it's problematic with the beatport4 plugin. Here we don't have a simple list to handle but a generator which does not answer well on my len() call:
File "/home/jojo/git/beets-beatport4/beetsplug/beatport4.py", line 629, in _get_artist
return MetadataSourcePlugin.get_artist(
File "/home/jojo/git/beets/beets/plugins.py", line 697, in get_artist
if idx < (len(artists) - 1): # Skip joining on last.
TypeError: object of type 'generator' has no len()
I need to find another way of finding out whether I'm handling the last object of artists
.... Suggestions welcome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a way to count the items in a way generators as well as dicts and lists or any other iterable should be allowed to be passed as the artists
object.
Still this is better and more readable than my first version? What do you think?
And btw. I forgot to mention before: I removed the "mysterious" string replace method. It was mysterious to me as well why this was there in the original get_artist() code. I would like to leave it out. I don't see the point of keeping it. It probably would just lead to confusion again in the future :-) You agree?
fab1d77
to
c62a6a6
Compare
Add an optional argument to MetadataSourcePlugin.get_artist method that enables making use of a field containing a keyword supposed to be used to combine artists together into a single string like "Feat.", "And", "Vs." and so on.
c62a6a6
to
75209d3
Compare
Pulled in master because of the recent Sphinx issue. All checks green again and ready for a final review @sampsyo. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice; looking good!
I did a heap of git blame
digging and traced the origin of that .replace
call back to the discogs plugin here:
3eb6f8e
That still doesn't really explain why it was needed in the first place. I think we are safe to drop it and, if it crops back up because of some weird Discogs data formatting issues, we can reexamine on a case-by-case basis.
beets/plugins.py
Outdated
for artist in artists: | ||
artist_string = "" | ||
# Count items (supports generators as well as lists, dicts, ...) | ||
total = sum(1 for _ in artists) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A different option for dealing with generators (iteratables, in general) passed here would be to eagerly convert the artists to a list. That is, you could do:
artists_list = list(artists)
…and then simply use len(artists_list)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do that. Thanks!
I just realilsed that my way of solving it didn't work anyway! Sorry for not testing better. It seems a generator is exhausted when it was counted with the sum(...) hack (it's been looped through to the end). I can't loop through it then because it's empty! Alright, sorry again, will fix soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's exactly the case!
beets/plugins.py
Outdated
# Use a join keyword if requested and available. | ||
if idx < (total - 1): # Skip joining on last. | ||
if join_key and artist.get(join_key, None): | ||
name += " " + artist[join_key] + " " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider f" {artist[join_key]} "
for legibility.
75209d3
to
5df959e
Compare
- Originally a list of "artists" was generated and joined together to a final string later. - This simplifies by concatinating to a final string within the main loop. - Also this commit gets rid of a mysterious replacement code (` ,' -> ',')
5df959e
to
f1794e8
Compare
I put both suggested fixes into the original commits to keep commit-clutter minimal. I think we are done :-) |
That does it! Nice work!! |
Description
data
attribute being used, contains the required "join" field already.)To Do
Documentation.Tests.