From 8d224d345a78844615091cfc2c46b881230bc1fd Mon Sep 17 00:00:00 2001 From: Matt Giuca Date: Fri, 8 Mar 2013 14:42:11 +1100 Subject: [PATCH] backend_ps: Do not write to a temporary file unless using an external distiller. If ps.usedistiller is omitted, writes directly to the output file, instead of creating a temporary file and subsequently copying it to the output. This is more efficient, and also works on restricted platforms such as Google App Engine, which are unable to provide a writable temporary file system. --- lib/matplotlib/backends/backend_ps.py | 45 ++++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 4dfe11e5345e..aa02e8959d68 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -5,6 +5,7 @@ # PY3KTODO: Get rid of "print >>fh" syntax from __future__ import division, print_function +import contextlib import glob, math, os, shutil, sys, time def _fn_name(): return sys._getframe(1).f_code.co_name import io @@ -1105,8 +1106,21 @@ def write(self, *kl, **kwargs): self.figure.set_facecolor(origfacecolor) self.figure.set_edgecolor(origedgecolor) - fd, tmpfile = mkstemp() - with io.open(fd, 'wb') as raw_fh: + if rcParams['ps.usedistiller']: + # We are going to use an external program to process the output. + # Write to a temporary file. + fd, tmpfile = mkstemp() + context_manager = io.open(fd, 'wb') + else: + # Write directly to outfile. + if passed_in_file_object: + @contextlib.contextmanager + def null_context(value): + yield value + context_manager = null_context(outfile) + else: + context_manager = open(outfile, 'wb') + with context_manager as raw_fh: if sys.version_info[0] >= 3: fh = io.TextIOWrapper(raw_fh, encoding="ascii") else: @@ -1181,20 +1195,21 @@ def write(self, *kl, **kwargs): if not isEPSF: print("%%EOF", file=fh) fh.flush() - if rcParams['ps.usedistiller'] == 'ghostscript': - gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox) - elif rcParams['ps.usedistiller'] == 'xpdf': - xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox) + if rcParams['ps.usedistiller']: + if rcParams['ps.usedistiller'] == 'ghostscript': + gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox) + elif rcParams['ps.usedistiller'] == 'xpdf': + xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox) - if passed_in_file_object: - with open(tmpfile, 'rb') as fh: - outfile.write(fh.read()) - else: - with open(outfile, 'w') as fh: - pass - mode = os.stat(outfile).st_mode - shutil.move(tmpfile, outfile) - os.chmod(outfile, mode) + if passed_in_file_object: + with open(tmpfile, 'rb') as fh: + outfile.write(fh.read()) + else: + with open(outfile, 'w') as fh: + pass + mode = os.stat(outfile).st_mode + shutil.move(tmpfile, outfile) + os.chmod(outfile, mode) def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor, orientation, isLandscape, papertype,