Skip to content

Commit 5c366fd

Browse files
committed
Simplified formats.open_
1 parent 52635c7 commit 5c366fd

File tree

5 files changed

+14
-24
lines changed

5 files changed

+14
-24
lines changed

picard/extension_points/formats.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Copyright (C) 2006-2008, 2012 Lukáš Lalinský
66
# Copyright (C) 2008 Will
7-
# Copyright (C) 2010, 2014, 2018-2020, 2023 Philipp Wolfer
7+
# Copyright (C) 2010, 2014, 2018-2020, 2023-2024 Philipp Wolfer
88
# Copyright (C) 2013 Michael Wiencek
99
# Copyright (C) 2013, 2017-2024 Laurent Monin
1010
# Copyright (C) 2016-2018 Sambhav Kothari
@@ -36,8 +36,8 @@
3636
def register_format(file_format):
3737
ext_point_formats.register(file_format.__module__, file_format)
3838
for ext in file_format.EXTENSIONS:
39-
_formats_extensions[ext[1:]] = file_format
39+
_formats_extensions[ext.lower()] = file_format
4040

4141

4242
def ext_to_format(ext):
43-
return _formats_extensions.get(ext, None)
43+
return _formats_extensions.get(ext.lower(), None)

picard/formats/util.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Copyright (C) 2006-2008, 2012 Lukáš Lalinský
66
# Copyright (C) 2008 Will
7-
# Copyright (C) 2010, 2014, 2018-2020, 2023 Philipp Wolfer
7+
# Copyright (C) 2010, 2014, 2018-2020, 2023-2024 Philipp Wolfer
88
# Copyright (C) 2013 Michael Wiencek
99
# Copyright (C) 2013, 2017-2019, 2021, 2023-2024 Laurent Monin
1010
# Copyright (C) 2016-2018 Sambhav Kothari
@@ -25,6 +25,7 @@
2525
# along with this program; if not, write to the Free Software
2626
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2727

28+
import os.path
2829

2930
from picard import log
3031
from picard.extension_points.formats import (
@@ -72,23 +73,12 @@ def open_(filename):
7273
"""Open the specified file and return a File instance with the appropriate format handler, or None."""
7374
try:
7475
# Use extension based opening as default
75-
i = filename.rfind(".")
76-
if i >= 0:
77-
ext = filename[i+1:].lower()
78-
file_format = ext_to_format(ext)
79-
if file_format is None:
80-
audio_file = guess_format(filename)
81-
else:
82-
audio_file = file_format(filename)
83-
else:
84-
# If there is no extension, try to guess the format based on file headers
85-
audio_file = guess_format(filename)
86-
if not audio_file:
87-
return None
88-
return audio_file
89-
except KeyError:
90-
# None is returned if both the methods fail
91-
return None
76+
_name, ext = os.path.splitext(filename)
77+
if ext:
78+
if file_format := ext_to_format(ext):
79+
return file_format(filename)
80+
# If detection by extension failed, try to guess the format based on file headers
81+
return guess_format(filename)
9282
except Exception as error:
9383
log.error("Error occurred:\n%s", error)
9484
return None

test/formats/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def setUp(self):
216216
self.testfile_path = os.path.join('test', 'data', self.testfile)
217217
self.testfile_ext = os.path.splitext(self.testfile)[1]
218218
self.filename = self.copy_of_original_testfile()
219-
self.format = ext_to_format(self.testfile_ext[1:])
219+
self.format = ext_to_format(self.testfile_ext)
220220

221221
def copy_of_original_testfile(self):
222222
return self.copy_file_tmp(self.testfile_path, self.testfile_ext)

test/formats/test_asf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CommonAsfTests:
4949
class AsfTestCase(CommonTests.TagFormatsTestCase):
5050

5151
def test_supports_tag(self):
52-
fmt = ext_to_format(self.testfile_ext[1:])
52+
fmt = ext_to_format(self.testfile_ext)
5353
self.assertTrue(fmt.supports_tag('copyright'))
5454
self.assertTrue(fmt.supports_tag('compilation'))
5555
self.assertTrue(fmt.supports_tag('bpm'))

test/formats/test_mp4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CommonMP4Tests:
4444

4545
class MP4TestCase(CommonTests.TagFormatsTestCase):
4646
def test_supports_tag(self):
47-
fmt = ext_to_format(self.testfile_ext[1:])
47+
fmt = ext_to_format(self.testfile_ext)
4848
self.assertTrue(fmt.supports_tag('copyright'))
4949
self.assertTrue(fmt.supports_tag('compilation'))
5050
self.assertTrue(fmt.supports_tag('bpm'))

0 commit comments

Comments
 (0)