From b17607192c2df5800bc305d0e656ffe8fc1e8bf9 Mon Sep 17 00:00:00 2001 From: Ben Miller Date: Sun, 22 Sep 2024 00:47:00 -0700 Subject: [PATCH] Add check for config.fail_on_decode_error --- audalign/recognizers/__init__.py | 11 ++++++++++- audalign/recognizers/correcognize/correcognize.py | 4 +++- .../correcognize_spectrogram.py | 4 +++- audalign/recognizers/fingerprint/__init__.py | 3 +++ audalign/recognizers/fingerprint/fingerprinter.py | 10 ++++++---- audalign/recognizers/visrecognize/visrecognize.py | 4 +++- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/audalign/recognizers/__init__.py b/audalign/recognizers/__init__.py index 4a63148..75bfd69 100644 --- a/audalign/recognizers/__init__.py +++ b/audalign/recognizers/__init__.py @@ -4,6 +4,7 @@ import audalign.filehandler as filehandler from audalign.config import BaseConfig +from pydub.exceptions import CouldntDecodeError class BaseRecognizer(ABC): @@ -47,10 +48,18 @@ def align_get_file_names( file_names = [os.path.basename(x) for x in file_list] elif file_dir: file_names = filehandler.get_audio_files_directory( - file_dir, False, self.config.can_read_extensions, self.config.cant_read_extensions) + file_dir, + False, + self.config.can_read_extensions, + self.config.cant_read_extensions, + ) elif fine_aud_file_dict: + if fine_aud_file_dict == None or len(fine_aud_file_dict.keys()) == 0: + raise CouldntDecodeError("No files found", fine_aud_file_dict) file_names = [os.path.basename(x) for x in fine_aud_file_dict.keys()] else: + if file_list == None or len(file_list) == 0: + raise CouldntDecodeError("No files found", file_list) file_names = [os.path.basename(x) for x in file_list] return file_names diff --git a/audalign/recognizers/correcognize/correcognize.py b/audalign/recognizers/correcognize/correcognize.py index c3f98ba..bad0f24 100644 --- a/audalign/recognizers/correcognize/correcognize.py +++ b/audalign/recognizers/correcognize/correcognize.py @@ -336,8 +336,10 @@ def _correcognize_dir( **kwargs, ) - except CouldntDecodeError: + except CouldntDecodeError as e: print(f'File "{against_file_path}" could not be decoded') + if config.fail_on_decode_error: + raise e return {} diff --git a/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py b/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py index 5cb5552..289ec15 100644 --- a/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py +++ b/audalign/recognizers/correcognizeSpectrogram/correcognize_spectrogram.py @@ -336,8 +336,10 @@ def _correcognize_dir( **kwargs, ) - except CouldntDecodeError: + except CouldntDecodeError as e: print(f'File "{against_file_path}" could not be decoded') + if config.fail_on_decode_error: + raise e return {} diff --git a/audalign/recognizers/fingerprint/__init__.py b/audalign/recognizers/fingerprint/__init__.py index 8f5f4e1..3adbb18 100644 --- a/audalign/recognizers/fingerprint/__init__.py +++ b/audalign/recognizers/fingerprint/__init__.py @@ -10,6 +10,7 @@ import pickle import json import typing +from pydub.exceptions import CouldntDecodeError class FingerprintRecognizer(BaseRecognizer): @@ -266,6 +267,8 @@ def _fingerprint_directory( print("All files in directory already fingerprinted") else: print("Directory contains 0 files or could not be found") + if self.config.fail_on_decode_error: + raise CouldntDecodeError("Directory contains 0 files or could not be found") return if _file_audsegs is not None: diff --git a/audalign/recognizers/fingerprint/fingerprinter.py b/audalign/recognizers/fingerprint/fingerprinter.py index 4d73137..f8adfff 100644 --- a/audalign/recognizers/fingerprint/fingerprinter.py +++ b/audalign/recognizers/fingerprint/fingerprinter.py @@ -52,10 +52,12 @@ def _fingerprint_worker( except FileNotFoundError: print(f'"{file_path}" not found') return None, None - except ( - CouldntDecodeError, - IndexError, - ): # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?) + except CouldntDecodeError as e: + print(f'File "{file_name}" could not be decoded') + if config.fail_on_decode_error: + raise e + return None, None + except IndexError: # Pydub throws IndexErrors for some files on Ubuntu (json, txt, others?) print(f'File "{file_name}" could not be decoded') return None, None elif type(file_path) == tuple: diff --git a/audalign/recognizers/visrecognize/visrecognize.py b/audalign/recognizers/visrecognize/visrecognize.py index 9d8a540..6b660dc 100644 --- a/audalign/recognizers/visrecognize/visrecognize.py +++ b/audalign/recognizers/visrecognize/visrecognize.py @@ -176,8 +176,10 @@ def _visrecognize_directory( imgB_title=os.path.basename(file_path), ) return single_file_match - except CouldntDecodeError: + except CouldntDecodeError as e: print(f'File "{file_path}" could not be decoded') + if config.fail_on_decode_error: + raise e return {}