allow templates/formatting when setting fields with modify

This commit is contained in:
Duncan Overbruck 2021-10-06 15:14:34 +02:00
parent ee8a4de247
commit 636e36ef7a
No known key found for this signature in database
GPG key ID: 335C1D17EC3D6E35
3 changed files with 20 additions and 4 deletions

View file

@ -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?

View file

@ -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 <pathformat>`.
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,

View file

@ -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):