Skip to content

Commit 930714a

Browse files
committed
Apply all matching rules to a field
1 parent c906401 commit 930714a

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

beetsplug/rewrite.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ def _(value: list[str], pat: re.Pattern[str], repl: str) -> list[str]:
4848
def apply_rewrite_rules(
4949
value: T, rules: list[tuple[re.Pattern[str], str]]
5050
) -> T:
51-
"""Apply the first matching rewrite rule to the given value."""
51+
"""Apply all matching rewrite rules to the given value."""
5252
for pattern, replacement in rules:
53-
if (new_value := rewrite_value(value, pattern, replacement)) != value:
54-
# Rewrite activated.
55-
return new_value
53+
value = rewrite_value(value, pattern, replacement)
5654
# Not activated; return original value.
5755
return value
5856

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ Bug fixes
8282
sources configuration is empty. :bug:`6336`
8383
- :doc:`plugins/rewrite` :doc:`plugins/advancedrewrite`: Fix rewriting
8484
multi-valued fields such as ``genres`` by applying rules to each matching list
85-
entry. :bug:`6515`
85+
entry. Additionally, apply rewrite rules in config order, so that multiple
86+
rules can be applied to the same field. :bug:`6515`
8687

8788
For plugin developers
8889
~~~~~~~~~~~~~~~~~~~~~

docs/plugins/rewrite.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ unchanged:
3939
rewrite:
4040
genres rock: Classic Rock
4141

42+
Rules are applied in the order they appear in the config. This means a
43+
multi-valued field can have several entries rewritten by different rules:
44+
45+
::
46+
47+
rewrite:
48+
genres rock: Classic Rock
49+
genres pop: Pop
50+
51+
With this configuration, a ``genres`` value such as ``rock; pop; techno`` is
52+
rewritten to ``Classic Rock; Pop; techno``.
53+
4254
As a convenience, the plugin applies patterns for the ``artist`` field to the
4355
``albumartist`` field as well. (Otherwise, you would probably want to duplicate
4456
every rule for ``artist`` and ``albumartist``.)

test/plugins/test_rewrite.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def test_rewrite_is_case_insensitive_and_leaves_non_matches_unchanged(
5252
assert matching_item.artist == "LOONA / ODD EYE CIRCLE"
5353
assert other_item.artist == "ARTMS"
5454

55-
@pytest.mark.xfail(reason="only the first pattern applied")
5655
def test_rewrite_applied_to_all_list_values(self):
5756
with self.configure_plugin(
5857
{"genres rock": "Classic Rock", "genres pop": "Pop"}

0 commit comments

Comments
 (0)