Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix OpenSCAD identifiers starting with a digit #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
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
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