Skip to content

Commit

Permalink
Move _rels folder name to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghostkeeper committed Oct 5, 2020
1 parent ed16960 commit 1dc8d4c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 56 deletions.
15 changes: 3 additions & 12 deletions io_mesh_3mf/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@
import urllib.parse # To parse relative target paths in relationships.
import xml.etree.ElementTree # To parse the relationships files.

from .constants import (
CONTENT_TYPES_NAMESPACE, # Namespace for writing content types files.
RELS_NAMESPACE, # Namespace for writing relationships files.
RELS_NAMESPACES, # Namespaces for reading relationships files.
CONTENT_TYPES_LOCATION, # Location of content types file.
MODEL_LOCATION, # Target of default relationship.
MODEL_REL, # Known relationship.
RELS_MIMETYPE, # Known content types.
MODEL_MIMETYPE
)
from .constants import *


# These are the different types of annotations we can store.
Expand Down Expand Up @@ -79,7 +70,7 @@ def add_rels(self, rels_file):
"""
# Relationships are evaluated relative to the path that the _rels folder around the .rels file is on. If any.
base_path = os.path.dirname(rels_file.name) + "/"
if os.path.basename(os.path.dirname(base_path)) == "_rels":
if os.path.basename(os.path.dirname(base_path)) == RELS_FOLDER:
base_path = os.path.dirname(os.path.dirname(base_path)) + "/"

try:
Expand Down Expand Up @@ -197,7 +188,7 @@ def write_rels(self, archive):
document = xml.etree.ElementTree.ElementTree(root)

# Write that XML document to a file.
rels_file = source + "_rels/.rels" # _rels folder in the "source" folder.
rels_file = source + RELS_FOLDER + "/.rels" # _rels folder in the "source" folder.
with archive.open(rels_file, 'w') as f:
document.write(f, xml_declaration=True, encoding='UTF-8', default_namespace=RELS_NAMESPACE)

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 @@ -25,6 +25,7 @@
# Default storage locations.
MODEL_LOCATION = "3D/3dmodel.model" # Conventional location for the 3D model data.
CONTENT_TYPES_LOCATION = "[Content_Types].xml" # Location of the content types definition.
RELS_FOLDER = "_rels" # Folder name to store relationships files in.

# Relationship types.
MODEL_REL = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel" # Relationship type of 3D models.
Expand Down
7 changes: 1 addition & 6 deletions io_mesh_3mf/export_3mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
import zipfile # To write zip archives, the shell of the 3MF file.

from .annotations import Annotations # To store file annotations
from .constants import (
conflicting_mustpreserve_contents,
MODEL_LOCATION,
MODEL_NAMESPACE,
MODEL_DEFAULT_UNIT
)
from .constants import *
from .metadata import Metadata # To store metadata from the Blender scene into the 3MF file.
from .unit_conversions import blender_to_metre, threemf_to_metre

Expand Down
10 changes: 1 addition & 9 deletions io_mesh_3mf/import_3mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,7 @@
import zipfile # To read the 3MF files which are secretly zip archives.

from .annotations import Annotations, ContentType, Relationship # To use annotations to decide on what to import.
from .constants import ( # Constants associated with the 3MF file format.
conflicting_mustpreserve_contents,
CONTENT_TYPES_LOCATION,
MODEL_DEFAULT_UNIT,
MODEL_MIMETYPE,
MODEL_NAMESPACES,
RELS_MIMETYPE,
SUPPORTED_EXTENSIONS
)
from .constants import *
from .metadata import MetadataEntry, Metadata # To store and serialize metadata.
from .unit_conversions import blender_to_metre, threemf_to_metre # To convert to Blender's units.

Expand Down
25 changes: 8 additions & 17 deletions test/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@
bpy_extras.io_utils.ImportHelper = MockImportHelper
bpy_extras.io_utils.ExportHelper = MockExportHelper
import io_mesh_3mf.annotations # Now finally we can import the unit under test.
from io_mesh_3mf.constants import (
CONTENT_TYPES_NAMESPACES,
RELS_NAMESPACE,
RELS_NAMESPACES,
THUMBNAIL_REL,
MODEL_LOCATION,
MODEL_REL,
MODEL_MIMETYPE,
RELS_MIMETYPE
)
from io_mesh_3mf.constants import *


class TestAnnotations(unittest.TestCase):
Expand Down Expand Up @@ -83,7 +74,7 @@ def test_add_rels_empty(self):
"""
# Construct an empty rels file.
root = xml.etree.ElementTree.Element(f"{{{RELS_NAMESPACE}}}Relationships")
rels_file = self.xml_to_filestream(root, "_rels/.rels")
rels_file = self.xml_to_filestream(root, RELS_FOLDER + "/.rels")

self.annotations.add_rels(rels_file)

Expand All @@ -102,7 +93,7 @@ def test_add_rels_relationship(self):
"Target": "/path/to/thumbnail.png",
"Type": THUMBNAIL_REL
})
rels_file = self.xml_to_filestream(root, "_rels/.rels")
rels_file = self.xml_to_filestream(root, RELS_FOLDER + "/.rels")

self.annotations.add_rels(rels_file)

Expand All @@ -124,7 +115,7 @@ def test_add_rels_duplicates(self):
"Target": "/path/to/thumbnail.png",
"Type": THUMBNAIL_REL
})
rels_file = self.xml_to_filestream(root, "_rels/.rels")
rels_file = self.xml_to_filestream(root, RELS_FOLDER + "/.rels")

self.annotations.add_rels(rels_file)
rels_file.seek(0)
Expand Down Expand Up @@ -155,7 +146,7 @@ def test_add_rels_missing_attributes(self):
# Missing target.
"Type": THUMBNAIL_REL
})
rels_file = self.xml_to_filestream(root, "_rels/.rels")
rels_file = self.xml_to_filestream(root, RELS_FOLDER + "/.rels")

self.annotations.add_rels(rels_file)

Expand All @@ -175,7 +166,7 @@ def test_add_rels_base_path(self):
"Type": THUMBNAIL_REL
})
# The _rels directory is NOT in the root of the archive.
rels_file = self.xml_to_filestream(root, "metadata/_rels/.rels")
rels_file = self.xml_to_filestream(root, "metadata/" + RELS_FOLDER + "/.rels")

self.annotations.add_rels(rels_file)

Expand Down Expand Up @@ -247,7 +238,7 @@ def test_add_content_types_known(self):
model_file = io.BytesIO()
model_file.name = "3D/3dmodel.model"
rels_file = io.BytesIO()
rels_file.name = "_rels/.rels"
rels_file.name = RELS_FOLDER + "/.rels"
files_by_content_type = {
MODEL_MIMETYPE: {model_file},
RELS_MIMETYPE: {rels_file}
Expand Down Expand Up @@ -382,7 +373,7 @@ 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/.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)
Expand Down
8 changes: 2 additions & 6 deletions test/export_3mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
bpy_extras.io_utils.ExportHelper = MockExportHelper
bpy_extras.node_shader_utils.PrincipledBSDFWrapper = MockPrincipledBSDFWrapper
import io_mesh_3mf.export_3mf # Now we may safely import the unit under test.
from io_mesh_3mf.constants import (
CONTENT_TYPES_LOCATION,
MODEL_NAMESPACE,
MODEL_NAMESPACES
)
from io_mesh_3mf.constants import *
from io_mesh_3mf.metadata import MetadataEntry


Expand Down Expand Up @@ -71,7 +67,7 @@ def test_create_archive(self):

self.assertSetEqual(
set(archive.namelist()),
{"_rels/.rels", CONTENT_TYPES_LOCATION},
{RELS_FOLDER + "/.rels", CONTENT_TYPES_LOCATION},
"There may only be these two files.")
finally:
if file_path is not None:
Expand Down
7 changes: 1 addition & 6 deletions test/import_3mf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@
bpy_extras.io_utils.ImportHelper = MockImportHelper
bpy_extras.io_utils.ExportHelper = MockExportHelper
import io_mesh_3mf.import_3mf # Now we may safely import the unit under test.
from io_mesh_3mf.constants import (
CONTENT_TYPES_LOCATION,
MODEL_NAMESPACE,
MODEL_MIMETYPE,
RELS_MIMETYPE
)
from io_mesh_3mf.constants import *
# To compare the metadata objects created by the code under test.
from io_mesh_3mf.metadata import Metadata, MetadataEntry

Expand Down

0 comments on commit 1dc8d4c

Please sign in to comment.