Skip to content

Commit 2d54763

Browse files
committed
Resolves #176 & #194. Use the first-found, not last-found include dir, and look in default app-install OpenSCAD libraries directories as well as per-user libraries dirs
1 parent 4715c82 commit 2d54763

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

solid/objects.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,14 +830,24 @@ def _openscad_library_paths() -> List[Path]:
830830
paths.append(Path(s))
831831

832832
default_paths = {
833-
'Linux': Path.home() / '.local/share/OpenSCAD/libraries',
834-
'Darwin': Path.home() / 'Documents/OpenSCAD/libraries',
835-
'Windows': Path('My Documents\OpenSCAD\libraries')
833+
'Linux': [
834+
Path.home() / '.local/share/OpenSCAD/libraries',
835+
Path('/usr/share/openscad/libraries')
836+
],
837+
'Darwin': [
838+
Path.home() / 'Documents/OpenSCAD/libraries',
839+
Path('/Applications/OpenSCAD.app/Contents/Resources/libraries')
840+
],
841+
'Windows': [
842+
Path('My Documents\OpenSCAD\libraries'),
843+
Path('c:\Program Files\OpenSCAD\libraries')
844+
],
836845
}
837846

838-
paths.append(default_paths[platform.system()])
847+
paths += default_paths.get(platform.system(), [])
839848
return paths
840849

850+
841851
def _find_library(library_name: PathStr) -> Path:
842852
result = Path(library_name)
843853

@@ -848,14 +858,17 @@ def _find_library(library_name: PathStr) -> Path:
848858
# print(f'Checking {f} -> {f.exists()}')
849859
if f.exists():
850860
result = f
861+
break
851862

852863
return result
853-
864+
854865
# use() & include() mimic OpenSCAD's use/include mechanics.
855866
# -- use() makes methods in scad_file_path.scad available to be called.
856867
# --include() makes those methods available AND executes all code in
857868
# scad_file_path.scad, which may have side effects.
858869
# Unless you have a specific need, call use().
870+
871+
859872
def use(scad_file_path: PathStr, use_not_include: bool = True, dest_namespace_dict: Dict = None):
860873
"""
861874
Opens scad_file_path, parses it for all usable calls,

solid/test/test_solidpython.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,38 @@ def test_import_scad(self):
233233
self.assertTrue(hasattr(examples, 'scad_to_include'))
234234
self.assertTrue(hasattr(examples.scad_to_include, 'steps'))
235235

236-
# TODO: we should test that:
236+
# Test that:
237237
# A) scad files in the designated OpenSCAD library directories
238238
# (path-dependent, see: solid.objects._openscad_library_paths())
239-
# are imported correctly. Not sure how to do this without writing
240-
# temp files to those directories. Seems like overkill for the moment
239+
# are imported correctly.
240+
# B) scad files in the designated app-install library directories
241+
from solid import objects
242+
lib_dirs = objects._openscad_library_paths()
243+
for i, ld in enumerate(lib_dirs):
244+
if ld.as_posix() == '.':
245+
continue
246+
if not ld.exists():
247+
continue
248+
temp_dirname = f'test_{i}'
249+
d = ld / temp_dirname
250+
d.mkdir(exist_ok=True)
251+
p = d / 'scad_to_include.scad'
252+
p.write_text(include_file.read_text())
253+
temp_file_str = f'{temp_dirname}/scad_to_include.scad'
254+
255+
mod = import_scad(temp_file_str)
256+
a = mod.steps(3)
257+
actual = scad_render(a)
258+
expected = f"use <{p.absolute()}>\n\n\nsteps(howmany = 3);"
259+
self.assertEqual(actual, expected,
260+
f'Unexpected file contents at {p} for dir: {ld}')
261+
262+
# remove generated file and directories
263+
p.unlink()
264+
d.rmdir()
241265

242266
def test_multiple_import_scad(self):
243-
# For Issue #172. Originally, multiple `import_scad()` calls would
267+
# For Issue #172. Originally, multiple `import_scad()` calls would
244268
# re-import the entire module, rather than cache a module after one use
245269
include_file = self.expand_scad_path("examples/scad_to_include.scad")
246270
mod1 = import_scad(include_file)

0 commit comments

Comments
 (0)