From 9b2388973c21b1dca976e9c846a974699827180a Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Wed, 22 Jul 2020 18:07:52 +0200 Subject: [PATCH] Add cachedir argument for export function Use system-specific user cache directory thanks to appdirs --- lokrez/export.py | 125 +++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 58 deletions(-) diff --git a/lokrez/export.py b/lokrez/export.py index 3741c22..e03915b 100644 --- a/lokrez/export.py +++ b/lokrez/export.py @@ -1,5 +1,6 @@ import os import subprocess +import tempfile import jinja2 @@ -10,87 +11,95 @@ def generate_outfile( context, outfilename, log = None, + cachedir = None, ): # Template rendering # ------------------------------------------------------------------------- log.info("Generating SVG using '{}' template".format(templatename)) + log.debug( + "Initializing jinja2 with template dir '{}'" \ + .format(templatesdir) + ) jj2_env = jinja2.Environment( - loader = jinja2.FileSystemLoader( templatesdir ) + loader = jinja2.FileSystemLoader( + str(templatesdir), + ) ) try: jj2_tpl = jj2_env.get_template( - os.path.join( - templatename, - "template.svg.j2", - ) + # Jinja specific path format + "{}/template.svg.j2".format(templatename), ) - except: + except Exception as e: log.error("Could not find template '{}'".format(templatename)) + log.debug(e, exc_info=1) return None - extension = outfilename.split(".")[-1] - # To SVG # ------------------------------------------------------------------------- - if extension == "svg": - jj2_tpl.stream(context).dump( outfilename ) + if outfilename.suffix == ".svg": + jj2_tpl.stream(context).dump( str(outfilename) ) return outfilename # To PNG with inkscape # ------------------------------------------------------------------------- - if extension == "png": + if outfilename.suffix == ".png": - try: - log.info("Exporting to {} using inkscape".format(extension)) - import subprocess + cachedir.mkdir(parents=True, exist_ok=True) - jj2_tpl.stream(context).dump( "tmp.svg" ) - - inkscape_process = subprocess.Popen( - [ - "inkscape", - "tmp.svg", - "--export-filename", - outfilename, - ], - stdout = subprocess.PIPE, - stderr = subprocess.STDOUT, - universal_newlines = True, - ) - - for line_out in iter(inkscape_process.stdout.readline, ""): - log.debug(line_out) - - inkscape_process.stdout.close() - - rv = inkscape_process.wait() - - if rv != 0: - raise Exception( - "Bad inkscape return code '{}'" \ - .format(inkscape_process.returncode) - ) - - os.unlink("tmp.svg") - - return outfilename - - except Exception as e: - log.warning("Failed to export with inkscape") + with tempfile.NamedTemporaryFile( + suffix="svg", + dir = str(cachedir), + ) as tmpsvg: try: - os.unlink("tmp.svg") - except: - pass - log.debug(e) + 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, + ) + + for line_out in iter(inkscape_process.stdout.readline, ""): + log.debug(line_out) + + inkscape_process.stdout.close() + + rv = inkscape_process.wait() + + if rv != 0: + raise Exception( + "Bad inkscape return code '{}'" \ + .format(inkscape_process.returncode) + ) + + + return outfilename + + except Exception as e: + log.warning("Failed to export with inkscape") + log.debug(e) # 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: import cairosvg @@ -105,16 +114,16 @@ def generate_outfile( svg_str = jj2_tpl.render(context) - if extension == "png": + if outfilename.suffix == ".png": conversion_fun = cairosvg.svg2png - elif extension == "pdf": + elif outfilename.suffix == ".pdf": conversion_fun = cairosvg.svg2pdf - elif extension == "ps": + elif outfilename.suffix == ".ps": conversion_fun = cairosvg.svg2ps conversion_fun( bytestring = svg_str, - write_to = outfilename, + write_to = str(outfilename), ) return outfilename @@ -125,7 +134,7 @@ def generate_outfile( "Can't export to '{}' : unsupported format '{}'" \ .format( outfilename, - extension, + outfilename.suffix, ) ) return None