Skip to content

Commit cfd9152

Browse files
authored
Merge pull request #12 from compas-dev/second-level-imports
Refactor mesh importer utility methods into public, second level API
2 parents ba1b24f + 5d7b8ab commit cfd9152

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313

14+
* Moved private methods `_get_file_format` and `_mesh_import` to `compas_robots.resources` and made them public (`get_file_format` and `mesh_import`).
15+
1416
### Removed
1517

1618

src/compas_robots/resources/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from __future__ import absolute_import
22

3+
from .mesh_importer import get_file_format
4+
from .mesh_importer import mesh_import
35
from .basic import AbstractMeshLoader
46
from .basic import DefaultMeshLoader
57
from .basic import LocalPackageMeshLoader
68
from .github import GithubPackageMeshLoader
79

810
__all__ = [
11+
"get_file_format",
12+
"mesh_import",
913
"AbstractMeshLoader",
1014
"DefaultMeshLoader",
1115
"LocalPackageMeshLoader",

src/compas_robots/resources/basic.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
except ImportError:
1010
from urlparse import urlparse
1111

12-
from compas.datastructures import Mesh
13-
14-
SUPPORTED_FORMATS = ("obj", "stl", "ply")
12+
from .mesh_importer import mesh_import
13+
from .mesh_importer import get_file_format
1514

1615

1716
class AbstractMeshLoader(object):
@@ -94,7 +93,7 @@ def can_load_mesh(self, url):
9493
return True
9594

9695
# Only OBJ loader supports remote files atm
97-
is_obj = _get_file_format(url) == "obj"
96+
is_obj = get_file_format(url) == "obj"
9897
return scheme in ("http", "https") and is_obj
9998

10099
def load_meshes(self, url, precision=None):
@@ -115,7 +114,7 @@ def load_meshes(self, url, precision=None):
115114
List of meshes.
116115
"""
117116
url = self._get_mesh_url(url)
118-
return _mesh_import(url, url, precision)
117+
return mesh_import(url, url, precision)
119118

120119
def _get_mesh_url(self, url):
121120
"""Concatenates basepath directory to URL only if defined in the keyword arguments.
@@ -141,14 +140,6 @@ def _get_mesh_url(self, url):
141140
return url
142141

143142

144-
def _get_file_format(url):
145-
# This could be much more elaborate
146-
# with an actual header check
147-
# and/or remote content-type check
148-
file_extension = url.split(".")[-1].lower()
149-
return file_extension
150-
151-
152143
class LocalPackageMeshLoader(AbstractMeshLoader):
153144
"""Loads suport package resources stored locally.
154145
@@ -240,27 +231,8 @@ def load_meshes(self, url, precision=None):
240231
List of meshes.
241232
"""
242233
local_file = self._get_local_path(url)
243-
return _mesh_import(url, local_file, precision)
234+
return mesh_import(url, local_file, precision)
244235

245236
def _get_local_path(self, url):
246237
_prefix, path = url.split(self.schema_prefix)
247238
return self.build_path(*path.split("/"))
248-
249-
250-
def _mesh_import(name, file, precision=None):
251-
"""Internal function to load meshes using the correct loader.
252-
253-
Name and file might be the same but not always, e.g. temp files."""
254-
file_extension = _get_file_format(name)
255-
256-
if file_extension not in SUPPORTED_FORMATS:
257-
raise NotImplementedError("Mesh type not supported: {}".format(file_extension))
258-
259-
if file_extension == "obj":
260-
return [Mesh.from_obj(file, precision)]
261-
elif file_extension == "stl":
262-
return [Mesh.from_stl(file, precision)]
263-
elif file_extension == "ply":
264-
return [Mesh.from_ply(file, precision)]
265-
266-
raise Exception

src/compas_robots/resources/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from urllib2 import urlopen
99

1010
from .basic import AbstractMeshLoader
11-
from .basic import _mesh_import
11+
from .mesh_importer import mesh_import
1212

1313

1414
class GithubPackageMeshLoader(AbstractMeshLoader):
@@ -112,4 +112,4 @@ def load_meshes(self, url, precision=None):
112112
_prefix, path = url.split(self.schema_prefix)
113113
url = self.build_url(path)
114114

115-
return _mesh_import(url, url, precision)
115+
return mesh_import(url, url, precision)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from compas.datastructures import Mesh
2+
3+
SUPPORTED_FORMATS = ("obj", "stl", "ply")
4+
5+
6+
def get_file_format(url):
7+
# This could be much more elaborate
8+
# with an actual header check
9+
# and/or remote content-type check
10+
file_extension = url.split(".")[-1].lower()
11+
return file_extension
12+
13+
14+
def mesh_import(name, file, precision=None):
15+
"""Internal function to load meshes using the correct loader.
16+
17+
Name and file might be the same but not always, e.g. temp files."""
18+
file_extension = get_file_format(name)
19+
20+
if file_extension not in SUPPORTED_FORMATS:
21+
raise NotImplementedError("Mesh type not supported: {}".format(file_extension))
22+
23+
if file_extension == "obj":
24+
return [Mesh.from_obj(file, precision)]
25+
elif file_extension == "stl":
26+
return [Mesh.from_stl(file, precision)]
27+
elif file_extension == "ply":
28+
return [Mesh.from_ply(file, precision)]
29+
30+
raise Exception

0 commit comments

Comments
 (0)