Skip to content

Commit

Permalink
Use new Xml class
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 20, 2024
1 parent 2eec82b commit 02aa354
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 293 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test:
poetry run tox

test_quick: autocomplete
poetry run tox -e py310,format,docs,lint
poetry run tox -e quick,format,docs,lint

test_real_binary:
pytest _test_real-binary.py
Expand Down
17 changes: 10 additions & 7 deletions mscxyz/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import lxml.etree as etree
from lxml.etree import _Element

from mscxyz import utils

if typing.TYPE_CHECKING:
from mscxyz.score import Score

Expand Down Expand Up @@ -102,7 +100,9 @@ def remap(self, remap_string: str) -> None:
new = pair.split(":")[1]
for element in self.elements:
if element.no == int(old):
utils.xml.find_safe(element.element, "no").text = str(int(new) - 1)
self.score.xml.find_safe("no", element.element).text = str(
int(new) - 1
)

def __extract_one_lyrics_verse(self, number: int, mscore: bool = False) -> None:
"""Extract a lyric verse by verse number.
Expand All @@ -116,9 +116,9 @@ def __extract_one_lyrics_verse(self, number: int, mscore: bool = False) -> None:
tag = element.element

if element.no != number:
utils.xml.remove(tag)
self.score.xml.remove(tag)
elif number != 1:
utils.xml.set_text(tag, "no", 0)
self.score.xml.set_text("no", 0, tag)

ext: str = "." + score.extension
new_name: str = str(score.path).replace(ext, "_" + str(number) + ext)
Expand Down Expand Up @@ -174,8 +174,11 @@ def fix_lyrics_verse(self, verse_number: int) -> None:
for element in self.elements:
if element.no == verse_number:
tag: _Element = element.element
element_text: _Element = utils.xml.find_safe(tag, "text")
text = utils.xml.get_text_safe(element_text)
element_text: _Element = self.score.xml.find_safe(
"text",
tag,
)
text = self.score.xml.get_text_safe(element_text)
element_syllabic: _Element = etree.Element("syllabic")
append_syllabic: bool = True
if text.endswith("-"):
Expand Down
22 changes: 11 additions & 11 deletions mscxyz/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,19 @@ def __init__(self, score: "Score") -> None:
self.xml_root = score.xml_root

def __get_element(self, field: str) -> _Element:
score_element: _Element = utils.xml.find_safe(self.xml_root, "Score")
element: _Element | None = utils.xml.xpath(
self.xml_root, '//metaTag[@name="' + field + '"]'
score_element: _Element = self.score.xml.find_safe("Score")
element: _Element | None = self.score.xml.xpath(
'//metaTag[@name="' + field + '"]'
)
if element is None:
element = utils.xml.create_sub_element(
element = self.score.xml.create_sub_element(
score_element, "metaTag", "", attrib={"name": field}
)
return element

def __get_text(self, field: str) -> str | None:
element: _Element | None = self.__get_element(field)
return utils.xml.get_text(element)
return self.score.xml.get_text(element)

def __set_text(self, field: str, value: str | None) -> None:
if value is None:
Expand Down Expand Up @@ -466,12 +466,12 @@ def __init__(self, score: "Score") -> None:
self.xml_root = score.xml_root
xpath = '/museScore/Score/Staff[@id="1"]'

vbox = utils.xml.xpath(self.xml_root, xpath + "/VBox")
vbox = self.score.xml.xpath(xpath + "/VBox")
if vbox is None:
vbox = lxml.etree.Element("VBox")
height = lxml.etree.SubElement(vbox, "height")
height.text = "10"
utils.xml.xpath_safe(self.xml_root, xpath).insert(0, vbox)
self.score.xml.xpath_safe(xpath).insert(0, vbox)
self.vbox = vbox

def __normalize_style_name(self, style: str) -> str:
Expand Down Expand Up @@ -532,17 +532,17 @@ def __create_text_element(self, style: str, text: str) -> None:
``Title`` or ``Composer`` or for v4 ``title`` or ``composer``.
:param text: The string inside the ``<text>`` tags.
"""
text_element: _Element = utils.xml.create_element("Text")
text_element: _Element = self.score.xml.create_element("Text")

