ftintitle: add 'auto' option

This commit is contained in:
Fabrice Laporte 2014-10-12 22:44:34 +02:00
parent e6bf8c21d8
commit cc6080fa21
2 changed files with 26 additions and 41 deletions

View file

@ -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)

View file

@ -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