Skip to content

Commit

Permalink
bug fix (If xsd_check is False) + tree.Tree.get_children_of_type() is…
Browse files Browse the repository at this point in the history
… added
  • Loading branch information
alexgorji committed Sep 14, 2023
1 parent a80b037 commit 6fa1399
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions musicxml/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Version 1.3.3

``tree.Tree.get_children_of_type(type_)`` added
Bug fix: `:obj:`~musicxml.xmlelement.xmlelement._final_checks()`` runs only if xsd_check is set to True.
74 changes: 74 additions & 0 deletions musicxml/tests/test_xmlelement/test_xsd_false.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from unittest import TestCase

from musicxml.exceptions import XMLElementChildrenRequired
from musicxml.xmlelement.xmlelement import *


class TestXSDFalse(TestCase):
def test_xml_element_with_xsd_false_simple(self):
xml_score_part = XMLScorePart()
with self.assertRaises(XMLElementChildrenRequired):
xml_score_part.to_string()

xml_score_part.xsd_check = False
assert xml_score_part.to_string() == '<score-part />\n'

def test_xml_part_list_xsd_false(self):
xml_part_list = XMLPartList()
with self.assertRaises(XMLElementChildrenRequired):
xml_part_list.to_string()
xml_part_list = XMLPartList(xsd_check=False)
# xml_part_list.xsd_check = False
assert xml_part_list.to_string() == '<part-list />\n'

xml_part_list.add_child(XMLScorePart(id='p-1'))

pg = xml_part_list.add_child(XMLPartGroup(number='1', type='start'))
pg.add_child(XMLGroupSymbol('square'))
pg.add_child(XMLGroupBarline('yes'))
xml_part_list.add_child(XMLScorePart(id='p-2'))

pg = xml_part_list.add_child(XMLPartGroup(number='2', type='start'))
pg.add_child(XMLGroupSymbol('bracket'))
pg.add_child(XMLGroupBarline('yes'))
xml_part_list.add_child(XMLScorePart(id='p-3'))

xml_part_list.add_child(XMLPartGroup(number='1', type='stop'))
xml_part_list.add_child(XMLPartGroup(number='2', type='stop'))
xml_part_list.add_child(XMLScorePart(id='p-4'))

xml_part_list.add_child(XMLScorePart(id='p-5'))

expected = """<part-list>
<score-part id="p-1" />
<part-group number="1" type="start">
<group-symbol>square</group-symbol>
<group-barline>yes</group-barline>
</part-group>
<score-part id="p-2" />
<part-group number="2" type="start">
<group-symbol>bracket</group-symbol>
<group-barline>yes</group-barline>
</part-group>
<score-part id="p-3" />
<part-group number="1" type="stop" />
<part-group number="2" type="stop" />
<score-part id="p-4" />
<score-part id="p-5" />
</part-list>
"""

assert xml_part_list.to_string() == expected

def test_xml_part_list_init_xsd_false(self):
xml_part_list = XMLPartList(xsd_check=False)
assert xml_part_list.to_string() == '<part-list />\n'

def test_xml_part_xsd_false_with_parent_xsd_true(self):
xml_score = XMLScorePartwise()
xml_part = xml_score.add_child(XMLPart(id='p1'))
xml_part.add_child(XMLMeasure(number='1'))
xml_part_list = XMLPartList(xsd_check=False)
xml_score.add_child(xml_part_list)
assert xml_part_list.to_string() == '<part-list />\n'
print(xml_score.to_string())
13 changes: 7 additions & 6 deletions musicxml/xmlelement/xmlelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ def _create_et_xml_element(self):
ET.indent(self._et_xml_element, space=" ", level=self.level)

def _final_checks(self, intelligent_choice=False):
self._check_required_value()
if self._child_container_tree:
required_children = self._child_container_tree.get_required_element_names(intelligent_choice=intelligent_choice)
if required_children:
raise XMLElementChildrenRequired(f"{self.__class__.__name__} requires at least following children: {required_children}")
if self.xsd_check:
self._check_required_value()
if self._child_container_tree:
required_children = self._child_container_tree.get_required_element_names(intelligent_choice=intelligent_choice)
if required_children:
raise XMLElementChildrenRequired(f"{self.__class__.__name__} requires at least following children: {required_children}")

self._check_required_attributes()
self._check_required_attributes()

for child in self.get_children():
child._final_checks(intelligent_choice=intelligent_choice)
Expand Down
7 changes: 7 additions & 0 deletions tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ def get_children(self) -> List['Tree']:
"""
return self._children

def get_children_of_type(self, type_) -> List['Tree']:
"""
:return: list of added children of type_.
:rtype: List[:obj:`~tree.tree.Tree`]
"""
return [ch for ch in self.get_children() if isinstance(ch, type_)]

def get_coordinates_in_tree(self) -> str:
"""
:return: 0 for ``root``. 1, 2, ... for layer 1. Other layers: x.y.z.... Example: 3.2.2 => third child of secod child of second child
Expand Down

0 comments on commit 6fa1399

Please sign in to comment.