Skip to content

Commit

Permalink
fix OpenSCAD identifiers starting with a digit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-dh committed May 16, 2021
1 parent f460842 commit fe6b85d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions solid/solidpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,17 @@ def new_openscad_class_str(class_name: str,
def _subbed_keyword(keyword: str) -> str:
"""
Append an underscore to any python reserved word.
Prepend an underscore to any OpenSCAD identifier starting with a digit.
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
"""
new_key = keyword + '_' if keyword in PYTHON_ONLY_RESERVED_WORDS else keyword
new_key = keyword

if keyword in PYTHON_ONLY_RESERVED_WORDS:
new_key = keyword + "_"

if keyword[0].isdigit():
new_key = "_" + keyword

if new_key != keyword:
print(f"\nFound OpenSCAD code that's not compatible with Python. \n"
f"Imported OpenSCAD code using `{keyword}` \n"
Expand All @@ -751,10 +759,16 @@ def _subbed_keyword(keyword: str) -> str:
def _unsubbed_keyword(subbed_keyword: str) -> str:
"""
Remove trailing underscore for already-subbed python reserved words.
Remove prepending underscore if remaining identifier starts with a digit.
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
"""
shortened = subbed_keyword[:-1]
return shortened if shortened in PYTHON_ONLY_RESERVED_WORDS else subbed_keyword
if subbed_keyword.endswith("_") and subbed_keyword[:-1] in PYTHON_ONLY_RESERVED_WORDS:
return subbed_keyword[:-1]

if subbed_keyword.startswith("_") and subbed_keyword[1].isdigit():
return subbed_keyword[1:]

return subbed_keyword

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

0 comments on commit fe6b85d

Please sign in to comment.