Skip to content

Commit

Permalink
Use tmpfile for passing data to pstoedit
Browse files Browse the repository at this point in the history
Otherwise this fails on the web instance as itz is pretty old. This
patch is not needed with newer versions of pstoedit and should be
removed as soon as the web instance is on the new server.
  • Loading branch information
florianfesti committed Mar 4, 2024
1 parent 5ea7362 commit cc63cb8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions boxes/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import shutil
import subprocess
import io
import tempfile

from boxes.drawing import SVGSurface, PSSurface, LBRN2Surface, Context

Expand All @@ -34,9 +35,9 @@ class Formats:
"svg_Ponoko": None,
"ps": None,
"lbrn2": None,
"dxf": "{pstoedit} -flat 0.1 -f dxf:-mm",
"gcode": "{pstoedit} -f gcode",
"plt": "{pstoedit} -f hpgl",
"dxf": "{pstoedit} -flat 0.1 -f dxf:-mm {input} -",
"gcode": "{pstoedit} -f gcode {input} -",
"plt": "{pstoedit} -f hpgl {input} -",
# "ai": "{pstoedit} -f ps2ai",
"pdf": "{ps2pdf} -dEPSCrop - -",
}
Expand Down Expand Up @@ -82,11 +83,20 @@ def getSurface(self, fmt):
def convert(self, data, fmt):

if fmt not in self._BASE_FORMATS:
tmpfile = ""
if fmt in ("dxf", "gcode", "plt"):
fd, tmpfile = tempfile.mkstemp()
os.write(fd, data.getvalue())
input_ = None
else:
input_ = data.getvalue()

cmd = self.formats[fmt].format(
pstoedit=self.pstoedit,
ps2pdf=self.ps2pdf).split()
ps2pdf=self.ps2pdf,
input=tmpfile).split()

result = subprocess.run(cmd, input=data.getvalue(), capture_output=True)
result = subprocess.run(cmd, input=input_, capture_output=True)
if result.returncode:
# XXX show stderr output
raise ValueError("Conversion failed. pstoedit returned %i\n\n %s" % (result.returncode, result.stderr))
Expand Down

0 comments on commit cc63cb8

Please sign in to comment.