From dfc5edec4014695501af30738c2f6fb6c3081d2d Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 17 Sep 2013 12:52:38 -0700 Subject: [PATCH] ftintitle: docstrings, new top-level function --- beetsplug/ftintitle.py | 110 +++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index 30b6cec4f..2f362cacd 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -19,8 +19,6 @@ from beets import ui import re -# Helpers. - def find_supplementary_artists(artistfield): return re.split( r'[fF]t\.|[fF]eaturing|[fF]eat\.|\b[wW]ith\b|&|vs\.|and', @@ -36,9 +34,11 @@ def detect_if_featuring_artist_already_in_title(title): ) -# Feat is already in title: only replace the artist field. def write_artist_field_only_and_print_edited_file_loc(track, albumartist, sort_artist): + """In the case where the featured artist is already in the title, just + rewrite the artist fields. + """ print track.__getattr__("path") print "new artist field",albumartist.strip() track.artist = albumartist @@ -46,10 +46,11 @@ def write_artist_field_only_and_print_edited_file_loc(track, albumartist, track.write() -# Write a new title and a new artist field. def write_artist_and_title_field_and_print_edited_file_loc(track, albumartist, title, feat_part, sort_artist): + """Modify both the artist and title fields. + """ print track.__getattr__("path") print "albumartist:",albumartist," title:",title," featuartist:",feat_part track.artist = albumartist @@ -58,16 +59,11 @@ def write_artist_and_title_field_and_print_edited_file_loc(track, albumartist, track.write() -# Split the extended artistfield in the extended part and -# albumartist. -def split_on_album_artist(albumartist, artistfield): - return re.split(albumartist, artistfield) - - -# Checks if title has a feat artist and calls the writing methods -# accordingly. def choose_writing_of_title_and_write(track, albumartist, title, - feat_part, sort_artist): + feat_part, sort_artist): + """Choose how to add new artists to the title and write the new + metadata. + """ # If already in title, only replace the artist field. if len(detect_if_featuring_artist_already_in_title(title)) > 1: # no replace title @@ -81,55 +77,63 @@ def choose_writing_of_title_and_write(track, albumartist, title, ) -# Filter the sort artist with a regex split and strip. def rewrite_sort_artist(sort_artist): + """Remove featuring artists from the artist sort field. + """ return find_supplementary_artists(sort_artist)[0].strip() +def ft_in_title(item): + """Look for featured artists in the item's artist fields and move + them to the title. + """ + artistfield = item.artist.strip() + title = item.title.strip() + albumartist = item.albumartist.strip() + sort_artist = item.artist_sort.strip() + supp_artists = find_supplementary_artists(artistfield) + + if len(supp_artists) > 1 and albumartist != artistfield: + # Found supplementary artist, and the albumArtist is + # not a perfect match. + + albumartist_split = artistfield.split(albumartist) + if len(albumartist_split) > 1 and albumartist_split[-1] != '': + # Check if the artist field is composed of the + # albumartist, AND check if the last element of + # the split is not empty. + # Last elements: + feat_part = find_supplementary_artists(albumartist_split[-1])[-1] + choose_writing_of_title_and_write(item, albumartist, title, + feat_part, sort_artist) + + elif len(albumartist_split) > 1 and \ + len(find_supplementary_artists(albumartist_split[0])) > 1: + # Check for inversion of artist and featuring, + # if feat is listed on the first split. + # First elements because of inversion: + feat_part = find_supplementary_artists(albumartist_split[0])[0] + choose_writing_of_title_and_write(item, albumartist, title, + feat_part, sort_artist) + + else: + print "#############################" + print "ftInTitle has not touched this track, " \ + "unsure what to do with this one.:" + print "artistfield: ",artistfield + print "albumArtist",albumartist + print "titleField: ",title + print item.__getattr__("path") + print "#############################" + + class FtInTitlePlugin(BeetsPlugin): def commands(self): cmd = ui.Subcommand('ftintitle', help='move featured artists to the title field') def func(lib, opts, args): - - for track in lib.items(): - artistfield = track.artist.strip() - title = track.title.strip() - albumartist = track.albumartist.strip() - sort_artist = track.artist_sort.strip() - supp_artists = find_supplementary_artists(artistfield) - - if len(supp_artists) > 1 and albumartist != artistfield: - # Found supplementary artist, and the albumArtist is - # not a perfect match. - - albumartist_split = split_on_album_artist(albumartist, - artistfield) - if len(albumartist_split) > 1 and \ - albumartist_split[-1] != '': - # Check if the artist field is composed of the - # albumartist, AND check if the last element of - # the split is not empty. - feat_part = find_supplementary_artists(albumartist_split[-1])[-1] #last elements - choose_writing_of_title_and_write(track, albumartist, - title, feat_part, - sort_artist) - - elif len(albumartist_split) > 1 and len(find_supplementary_artists(albumartist_split[0])) > 1: - # Check for inversion of artist and featuring, - # if feat is listed on the first split. - feat_part = find_supplementary_artists(albumartist_split[0])[0] #first elements because of inversion - choose_writing_of_title_and_write(track,albumartist,title,feat_part,sort_artist) - - else: - print "#############################" - print "ftInTitle has not touched this track, unsure what to do with this one.:" - print "artistfield: ",artistfield - print "albumArtist",albumartist - print "titleField: ",title - print track.__getattr__("path") - print "#############################" - + for item in lib.items(): + ft_in_title(item) print "Manual 'beet update' run is recommended. " cmd.func = func return [cmd]