diff --git a/picard/browser/addrelease.py b/picard/browser/addrelease.py index 690f921e77..4b76badb0a 100644 --- a/picard/browser/addrelease.py +++ b/picard/browser/addrelease.py @@ -2,7 +2,7 @@ # # Picard, the next-generation MusicBrainz tagger # -# Copyright (C) 2021-2023 Philipp Wolfer +# Copyright (C) 2021-2023, 2025 Philipp Wolfer # Copyright (C) 2021-2024 Laurent Monin # Copyright (C) 2022 Bob Swift # Copyright (C) 2022 jesus2099 @@ -23,6 +23,7 @@ from html import escape +from operator import attrgetter from secrets import token_bytes from PyQt6.QtCore import QCoreApplication @@ -112,14 +113,6 @@ def serve_form(token): raise InvalidTokenError -def extract_discnumber(metadata): - try: - discnumber = metadata.get('discnumber', '1').split('/')[0] - return int(discnumber) - except ValueError: - return 1 - - def _open_url_with_token(payload): token = jwt.encode(payload, __key, algorithm=__algorithm) if isinstance(token, bytes): # For compatibility with PyJWT 1.x @@ -214,9 +207,9 @@ def mkey(disc, track, name): disc_counter = 0 track_counter = 0 last_discnumber = None - for f in files: + for f in sorted(files, key=attrgetter('discnumber', 'tracknumber')): m = f.metadata - discnumber = extract_discnumber(m) + discnumber = f.discnumber if last_discnumber is not None and discnumber != last_discnumber: disc_counter += 1 track_counter = 0 diff --git a/picard/item.py b/picard/item.py index 8527af599a..970e68fc84 100644 --- a/picard/item.py +++ b/picard/item.py @@ -3,7 +3,7 @@ # Picard, the next-generation MusicBrainz tagger # # Copyright (C) 2006-2007 Lukáš Lalinský -# Copyright (C) 2010, 2018, 2020-2022, 2024 Philipp Wolfer +# Copyright (C) 2010, 2018, 2020-2022, 2024-2025 Philipp Wolfer # Copyright (C) 2011-2012 Michael Wiencek # Copyright (C) 2012 Chad Wilson # Copyright (C) 2013, 2020-2021, 2023-2024 Laurent Monin @@ -108,17 +108,18 @@ def load(self, priority=False, refresh=False): @property def tracknumber(self): """The track number as an int.""" - try: - return int(self.metadata['tracknumber']) - except BaseException: - return 0 + return self._track_or_disc_number('tracknumber') @property def discnumber(self): """The disc number as an int.""" + return self._track_or_disc_number('discnumber') + + def _track_or_disc_number(self, field): + """Extract tracknumber or discnumber as int, defaults to 0.""" try: - return int(self.metadata['discnumber']) - except BaseException: + return int(self.metadata.get(field, '0').split('/')[0]) + except ValueError: return 0 @property diff --git a/test/test_browser_addrelease.py b/test/test_browser_addrelease.py deleted file mode 100644 index 1744b0edf3..0000000000 --- a/test/test_browser_addrelease.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Picard, the next-generation MusicBrainz tagger -# -# Copyright (C) 2021 Philipp Wolfer -# Copyright (C) 2021-2022 Laurent Monin -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - -from test.picardtestcase import PicardTestCase - -from picard.browser.addrelease import extract_discnumber -from picard.metadata import Metadata - - -class BrowserAddreleaseTest(PicardTestCase): - - def test_extract_discnumber(self): - self.assertEqual(1, extract_discnumber(Metadata())) - self.assertEqual(1, extract_discnumber(Metadata({'discnumber': '1'}))) - self.assertEqual(42, extract_discnumber(Metadata({'discnumber': '42'}))) - self.assertEqual(3, extract_discnumber(Metadata({'discnumber': '3/12'}))) - self.assertEqual(3, extract_discnumber(Metadata({'discnumber': ' 3 / 12 '}))) diff --git a/test/test_file.py b/test/test_file.py index 55186c6c04..4779d50cf5 100644 --- a/test/test_file.py +++ b/test/test_file.py @@ -2,7 +2,7 @@ # # Picard, the next-generation MusicBrainz tagger # -# Copyright (C) 2018-2024 Philipp Wolfer +# Copyright (C) 2018-2025 Philipp Wolfer # Copyright (C) 2019-2022, 2024 Laurent Monin # Copyright (C) 2021 Bob Swift # Copyright (C) 2021 Sophist-UK @@ -64,6 +64,10 @@ def test_tracknumber(self): self.assertEqual(42, self.file.tracknumber) self.file.metadata['tracknumber'] = 'FOURTYTWO' self.assertEqual(0, self.file.tracknumber) + self.file.metadata['tracknumber'] = '3/12' + self.assertEqual(3, self.file.tracknumber) + self.file.metadata['tracknumber'] = '3 / 12' + self.assertEqual(3, self.file.tracknumber) def test_discnumber(self): self.assertEqual(0, self.file.discnumber) @@ -71,6 +75,10 @@ def test_discnumber(self): self.assertEqual(42, self.file.discnumber) self.file.metadata['discnumber'] = 'FOURTYTWO' self.assertEqual(0, self.file.discnumber) + self.file.metadata["discnumber"] = '3/12' + self.assertEqual(3, self.file.discnumber) + self.file.metadata["discnumber"] = '3 / 12' + self.assertEqual(3, self.file.discnumber) def test_set_acoustid_fingerprint(self): fingerprint = 'foo'