Skip to content

Commit

Permalink
Slugifying filenames (see #135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregwar committed Jan 21, 2025
1 parent 2105c20 commit 70f583c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions onshape_to_robot/onshape_to_robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ def partIsIgnore(name):
return name in config['ignore']
else:
return name not in config['whitelist']

def slugify(value):
return "".join(c if c.isalnum() else "_" for c in value).strip("_")

# Adds a part to the current robot link


def addPart(occurrence, matrix):
part = occurrence['instance']

Expand Down Expand Up @@ -79,7 +80,7 @@ def addPart(occurrence, matrix):
if partIsIgnore(justPart):
stlFile = None
else:
stlFile = prefix.replace('/', '_')+'.stl'
stlFile = slugify(prefix)+'.stl'
# shorten the configuration to a maximum number of chars to prevent errors. Necessary for standard parts like screws
if len(part['configuration']) > 40:
shortend_configuration = hashlib.md5(
Expand All @@ -91,7 +92,7 @@ def addPart(occurrence, matrix):
with open(config['outputDirectory']+'/'+stlFile, 'wb') as stream:
stream.write(stl)

stlMetadata = prefix.replace('/', '_')+'.part'
stlMetadata = slugify(prefix)+'.part'
with open(config['outputDirectory']+'/'+stlMetadata, 'w', encoding="utf-8") as stream:
json.dump(part, stream, indent=4, sort_keys=True)

Expand Down

0 comments on commit 70f583c

Please sign in to comment.