From 7f65b21fedb1e79df27da37f7a5e6a6b21782f20 Mon Sep 17 00:00:00 2001 From: jeff <1105041+jeff-dh@users.noreply.github.com> Date: Sun, 23 May 2021 00:15:15 +0200 Subject: [PATCH] fix #168 - I'm pretty sure this is what it is supposed to be - I would suggest to rename the function to `resolve_scad_filename` --- solid/helpers.py | 42 ++++++++++++++++++++++++++++++++++ solid/objects.py | 38 +----------------------------- solid/solidpython.py | 6 ++++- solid/test/test_solidpython.py | 12 ++++++---- 4 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 solid/helpers.py diff --git a/solid/helpers.py b/solid/helpers.py new file mode 100644 index 00000000..df6af677 --- /dev/null +++ b/solid/helpers.py @@ -0,0 +1,42 @@ +from pathlib import Path +from typing import List, Union +PathStr = Union[Path, str] + + +def _openscad_library_paths() -> List[Path]: + """ + Return system-dependent OpenSCAD library paths or paths defined in os.environ['OPENSCADPATH'] + """ + import platform + import os + import re + + paths = [Path('.')] + + user_path = os.environ.get('OPENSCADPATH') + if user_path: + for s in re.split(r'\s*[;:]\s*', user_path): + paths.append(Path(s)) + + default_paths = { + 'Linux': Path.home() / '.local/share/OpenSCAD/libraries', + 'Darwin': Path.home() / 'Documents/OpenSCAD/libraries', + 'Windows': Path('My Documents\OpenSCAD\libraries') + } + + paths.append(default_paths[platform.system()]) + return paths + +def _find_library(library_name: PathStr) -> Path: + result = Path(library_name) + + if not result.is_absolute(): + paths = _openscad_library_paths() + for p in paths: + f = p / result + # print(f'Checking {f} -> {f.exists()}') + if f.exists(): + result = f + + return result + diff --git a/solid/objects.py b/solid/objects.py index 2307e804..18fe5463 100644 --- a/solid/objects.py +++ b/solid/objects.py @@ -6,6 +6,7 @@ from typing import Dict, Optional, Sequence, Tuple, Union, List from .solidpython import IncludedOpenSCADObject, OpenSCADObject +from .helpers import _find_library, _openscad_library_paths PathStr = Union[Path, str] @@ -798,43 +799,6 @@ def _import_scad(scad: Path) -> Optional[SimpleNamespace]: return namespace -def _openscad_library_paths() -> List[Path]: - """ - Return system-dependent OpenSCAD library paths or paths defined in os.environ['OPENSCADPATH'] - """ - import platform - import os - import re - - paths = [Path('.')] - - user_path = os.environ.get('OPENSCADPATH') - if user_path: - for s in re.split(r'\s*[;:]\s*', user_path): - paths.append(Path(s)) - - default_paths = { - 'Linux': Path.home() / '.local/share/OpenSCAD/libraries', - 'Darwin': Path.home() / 'Documents/OpenSCAD/libraries', - 'Windows': Path('My Documents\OpenSCAD\libraries') - } - - paths.append(default_paths[platform.system()]) - return paths - -def _find_library(library_name: PathStr) -> Path: - result = Path(library_name) - - if not result.is_absolute(): - paths = _openscad_library_paths() - for p in paths: - f = p / result - # print(f'Checking {f} -> {f.exists()}') - if f.exists(): - result = f - - return result - # use() & include() mimic OpenSCAD's use/include mechanics. # -- use() makes methods in scad_file_path.scad available to be called. # --include() makes those methods available AND executes all code in diff --git a/solid/solidpython.py b/solid/solidpython.py index 312c7f0e..c025df7c 100755 --- a/solid/solidpython.py +++ b/solid/solidpython.py @@ -26,6 +26,8 @@ import pkg_resources import regex as re +from .helpers import _find_library + PathStr = Union[Path, str] AnimFunc = Callable[[Optional[float]], 'OpenSCADObject'] # These are features added to SolidPython but NOT in OpenSCAD. @@ -369,7 +371,9 @@ class IncludedOpenSCADObject(OpenSCADObject): """ def __init__(self, name, params, include_file_path, use_not_include=False, **kwargs): - self.include_file_path = self._get_include_path(include_file_path) + #this call is more or less redudant, because objects.py:854 already calls + #_find_library and ensures the path is already resolved...... + self.include_file_path = _find_library(include_file_path) use_str = 'use' if use_not_include else 'include' self.include_string = f'{use_str} <{self.include_file_path}>\n' diff --git a/solid/test/test_solidpython.py b/solid/test/test_solidpython.py index 14fe7349..48772ad9 100755 --- a/solid/test/test_solidpython.py +++ b/solid/test/test_solidpython.py @@ -15,6 +15,8 @@ from solid.solidpython import scad_render, scad_render_animated_file, scad_render_to_file from solid.test.ExpandedTestCase import DiffOutput +from solid.helpers import _find_library + scad_test_case_templates = [ {'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]]}, }, {'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): a = steps(3) # type: ignore actual = scad_render(a) - abs_path = a._get_include_path(include_file) + abs_path = _find_library(include_file) expected = f"use <{abs_path}>\n\n\nsteps(howmany = 3);" self.assertEqual(expected, actual) @@ -208,7 +210,7 @@ def test_import_scad(self): a = mod.steps(3) actual = scad_render(a) - abs_path = a._get_include_path(include_file) + abs_path = _find_library(include_file) expected = f"use <{abs_path}>\n\n\nsteps(howmany = 3);" self.assertEqual(expected, actual) @@ -244,7 +246,7 @@ def test_imported_scad_arguments(self): points = mod.scad_points(); poly = polygon(points); actual = scad_render(poly); - abs_path = points._get_include_path(include_file) + abs_path = _find_library(include_file) expected = f'use <{abs_path}>\n\n\npolygon(points = scad_points());' self.assertEqual(expected, actual) @@ -277,7 +279,7 @@ def test_include(self): a = steps(3) # type: ignore actual = scad_render(a) - abs_path = a._get_include_path(include_file) + abs_path = _find_library(include_file) expected = f"include <{abs_path}>\n\n\nsteps(howmany = 3);" self.assertEqual(expected, actual) @@ -287,7 +289,7 @@ def test_extra_args_to_included_scad(self): a = mod.steps(3, external_var=True) actual = scad_render(a) - abs_path = a._get_include_path(include_file) + abs_path = _find_library(include_file) expected = f"use <{abs_path}>\n\n\nsteps(external_var = true, howmany = 3);" self.assertEqual(expected, actual)