Use simpler approach of asserting that at most one handler of import_task_before_choice returns an action.

This commit is contained in:
Ryan Lanny Jenkins 2021-03-28 16:53:01 -05:00
parent 39b5a7636c
commit 79616b42ed
4 changed files with 13 additions and 28 deletions

View file

@ -501,22 +501,6 @@ def send(event, **arguments):
return results
def send_seq(event, **arguments):
"""Like `send` but passes the result of the previous event handler to the
next, and returns only the result of the last non-None event handler.
`event` is the name of the event to send, all other named arguments
are passed along to the handlers.
"""
log.debug(u'Sequentially sending event: {0}', event)
previous = None
for handler in event_handlers()[event]:
result = handler(previous=previous, **arguments)
previous = result or previous
return previous
def feat_tokens(for_artist=True):
"""Return a regular expression that matches phrases like "featuring"
that separate a main artist or a song title from secondary artists.

View file

@ -700,10 +700,16 @@ class TerminalImportSession(importer.ImportSession):
# Let plugins display info or prompt the user before we go through the
# process of selecting candidate.
action = plugins.send_seq('import_task_before_choice',
results = plugins.send('import_task_before_choice',
session=self, task=task)
if action is not None:
return action
actions = [action for action in results if action]
if len(actions) == 1:
return actions[0]
elif len(actions) > 1:
raise Exception(
u'Only one handler for `import_task_before_choice` may return '
u'an action.')
# Take immediate action if appropriate.
action = _summary_judgment(task.rec)

View file

@ -173,11 +173,7 @@ class BadFiles(BeetsPlugin):
if checks_failed:
task._badfiles_checks_failed = checks_failed
def on_import_task_before_choice(self, task, session, previous):
# Already skipping, so no need to notify the user of anything
if previous == importer.action.SKIP:
return None
def on_import_task_before_choice(self, task, session):
if hasattr(task, '_badfiles_checks_failed'):
ui.print_('{} one or more files failed checks:'
.format(ui.colorize('text_warning', 'BAD')))

View file

@ -204,9 +204,8 @@ The events currently available are:
before any decision is made about how/if to import or tag. Can be used to
present information about the task or initiate interaction with the user
before importing occurs. Return an importer action to take a specific action.
Parameters: ``task``, ``session``, and ``previous`` (which is the action
specified by the previous event handler for the same task, allowing an early
exist if e.g. a previous plugin has already selected to skip the import)
Only one handler may return a non-None result.
Parameters: ``task`` and ``session``
* `import_task_choice`: called after a decision has been made about an import
task. This event can be used to initiate further interaction with the user.