Revert configuration format changes

This commit is contained in:
Nicholas Boyd Isacsson 2024-10-14 22:43:47 +02:00
parent 48c7435780
commit ffdc3f73ab
3 changed files with 24 additions and 76 deletions

View file

@ -18,7 +18,6 @@ Uses user-specified substitution rules to canonicalize names for path formats.
"""
import re
import textwrap
from beets.plugins import BeetsPlugin
@ -49,31 +48,8 @@ class Substitute(BeetsPlugin):
substitute rules.
"""
super().__init__()
self.substitute_rules = []
self.template_funcs["substitute"] = self.tmpl_substitute
if isinstance(self.config.get(), dict):
self._log.warning(
"Unordered configuration is deprecated, as it leads to"
+ " unpredictable behaviour on overlapping rules.\n"
+ textwrap.dedent(
"""
Old syntax:
substitute:
a: b
c: d
New syntax:
substitute:
- a: b
- c: d
"""
)
)
pairs = self.config.flatten().items()
else:
pairs = self.config.as_pairs()
for key, value in pairs:
pattern = re.compile(key, flags=re.IGNORECASE)
self.substitute_rules.append((pattern, value))
self.substitute_rules = [
(re.compile(key, flags=re.IGNORECASE), value)
for key, value in self.config.flatten().items()
]

View file

@ -9,14 +9,14 @@ Experience to be sorted into the same folder as solo Hendrix albums.
This plugin is intended as a replacement for the ``rewrite`` plugin. While
the ``rewrite`` plugin modifies the metadata, this plugin does not.
Enable the ``substitute`` plugin (see :ref:`using-plugins`), then make a ``substitute:`` section in your config file to contain a list of your rules.
Enable the ``substitute`` plugin (see :ref:`using-plugins`), then make a ``substitute:`` section in your config file to contain your rules.
Each rule consists of a case-insensitive regular expression pattern, and a
replacement string. For example, you might use:
.. code-block:: yaml
substitute:
- .*jimi hendrix.*: Jimi Hendrix
.*jimi hendrix.*: Jimi Hendrix
The replacement can be an expression utilising the matched regex, allowing us
to create more general rules. Say for example, we want to sort all albums by
@ -27,7 +27,7 @@ group in the output, discarding the rest of the string.
.. code-block:: yaml
substitute:
- ^(.*?)(,| &| and).*: \1
^(.*?)(,| &| and).*: \1
This would handle all the below cases in a single rule:

View file

@ -14,7 +14,7 @@
"""Test the substitute plugin regex functionality."""
from beets.test.helper import PluginTestCase, capture_log
from beets.test.helper import PluginTestCase
from beetsplug.substitute import Substitute
@ -29,29 +29,29 @@ class SubstitutePluginTest(PluginTestCase):
def test_simple_substitute(self):
self.run_substitute(
[
{"a": "b"},
{"b": "c"},
{"c": "d"},
],
{
"a": "b",
"b": "c",
"c": "d",
},
[("a", "b"), ("b", "c"), ("c", "d")],
)
def test_case_insensitivity(self):
self.run_substitute([{"a": "b"}], [("A", "b")])
self.run_substitute({"a": "b"}, [("A", "b")])
def test_unmatched_input_preserved(self):
self.run_substitute([{"a": "b"}], [("c", "c")])
self.run_substitute({"a": "b"}, [("c", "c")])
def test_regex_to_static(self):
self.run_substitute(
[{".*jimi hendrix.*": "Jimi Hendrix"}],
{".*jimi hendrix.*": "Jimi Hendrix"},
[("The Jimi Hendrix Experience", "Jimi Hendrix")],
)
def test_regex_capture_group(self):
self.run_substitute(
[{"^(.*?)(,| &| and).*": r"\1"}],
{"^(.*?)(,| &| and).*": r"\1"},
[
("King Creosote & Jon Hopkins", "King Creosote"),
(
@ -64,44 +64,16 @@ class SubstitutePluginTest(PluginTestCase):
)
def test_partial_substitution(self):
self.run_substitute([{r"\.": ""}], [("U.N.P.O.C.", "UNPOC")])
self.run_substitute({r"\.": ""}, [("U.N.P.O.C.", "UNPOC")])
def test_break_on_first_match(self):
self.run_substitute(
[
{"a": "b"},
{"[ab]": "c"},
],
[
("a", "b"),
("b", "c"),
],
)
def test_deprecated_config(self):
self.run_substitute(
{
"a": "b",
"b": "c",
"c": "d",
"a": "x",
"[ab]": "y",
},
[("a", "b"), ("b", "c"), ("c", "d")],
[
("a", "x"),
("b", "y"),
],
)
def test_deprecated_config_warning(self):
with capture_log() as logs:
with self.configure_plugin(
{
"a": "b",
"b": "c",
"c": "d",
}
):
assert any(
[
"Unordered configuration is deprecated, as it leads to"
+ " unpredictable behaviour on overlapping rules"
in log
for log in logs
]
)