Skip to content

Commit

Permalink
Use Xml class
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 21, 2024
1 parent 78efd09 commit f08e844
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
9 changes: 3 additions & 6 deletions mscxyz/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from pathlib import Path
from typing import Any

import lxml
import lxml.etree
import tmep
from lxml.etree import _Element

Expand Down Expand Up @@ -184,7 +182,7 @@ def __get_element(self, field: str) -> _Element:
'//metaTag[@name="' + field + '"]'
)
if element is None:
element = self.score.xml.create_sub_element(
_, element = self.score.xml.create_sub_element(
score_element, "metaTag", "", attrib={"name": field}
)
return element
Expand Down Expand Up @@ -468,9 +466,8 @@ def __init__(self, score: "Score") -> None:

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"
vbox, _ = self.score.xml.create_sub_element("VBox", "height", "10")

self.score.xml.xpath_safe(xpath).insert(0, vbox)
self.vbox = vbox

Expand Down
34 changes: 18 additions & 16 deletions mscxyz/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ def replace(old: _Element, new: _Element) -> None:
if parent is not None:
parent.replace(old, new)

@staticmethod
def create_element(tag_name: str) -> _Element:
return Element(tag_name)

@staticmethod
def create_sub_element(
parent: _Element | str,
tag_name: str,
text: Optional[str] = None,
attrib: Optional[_DictAnyStr] = None,
) -> tuple[_Element, _Element]:
if isinstance(parent, str):
parent = Xml.create_element(parent)
sub_element: _Element = SubElement(parent, tag_name, attrib=attrib)
if text:
sub_element.text = text
return (parent, sub_element)

@staticmethod
def remove(element: _Element | None) -> None:
"""
Expand All @@ -290,22 +308,6 @@ def remove(element: _Element | None) -> None:

parent.remove(element)

@staticmethod
def create_element(tag_name: str) -> _Element:
return Element(tag_name)

@staticmethod
def create_sub_element(
parent: _Element,
tag_name: str,
text: Optional[str] = None,
attrib: Optional[_DictAnyStr] = None,
) -> _Element:
element: _Element = SubElement(parent, tag_name, attrib=attrib)
if text:
element.text = text
return element

def remove_tags(self, *element_paths: str) -> Xml:
"""
:param element_path: A `element path expression
Expand Down
9 changes: 7 additions & 2 deletions tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_xpath() -> None:
assert element is None


class TestXpathSave:
class TestMethodXpathSave:
def test_xpath_safe(self) -> None:
element = xml.xpath_safe(".//Score")
assert element.tag == "Score"
Expand Down Expand Up @@ -74,7 +74,12 @@ def test_xml_write(tmp_path: Path) -> None:
)


class TestRemove:
def test_method_create_sub_element() -> None:
element, _ = xml.create_sub_element("parent", "child", "test")
assert "<parent><child>test</child></parent>" in xml.tostring(element)


class TestMethodRemoveTags:
def test_element_with_childs(self, custom_xml: Xml) -> None:
assert (
"<root><d>some text<e/></d></root>"
Expand Down

0 comments on commit f08e844

Please sign in to comment.