Skip to content

Commit

Permalink
Use the same gs process to convert all of the images from PDF to PS
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Jun 13, 2012
1 parent d638c9b commit bf23f41
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 27 deletions.
51 changes: 24 additions & 27 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import matplotlib
from matplotlib.testing.noseclasses import ImageComparisonFailure
from matplotlib.testing import image_util
from matplotlib.testing import image_util, util
from matplotlib import _png
from matplotlib import _get_configdir
from distutils import version
Expand Down Expand Up @@ -138,32 +138,29 @@ def convert(old, new):

return convert

try:
import ghostscript
except ImportError:
if matplotlib.checkdep_ghostscript() is not None:
# FIXME: make checkdep_ghostscript return the command
if sys.platform == 'win32':
gs = 'gswin32c'
else:
gs = 'gs'
cmd = lambda old, new: \
[gs, '-q', '-sDEVICE=png16m', '-dNOPAUSE', '-dBATCH', '-dSAFER',
'-sOutputFile=' + new, old]
converter['pdf'] = make_external_conversion_command(cmd)
converter['eps'] = make_external_conversion_command(cmd)
else:
def ghostscript_lib_converter(old, new):
args = [
'matplotlib', '-q', '-sDEVICE=png16m', '-dNOPAUSE', '-dBATCH',
'-dSAFER', '-sOutputFile=' + new, old]
try:
gs = ghostscript.Ghostscript(*args)
finally:
gs.exit()
ghostscript.cleanup()
converter['pdf'] = ghostscript_lib_converter
converter['eps'] = ghostscript_lib_converter
if matplotlib.checkdep_ghostscript() is not None:
def make_ghostscript_conversion_command():
# FIXME: make checkdep_ghostscript return the command
if sys.platform == 'win32':
gs = 'gswin32c'
else:
gs = 'gs'
cmd = [gs, '-q', '-sDEVICE=png16m', '-sOutputFile=-']

process = util.MiniExpect(cmd)

def do_convert(old, new):
process.expect("GS>")
process.sendline("(%s) run" % old)
with open(new, 'wb') as fd:
process.expect(">>showpage, press <return> to continue<<", fd)
process.sendline('')

return do_convert

converter['pdf'] = make_ghostscript_conversion_command()
converter['eps'] = make_ghostscript_conversion_command()


if matplotlib.checkdep_inkscape() is not None:
cmd = lambda old, new: \
Expand Down
67 changes: 67 additions & 0 deletions lib/matplotlib/testing/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import subprocess


class MiniExpect:
"""
This is a very basic version of pexpect, providing only the
functionality necessary for the testing framework, built on top of
`subprocess` rather than directly on lower-level calls.
"""
def __init__(self, args):
"""
Start the subprocess so it may start accepting commands.
*args* is a list of commandline arguments to pass to
`subprocess.Popen`.
"""
self._name = args[0]
self._process = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

def check_alive(self):
"""
Raises a RuntimeError if the process is no longer alive.
"""
returncode = self._process.poll()
if returncode is not None:
raise RuntimeError("%s unexpectedly quit" % self._name)

def sendline(self, line):
"""
Send a line to the process.
"""
self.check_alive()
stdin = self._process.stdin
stdin.write(line)
stdin.write('\n')
stdin.flush()

def expect(self, s, output=None):
"""
Wait for the string *s* to appear in the child process's output.
*output* (optional) is a writable file object where all of the
content preceding *s* will be written.
"""
self.check_alive()
read = self._process.stdout.read
pos = 0
buf = ''
while True:
char = read(1)
if not char:
raise IOError("Unexpected end-of-file")
elif char == s[pos]:
buf += char
pos += 1
if pos == len(s):
return
else:
if output is not None:
output.write(buf)
output.write(char)
buf = ''
pos = 0

0 comments on commit bf23f41

Please sign in to comment.