Skip to content

Commit 7f65b21

Browse files
committed
- I'm pretty sure this is what it is supposed to be - I would suggest to rename the function to `resolve_scad_filename`
1 parent 7a0f612 commit 7f65b21

File tree

4 files changed

+55
-43
lines changed

4 files changed

+55
-43
lines changed

solid/helpers.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pathlib import Path
2+
from typing import List, Union
3+
PathStr = Union[Path, str]
4+
5+
6+
def _openscad_library_paths() -> List[Path]:
7+
"""
8+
Return system-dependent OpenSCAD library paths or paths defined in os.environ['OPENSCADPATH']
9+
"""
10+
import platform
11+
import os
12+
import re
13+
14+
paths = [Path('.')]
15+
16+
user_path = os.environ.get('OPENSCADPATH')
17+
if user_path:
18+
for s in re.split(r'\s*[;:]\s*', user_path):
19+
paths.append(Path(s))
20+
21+
default_paths = {
22+
'Linux': Path.home() / '.local/share/OpenSCAD/libraries',
23+
'Darwin': Path.home() / 'Documents/OpenSCAD/libraries',
24+
'Windows': Path('My Documents\OpenSCAD\libraries')
25+
}
26+
27+
paths.append(default_paths[platform.system()])
28+
return paths
29+
30+
def _find_library(library_name: PathStr) -> Path:
31+
result = Path(library_name)
32+
33+
if not result.is_absolute():
34+
paths = _openscad_library_paths()
35+
for p in paths:
36+
f = p / result
37+
# print(f'Checking {f} -> {f.exists()}')
38+
if f.exists():
39+
result = f
40+
41+
return result
42+

solid/objects.py

+1-37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Dict, Optional, Sequence, Tuple, Union, List
77

88
from .solidpython import IncludedOpenSCADObject, OpenSCADObject
9+
from .helpers import _find_library, _openscad_library_paths
910

1011
PathStr = Union[Path, str]
1112

@@ -798,43 +799,6 @@ def _import_scad(scad: Path) -> Optional[SimpleNamespace]:
798799

799800
return namespace
800801

801-
def _openscad_library_paths() -> List[Path]:
802-
"""
803-
Return system-dependent OpenSCAD library paths or paths defined in os.environ['OPENSCADPATH']
804-
"""
805-
import platform
806-
import os
807-
import re
808-
809-
paths = [Path('.')]
810-
811-
user_path = os.environ.get('OPENSCADPATH')
812-
if user_path:
813-
for s in re.split(r'\s*[;:]\s*', user_path):
814-
paths.append(Path(s))
815-
816-
default_paths = {
817-
'Linux': Path.home() / '.local/share/OpenSCAD/libraries',
818-
'Darwin': Path.home() / 'Documents/OpenSCAD/libraries',
819-
'Windows': Path('My Documents\OpenSCAD\libraries')
820-
}
821-
822-
paths.append(default_paths[platform.system()])
823-
return paths
824-
825-
def _find_library(library_name: PathStr) -> Path:
826-
result = Path(library_name)
827-
828-
if not result.is_absolute():
829-
paths = _openscad_library_paths()
830-
for p in paths:
831-
f = p / result
832-
# print(f'Checking {f} -> {f.exists()}')
833-
if f.exists():
834-
result = f
835-
836-
return result
837-
838802
# use() & include() mimic OpenSCAD's use/include mechanics.
839803
# -- use() makes methods in scad_file_path.scad available to be called.
840804
# --include() makes those methods available AND executes all code in

solid/solidpython.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import pkg_resources
2727
import regex as re
2828

29+
from .helpers import _find_library
30+
2931
PathStr = Union[Path, str]
3032
AnimFunc = Callable[[Optional[float]], 'OpenSCADObject']
3133
# These are features added to SolidPython but NOT in OpenSCAD.
@@ -369,7 +371,9 @@ class IncludedOpenSCADObject(OpenSCADObject):
369371
"""
370372

371373
def __init__(self, name, params, include_file_path, use_not_include=False, **kwargs):
372-
self.include_file_path = self._get_include_path(include_file_path)
374+
#this call is more or less redudant, because objects.py:854 already calls
375+
#_find_library and ensures the path is already resolved......
376+
self.include_file_path = _find_library(include_file_path)
373377

374378
use_str = 'use' if use_not_include else 'include'
375379
self.include_string = f'{use_str} <{self.include_file_path}>\n'

solid/test/test_solidpython.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from solid.solidpython import scad_render, scad_render_animated_file, scad_render_to_file
1616
from solid.test.ExpandedTestCase import DiffOutput
1717

18+
from solid.helpers import _find_library
19+
1820
scad_test_case_templates = [
1921
{'name': 'polygon', 'class': 'polygon' , 'kwargs': {'paths': [[0, 1, 2]]}, 'expected': '\n\npolygon(paths = [[0, 1, 2]], points = [[0, 0], [1, 0], [0, 1]]);', 'args': {'points': [[0, 0, 0], [1, 0, 0], [0, 1, 0]]}, },
2022
{'name': 'polygon', 'class': 'polygon' , 'kwargs': {}, 'expected': '\n\npolygon(points = [[0, 0], [1, 0], [0, 1]]);', 'args': {'points': [[0, 0, 0], [1, 0, 0], [0, 1, 0]]}, },
@@ -198,7 +200,7 @@ def test_use(self):
198200
a = steps(3) # type: ignore
199201
actual = scad_render(a)
200202

201-
abs_path = a._get_include_path(include_file)
203+
abs_path = _find_library(include_file)
202204
expected = f"use <{abs_path}>\n\n\nsteps(howmany = 3);"
203205
self.assertEqual(expected, actual)
204206

@@ -208,7 +210,7 @@ def test_import_scad(self):
208210
a = mod.steps(3)
209211
actual = scad_render(a)
210212

211-
abs_path = a._get_include_path(include_file)
213+
abs_path = _find_library(include_file)
212214
expected = f"use <{abs_path}>\n\n\nsteps(howmany = 3);"
213215
self.assertEqual(expected, actual)
214216

@@ -244,7 +246,7 @@ def test_imported_scad_arguments(self):
244246
points = mod.scad_points();
245247
poly = polygon(points);
246248
actual = scad_render(poly);
247-
abs_path = points._get_include_path(include_file)
249+
abs_path = _find_library(include_file)
248250
expected = f'use <{abs_path}>\n\n\npolygon(points = scad_points());'
249251
self.assertEqual(expected, actual)
250252

@@ -277,7 +279,7 @@ def test_include(self):
277279
a = steps(3) # type: ignore
278280

279281
actual = scad_render(a)
280-
abs_path = a._get_include_path(include_file)
282+
abs_path = _find_library(include_file)
281283
expected = f"include <{abs_path}>\n\n\nsteps(howmany = 3);"
282284
self.assertEqual(expected, actual)
283285

@@ -287,7 +289,7 @@ def test_extra_args_to_included_scad(self):
287289
a = mod.steps(3, external_var=True)
288290
actual = scad_render(a)
289291

290-
abs_path = a._get_include_path(include_file)
292+
abs_path = _find_library(include_file)
291293
expected = f"use <{abs_path}>\n\n\nsteps(external_var = true, howmany = 3);"
292294
self.assertEqual(expected, actual)
293295

0 commit comments

Comments
 (0)