mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +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 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,13 +25,17 @@ 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)
|
||||||
|
for pattern, replacement in rules:
|
||||||
if pattern.match(value):
|
if pattern.match(value):
|
||||||
# Rewrite activated.
|
# Rewrite activated.
|
||||||
return replacement
|
return replacement
|
||||||
else:
|
|
||||||
# Not activated; return original value.
|
# Not activated; return original value.
|
||||||
return value
|
return value
|
||||||
return fieldfunc
|
return fieldfunc
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue