Skip to content

Commit b1ac306

Browse files
committed
moved [un]subbed_keyword into helpers.py
1 parent 9a68259 commit b1ac306

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

solid/helpers.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import keyword
2+
PYTHON_ONLY_RESERVED_WORDS = keyword.kwlist
3+
4+
def _subbed_keyword(keyword: str) -> str:
5+
"""
6+
Append an underscore to any python reserved word.
7+
Prepend an underscore to any OpenSCAD identifier starting with a digit.
8+
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
9+
"""
10+
new_key = keyword
11+
12+
if keyword in PYTHON_ONLY_RESERVED_WORDS:
13+
new_key = keyword + "_"
14+
15+
if keyword[0].isdigit():
16+
new_key = "_" + keyword
17+
18+
if new_key != keyword:
19+
print(f"\nFound OpenSCAD code that's not compatible with Python. \n"
20+
f"Imported OpenSCAD code using `{keyword}` \n"
21+
f"can be accessed with `{new_key}` in SolidPython\n")
22+
return new_key
23+
24+
def _unsubbed_keyword(subbed_keyword: str) -> str:
25+
"""
26+
Remove trailing underscore for already-subbed python reserved words.
27+
Remove prepending underscore if remaining identifier starts with a digit.
28+
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
29+
"""
30+
if subbed_keyword.endswith("_") and subbed_keyword[:-1] in PYTHON_ONLY_RESERVED_WORDS:
31+
return subbed_keyword[:-1]
32+
33+
if subbed_keyword.startswith("_") and subbed_keyword[1].isdigit():
34+
return subbed_keyword[1:]
35+
36+
return subbed_keyword
37+

solid/solidpython.py

+2-40
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import sys
1717
import tempfile
1818
from pathlib import Path
19-
import keyword
2019

2120
from typing import Set, Sequence, List, Callable, Optional, Union, Iterable
2221

@@ -26,18 +25,14 @@
2625
import pkg_resources
2726
import regex as re
2827

28+
from .helpers import _subbed_keyword, _unsubbed_keyword
29+
2930
PathStr = Union[Path, str]
3031
AnimFunc = Callable[[Optional[float]], 'OpenSCADObject']
3132
# These are features added to SolidPython but NOT in OpenSCAD.
3233
# Mark them for special treatment
3334
non_rendered_classes = ['hole', 'part']
3435

35-
# Words reserved in Python but not OpenSCAD
36-
# Re: https://github.com/SolidCode/SolidPython/issues/99
37-
38-
PYTHON_ONLY_RESERVED_WORDS = keyword.kwlist
39-
40-
4136
# =========================
4237
# = Internal Utilities =
4338
# =========================
@@ -709,39 +704,6 @@ def new_openscad_class_str(class_name: str,
709704

710705
return result
711706

712-
def _subbed_keyword(keyword: str) -> str:
713-
"""
714-
Append an underscore to any python reserved word.
715-
Prepend an underscore to any OpenSCAD identifier starting with a digit.
716-
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
717-
"""
718-
new_key = keyword
719-
720-
if keyword in PYTHON_ONLY_RESERVED_WORDS:
721-
new_key = keyword + "_"
722-
723-
if keyword[0].isdigit():
724-
new_key = "_" + keyword
725-
726-
if new_key != keyword:
727-
print(f"\nFound OpenSCAD code that's not compatible with Python. \n"
728-
f"Imported OpenSCAD code using `{keyword}` \n"
729-
f"can be accessed with `{new_key}` in SolidPython\n")
730-
return new_key
731-
732-
def _unsubbed_keyword(subbed_keyword: str) -> str:
733-
"""
734-
Remove trailing underscore for already-subbed python reserved words.
735-
Remove prepending underscore if remaining identifier starts with a digit.
736-
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
737-
"""
738-
if subbed_keyword.endswith("_") and subbed_keyword[:-1] in PYTHON_ONLY_RESERVED_WORDS:
739-
return subbed_keyword[:-1]
740-
741-
if subbed_keyword.startswith("_") and subbed_keyword[1].isdigit():
742-
return subbed_keyword[1:]
743-
744-
return subbed_keyword
745707

746708
# now that we have the base class defined, we can do a circular import
747709
from . import objects

0 commit comments

Comments
 (0)