Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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`
  • Loading branch information
jeff-dh committed May 22, 2021
commit 7f65b21fedb1e79df27da37f7a5e6a6b21782f20
42 changes: 42 additions & 0 deletions solid/helpers.py
Original file line number Diff line number Diff line change
@@ -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

38 changes: 1 addition & 37 deletions solid/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion solid/solidpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'
Expand Down
12 changes: 7 additions & 5 deletions solid/test/test_solidpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]}, },
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down