Skip to content

Commit

Permalink
allow templates/formatting when setting fields with modify
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncaen committed Oct 26, 2021
1 parent ee8a4de commit 636e36e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 3 additions & 4 deletions beets/ui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions test/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ def test_selective_modify(self):
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):
Expand Down Expand Up @@ -318,6 +325,13 @@ def test_album_not_move(self):
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):
Expand Down

0 comments on commit 636e36e

Please sign in to comment.