if self.score.version_major in (2, 3):
style = style.title()
elif self.score.version_major == 4:
style = style.lower()

utils.xml.create_sub_element(
self.score.xml.create_sub_element(
text_element, "style", self.__normalize_style_name(style)
)
utils.xml.create_sub_element(text_element, "text", text)
self.score.xml.create_sub_element(text_element, "text", text)
self.vbox.append(text_element)

def __set_text(self, style: str, text: str | None) -> None:
Expand All @@ -567,7 +567,7 @@ def __remove_text_element(self, style: str) -> None:
:param style: The string inside the ``<style>`` tags, for example
``Title`` or ``Composer`` or for v4 ``title`` or ``composer``.
"""
utils.xml.remove(self.__get_element(style))
self.score.xml.remove(self.__get_element(style))
return None

# composer -> Composer
Expand Down
6 changes: 3 additions & 3 deletions mscxyz/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def save(self, new_dest: str = "", mscore: bool = False) -> None:
"//StaffText/text",
"//Jump/continueAt",
):
x: list[_Element] | None = utils.xml.xpathall(self.xml_root, xpath)
x: list[_Element] | None = self.xml.xpathall(xpath)
if x:
for tag in x:
if not tag.text:
Expand All @@ -269,15 +269,15 @@ def save(self, new_dest: str = "", mscore: bool = False) -> None:

if self.extension == "mscz":
xml_dest = self.xml_file
utils.xml.write(xml_dest, self.xml_root)
self.xml.write(xml_dest)

# Since MuseScore 4 the style is stored in a separate file.
if self.style_file:
element: _Element = lxml.etree.Element(
"museScore", {"version": str(self.version)}
)
element.append(self.style.parent_element)
utils.xml.write(self.style_file, element)
self.xml.write(self.style_file, element)

if self.extension == "mscz" and self.zip_container:
self.zip_container.save(dest)
Expand Down
15 changes: 8 additions & 7 deletions mscxyz/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ def __init__(self, score: "Score") -> None:
self.score = score

if self.score.style_file:
self.parent_element = utils.xml.find_safe(
utils.xml.read(self.score.style_file), "Style"
self.parent_element = self.score.xml.find_safe(
"Style",
self.score.xml.parse_file(self.score.style_file),
)
else:
element: _Element | None = self.score.xml_root.find("Score/Style")
Expand Down Expand Up @@ -313,8 +314,8 @@ def __get_text_style_element(self, name: str) -> _Element:
"This operation is only allowed for MuseScore 2 score files"
)

child: _Element | None = utils.xml.xpath(
self.score.xml_root, f'//TextStyle/name[contains(., "{name}")]'
child: _Element | None = self.score.xml.xpath(
f'//TextStyle/name[contains(., "{name}")]'
)

if child is not None:
Expand Down Expand Up @@ -392,7 +393,7 @@ def get_all_fonts(self) -> list[tuple[str, str]]:
output: list[tuple[str, str]] = []
for element in self.parent_element:
if "FontFace" in element.tag:
output.append((element.tag, utils.xml.get_text_safe(element)))
output.append((element.tag, self.score.xml.get_text_safe(element)))
return output

def print_all_font_faces(self) -> None:
Expand Down Expand Up @@ -519,7 +520,7 @@ def set_musical_text_font(self, font_face: str) -> StyleChanges:
return self.set("musicalTextFont", font_face)

def __set_parent_style_element(self, parent_style: _Element) -> None:
score_element: _Element = utils.xml.find_safe(self.score.xml_root, "Score")
score_element: _Element = self.score.xml.find_safe("Score")
score_element.insert(0, parent_style)
self.parent_element = parent_style

Expand Down Expand Up @@ -553,7 +554,7 @@ def load_styles_as_string(self, styles: str) -> None:
self.__set_parent_style_element(style[0])

def load_style_file(self, file: str | Path | TextIOWrapper) -> None:
style: _Element = utils.xml.read(file)
style: _Element = self.score.xml.parse_file(file)
self.__set_parent_style_element(style[0])

def reload(self, save: bool = False) -> Style:
Expand Down
Loading

0 comments on commit 02aa354

Please sign in to comment.