Skip to content

Commit

Permalink
Move rels folder to constant
Browse files Browse the repository at this point in the history
I'm probably not going to move all strings to constants. But we can make a start, right.
  • Loading branch information
Ghostkeeper committed Oct 10, 2020
1 parent 1dc8d4c commit 6de4be1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion io_mesh_3mf/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def add_rels(self, rels_file):
f"Relationship file {rels_file.name} has malformed XML (position {e.position[0]}:{e.position[1]}).")
return # Skip this file.

for relationship_node in root.iterfind("rel:Relationship", RELS_NAMESPACES):
for relationship_node in root.iterfind(RELS_RELATIONSHIP_FIND, RELS_NAMESPACES):
try:
target = relationship_node.attrib["Target"]
namespace = relationship_node.attrib["Type"]
Expand Down
1 change: 1 addition & 0 deletions io_mesh_3mf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@
RELS_NAMESPACES = { # Namespaces used for the rels files.
"rel": RELS_NAMESPACE
}
RELS_RELATIONSHIP_FIND = "rel:Relationship"
11 changes: 6 additions & 5 deletions test/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def test_write_rels_empty(self):

file.seek(0)
root = xml.etree.ElementTree.ElementTree(file=file).getroot()
relationships = root.findall("rel:Relationship", namespaces=RELS_NAMESPACES)
relationships = root.findall(RELS_RELATIONSHIP_FIND, namespaces=RELS_NAMESPACES)
self.assertEqual(
len(relationships),
1,
Expand Down Expand Up @@ -328,7 +328,7 @@ def test_write_rels_different_annotations(self):

file.seek(0)
root = xml.etree.ElementTree.ElementTree(file=file).getroot()
relationships = root.findall("rel:Relationship", namespaces=RELS_NAMESPACES)
relationships = root.findall(RELS_RELATIONSHIP_FIND, namespaces=RELS_NAMESPACES)
self.assertEqual(
len(relationships),
1,
Expand All @@ -349,7 +349,7 @@ def test_write_rels_relationship(self):

file.seek(0)
root = xml.etree.ElementTree.ElementTree(file=file).getroot()
relationships = root.findall("rel:Relationship", namespaces=RELS_NAMESPACES)
relationships = root.findall(RELS_RELATIONSHIP_FIND, namespaces=RELS_NAMESPACES)
self.assertEqual(
len(relationships),
2,
Expand All @@ -373,14 +373,15 @@ def test_write_rels_different_source(self):
custom_file = io.BytesIO()
custom_file.close = lambda: None
# Return the correct file handle depending on which file is opened.
archive.open = lambda fname, *args, **kwargs: custom_file if fname == "3D/" + RELS_FOLDER + "/.rels" else root_file
archive.open = lambda fname, *args, **kwargs:\
custom_file if fname == "3D/" + RELS_FOLDER + "/.rels" else root_file

self.annotations.annotations["file.txt"] = {io_mesh_3mf.annotations.Relationship(namespace="nsp", source="3D/")}
self.annotations.write_rels(archive)

custom_file.seek(0)
root = xml.etree.ElementTree.ElementTree(file=custom_file).getroot()
relationships = root.findall("rel:Relationship", namespaces=RELS_NAMESPACES)
relationships = root.findall(RELS_RELATIONSHIP_FIND, namespaces=RELS_NAMESPACES)
self.assertEqual(len(relationships), 1, "Only the custom relationship got saved to this file.")
self.assertEqual(relationships[0].attrib["Target"], "/file.txt", "The target of the relationship is absolute.")
self.assertEqual(relationships[0].attrib["Type"], "nsp", "This is the namespace we added.")
Expand Down

0 comments on commit 6de4be1

Please sign in to comment.