From 1e07495c261b5ab8e53d076c6caf55b487aa397a Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Thu, 30 Apr 2020 11:41:21 -0300 Subject: [PATCH] PICARD-1809: Load based on extensions, fallback to guessing if it fails --- picard/file.py | 11 ++++++++++- picard/formats/__init__.py | 15 ++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/picard/file.py b/picard/file.py index 5ab51deb5..0a4e77613 100644 --- a/picard/file.py +++ b/picard/file.py @@ -165,7 +165,16 @@ class File(QtCore.QObject, Item): if self.tagger.stopping: log.debug("File not loaded because %s is stopping: %r", PICARD_APP_NAME, self.filename) return None - return self._load(filename) + try: + # Try loading based on extension first + return self._load(filename) + except Exception: + from picard.formats import guess_format + # If it fails, force format guessing and try loading again + file_format = guess_format(filename) + if not file_format and type(file_format) == type(self): + raise + return file_format._load(filename) def _load(self, filename): """Load metadata from the file.""" diff --git a/picard/formats/__init__.py b/picard/formats/__init__.py index 4783d39fc..6b496f688 100644 --- a/picard/formats/__init__.py +++ b/picard/formats/__init__.py @@ -79,15 +79,16 @@ def guess_format(filename, options=_formats): def open_(filename): """Open the specified file and return a File instance with the appropriate format handler, or None.""" try: - # First try to guess the format on the basis of file headers - audio_file = guess_format(filename) - if not audio_file: - i = filename.rfind(".") - if i < 0: - return None + # Use extension based opening as default + i = filename.rfind(".") + if i >= 0: ext = filename[i+1:].lower() - # Switch to extension based opening if guess_format fails audio_file = _extensions[ext](filename) + else: + # If there is no extension, try to guess the format based on file headers + audio_file = guess_format(filename) + if not audio_file: + return None return audio_file except KeyError: # None is returned if both the methods fail