Skip to content

Commit

Permalink
Fix style clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Feb 4, 2024
1 parent e89e9f1 commit 5a35588
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
9 changes: 7 additions & 2 deletions mscxyz/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def __init__(self, src: str | Path) -> None:
if self.extension == "mscz" and self.version_major == 4 and self.zip_container:
self.style_file = self.zip_container.score_style_file

# Initialize the Style class to embed the style file into the score file.
self.style

@property
def xml_string(self) -> str:
return self.xml.tostring(self.xml_root)
Expand Down Expand Up @@ -267,6 +270,7 @@ def save(self, new_dest: str = "", mscore: bool = False) -> None:
and self.__xml_string_initial == self.xml_string
):
return

if new_dest:
dest: str = new_dest
else:
Expand All @@ -276,16 +280,17 @@ def save(self, new_dest: str = "", mscore: bool = False) -> None:

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

# Since MuseScore 4 the style is stored in a separate file.
if self.style_file:
element = self.xml.create_element(
"museScore", {"version": str(self.version)}
)

element.append(self.style.parent_element)
self.xml.write(self.style_file, element)
self.xml.remove_tags("./Score/Style")

self.xml.write(xml_dest)

if self.extension == "mscz" and self.zip_container:
self.zip_container.save(dest)
Expand Down
28 changes: 13 additions & 15 deletions mscxyz/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,25 @@ def xml(self) -> XmlManipulator:

def __init__(self, score: "Score") -> None:
self.score = score

style_root_score: _Element | None = self.xml.find("Score/Style")
if style_root_score is None:
style_root_score = self.__create_parent_style()

parent_element = self.__get_parent_element()
if self.score.style_file:
self.parent_element = self.xml.find_safe(
"Style",
self.xml.parse_file(self.score.style_file),
)
self.xml.replace(style_root_score, self.parent_element)
self.xml.replace(parent_element, self.parent_element)
else:
self.parent_element = style_root_score
self.parent_element = parent_element

def __create_parent_style(self) -> _Element:
def __get_parent_element(self) -> _Element:
"""
Create the parent style element.
Get the parent style element. The element is created if it doesn’t exists.
:return: The created parent style element.
"""
parent_element = self.xml.find("Score/Style")
if parent_element is not None:
return parent_element
score: _Element | None = self.xml.find("Score")
if score is None:
raise ValueError("The score file has no <Score> tag.")
Expand Down Expand Up @@ -295,7 +294,6 @@ def clean(self) -> None:
"""Remove the style, the layout breaks, the stem directions and the
``font``, ``b``, ``i``, ``pos``, ``offset`` tags"""
self.xml.remove_tags(
"./Score/Style",
".//LayoutBreak",
".//StemDirection",
".//font",
Expand All @@ -304,6 +302,7 @@ def clean(self) -> None:
".//pos",
".//offset",
)
self.parent_element.clear()

def get(self, style_name: str, raise_exception: bool = True) -> str | None:
"""
Expand Down Expand Up @@ -588,9 +587,8 @@ def musical_text_font(self, font_face: str) -> None:
font_face.replace("Emmentaler", "MScore").replace("Gonville", "Gootville"),
)

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

def load_styles_as_string(self, styles: str) -> None:
Expand Down Expand Up @@ -620,11 +618,11 @@ def load_styles_as_string(self, styles: str) -> None:
styles = f'<?xml version="1.0"?>\n<museScore version="{self.score.version}"><Style>{styles}</Style></museScore>'

style = self.xml.parse_string(styles)
self.__set_parent_style_element(style[0])
self.__replace_parent_element(style[0])

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

def reload(self, save: bool = False) -> Style:
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_method_clean(self) -> None:
score.save()
score = Score(str(score.path))
xml_tree = score.xml_root
assert xml_tree.xpath("/museScore/Score/Style") == []
assert score.style.styles == []
assert xml_tree.xpath("//LayoutBreak") == []
assert xml_tree.xpath("//StemDirection") == []
assert xml_tree.xpath("//font") == []
Expand Down Expand Up @@ -346,7 +346,7 @@ def test_clean_add_style(self) -> None:
.append_score("score.mscz")
.score()
)
assert "<musicalSymbolFont>MuseJazz</musicalSymbolFont>" in score.read_as_text()
assert "<musicalSymbolFont>MuseJazz</musicalSymbolFont>" in score.xml_string


def test_load_style_file() -> None:
Expand Down

0 comments on commit 5a35588

Please sign in to comment.