Skip to content

Commit 91277a1

Browse files
authored
Merge pull request #4845 from Maxr1998/advancedrewrite
2 parents d8a6fd6 + 9660dd6 commit 91277a1

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

beetsplug/advancedrewrite.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# This file is part of beets.
2+
# Copyright 2023, Max Rumpf.
3+
#
4+
# Permission is hereby granted, free of charge, to any person obtaining
5+
# a copy of this software and associated documentation files (the
6+
# "Software"), to deal in the Software without restriction, including
7+
# without limitation the rights to use, copy, modify, merge, publish,
8+
# distribute, sublicense, and/or sell copies of the Software, and to
9+
# permit persons to whom the Software is furnished to do so, subject to
10+
# the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be
13+
# included in all copies or substantial portions of the Software.
14+
15+
"""Plugin to rewrite fields based on a given query."""
16+
17+
from collections import defaultdict
18+
import shlex
19+
20+
import confuse
21+
from beets import ui
22+
from beets.dbcore import AndQuery, query_from_strings
23+
from beets.library import Item, Album
24+
from beets.plugins import BeetsPlugin
25+
26+
27+
def rewriter(field, rules):
28+
"""Template field function factory.
29+
30+
Create a template field function that rewrites the given field
31+
with the given rewriting rules.
32+
``rules`` must be a list of (query, replacement) pairs.
33+
"""
34+
def fieldfunc(item):
35+
value = item._values_fixed[field]
36+
for query, replacement in rules:
37+
if query.match(item):
38+
# Rewrite activated.
39+
return replacement
40+
# Not activated; return original value.
41+
return value
42+
43+
return fieldfunc
44+
45+
46+
class AdvancedRewritePlugin(BeetsPlugin):
47+
"""Plugin to rewrite fields based on a given query."""
48+
49+
def __init__(self):
50+
"""Parse configuration and register template fields for rewriting."""
51+
super().__init__()
52+
53+
template = confuse.Sequence({
54+
'match': str,
55+
'field': str,
56+
'replacement': str,
57+
})
58+
59+
# Gather all the rewrite rules for each field.
60+
rules = defaultdict(list)
61+
for rule in self.config.get(template):
62+
query = query_from_strings(AndQuery, Item, prefixes={},
63+
query_parts=shlex.split(rule['match']))
64+
fieldname = rule['field']
65+
replacement = rule['replacement']
66+
if fieldname not in Item._fields:
67+
raise ui.UserError(
68+
"invalid field name (%s) in rewriter" % fieldname)
69+
self._log.debug('adding template field {0} → {1}',
70+
fieldname, replacement)
71+
rules[fieldname].append((query, replacement))
72+
if fieldname == 'artist':
73+
# Special case for the artist field: apply the same
74+
# rewrite for "albumartist" as well.
75+
rules['albumartist'].append((query, replacement))
76+
77+
# Replace each template field with the new rewriter function.
78+
for fieldname, fieldrules in rules.items():
79+
getter = rewriter(fieldname, fieldrules)
80+
self.template_fields[fieldname] = getter
81+
if fieldname in Album._fields:
82+
self.album_template_fields[fieldname] = getter

docs/changelog.rst

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ New features:
137137
* :doc:`/plugins/fetchart`: Fix the error with CoverArtArchive where no cover
138138
would be found when the `maxwidth` option matches a pre-sized thumbnail size,
139139
but no thumbnail is provided by CAA. We now fallback to the raw image.
140+
* :doc:`/plugins/advancedrewrite`: Add an advanced version of the `rewrite`
141+
plugin which allows to replace fields based on a given library query.
140142

141143
Bug fixes:
142144

docs/plugins/advancedrewrite.rst

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Advanced Rewrite Plugin
2+
=======================
3+
4+
The ``advancedrewrite`` plugin lets you easily substitute values
5+
in your templates and path formats, similarly to the :doc:`/plugins/rewrite`.
6+
Please make sure to read the documentation of that plugin first.
7+
8+
The *advanced* rewrite plugin doesn't match the rewritten field itself,
9+
but instead checks if the given item matches a :doc:`query </reference/query>`.
10+
Only then, the field is replaced with the given value.
11+
12+
To use advanced field rewriting, first enable the ``advancedrewrite`` plugin
13+
(see :ref:`using-plugins`).
14+
Then, make a ``advancedrewrite:`` section in your config file to contain
15+
your rewrite rules.
16+
17+
In contrast to the normal ``rewrite`` plugin, you need to provide a list
18+
of replacement rule objects, each consisting of a query, a field name,
19+
and the replacement value.
20+
21+
For example, to credit all songs of ODD EYE CIRCLE before 2023
22+
to their original group name, you can use the following rule::
23+
24+
advancedrewrite:
25+
- match: "mb_artistid:dec0f331-cb08-4c8e-9c9f-aeb1f0f6d88c year:..2022"
26+
field: artist
27+
replacement: "이달의 소녀 오드아이써클"
28+
29+
As a convenience, the plugin applies patterns for the ``artist`` field to the
30+
``albumartist`` field as well. (Otherwise, you would probably want to duplicate
31+
every rule for ``artist`` and ``albumartist``.)
32+
33+
A word of warning: This plugin theoretically only applies to templates and path
34+
formats; it initially does not modify files' metadata tags or the values
35+
tracked by beets' library database, but since it *rewrites all field lookups*,
36+
it modifies the file's metadata anyway. See comments in issue :bug:`2786`.
37+
38+
As an alternative to this plugin the simpler :doc:`/plugins/rewrite` or
39+
similar :doc:`/plugins/substitute` can be used.

docs/plugins/index.rst

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ following to your configuration::
6161

6262
absubmit
6363
acousticbrainz
64+
advancedrewrite
6465
albumtypes
6566
aura
6667
autobpm
@@ -246,6 +247,9 @@ Path Formats
246247
:doc:`rewrite <rewrite>`
247248
Substitute values in path formats.
248249

250+
:doc:`advancedrewrite <advancedrewrite>`
251+
Substitute field values for items matching a query.
252+
249253
:doc:`substitute <substitute>`
250254
As an alternative to :doc:`rewrite <rewrite>`, use this plugin. The main
251255
difference between them is that this plugin never modifies the files

0 commit comments

Comments
 (0)