bot/bottleclip.py

38 lines
1 KiB
Python

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