mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 08:39:17 +01:00
fix rewrite bug w/ multiple rules for a single field
This commit is contained in:
parent
a58253b79c
commit
dbab290ba4
2 changed files with 25 additions and 16 deletions
|
|
@ -17,6 +17,7 @@ formats.
|
|||
"""
|
||||
import re
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets import ui
|
||||
|
|
@ -24,13 +25,17 @@ from beets import library
|
|||
|
||||
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):
|
||||
value = getattr(item, fieldname)
|
||||
value = getattr(item, field)
|
||||
for pattern, replacement in rules:
|
||||
if pattern.match(value):
|
||||
# Rewrite activated.
|
||||
return replacement
|
||||
else:
|
||||
# Not activated; return original value.
|
||||
return value
|
||||
return fieldfunc
|
||||
|
|
@ -41,6 +46,8 @@ class RewritePlugin(BeetsPlugin):
|
|||
def configure(self, config):
|
||||
cls = type(self)
|
||||
|
||||
# Gather all the rewrite rules for each field.
|
||||
rules = defaultdict(list)
|
||||
for key, value in config.items('rewrite', True):
|
||||
try:
|
||||
fieldname, pattern = key.split(None, 1)
|
||||
|
|
@ -51,12 +58,12 @@ class RewritePlugin(BeetsPlugin):
|
|||
fieldname)
|
||||
log.debug(u'adding template field %s' % key)
|
||||
pattern = re.compile(pattern, re.I)
|
||||
|
||||
# Replace the template field with the new function.
|
||||
cls.template_fields[fieldname] = rewriter(fieldname, pattern,
|
||||
value)
|
||||
rules[fieldname].append((pattern, value))
|
||||
if fieldname == 'artist':
|
||||
# Special case for the artist field: apply the same rewrite for
|
||||
# "albumartist" as well.
|
||||
cls.template_fields['albumartist'] = rewriter('albumartist',
|
||||
pattern, value)
|
||||
# Special case for the artist field: apply the same
|
||||
# rewrite for "albumartist" as well.
|
||||
rules['albumartist'].append((pattern, value))
|
||||
|
||||
# Replace each template field with the new rewriter function.
|
||||
for fieldname, fieldrules in rules.iteritems():
|
||||
cls.template_fields[fieldname] = rewriter(fieldname, fieldrules)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ Changelog
|
|||
|
||||
* The :doc:`/plugins/lyrics`, originally by `Peter Brunner`_, is revamped and
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue