This commit is contained in:
J0J0 Todos 2026-02-03 03:22:20 +10:00 committed by GitHub
commit 4fe5c385f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 15 deletions

View file

@ -26,17 +26,19 @@ class ImportSourcePlugin(BeetsPlugin):
}
)
self.import_stages = [self.import_stage]
self.register_listener("item_removed", self.suggest_removal)
# In order to stop future removal suggestions for an album we keep
# track of `mb_albumid`s in this set.
self.stop_suggestions_for_albums = set()
# During reimports (import --library) both the import_task_choice and
# the item_removed event are triggered. The item_removed event is
# triggered first. For the import_task_choice event we prevent removal
# suggestions using the existing stop_suggestions_for_album mechanism.
self.register_listener(
"import_task_choice", self.prevent_suggest_removal
)
# Only register removal suggestion listeners if the feature is enabled
if self.config["suggest_removal"]:
# In order to stop future removal suggestions for an album we keep
# track of `mb_albumid`s in this set.
self.stop_suggestions_for_albums = set()
self.register_listener("item_removed", self.suggest_removal)
# During reimports (import --library) both the import_task_choice and
# the item_removed event are triggered. The item_removed event is
# triggered first. For the import_task_choice event we prevent removal
# suggestions using the existing stop_suggestions_for_album mechanism.
self.register_listener(
"import_task_choice", self.prevent_suggest_removal
)
def prevent_suggest_removal(self, session, task):
if task.skip:
@ -60,10 +62,7 @@ class ImportSourcePlugin(BeetsPlugin):
def suggest_removal(self, item):
"""Prompts the user to delete the original path the item was imported from."""
if (
not self.config["suggest_removal"]
or item.mb_albumid in self.stop_suggestions_for_albums
):
if item.mb_albumid in self.stop_suggestions_for_albums:
return
if "source_path" not in item:

View file

@ -144,3 +144,35 @@ class ImportSourceTest(PluginMixin, AutotagImportTestCase):
plugin = plugins._instances[0]
mock_task = MockTask()
plugin.prevent_suggest_removal(None, mock_task)
class ImportSourceTestListenerRegistration(PluginMixin, AutotagImportTestCase):
"""Test listener registration based on config."""
plugin = "importsource"
preload_plugin = False
def setUp(self):
preserve_plugin_listeners()
super().setUp()
def test_listeners_not_registered_when_disabled(self):
"""Test that listeners are not registered when suggest_removal is False."""
self.config[self.plugin]["suggest_removal"] = False
self.load_plugins()
plugin = plugins._instances[0]
assert not hasattr(plugin, "stop_suggestions_for_albums")
assert "item_removed" not in plugin._raw_listeners
assert "import_task_choice" not in plugin._raw_listeners
def test_listeners_registered_when_enabled(self):
"""Test that listeners are registered when suggest_removal is True."""
self.config[self.plugin]["suggest_removal"] = True
self.load_plugins()
plugin = plugins._instances[0]
assert hasattr(plugin, "stop_suggestions_for_albums")
assert isinstance(plugin.stop_suggestions_for_albums, set)
assert "item_removed" in plugin._raw_listeners
assert "import_task_choice" in plugin._raw_listeners