From 02aa354172713e02453bde9e080b91765037394a Mon Sep 17 00:00:00 2001 From: Josef Friedrich Date: Sat, 20 Jan 2024 16:05:27 +0100 Subject: [PATCH] Use new Xml class --- Makefile | 2 +- mscxyz/lyrics.py | 17 ++-- mscxyz/meta.py | 22 ++--- mscxyz/score.py | 6 +- mscxyz/style.py | 15 +-- mscxyz/utils.py | 211 +------------------------------------------ tests/test_lyrics.py | 10 +- tests/test_utils.py | 47 ---------- tox.ini | 9 +- 9 files changed, 46 insertions(+), 293 deletions(-) diff --git a/Makefile b/Makefile index 564c4c9..7eacfae 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/mscxyz/lyrics.py b/mscxyz/lyrics.py index b83f12a..7d3d364 100644 --- a/mscxyz/lyrics.py +++ b/mscxyz/lyrics.py @@ -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 @@ -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. @@ -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) @@ -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("-"): diff --git a/mscxyz/meta.py b/mscxyz/meta.py index 2344eaa..cf612e7 100644 --- a/mscxyz/meta.py +++ b/mscxyz/meta.py @@ -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: @@ -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: @@ -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 ```` 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: @@ -567,7 +567,7 @@ def __remove_text_element(self, style: str) -> None: :param style: The string inside the ``