boxespy/boxes/formats.py

104 lines
3.4 KiB
Python
Raw Normal View History

2019-01-11 13:16:19 +01:00
#!/usr/bin/env python3
# Copyright (C) 2013-2014 Florian Festi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-06-27 09:10:00 +02:00
import os
import shutil
import subprocess
import tempfile
from boxes.drawing import SVGSurface, PSSurface, LBRN2Surface, Context
2016-06-27 09:10:00 +02:00
2016-08-17 15:07:41 +02:00
class Formats:
pstoedit_candidates = ["/usr/bin/pstoedit", "pstoedit", "pstoedit.exe"]
ps2pdf_candidates = ["/usr/bin/ps2pdf", "ps2pdf", "ps2pdf.exe"]
2016-06-27 09:10:00 +02:00
_BASE_FORMATS = ['svg', 'svg_Ponoko', 'ps', 'lbrn2']
2016-06-27 09:10:00 +02:00
formats = {
2016-08-17 15:07:41 +02:00
"svg": None,
"svg_Ponoko": None,
2016-08-17 15:07:41 +02:00
"ps": None,
"lbrn2": None,
"dxf": "{pstoedit} -flat 0.1 -f dxf:-mm {input} {output}",
"gcode": "{pstoedit} -f gcode {input} {output}",
"plt": "{pstoedit} -f plot-hpgl {input} {output}",
# "ai": "{pstoedit} -f ps2ai {input} {output}",
"pdf": "{ps2pdf} -dEPSCrop {input} {output}",
2016-06-27 09:10:00 +02:00
}
http_headers = {
2016-08-17 15:07:41 +02:00
"svg": [('Content-type', 'image/svg+xml; charset=utf-8')],
"svg_Ponoko": [('Content-type', 'image/svg+xml; charset=utf-8')],
2016-08-17 15:07:41 +02:00
"ps": [('Content-type', 'application/postscript')],
"lbrn2": [('Content-type', 'application/lbrn2')],
2016-08-17 15:07:41 +02:00
"dxf": [('Content-type', 'image/vnd.dxf')],
"plt": [('Content-type', ' application/vnd.hp-hpgl')],
"gcode": [('Content-type', 'text/plain; charset=utf-8')],
2016-06-27 09:10:00 +02:00
# "" : [('Content-type', '')],
}
2016-08-17 15:07:41 +02:00
2023-01-08 19:41:02 +01:00
def __init__(self) -> None:
for cmd in self.pstoedit_candidates:
self.pstoedit = shutil.which(cmd)
if self.pstoedit:
break
for cmd in self.ps2pdf_candidates:
self.ps2pdf = shutil.which(cmd)
if self.ps2pdf:
break
2016-06-27 09:10:00 +02:00
def getFormats(self):
if self.pstoedit:
2016-06-27 09:10:00 +02:00
return sorted(self.formats.keys())
return self._BASE_FORMATS
2016-06-27 09:10:00 +02:00
def getSurface(self, fmt, filename):
if fmt in ("svg", "svg_Ponoko"):
surface = SVGSurface(filename)
2022-03-27 22:09:29 +02:00
elif fmt == "lbrn2":
surface = LBRN2Surface(filename)
2016-06-27 09:10:00 +02:00
else:
2022-03-27 22:09:29 +02:00
surface = PSSurface(filename)
2016-06-27 09:10:00 +02:00
ctx = Context(surface)
2016-06-27 09:10:00 +02:00
return surface, ctx
def convert(self, filename, fmt, metadata=None):
2016-06-27 09:10:00 +02:00
if fmt not in self._BASE_FORMATS:
fd, tmpfile = tempfile.mkstemp(dir=os.path.dirname(filename))
cmd = self.formats[fmt].format(
pstoedit=self.pstoedit,
ps2pdf=self.ps2pdf,
input=filename,
output=tmpfile).split()
2016-06-27 09:10:00 +02:00
err = subprocess.call(cmd)
2016-08-17 15:07:41 +02:00
2016-06-27 09:10:00 +02:00
if err:
# XXX show stderr output
try:
os.unlink(tmpfile)
except:
pass
2016-06-27 09:10:00 +02:00
raise ValueError("Conversion failed. pstoedit returned %i" % err)
2016-08-17 15:07:41 +02:00
2016-06-27 09:10:00 +02:00
os.rename(tmpfile, filename)