2020-07-09 08:31:22 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
2020-07-22 12:07:52 -04:00
|
|
|
import tempfile
|
2020-07-09 08:31:22 -04:00
|
|
|
|
|
|
|
import jinja2
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
def generate_outfile(
|
|
|
|
templatesdir,
|
|
|
|
templatename,
|
|
|
|
context,
|
|
|
|
outfilename,
|
|
|
|
log = None,
|
2020-07-22 12:07:52 -04:00
|
|
|
cachedir = None,
|
2020-07-09 08:31:22 -04:00
|
|
|
):
|
|
|
|
|
|
|
|
# Template rendering
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
log.info("Generating SVG using '{}' template".format(templatename))
|
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
log.debug(
|
|
|
|
"Initializing jinja2 with template dir '{}'" \
|
|
|
|
.format(templatesdir)
|
|
|
|
)
|
2020-07-09 08:31:22 -04:00
|
|
|
jj2_env = jinja2.Environment(
|
2020-07-22 12:07:52 -04:00
|
|
|
loader = jinja2.FileSystemLoader(
|
|
|
|
str(templatesdir),
|
|
|
|
)
|
2020-07-09 08:31:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
jj2_tpl = jj2_env.get_template(
|
2020-07-22 12:07:52 -04:00
|
|
|
# Jinja specific path format
|
|
|
|
"{}/template.svg.j2".format(templatename),
|
2020-07-09 08:31:22 -04:00
|
|
|
)
|
2020-07-22 12:07:52 -04:00
|
|
|
except Exception as e:
|
2020-07-09 08:31:22 -04:00
|
|
|
log.error("Could not find template '{}'".format(templatename))
|
2020-07-22 12:07:52 -04:00
|
|
|
log.debug(e, exc_info=1)
|
2020-07-09 08:31:22 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
# To SVG
|
|
|
|
# -------------------------------------------------------------------------
|
2020-07-22 12:07:52 -04:00
|
|
|
if outfilename.suffix == ".svg":
|
|
|
|
jj2_tpl.stream(context).dump( str(outfilename) )
|
2020-07-09 08:31:22 -04:00
|
|
|
return outfilename
|
|
|
|
|
|
|
|
# To PNG with inkscape
|
|
|
|
# -------------------------------------------------------------------------
|
2020-07-22 12:07:52 -04:00
|
|
|
if outfilename.suffix == ".png":
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
cachedir.mkdir(parents=True, exist_ok=True)
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
with tempfile.NamedTemporaryFile(
|
|
|
|
suffix="svg",
|
|
|
|
dir = str(cachedir),
|
|
|
|
) as tmpsvg:
|
|
|
|
try:
|
|
|
|
log.info(
|
|
|
|
"Exporting to {} using inkscape" \
|
|
|
|
.format(outfilename.suffix),
|
|
|
|
)
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
jj2_tpl.stream(context).dump( tmpsvg )
|
|
|
|
|
|
|
|
inkscape_process = subprocess.Popen(
|
|
|
|
[
|
|
|
|
"inkscape",
|
|
|
|
tmpsvg.name,
|
|
|
|
"--export-filename",
|
|
|
|
str(outfilename),
|
|
|
|
],
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.STDOUT,
|
|
|
|
universal_newlines = True,
|
|
|
|
)
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
for line_out in iter(inkscape_process.stdout.readline, ""):
|
|
|
|
log.debug(line_out)
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
inkscape_process.stdout.close()
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
rv = inkscape_process.wait()
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
if rv != 0:
|
|
|
|
raise Exception(
|
|
|
|
"Bad inkscape return code '{}'" \
|
|
|
|
.format(inkscape_process.returncode)
|
|
|
|
)
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-16 16:22:25 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
return outfilename
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
except Exception as e:
|
|
|
|
log.warning("Failed to export with inkscape")
|
|
|
|
log.debug(e)
|
2020-07-09 08:31:22 -04:00
|
|
|
|
|
|
|
# To png, pdf or ps with cairosvg
|
|
|
|
# -------------------------------------------------------------------------
|
2020-07-22 12:07:52 -04:00
|
|
|
if outfilename.suffix in [ ".png", ".pdf", ".ps" ]:
|
2020-07-09 08:31:22 -04:00
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
log.info("Exporting to {} using cairosvg".format(outfilename.suffix))
|
2020-07-09 08:31:22 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
import cairosvg
|
|
|
|
except ImportError as e:
|
|
|
|
log.error(
|
|
|
|
"Failed to export to '{}' with cairosvg" \
|
|
|
|
.format(
|
|
|
|
outfilename,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
log.debug(e)
|
|
|
|
|
|
|
|
svg_str = jj2_tpl.render(context)
|
|
|
|
|
2020-07-22 12:07:52 -04:00
|
|
|
if outfilename.suffix == ".png":
|
2020-07-09 08:31:22 -04:00
|
|
|
conversion_fun = cairosvg.svg2png
|
2020-07-22 12:07:52 -04:00
|
|
|
elif outfilename.suffix == ".pdf":
|
2020-07-09 08:31:22 -04:00
|
|
|
conversion_fun = cairosvg.svg2pdf
|
2020-07-22 12:07:52 -04:00
|
|
|
elif outfilename.suffix == ".ps":
|
2020-07-09 08:31:22 -04:00
|
|
|
conversion_fun = cairosvg.svg2ps
|
|
|
|
|
|
|
|
conversion_fun(
|
|
|
|
bytestring = svg_str,
|
2020-07-22 12:07:52 -04:00
|
|
|
write_to = str(outfilename),
|
2020-07-09 08:31:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return outfilename
|
|
|
|
|
|
|
|
# To unsupported format
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
log.error(
|
|
|
|
"Can't export to '{}' : unsupported format '{}'" \
|
|
|
|
.format(
|
|
|
|
outfilename,
|
2020-07-22 12:07:52 -04:00
|
|
|
outfilename.suffix,
|
2020-07-09 08:31:22 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return None
|