* wrap/python/tests/ipnbdoctest.py: Skip if IPython is missing.

This commit is contained in:
Alexandre Duret-Lutz 2015-03-08 10:50:17 +01:00
parent 3bf3d2c8a1
commit 72c7ad9fcd

View file

@ -4,7 +4,8 @@ simple example script for running and testing notebooks.
Usage: `ipnbdoctest.py foo.ipynb [bar.ipynb [...]]` Usage: `ipnbdoctest.py foo.ipynb [bar.ipynb [...]]`
Each cell is submitted to the kernel, and the outputs are compared with those stored in the notebook. Each cell is submitted to the kernel, and the outputs are compared
with those stored in the notebook.
""" """
from __future__ import print_function from __future__ import print_function
@ -21,6 +22,13 @@ except ImportError:
print('Python 3.x is needed to run this script.') print('Python 3.x is needed to run this script.')
sys.exit(77) sys.exit(77)
import imp
try:
imp.find_module('IPython')
except:
print('IPython is needed to run this script.')
sys.exit(77)
try: try:
from IPython.kernel import KernelManager from IPython.kernel import KernelManager
except ImportError: except ImportError:
@ -42,7 +50,8 @@ def compare_png(a64, b64):
def sanitize(s): def sanitize(s):
"""sanitize a string for comparison. """sanitize a string for comparison.
fix universal newlines, strip trailing newlines, and normalize likely random values (memory addresses and UUIDs) fix universal newlines, strip trailing newlines, and normalize likely
random values (memory addresses and UUIDs)
""" """
if not isinstance(s, str): if not isinstance(s, str):
return s return s
@ -73,18 +82,20 @@ def consolidate_outputs(outputs):
elif out.type == 'pyerr': elif out.type == 'pyerr':
data['pyerr'] = dict(ename=out.ename, evalue=out.evalue) data['pyerr'] = dict(ename=out.ename, evalue=out.evalue)
else: else:
for key in ('png', 'svg', 'latex', 'html', 'javascript', 'text', 'jpeg',): for key in ('png', 'svg', 'latex', 'html',
'javascript', 'text', 'jpeg',):
if key in out: if key in out:
data[key].append(out[key]) data[key].append(out[key])
return data return data
def compare_outputs(test, ref, skip_compare=('png', 'traceback', 'latex', 'prompt_number')): def compare_outputs(test, ref, skip_cmp=('png', 'traceback',
'latex', 'prompt_number')):
for key in ref: for key in ref:
if key not in test: if key not in test:
print("missing key: %s != %s" % (test.keys(), ref.keys())) print("missing key: %s != %s" % (test.keys(), ref.keys()))
return False return False
elif key not in skip_compare and sanitize(test[key]) != sanitize(ref[key]): elif key not in skip_cmp and sanitize(test[key]) != sanitize(ref[key]):
print("mismatch %s:" % key) print("mismatch %s:" % key)
exp = ref[key] exp = ref[key]
eff = test[key] eff = test[key]
@ -146,7 +157,8 @@ def run_cell(shell, iopub, cell):
def test_notebook(nb): def test_notebook(nb):
km = KernelManager() km = KernelManager()
km.start_kernel(extra_arguments=['--pylab=inline'], stderr=open(os.devnull, 'w')) km.start_kernel(extra_arguments=['--pylab=inline'],
stderr=open(os.devnull, 'w'))
try: try:
kc = km.client() kc = km.client()
kc.start_channels() kc.start_channels()