Skip to content

Commit c2b4a82

Browse files
committed
[property] Refactor export_leaf method
Refactors the Property.export_leaf method and the corresponding tests. When exporting a document from a Property the direct parent now also exports all Properties and not just the Property that starts the export.
1 parent fa8436a commit c2b4a82

File tree

2 files changed

+35
-56
lines changed

2 files changed

+35
-56
lines changed

odml/property.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -841,26 +841,16 @@ def pprint(self, indent=2, max_length=80, current_depth=-1):
841841

842842
def export_leaf(self):
843843
"""
844-
Export only the path from this property to the root.
845-
Include all properties of parent sections.
846-
847-
:returns: cloned odml tree to the root of the current document.
848-
"""
849-
curr = self.parent
850-
par = self.parent
851-
child = self.parent
852-
853-
while curr is not None:
854-
par = curr.clone(children=False, keep_id=True)
855-
if curr != self.parent:
856-
par.append(child)
857-
if hasattr(curr, 'properties'):
858-
if curr == self.parent:
859-
par.append(self.clone(keep_id=True))
860-
else:
861-
for prop in curr.properties:
862-
par.append(prop.clone(keep_id=True))
863-
child = par
864-
curr = curr.parent
865-
866-
return par
844+
Export the path including all direct parents from this Property
845+
to the root of the document. Section properties are included,
846+
Subsections are not included.
847+
848+
:returns: Cloned odml tree to the root of the current document.
849+
"""
850+
export = self
851+
if export.parent:
852+
# Section.export_leaf will take care of the full export and
853+
# include the current Property.
854+
export = export.parent.export_leaf()
855+
856+
return export

test/test_property.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -760,39 +760,28 @@ def test_comparison(self):
760760

761761
def test_export_leaf(self):
762762
doc = Document()
763-
first = doc.create_section("first")
764-
second = first.create_section("second")
765-
first.create_section("third")
766-
767-
name = "prop1"
768-
values = [1.3]
769-
first.create_property(name, value=values)
770-
771-
name = "prop2"
772-
values = ["words"]
773-
second.create_property(name, value=values)
774-
775-
name = "prop3"
776-
values = ["a", "b"]
777-
second.create_property(name, value=values)
778-
779-
name = "prop4"
780-
values = [3]
781-
second.create_property(name, value=values)
782-
783-
name = "prop5"
784-
values = ["abc"]
785-
first.create_property(name, value=values)
786-
787-
ex1 = first.properties["prop1"].export_leaf()
788-
self.assertEqual(len(ex1['first'].properties), 1)
789-
self.assertEqual(len(ex1['first'].sections), 0)
790-
791-
ex2 = second.properties["prop2"].export_leaf()
792-
self.assertEqual(len(ex2.sections), 1)
793-
self.assertEqual(len(ex2['first'].properties), 2)
794-
self.assertEqual(len(ex2['first'].sections), 1)
795-
self.assertEqual(len(ex2['first']['second'].properties), 1)
763+
764+
sec_a_name = "first"
765+
sec_b_name = "second"
766+
first = doc.create_section(sec_a_name)
767+
second = first.create_section(sec_b_name)
768+
_ = first.create_section("third")
769+
770+
prop_aa = first.create_property("prop1", value=[1.3])
771+
_ = first.create_property("prop5", value=["abc"])
772+
prop_ba = second.create_property("prop2", value=["words"])
773+
_ = second.create_property("prop3", value=["a", "b"])
774+
_ = second.create_property("prop4", value=[3])
775+
776+
export_doc = prop_aa.export_leaf()
777+
self.assertEqual(len(export_doc[sec_a_name].properties), 2)
778+
self.assertEqual(len(export_doc[sec_a_name].sections), 0)
779+
780+
export_doc = prop_ba.export_leaf()
781+
self.assertEqual(len(export_doc.sections), 1)
782+
self.assertEqual(len(export_doc[sec_a_name].properties), 2)
783+
self.assertEqual(len(export_doc[sec_a_name].sections), 1)
784+
self.assertEqual(len(export_doc[sec_a_name][sec_b_name].properties), 3)
796785

797786
def test_values_cardinality(self):
798787
doc = Document()

0 commit comments

Comments
 (0)