Skip to content

Commit cc00d7b

Browse files
committed
badfiles: added config options to automatically take action on error/warning
1 parent b3c42a3 commit cc00d7b

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

beetsplug/badfiles.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,68 @@ def on_import_task_start(self, task, session):
170170
if checks_failed:
171171
task._badfiles_checks_failed = checks_failed
172172

173+
def handle_import_action(self, action, failure_type):
174+
if action == "abort":
175+
ui.print_(
176+
f"{ui.colorize('text_warning', 'Aborting')}"
177+
f" due to import_action_on_{failure_type} configuration"
178+
)
179+
raise importer.ImportAbortError()
180+
elif action == "skip":
181+
ui.print_(
182+
f"{ui.colorize('text_warning', 'Skipping')}"
183+
f" due to import_action_on_{failure_type} configuration"
184+
)
185+
return importer.Action.SKIP
186+
elif action == "continue":
187+
ui.print_(
188+
f"{ui.colorize('text_warning', 'Continuing')}"
189+
f" due to import_action_on_{failure_type} configuration"
190+
)
191+
return None
192+
else:
193+
ui.print_(
194+
ui.colorize(
195+
"text_warning",
196+
f"Got invalid import_action_on_{failure_type}"
197+
f" configuration: {action}",
198+
)
199+
)
200+
ui.print_(
201+
ui.colorize(
202+
"text_warning",
203+
f"import_action_on_{failure_type} should be one of:"
204+
f" ask abort skip continue",
205+
)
206+
)
207+
raise importer.ImportAbortError()
208+
173209
def on_import_task_before_choice(self, task, session):
174210
if hasattr(task, "_badfiles_checks_failed"):
211+
warning_action = self.config["import_action_on_warning"].get("ask")
212+
error_action = self.config["import_action_on_error"].get("ask")
213+
175214
ui.print_(
176215
f"{ui.colorize('text_warning', 'BAD')} one or more files failed"
177216
" checks:"
178217
)
218+
219+
found_warning = False
220+
found_error = False
179221
for error in task._badfiles_checks_failed:
180222
for error_line in error:
223+
if "warning" in error_line.lower():
224+
found_warning = True
225+
if "error" in error_line.lower():
226+
found_error = True
227+
181228
ui.print_(error_line)
182229

230+
if found_error and error_action != "ask":
231+
return self.handle_import_action(error_action, "error")
232+
elif found_warning and warning_action != "ask":
233+
return self.handle_import_action(warning_action, "warning")
234+
183235
ui.print_()
184236
ui.print_("What would you like to do?")
185237

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ been dropped.
1212

1313
New features:
1414

15+
- :doc:`plugins/badfiles`: Added settings for auto error and warning actions.
1516
- :doc:`plugins/fetchart`: Added config setting for a fallback cover art image.
1617
- :doc:`plugins/ftintitle`: Added argument for custom feat. words in ftintitle.
1718
- :doc:`plugins/ftintitle`: Added album template value ``album_artist_no_feat``.

docs/plugins/badfiles.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ You can also add custom commands for a specific extension, like this:
2020

2121
badfiles:
2222
check_on_import: yes
23+
import_action_on_error: skip
24+
import_action_on_warning: continue
2325
commands:
2426
ogg: myoggchecker --opt1 --opt2
2527
flac: flac --test --warnings-as-errors --silent
@@ -32,6 +34,11 @@ You can run the checkers when importing files by using the ``check_on_import``
3234
option. When on, checkers will be run against every imported file and warnings
3335
and errors will be presented when selecting a tagging option.
3436

37+
`import_action_on_error` and `import_action_on_warning` can be used to take
38+
automatic action on warning and errors. Both options default to `ask`.
39+
Valid options for both `import_action_on_(warning|error)` are
40+
`ask skip abort continue`.
41+
3542
.. _flac: https://xiph.org/flac/
3643

3744
.. _mp3val: http://mp3val.sourceforge.net/

0 commit comments

Comments
 (0)