From 249b420b1982f8a3fc4dc35430e47280c7d81cc5 Mon Sep 17 00:00:00 2001 From: Daniele Ferone Date: Tue, 7 Feb 2023 15:34:50 +0100 Subject: [PATCH] Added plugin --- beetsplug/substitute.py | 40 +++++++++++++++++++++++++++++++++++++ docs/plugins/substitute.rst | 27 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 beetsplug/substitute.py create mode 100644 docs/plugins/substitute.rst diff --git a/beetsplug/substitute.py b/beetsplug/substitute.py new file mode 100644 index 000000000..9d6fc2d04 --- /dev/null +++ b/beetsplug/substitute.py @@ -0,0 +1,40 @@ +# This file is part of beets. +# Copyright 2016, Adrian Sampson. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. + +"""Uses user-specified substituting rules to canonicalize names for path +formats without modifying the tags of the songs. +""" +from beets.plugins import BeetsPlugin +import re + +_substitute_rules = [] + +class Substitute(BeetsPlugin): + def __init__(self): + super(Substitute, self).__init__() + self.template_funcs['substitute'] = _tmpl_substitute + + for key, view in self.config.items(): + value = view.as_str() + pattern = re.compile(key.lower()) + _substitute_rules.append((pattern, value)) + +def _tmpl_substitute(text): + if text: + for pattern, replacement in _substitute_rules: + if pattern.match(text.lower()): + return replacement + return text + else: + return u'' diff --git a/docs/plugins/substitute.rst b/docs/plugins/substitute.rst new file mode 100644 index 000000000..867a7b391 --- /dev/null +++ b/docs/plugins/substitute.rst @@ -0,0 +1,27 @@ +Substitute Plugin +============== + +The ``substitute`` plugin lets you easily substitute values in your templates and +path formats. Specifically, it is intended to let you *canonicalize* names +such as artists: for example, perhaps you want albums from The Jimi Hendrix +Experience to be sorted into the same folder as solo Hendrix albums. + +To use field substituting, first 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 a regular expression pattern, and a +replacement value. Rules are written ``regex: replacement``. +For example, this line implements the Jimi Hendrix example above:: + + rewrite: + The Jimi Hendrix Experience: Jimi Hendrix + +The pattern is a case-insensitive regular expression. This means you can use +ordinary regular expression syntax to match multiple artists. For example, you +might use:: + + rewrite: + .*jimi hendrix.*: Jimi Hendrix + +This plugin is intented as a replacement for the ``rewrite`` plugin. Indeed, while +the ``rewrite`` plugin modifies the metadata, this plugin does not.