From 455f5fd19984d993afd8cdcccdc172b7ef8ad064 2014-11-06 19:51:05 From: Min RK Date: 2014-11-06 19:51:05 Subject: [PATCH] enable reading v4 notebooks - add v4 to reader - validation is not backported - update some tests to reflect this - test opening v4 notebooks --- diff --git a/IPython/nbformat/reader.py b/IPython/nbformat/reader.py index ded716c..68829dc 100644 --- a/IPython/nbformat/reader.py +++ b/IPython/nbformat/reader.py @@ -21,11 +21,13 @@ import json from . import v1 from . import v2 from . import v3 +from . import v4 versions = { 1: v1, 2: v2, 3: v3, + 4: v4, } #----------------------------------------------------------------------------- diff --git a/IPython/nbformat/tests/test_current.py b/IPython/nbformat/tests/test_current.py index 8fc99ba..85a5a4b 100644 --- a/IPython/nbformat/tests/test_current.py +++ b/IPython/nbformat/tests/test_current.py @@ -1,31 +1,19 @@ """ Contains tests class for current.py """ -#----------------------------------------------------------------------------- -# Copyright (C) 2013 The IPython Development Team -# -# Distributed under the terms of the BSD License. The full license is in -# the file COPYING, distributed as part of this software. -#----------------------------------------------------------------------------- -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. from .base import TestsBase from ..reader import get_version from ..current import read, current_nbformat -#----------------------------------------------------------------------------- -# Classes and functions -#----------------------------------------------------------------------------- - class TestCurrent(TestsBase): - def test_read(self): - """Can older notebooks be opened and automatically converted to the current - nbformat?""" + def test_read_v2(self): + """Can v2 notebooks be opened into the current nbformat?""" # Open a version 2 notebook. with self.fopen(u'test2.ipynb', u'r') as f: @@ -34,3 +22,14 @@ class TestCurrent(TestsBase): # Check that the notebook was upgraded to the latest version automatically. (major, minor) = get_version(nb) self.assertEqual(major, current_nbformat) + + def test_read_v4(self): + """Can v4 notebooks be opened into the current nbformat?""" + + # open a v4 notebook + with self.fopen(u'test4.ipynb', u'r') as f: + nb = read(f, u'json') + + # Check that the notebook was converted to current automatically. + (major, minor) = get_version(nb) + self.assertEqual(major, current_nbformat) diff --git a/IPython/nbformat/v3/tests/nbexamples.py b/IPython/nbformat/v3/tests/nbexamples.py index 713957d..898a032 100644 --- a/IPython/nbformat/v3/tests/nbexamples.py +++ b/IPython/nbformat/v3/tests/nbexamples.py @@ -13,12 +13,11 @@ from ..nbbase import ( png = encodestring(os.urandom(5)).decode('ascii') jpeg = encodestring(os.urandom(6)).decode('ascii') -ws = new_worksheet(name='worksheet1') +ws = new_worksheet() ws.cells.append(new_text_cell( u'html', source='Some NumPy Examples', - rendered='Some NumPy Examples' )) @@ -31,7 +30,6 @@ ws.cells.append(new_code_cell( ws.cells.append(new_text_cell( u'markdown', source='A random array', - rendered='A random array' )) ws.cells.append(new_text_cell( @@ -70,7 +68,7 @@ ws.cells.append(new_code_cell( output_png=png, output_jpeg=jpeg, output_svg=u'', - output_json=u'json data', + output_json=u'{"json": "data"}', output_javascript=u'var i=0;', prompt_number=3 ),new_output( @@ -81,7 +79,7 @@ ws.cells.append(new_code_cell( output_png=png, output_jpeg=jpeg, output_svg=u'', - output_json=u'json data', + output_json=u'{"json": "data"}', output_javascript=u'var i=0;' ),new_output( output_type=u'pyerr', @@ -104,7 +102,7 @@ md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here', modified=u'8601_goes_here',gistid=u'21341231',authors=authors) nb0 = new_notebook( - worksheets=[ws, new_worksheet(name='worksheet2')], + worksheets=[ws, new_worksheet()], metadata=md ) diff --git a/IPython/nbformat/v4/convert.py b/IPython/nbformat/v4/convert.py index 9109c14..a78893c 100644 --- a/IPython/nbformat/v4/convert.py +++ b/IPython/nbformat/v4/convert.py @@ -12,15 +12,10 @@ from .nbbase import ( ) from IPython.nbformat import v3 -from IPython.utils.log import get_logger def _warn_if_invalid(nb, version): """Log validation errors, if there are any.""" - from IPython.nbformat import validate, ValidationError - try: - validate(nb, version=version) - except ValidationError as e: - get_logger().error("Notebook JSON is not valid v%i: %s", version, e) + return def upgrade(nb, from_version=3, from_minor=0): """Convert a notebook to v4. diff --git a/IPython/nbformat/v4/nbbase.py b/IPython/nbformat/v4/nbbase.py index 1827bf7..b4b4e9d 100644 --- a/IPython/nbformat/v4/nbbase.py +++ b/IPython/nbformat/v4/nbbase.py @@ -18,9 +18,8 @@ nbformat_schema = 'nbformat.v4.schema.json' def validate(node, ref=None): - """validate a v4 node""" - from .. import validate - return validate(node, ref=ref, version=nbformat) + """nbformat validation is not backported""" + return def new_output(output_type, data=None, **kwargs): diff --git a/IPython/nbformat/v4/tests/test_convert.py b/IPython/nbformat/v4/tests/test_convert.py index 04f871b..bb7ab94 100644 --- a/IPython/nbformat/v4/tests/test_convert.py +++ b/IPython/nbformat/v4/tests/test_convert.py @@ -3,7 +3,7 @@ import copy import nose.tools as nt -from IPython.nbformat import validate +from ..nbbase import validate from .. import convert from . import nbexamples diff --git a/IPython/nbformat/v4/tests/test_nbbase.py b/IPython/nbformat/v4/tests/test_nbbase.py index 6172657..fda9388 100644 --- a/IPython/nbformat/v4/tests/test_nbbase.py +++ b/IPython/nbformat/v4/tests/test_nbbase.py @@ -3,7 +3,6 @@ import nose.tools as nt -from IPython.nbformat.validator import isvalid, validate, ValidationError from ..nbbase import ( NotebookNode, nbformat, new_code_cell, new_markdown_cell, new_notebook, diff --git a/IPython/nbformat/v4/tests/test_validate.py b/IPython/nbformat/v4/tests/test_validate.py deleted file mode 100644 index dbef2cb..0000000 --- a/IPython/nbformat/v4/tests/test_validate.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Tests for nbformat validation""" - -# Copyright (c) IPython Development Team. -# Distributed under the terms of the Modified BSD License. - -import io -import os - -import nose.tools as nt - -from IPython.nbformat.validator import validate, ValidationError -from ..nbjson import reads -from ..nbbase import ( - nbformat, - new_code_cell, new_markdown_cell, new_notebook, - new_output, new_raw_cell, -) - -def validate4(obj, ref=None): - return validate(obj, ref, version=nbformat) - -def test_valid_code_cell(): - cell = new_code_cell() - validate4(cell, 'code_cell') - -def test_invalid_code_cell(): - cell = new_code_cell() - - cell['source'] = 5 - with nt.assert_raises(ValidationError): - validate4(cell, 'code_cell') - - cell = new_code_cell() - del cell['metadata'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'code_cell') - - cell = new_code_cell() - del cell['source'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'code_cell') - - cell = new_code_cell() - del cell['cell_type'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'code_cell') - -def test_invalid_markdown_cell(): - cell = new_markdown_cell() - - cell['source'] = 5 - with nt.assert_raises(ValidationError): - validate4(cell, 'markdown_cell') - - cell = new_markdown_cell() - del cell['metadata'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'markdown_cell') - - cell = new_markdown_cell() - del cell['source'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'markdown_cell') - - cell = new_markdown_cell() - del cell['cell_type'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'markdown_cell') - -def test_invalid_raw_cell(): - cell = new_raw_cell() - - cell['source'] = 5 - with nt.assert_raises(ValidationError): - validate4(cell, 'raw_cell') - - cell = new_raw_cell() - del cell['metadata'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'raw_cell') - - cell = new_raw_cell() - del cell['source'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'raw_cell') - - cell = new_raw_cell() - del cell['cell_type'] - - with nt.assert_raises(ValidationError): - validate4(cell, 'raw_cell') - -def test_sample_notebook(): - here = os.path.dirname(__file__) - with io.open(os.path.join(here, os.pardir, os.pardir, 'tests', "test4.ipynb"), encoding='utf-8') as f: - nb = reads(f.read()) - validate4(nb)