Skip to content

Commit

Permalink
Improve version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Dec 31, 2023
1 parent 470fec2 commit c07f253
Show file tree
Hide file tree
Showing 9 changed files with 1,751 additions and 24 deletions.
39 changes: 30 additions & 9 deletions mscxyz/score_file_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ class MscoreXmlTree(MscoreFile):
:param relpath: The relative (or absolute) path of a MuseScore file.
"""

xml_tree: _ElementTree

version_major: int
"""The major MuseScore version, for example 2 or 3"""

Expand All @@ -243,19 +245,38 @@ class MscoreXmlTree(MscoreFile):
def __init__(self, relpath: str) -> None:
super(MscoreXmlTree, self).__init__(relpath)
try:
self.xml_tree: _ElementTree = lxml.etree.parse(self.loadpath)
self.xml_tree = lxml.etree.parse(self.loadpath)
except lxml.etree.XMLSyntaxError as e:
self.errors.append(e)
else:
self.xml_root: _Element = self.xml_tree.getroot()
musescore: _XPathObject = self.xml_tree.xpath("/museScore")
version = musescore[0].get("version")
self.version_major = int(version.split(".")[0])
self.version = float(version)

def add_sub_element(self, root_tag, tag, text: str) -> None:
tag: _Element = lxml.etree.SubElement(root_tag, tag)
tag.text = text
self.version = self.get_version()
self.version_major = int(self.version)

def get_version(self) -> float:
"""
Get the version number of the MuseScore file.
:return: The version number as a float.
:raises ValueError: If the version number cannot be retrieved.
"""
version: _XPathObject = self.xml_tree.xpath("number(/museScore[1]/@version)")
if isinstance(version, float):
return version
raise ValueError("Could not get version number")

def add_sub_element(self, root_tag: _Element, tag: str, text: str) -> _Element:
"""
Adds a sub-element to the given root element with the specified tag and text.
:param root_tag: The root element to which the sub-element will be added.
:param tag: The tag name of the sub-element.
:param text: The text content of the sub-element.
:return: The newly created sub-element.
"""
element: _Element = lxml.etree.SubElement(root_tag, tag)
element.text = text
return element

def strip_tags(self, *tag_names: str) -> None:
"""Delete / strip some tag names."""
Expand Down
Binary file added tests/files_mscore4/simple.mscz
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/files_mscore4/simple/META-INF/container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<rootfiles>
<rootfile full-path="score_style.mss"/>
<rootfile full-path="simple.mscx"/>
<rootfile full-path="Thumbnails/thumbnail.png"/>
<rootfile full-path="audiosettings.json"/>
<rootfile full-path="viewsettings.json"/>
</rootfiles>
</container>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 117 additions & 0 deletions tests/files_mscore4/simple/audiosettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"activeSoundProfile": "MuseScore Basic",
"aux": [
{
"out": {
"balance": 0,
"fxChain": {
"0": {
"active": true,
"chainOrder": 0,
"resourceMeta": {
"attributes": {
},
"hasNativeEditorSupport": true,
"id": "Muse Reverb",
"type": "muse_plugin",
"vendor": "Muse"
},
"unitConfiguration": {
}
}
},
"volumeDb": 0
},
"soloMuteState": {
"mute": false,
"solo": false
}
},
{
"out": {
"balance": 0,
"fxChain": {
},
"volumeDb": 0
},
"soloMuteState": {
"mute": false,
"solo": false
}
}
],
"master": {
"balance": 0,
"fxChain": {
},
"volumeDb": 0
},
"tracks": [
{
"in": {
"resourceMeta": {
"attributes": {
"playbackSetupData": "last.last.last",
"soundFontName": "MS Basic"
},
"hasNativeEditorSupport": false,
"id": "MS Basic",
"type": "fluid_soundfont",
"vendor": "Fluid"
},
"unitConfiguration": {
}
},
"instrumentId": "piano",
"out": {
"auxSends": [
{
"active": true,
"signalAmount": 0.30000001192092896
},
{
"active": true,
"signalAmount": 0.30000001192092896
}
],
"balance": 0,
"fxChain": {
},
"volumeDb": 0
},
"partId": "1",
"soloMuteState": {
"mute": false,
"solo": false
}
},
{
"in": {
"resourceMeta": {
"attributes": {
"playbackSetupData": "last.last.last",
"soundFontName": "MS Basic"
},
"hasNativeEditorSupport": false,
"id": "MS Basic",
"type": "fluid_soundfont",
"vendor": "Fluid"
},
"unitConfiguration": {
}
},
"instrumentId": "metronome",
"out": {
"balance": 0,
"fxChain": {
},
"volumeDb": 0
},
"partId": "999",
"soloMuteState": {
"mute": false,
"solo": false
}
}
]
}
Loading

0 comments on commit c07f253

Please sign in to comment.