From 636e36ef7a42ae8edf50da97c16e293d387e41c5 Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Wed, 6 Oct 2021 15:14:34 +0200 Subject: [PATCH] allow templates/formatting when setting fields with modify --- beets/ui/commands.py | 7 +++---- docs/reference/cli.rst | 3 +++ test/test_ui.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index 3a3374013..27ec17f5b 100755 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -1411,9 +1411,6 @@ def modify_items(lib, mods, dels, query, write, move, album, confirm): # Parse key=value specifications into a dictionary. model_cls = library.Album if album else library.Item - for key, value in mods.items(): - mods[key] = model_cls._parse(key, value) - # Get the items to modify. items, albums = _do_query(lib, query, album, False) objs = albums if album else items @@ -1424,7 +1421,9 @@ def modify_items(lib, mods, dels, query, write, move, album, confirm): .format(len(objs), 'album' if album else 'item')) changed = [] for obj in objs: - if print_and_modify(obj, mods, dels) and obj not in changed: + obj_mods = {key: model_cls._parse(key, format(obj, value)) + for key, value in mods.items()} + if print_and_modify(obj, obj_mods, dels) and obj not in changed: changed.append(obj) # Still something to do? diff --git a/docs/reference/cli.rst b/docs/reference/cli.rst index 9b0e6f482..d3d5474aa 100644 --- a/docs/reference/cli.rst +++ b/docs/reference/cli.rst @@ -263,6 +263,9 @@ artist="Tom Tom Club"`` will change the artist for the track "Genius of Love." To remove fields (which is only possible for flexible attributes), follow a field name with an exclamation point: ``field!``. +Values support the same template syntax as beets' +:doc:`path formats `. + The ``-a`` switch operates on albums instead of individual tracks. Without this flag, the command will only change *track-level* data, even if all the tracks belong to the same album. If you want to change an *album-level* field, diff --git a/test/test_ui.py b/test/test_ui.py index 9804b0a12..82a21ab51 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -287,6 +287,13 @@ class ModifyTest(unittest.TestCase, TestHelper): self.assertEqual(len(list(original_items)), 3) self.assertEqual(len(list(new_items)), 7) + def test_modify_formatted(self): + item = self.lib.items().get() + orig_title = item.title + self.modify("title=${title} - append") + item.load() + self.assertEqual(item.title, f"{orig_title} - append") + # Album Tests def test_modify_album(self): @@ -318,6 +325,13 @@ class ModifyTest(unittest.TestCase, TestHelper): item.read() self.assertNotIn(b'newAlbum', item.path) + def test_modify_album_formatted(self): + item = self.lib.items().get() + orig_album = item.album + self.modify("--album", "album=${album} - append") + item.load() + self.assertEqual(item.album, f"{orig_album} - append") + # Misc def test_write_initial_key_tag(self):