Fix export issue

PNG came out wrong because of concurrency (?) issues where the SVG file
was converted before being fully generated. The solution was to close
the temporary file between each operation.
master
Lertsenem 2020-09-02 09:09:35 +02:00
parent 4c6408bd61
commit f35d467a40
1 changed files with 43 additions and 38 deletions

View File

@ -51,51 +51,56 @@ def generate_outfile(
cachedir.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(
tmpsvg = tempfile.NamedTemporaryFile(
suffix=".svg",
mode = "w",
delete = False,
dir = str(cachedir),
) as tmpsvg:
try:
log.info(
"Exporting to {} using inkscape" \
.format(outfilename.suffix),
)
tmpsvg.close()
try:
log.info(
"Exporting to {} using inkscape" \
.format(outfilename.suffix),
)
import subprocess
jj2_tpl.stream(context).dump( tmpsvg.name )
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)
)
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
return outfilename
except Exception as e:
log.warning("Failed to export with inkscape")
log.debug(e, exc_info=True)
except Exception as e:
log.warning("Failed to export with inkscape")
log.debug(e, exc_info=True)
finally:
os.unlink(tmpsvg.name)
# To png, pdf or ps with cairosvg
# -------------------------------------------------------------------------