From b0009f93f559dbfea2516e5ad4624fcaa5b85837 Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Thu, 16 Jan 2025 22:39:07 +0100 Subject: [PATCH] mbsync: add timid flag --- beetsplug/mbsync.py | 71 +++++++++++++++++++++++++++++------------ docs/changelog.rst | 3 ++ docs/plugins/mbsync.rst | 2 ++ 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/beetsplug/mbsync.py b/beetsplug/mbsync.py index 283c401865..eb8545c34a 100644 --- a/beetsplug/mbsync.py +++ b/beetsplug/mbsync.py @@ -58,6 +58,13 @@ def commands(self): dest="write", help="don't write updated metadata to files", ) + cmd.parser.add_option( + "-t", + "--timid", + dest="timid", + action="store_true", + help="always confirm all actions", + ) cmd.parser.add_format_option() cmd.func = self.func return [cmd] @@ -66,13 +73,14 @@ def func(self, lib, opts, args): """Command handler for the mbsync function.""" move = ui.should_move(opts.move) pretend = opts.pretend + timid = opts.timid write = ui.should_write(opts.write) query = ui.decargs(args) - self.singletons(lib, query, move, pretend, write) - self.albums(lib, query, move, pretend, write) + self.singletons(lib, query, move, pretend, write, timid) + self.albums(lib, query, move, pretend, write, timid) - def singletons(self, lib, query, move, pretend, write): + def singletons(self, lib, query, move, pretend, write, timid): """Retrieve and apply info from the autotagger for items matched by query. """ @@ -102,12 +110,26 @@ def singletons(self, lib, query, move, pretend, write): ) continue + item_old = item.copy() + autotag.apply_item_metadata(item, track_info) + if not ui.show_model_changes(item, item_old): + continue + + if timid: + print() + choice = ui.input_options(("Apply", "cancel", "skip")) + if choice == "a": # Apply. + pass + elif choice == "c": # Cancel. + return + elif choice == "s": # Skip. + continue + # Apply. with lib.transaction(): - autotag.apply_item_metadata(item, track_info) apply_item_changes(lib, item, move, pretend, write) - def albums(self, lib, query, move, pretend, write): + def albums(self, lib, query, move, pretend, write, timid): """Retrieve and apply info from the autotagger for albums matched by query and their items. """ @@ -174,28 +196,37 @@ def albums(self, lib, query, move, pretend, write): mapping[item] = c break + # Gather changes. + changed = [] + items_old = [item.copy() if item.id < 0 else None for item in items] + autotag.apply_metadata(album_info, mapping) + for item, item_old in zip(items, items_old): + if ui.show_model_changes(item, item_old): + changed.append(item) + + if len(changed) == 0: + continue + + if timid: + print() + choice = ui.input_options(("Apply", "cancel", "skip")) + if choice == "a": # Apply. + pass + elif choice == "c": # Cancel. + return + elif choice == "s": # Skip. + continue + # Apply. self._log.debug("applying changes to {}", album_formatted) with lib.transaction(): - autotag.apply_metadata(album_info, mapping) - changed = False - # Find any changed item to apply MusicBrainz changes to album. - any_changed_item = items[0] - for item in items: - item_changed = ui.show_model_changes(item) - changed |= item_changed - if item_changed: - any_changed_item = item - apply_item_changes(lib, item, move, pretend, write) - - if not changed: - # No change to any item. - continue + for item in changed: + apply_item_changes(lib, item, move, pretend, write) if not pretend: # Update album structure to reflect an item in it. for key in library.Album.item_keys: - a[key] = any_changed_item[key] + a[key] = changed[0][key] a.store() # Move album art (and any inconsistent items). diff --git a/docs/changelog.rst b/docs/changelog.rst index 186749d461..4976373c78 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -11,6 +11,9 @@ been dropped. New features: +* :doc:`/plugins/mbsync`: gained a new ``--timid`` flag to print and + confirm the changes before applying. :bug:`4250` + Bug fixes: * :doc:`plugins/lyrics`: LRCLib will fallback to plain lyrics if synced lyrics diff --git a/docs/plugins/mbsync.rst b/docs/plugins/mbsync.rst index 1c8663dcae..daa4c5762c 100644 --- a/docs/plugins/mbsync.rst +++ b/docs/plugins/mbsync.rst @@ -36,3 +36,5 @@ The command has a few command-line options: * To customize the output of unrecognized items, use the ``-f`` (``--format``) option. The default output is ``format_item`` or ``format_album`` for items and albums, respectively. +* To prompt for confirmation before applying changes, use the ``-t`` + (``--timid``) option.