Run bottleclip generator as subprocess
This commit is contained in:
parent
3a721d9e29
commit
deced2d3a7
4 changed files with 46 additions and 55 deletions
38
bottleclip.py
Normal file
38
bottleclip.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
import os
|
||||
import subprocess
|
||||
from os.path import abspath, join
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import List
|
||||
|
||||
|
||||
def resource_dir() -> str:
|
||||
p = os.getenv("BOTTLECLIP_RESOURCES")
|
||||
assert p is not None
|
||||
return p
|
||||
|
||||
|
||||
def list_icons() -> List[str]:
|
||||
files = os.listdir(join(resource_dir(), "icons"))
|
||||
return sorted(set(files) - {"README.md"})
|
||||
|
||||
|
||||
def create_stl(label: str, icon: str, ears: bool) -> NamedTemporaryFile:
|
||||
icon_path = abspath(join(resource_dir(), "icons", icon))
|
||||
ears_str = "true" if ears else "false"
|
||||
|
||||
scad = NamedTemporaryFile(dir=resource_dir(), suffix=".scad")
|
||||
with open(join(resource_dir(), "bottle-clip.scad"), "rb") as f:
|
||||
scad.write(f.read())
|
||||
scad.write(b"\n\n")
|
||||
scad.write(
|
||||
f'bottle_clip(name="{label}", logo="{icon_path}", ears={ears_str});'.encode(
|
||||
"utf-8"
|
||||
)
|
||||
)
|
||||
scad.flush()
|
||||
|
||||
stl = NamedTemporaryFile(suffix=".scad")
|
||||
subprocess.run(["openscad", scad.name, "--export-format", "binstl", "-o", stl.name])
|
||||
stl.seek(0)
|
||||
|
||||
return stl
|
Loading…
Add table
Add a link
Reference in a new issue