Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions beetsplug/importsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ def __init__(self):
}
)
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:
Expand All @@ -60,10 +62,7 @@ def import_stage(self, _, task):

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:
Expand Down
32 changes: 32 additions & 0 deletions test/plugins/test_importsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,35 @@ def imported_items(self):
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
Loading