From c10bfab055a3236986dab77400ec2c5ffad57df4 Mon Sep 17 00:00:00 2001 From: fosfrancesco Date: Fri, 8 Sep 2023 12:55:36 +0200 Subject: [PATCH 1/2] fixed bug in case of match files load_score works based on the extension --- partitura/io/__init__.py | 52 +++++++++++++--------------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/partitura/io/__init__.py b/partitura/io/__init__.py index c142a681..be42804e 100644 --- a/partitura/io/__init__.py +++ b/partitura/io/__init__.py @@ -35,7 +35,7 @@ def load_score(filename: PathLike, force_note_ids="keep") -> Score: """ Load a score format supported by partitura. Currently the accepted formats are MusicXML, MIDI, Kern and MEI, plus all formats for which - MuseScore has support import-support (requires MuseScore 3). + MuseScore has support import-support (requires MuseScore 4 or 3). Parameters ---------- @@ -54,20 +54,15 @@ def load_score(filename: PathLike, force_note_ids="keep") -> Score: scr: :class:`partitura.score.Score` A score instance. """ - part = None - - # Catch exceptions - exception_dictionary = dict() - # Load MusicXML - try: + extension = filename.split(".")[-1].lower() + if extension in ("mxl", "xml", "musicxml"): + # Load MusicXML return load_musicxml( filename=filename, force_note_ids=force_note_ids, ) - except Exception as e: - exception_dictionary["MusicXML"] = e - # Load MIDI - try: + elif extension in ["midi", "mid"]: + # Load MIDI if (force_note_ids is None) or (not force_note_ids): assign_note_ids = False else: @@ -76,44 +71,29 @@ def load_score(filename: PathLike, force_note_ids="keep") -> Score: filename=filename, assign_note_ids=assign_note_ids, ) - except Exception as e: - exception_dictionary["MIDI"] = e - # Load MEI - try: + elif extension in ["mei"]: + # Load MEI return load_mei(filename=filename) - except Exception as e: - exception_dictionary["MEI"] = e - # Load Kern - try: + elif extension in ["kern", "krn"]: return load_kern( filename=filename, force_note_ids=force_note_ids, ) - except Exception as e: - exception_dictionary["Kern"] = e - # Load MuseScore - try: + elif extension in ["mscz", "mscx", "musescore", "mscore", "ms"]: + # Load MuseScore return load_via_musescore( filename=filename, force_note_ids=force_note_ids, ) - except Exception as e: - exception_dictionary["MuseScore"] = e - try: + elif extension in ["match"]: # Load the score information from a Matchfile - _, _, part = load_match( + _, _, score = load_match( filename=filename, create_score=True, ) - - except Exception as e: - exception_dictionary["matchfile"] = e - if part is None: - for score_format, exception in exception_dictionary.items(): - print(f"Error loading score as {score_format}:") - print(exception) - - raise NotSupportedFormatError + return score + else: + raise NotSupportedFormatError(f"{filename.split('.')[-1].lower()} file extension is not supported. If this should be supported, consider editing partitura/io/__init__.py file") def load_score_as_part(filename: PathLike) -> Part: From 9ce24be24d16b3b1a67370ccfcd17ceccc3bf1f0 Mon Sep 17 00:00:00 2001 From: fosfrancesco Date: Tue, 12 Sep 2023 16:39:53 +0200 Subject: [PATCH 2/2] use os function to get the file extension --- partitura/io/__init__.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/partitura/io/__init__.py b/partitura/io/__init__.py index be42804e..2e67cef2 100644 --- a/partitura/io/__init__.py +++ b/partitura/io/__init__.py @@ -4,6 +4,7 @@ This module contains methods for importing and exporting symbolic music formats. """ from typing import Union +import os from .importmusicxml import load_musicxml from .importmidi import load_score_midi, load_performance_midi @@ -54,14 +55,15 @@ def load_score(filename: PathLike, force_note_ids="keep") -> Score: scr: :class:`partitura.score.Score` A score instance. """ - extension = filename.split(".")[-1].lower() - if extension in ("mxl", "xml", "musicxml"): + + extension = os.path.splitext(filename)[-1].lower() + if extension in (".mxl", ".xml", ".musicxml"): # Load MusicXML return load_musicxml( filename=filename, force_note_ids=force_note_ids, ) - elif extension in ["midi", "mid"]: + elif extension in [".midi", ".mid"]: # Load MIDI if (force_note_ids is None) or (not force_note_ids): assign_note_ids = False @@ -71,21 +73,21 @@ def load_score(filename: PathLike, force_note_ids="keep") -> Score: filename=filename, assign_note_ids=assign_note_ids, ) - elif extension in ["mei"]: + elif extension in [".mei"]: # Load MEI return load_mei(filename=filename) - elif extension in ["kern", "krn"]: + elif extension in [".kern", ".krn"]: return load_kern( filename=filename, force_note_ids=force_note_ids, ) - elif extension in ["mscz", "mscx", "musescore", "mscore", "ms"]: + elif extension in [".mscz", ".mscx", ".musescore", ".mscore", ".ms"]: # Load MuseScore return load_via_musescore( filename=filename, force_note_ids=force_note_ids, ) - elif extension in ["match"]: + elif extension in [".match"]: # Load the score information from a Matchfile _, _, score = load_match( filename=filename, @@ -93,7 +95,7 @@ def load_score(filename: PathLike, force_note_ids="keep") -> Score: ) return score else: - raise NotSupportedFormatError(f"{filename.split('.')[-1].lower()} file extension is not supported. If this should be supported, consider editing partitura/io/__init__.py file") + raise NotSupportedFormatError(f"{extension} file extension is not supported. If this should be supported, consider editing partitura/io/__init__.py file") def load_score_as_part(filename: PathLike) -> Part: