diff --git a/CHANGELOG.md b/CHANGELOG.md index 32191d780..f66c1adc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +* Moved private methods `_get_file_format` and `_mesh_import` to `compas_robots.resources` and made them public (`get_file_format` and `mesh_import`). + ### Removed diff --git a/src/compas_robots/resources/__init__.py b/src/compas_robots/resources/__init__.py index 54e0e4843..6b574a2d6 100644 --- a/src/compas_robots/resources/__init__.py +++ b/src/compas_robots/resources/__init__.py @@ -1,11 +1,15 @@ from __future__ import absolute_import +from .mesh_importer import get_file_format +from .mesh_importer import mesh_import from .basic import AbstractMeshLoader from .basic import DefaultMeshLoader from .basic import LocalPackageMeshLoader from .github import GithubPackageMeshLoader __all__ = [ + "get_file_format", + "mesh_import", "AbstractMeshLoader", "DefaultMeshLoader", "LocalPackageMeshLoader", diff --git a/src/compas_robots/resources/basic.py b/src/compas_robots/resources/basic.py index 3eff049c1..9cbecef24 100644 --- a/src/compas_robots/resources/basic.py +++ b/src/compas_robots/resources/basic.py @@ -9,9 +9,8 @@ except ImportError: from urlparse import urlparse -from compas.datastructures import Mesh - -SUPPORTED_FORMATS = ("obj", "stl", "ply") +from .mesh_importer import mesh_import +from .mesh_importer import get_file_format class AbstractMeshLoader(object): @@ -94,7 +93,7 @@ def can_load_mesh(self, url): return True # Only OBJ loader supports remote files atm - is_obj = _get_file_format(url) == "obj" + is_obj = get_file_format(url) == "obj" return scheme in ("http", "https") and is_obj def load_meshes(self, url, precision=None): @@ -115,7 +114,7 @@ def load_meshes(self, url, precision=None): List of meshes. """ url = self._get_mesh_url(url) - return _mesh_import(url, url, precision) + return mesh_import(url, url, precision) def _get_mesh_url(self, url): """Concatenates basepath directory to URL only if defined in the keyword arguments. @@ -141,14 +140,6 @@ def _get_mesh_url(self, url): return url -def _get_file_format(url): - # This could be much more elaborate - # with an actual header check - # and/or remote content-type check - file_extension = url.split(".")[-1].lower() - return file_extension - - class LocalPackageMeshLoader(AbstractMeshLoader): """Loads suport package resources stored locally. @@ -240,27 +231,8 @@ def load_meshes(self, url, precision=None): List of meshes. """ local_file = self._get_local_path(url) - return _mesh_import(url, local_file, precision) + return mesh_import(url, local_file, precision) def _get_local_path(self, url): _prefix, path = url.split(self.schema_prefix) return self.build_path(*path.split("/")) - - -def _mesh_import(name, file, precision=None): - """Internal function to load meshes using the correct loader. - - Name and file might be the same but not always, e.g. temp files.""" - file_extension = _get_file_format(name) - - if file_extension not in SUPPORTED_FORMATS: - raise NotImplementedError("Mesh type not supported: {}".format(file_extension)) - - if file_extension == "obj": - return [Mesh.from_obj(file, precision)] - elif file_extension == "stl": - return [Mesh.from_stl(file, precision)] - elif file_extension == "ply": - return [Mesh.from_ply(file, precision)] - - raise Exception diff --git a/src/compas_robots/resources/github.py b/src/compas_robots/resources/github.py index 76322c39d..6b2eeefc0 100644 --- a/src/compas_robots/resources/github.py +++ b/src/compas_robots/resources/github.py @@ -8,7 +8,7 @@ from urllib2 import urlopen from .basic import AbstractMeshLoader -from .basic import _mesh_import +from .mesh_importer import mesh_import class GithubPackageMeshLoader(AbstractMeshLoader): @@ -112,4 +112,4 @@ def load_meshes(self, url, precision=None): _prefix, path = url.split(self.schema_prefix) url = self.build_url(path) - return _mesh_import(url, url, precision) + return mesh_import(url, url, precision) diff --git a/src/compas_robots/resources/mesh_importer.py b/src/compas_robots/resources/mesh_importer.py new file mode 100644 index 000000000..35eb1f01e --- /dev/null +++ b/src/compas_robots/resources/mesh_importer.py @@ -0,0 +1,30 @@ +from compas.datastructures import Mesh + +SUPPORTED_FORMATS = ("obj", "stl", "ply") + + +def get_file_format(url): + # This could be much more elaborate + # with an actual header check + # and/or remote content-type check + file_extension = url.split(".")[-1].lower() + return file_extension + + +def mesh_import(name, file, precision=None): + """Internal function to load meshes using the correct loader. + + Name and file might be the same but not always, e.g. temp files.""" + file_extension = get_file_format(name) + + if file_extension not in SUPPORTED_FORMATS: + raise NotImplementedError("Mesh type not supported: {}".format(file_extension)) + + if file_extension == "obj": + return [Mesh.from_obj(file, precision)] + elif file_extension == "stl": + return [Mesh.from_stl(file, precision)] + elif file_extension == "ply": + return [Mesh.from_ply(file, precision)] + + raise Exception