fix rewrite bug w/ multiple rules for a single field

This commit is contained in:
Adrian Sampson 2012-01-25 13:32:22 -08:00
parent a58253b79c
commit dbab290ba4
2 changed files with 25 additions and 16 deletions

View file

@ -17,6 +17,7 @@ formats.
""" """
import re import re
import logging import logging
from collections import defaultdict
from beets.plugins import BeetsPlugin from beets.plugins import BeetsPlugin
from beets import ui from beets import ui
@ -24,15 +25,19 @@ from beets import library
log = logging.getLogger('beets') log = logging.getLogger('beets')
def rewriter(fieldname, pattern, replacement): def rewriter(field, rules):
"""Create a template field function that rewrites the given field
with the given rewriting rules. ``rules`` must be a list of
(pattern, replacement) pairs.
"""
def fieldfunc(item): def fieldfunc(item):
value = getattr(item, fieldname) value = getattr(item, field)
if pattern.match(value): for pattern, replacement in rules:
# Rewrite activated. if pattern.match(value):
return replacement # Rewrite activated.
else: return replacement
# Not activated; return original value. # Not activated; return original value.
return value return value
return fieldfunc return fieldfunc
class RewritePlugin(BeetsPlugin): class RewritePlugin(BeetsPlugin):
@ -41,6 +46,8 @@ class RewritePlugin(BeetsPlugin):
def configure(self, config): def configure(self, config):
cls = type(self) cls = type(self)
# Gather all the rewrite rules for each field.
rules = defaultdict(list)
for key, value in config.items('rewrite', True): for key, value in config.items('rewrite', True):
try: try:
fieldname, pattern = key.split(None, 1) fieldname, pattern = key.split(None, 1)
@ -51,12 +58,12 @@ class RewritePlugin(BeetsPlugin):
fieldname) fieldname)
log.debug(u'adding template field %s' % key) log.debug(u'adding template field %s' % key)
pattern = re.compile(pattern, re.I) pattern = re.compile(pattern, re.I)
rules[fieldname].append((pattern, value))
# Replace the template field with the new function.
cls.template_fields[fieldname] = rewriter(fieldname, pattern,
value)
if fieldname == 'artist': if fieldname == 'artist':
# Special case for the artist field: apply the same rewrite for # Special case for the artist field: apply the same
# "albumartist" as well. # rewrite for "albumartist" as well.
cls.template_fields['albumartist'] = rewriter('albumartist', rules['albumartist'].append((pattern, value))
pattern, value)
# Replace each template field with the new rewriter function.
for fieldname, fieldrules in rules.iteritems():
cls.template_fields[fieldname] = rewriter(fieldname, fieldrules)

View file

@ -6,6 +6,8 @@ Changelog
* The :doc:`/plugins/lyrics`, originally by `Peter Brunner`_, is revamped and * The :doc:`/plugins/lyrics`, originally by `Peter Brunner`_, is revamped and
included with beets, making it easy to fetch **song lyrics**. included with beets, making it easy to fetch **song lyrics**.
* Fix a bug in the ``rewrite`` plugin that broke the use of multiple rules for
a single field.
1.0b12 (January 16, 2012) 1.0b12 (January 16, 2012)