From cc6080fa21ecc85a4f1500cd2cc2ae9d10731354 Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Sun, 12 Oct 2014 22:44:34 +0200 Subject: [PATCH 1/5] ftintitle: add 'auto' option --- beetsplug/ftintitle.py | 65 ++++++++++++++------------------------ docs/plugins/ftintitle.rst | 2 ++ 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index 831236b3c..dd3460fd8 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -16,10 +16,12 @@ """ from beets.plugins import BeetsPlugin from beets import ui -from beets.util import displayable_path from beets import config +import logging import re +log = logging.getLogger('beets') + def split_on_feat(artist): """Given an artist string, split the "main" artist from any artist @@ -69,55 +71,30 @@ def update_metadata(item, feat_part, drop_feat): item.title = new_title -def ft_in_title(item, drop_feat): +def ft_in_title(item, drop_feat, write): """Look for featured artists in the item's artist fields and move them to the title. """ artist = item.artist.strip() - albumartist = item.albumartist.strip() - # Check whether there is a featured artist on this track and the - # artist field does not exactly match the album artist field. In - # that case, we attempt to move the featured artist to the title. - _, featured = split_on_feat(artist) - if featured and albumartist != artist and albumartist: - ui.print_(displayable_path(item.path)) - feat_part = None + _, feat_part = split_on_feat(artist) - # Look for the album artist in the artist field. If it's not - # present, give up. - albumartist_split = artist.split(albumartist) - if len(albumartist_split) <= 1: - ui.print_('album artist not present in artist') + if feat_part: + update_metadata(item, feat_part, drop_feat) + else: + ui.print_(u'no featuring artists found') - # If the last element of the split (the right-hand side of the - # album artist) is nonempty, then it probably contains the - # featured artist. - elif albumartist_split[-1] != '': - # Extract the featured artist from the right-hand side. - _, feat_part = split_on_feat(albumartist_split[-1]) - - # Otherwise, if there's nothing on the right-hand side, look for a - # featuring artist on the left-hand side. - else: - lhs, rhs = split_on_feat(albumartist_split[0]) - if rhs: - feat_part = lhs - - # If we have a featuring artist, move it to the title. - if feat_part: - update_metadata(item, feat_part, drop_feat) - else: - ui.print_(u'no featuring artists found') - - ui.print_() + if write: + item.try_write() + item.store() class FtInTitlePlugin(BeetsPlugin): def __init__(self): super(FtInTitlePlugin, self).__init__() - + self.import_stages = [self.imported] self.config.add({ + 'auto': True, 'drop': False }) @@ -138,10 +115,16 @@ class FtInTitlePlugin(BeetsPlugin): write = config['import']['write'].get(bool) for item in lib.items(ui.decargs(args)): - ft_in_title(item, drop_feat) - item.store() - if write: - item.try_write() + ft_in_title(item, drop_feat, write) self._command.func = func return [self._command] + + def imported(self, session, task): + """Import hook for moving featuring artist automatically. + """ + drop_feat = self.config['drop'].get(bool) + write = config['import']['write'].get(bool) + if self.config['auto'].get(bool): + for item in task.imported_items(): + ft_in_title(item, drop_feat, write) diff --git a/docs/plugins/ftintitle.rst b/docs/plugins/ftintitle.rst index ed13cb840..c8bf48f46 100644 --- a/docs/plugins/ftintitle.rst +++ b/docs/plugins/ftintitle.rst @@ -21,4 +21,6 @@ If you prefer to remove featured artists entirely instead of adding them to the title field, either use the ``-d`` flag to the command or set the ``ftintitle.drop`` config option. +To disable this plugin on import, set the ``auto`` config option to false. + .. _MusicBrainz style: http://musicbrainz.org/doc/Style From 740b510ed717f3d872f1912313a2b38e30fa4862 Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Mon, 13 Oct 2014 21:21:28 +0200 Subject: [PATCH 2/5] restoring ft_in_title implementation --- beetsplug/ftintitle.py | 45 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index dd3460fd8..8fe70764d 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -16,6 +16,7 @@ """ from beets.plugins import BeetsPlugin from beets import ui +from beets.util import displayable_path from beets import config import logging import re @@ -71,22 +72,48 @@ def update_metadata(item, feat_part, drop_feat): item.title = new_title -def ft_in_title(item, drop_feat, write): +def ft_in_title(item, drop_feat): """Look for featured artists in the item's artist fields and move them to the title. """ artist = item.artist.strip() + albumartist = item.albumartist.strip() - _, feat_part = split_on_feat(artist) + # Check whether there is a featured artist on this track and the + # artist field does not exactly match the album artist field. In + # that case, we attempt to move the featured artist to the title. + _, featured = split_on_feat(artist) + if featured and albumartist != artist and albumartist: + ui.print_(displayable_path(item.path)) + feat_part = None - if feat_part: - update_metadata(item, feat_part, drop_feat) - else: - ui.print_(u'no featuring artists found') + # Look for the album artist in the artist field. If it's not + # present, give up. + albumartist_split = artist.split(albumartist) + if len(albumartist_split) <= 1: + ui.print_('album artist not present in artist') - if write: - item.try_write() - item.store() + # If the last element of the split (the right-hand side of the + # album artist) is nonempty, then it probably contains the + # featured artist. + elif albumartist_split[-1] != '': + # Extract the featured artist from the right-hand side. + _, feat_part = split_on_feat(albumartist_split[-1]) + + # Otherwise, if there's nothing on the right-hand side, look for a + # featuring artist on the left-hand side. + else: + lhs, rhs = split_on_feat(albumartist_split[0]) + if rhs: + feat_part = lhs + + # If we have a featuring artist, move it to the title. + if feat_part: + update_metadata(item, feat_part, drop_feat) + else: + ui.print_(u'no featuring artists found') + + ui.print_() class FtInTitlePlugin(BeetsPlugin): From 4884ae3c46bcd026dc19f48c9859675f889757d7 Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Mon, 13 Oct 2014 22:25:09 +0200 Subject: [PATCH 3/5] register import hook only if needed --- beetsplug/ftintitle.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index 8fe70764d..f783cfa32 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -119,7 +119,7 @@ def ft_in_title(item, drop_feat): class FtInTitlePlugin(BeetsPlugin): def __init__(self): super(FtInTitlePlugin, self).__init__() - self.import_stages = [self.imported] + self.config.add({ 'auto': True, 'drop': False @@ -134,6 +134,9 @@ class FtInTitlePlugin(BeetsPlugin): action='store_true', default=False, help='drop featuring from artists and ignore title update') + if self.config['auto']: + self.import_stages = [self.imported] + def commands(self): def func(lib, opts, args): @@ -152,6 +155,6 @@ class FtInTitlePlugin(BeetsPlugin): """ drop_feat = self.config['drop'].get(bool) write = config['import']['write'].get(bool) - if self.config['auto'].get(bool): - for item in task.imported_items(): - ft_in_title(item, drop_feat, write) + + for item in task.imported_items(): + ft_in_title(item, drop_feat, write) From ac57ef0e67edca143e62f17f3492ed1d8022f29f Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Tue, 14 Oct 2014 20:58:36 +0200 Subject: [PATCH 4/5] restore write argument for ft_in_title --- beetsplug/ftintitle.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index f783cfa32..eaeb641e9 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -72,7 +72,7 @@ def update_metadata(item, feat_part, drop_feat): item.title = new_title -def ft_in_title(item, drop_feat): +def ft_in_title(item, drop_feat, write): """Look for featured artists in the item's artist fields and move them to the title. """ @@ -114,7 +114,9 @@ def ft_in_title(item, drop_feat): ui.print_(u'no featuring artists found') ui.print_() - + if write: + item.try_write() + item.store() class FtInTitlePlugin(BeetsPlugin): def __init__(self): From 343972f5d00ca12e374f094e39ee82fc735514cc Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Sat, 18 Oct 2014 11:04:10 +0200 Subject: [PATCH 5/5] move metadata management in command function --- beetsplug/ftintitle.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index eaeb641e9..8a92d20df 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -114,9 +114,7 @@ def ft_in_title(item, drop_feat, write): ui.print_(u'no featuring artists found') ui.print_() - if write: - item.try_write() - item.store() + class FtInTitlePlugin(BeetsPlugin): def __init__(self): @@ -147,7 +145,10 @@ class FtInTitlePlugin(BeetsPlugin): write = config['import']['write'].get(bool) for item in lib.items(ui.decargs(args)): - ft_in_title(item, drop_feat, write) + ft_in_title(item, drop_feat) + item.store() + if write: + item.try_write() self._command.func = func return [self._command] @@ -156,7 +157,6 @@ class FtInTitlePlugin(BeetsPlugin): """Import hook for moving featuring artist automatically. """ drop_feat = self.config['drop'].get(bool) - write = config['import']['write'].get(bool) for item in task.imported_items(): - ft_in_title(item, drop_feat, write) + ft_in_title(item, drop_feat)