Add cachedir argument for export function
Use system-specific user cache directory thanks to appdirsmaster
parent
3506f8de17
commit
9b2388973c
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
|
@ -10,51 +11,64 @@ def generate_outfile(
|
||||||
context,
|
context,
|
||||||
outfilename,
|
outfilename,
|
||||||
log = None,
|
log = None,
|
||||||
|
cachedir = None,
|
||||||
):
|
):
|
||||||
|
|
||||||
# Template rendering
|
# Template rendering
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
log.info("Generating SVG using '{}' template".format(templatename))
|
log.info("Generating SVG using '{}' template".format(templatename))
|
||||||
|
|
||||||
|
log.debug(
|
||||||
|
"Initializing jinja2 with template dir '{}'" \
|
||||||
|
.format(templatesdir)
|
||||||
|
)
|
||||||
jj2_env = jinja2.Environment(
|
jj2_env = jinja2.Environment(
|
||||||
loader = jinja2.FileSystemLoader( templatesdir )
|
loader = jinja2.FileSystemLoader(
|
||||||
|
str(templatesdir),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
jj2_tpl = jj2_env.get_template(
|
jj2_tpl = jj2_env.get_template(
|
||||||
os.path.join(
|
# Jinja specific path format
|
||||||
templatename,
|
"{}/template.svg.j2".format(templatename),
|
||||||
"template.svg.j2",
|
|
||||||
)
|
)
|
||||||
)
|
except Exception as e:
|
||||||
except:
|
|
||||||
log.error("Could not find template '{}'".format(templatename))
|
log.error("Could not find template '{}'".format(templatename))
|
||||||
|
log.debug(e, exc_info=1)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
extension = outfilename.split(".")[-1]
|
|
||||||
|
|
||||||
# To SVG
|
# To SVG
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
if extension == "svg":
|
if outfilename.suffix == ".svg":
|
||||||
jj2_tpl.stream(context).dump( outfilename )
|
jj2_tpl.stream(context).dump( str(outfilename) )
|
||||||
return outfilename
|
return outfilename
|
||||||
|
|
||||||
# To PNG with inkscape
|
# To PNG with inkscape
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
if extension == "png":
|
if outfilename.suffix == ".png":
|
||||||
|
|
||||||
|
cachedir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
with tempfile.NamedTemporaryFile(
|
||||||
|
suffix="svg",
|
||||||
|
dir = str(cachedir),
|
||||||
|
) as tmpsvg:
|
||||||
try:
|
try:
|
||||||
log.info("Exporting to {} using inkscape".format(extension))
|
log.info(
|
||||||
|
"Exporting to {} using inkscape" \
|
||||||
|
.format(outfilename.suffix),
|
||||||
|
)
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
jj2_tpl.stream(context).dump( "tmp.svg" )
|
jj2_tpl.stream(context).dump( tmpsvg )
|
||||||
|
|
||||||
inkscape_process = subprocess.Popen(
|
inkscape_process = subprocess.Popen(
|
||||||
[
|
[
|
||||||
"inkscape",
|
"inkscape",
|
||||||
"tmp.svg",
|
tmpsvg.name,
|
||||||
"--export-filename",
|
"--export-filename",
|
||||||
outfilename,
|
str(outfilename),
|
||||||
],
|
],
|
||||||
stdout = subprocess.PIPE,
|
stdout = subprocess.PIPE,
|
||||||
stderr = subprocess.STDOUT,
|
stderr = subprocess.STDOUT,
|
||||||
|
@ -74,23 +88,18 @@ def generate_outfile(
|
||||||
.format(inkscape_process.returncode)
|
.format(inkscape_process.returncode)
|
||||||
)
|
)
|
||||||
|
|
||||||
os.unlink("tmp.svg")
|
|
||||||
|
|
||||||
return outfilename
|
return outfilename
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning("Failed to export with inkscape")
|
log.warning("Failed to export with inkscape")
|
||||||
try:
|
|
||||||
os.unlink("tmp.svg")
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
log.debug(e)
|
log.debug(e)
|
||||||
|
|
||||||
# To png, pdf or ps with cairosvg
|
# To png, pdf or ps with cairosvg
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
if extension in [ "png", "pdf", "ps" ]:
|
if outfilename.suffix in [ ".png", ".pdf", ".ps" ]:
|
||||||
|
|
||||||
log.info("Exporting to {} using cairosvg".format(extension))
|
log.info("Exporting to {} using cairosvg".format(outfilename.suffix))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import cairosvg
|
import cairosvg
|
||||||
|
@ -105,16 +114,16 @@ def generate_outfile(
|
||||||
|
|
||||||
svg_str = jj2_tpl.render(context)
|
svg_str = jj2_tpl.render(context)
|
||||||
|
|
||||||
if extension == "png":
|
if outfilename.suffix == ".png":
|
||||||
conversion_fun = cairosvg.svg2png
|
conversion_fun = cairosvg.svg2png
|
||||||
elif extension == "pdf":
|
elif outfilename.suffix == ".pdf":
|
||||||
conversion_fun = cairosvg.svg2pdf
|
conversion_fun = cairosvg.svg2pdf
|
||||||
elif extension == "ps":
|
elif outfilename.suffix == ".ps":
|
||||||
conversion_fun = cairosvg.svg2ps
|
conversion_fun = cairosvg.svg2ps
|
||||||
|
|
||||||
conversion_fun(
|
conversion_fun(
|
||||||
bytestring = svg_str,
|
bytestring = svg_str,
|
||||||
write_to = outfilename,
|
write_to = str(outfilename),
|
||||||
)
|
)
|
||||||
|
|
||||||
return outfilename
|
return outfilename
|
||||||
|
@ -125,7 +134,7 @@ def generate_outfile(
|
||||||
"Can't export to '{}' : unsupported format '{}'" \
|
"Can't export to '{}' : unsupported format '{}'" \
|
||||||
.format(
|
.format(
|
||||||
outfilename,
|
outfilename,
|
||||||
extension,
|
outfilename.suffix,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in New Issue