From f1ae99ef57572ed62a989696f2a5d26869bcdb20 2014-10-08 19:33:32 From: MinRK Date: 2014-10-08 19:33:32 Subject: [PATCH] fix some validation bugs in v3 Python API had many inconsistencies with the actual spec --- diff --git a/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py b/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py index 1df7e0e..baa9db9 100644 --- a/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py +++ b/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py @@ -1,27 +1,14 @@ -""" -Module with tests for the coalescestreams preprocessor -""" +"""Tests for the coalescestreams preprocessor""" -#----------------------------------------------------------------------------- -# Copyright (c) 2013, the IPython Development Team. -# +# Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- from IPython.nbformat import current as nbformat from .base import PreprocessorTestsBase from ..coalescestreams import coalesce_streams -#----------------------------------------------------------------------------- -# Class -#----------------------------------------------------------------------------- class TestCoalesceStreams(PreprocessorTestsBase): """Contains test functions for coalescestreams.py""" @@ -47,7 +34,7 @@ class TestCoalesceStreams(PreprocessorTestsBase): nbformat.new_output(output_type="stream", stream="stdout", output_text="6"), nbformat.new_output(output_type="stream", stream="stdout", output_text="7")] cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)] - worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] + worksheets = [nbformat.new_worksheet(cells=cells)] nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets) res = self.build_resources() @@ -64,7 +51,7 @@ class TestCoalesceStreams(PreprocessorTestsBase): nbformat.new_output(output_type="stream", stream="stdout", output_text="\rc\n"), nbformat.new_output(output_type="stream", stream="stdout", output_text="z\rz\rd")] cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)] - worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] + worksheets = [nbformat.new_worksheet(cells=cells)] nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets) res = self.build_resources() diff --git a/IPython/nbconvert/preprocessors/tests/test_revealhelp.py b/IPython/nbconvert/preprocessors/tests/test_revealhelp.py index 07b27fc..5f73cfc 100644 --- a/IPython/nbconvert/preprocessors/tests/test_revealhelp.py +++ b/IPython/nbconvert/preprocessors/tests/test_revealhelp.py @@ -1,18 +1,7 @@ -""" -Module with tests for the revealhelp preprocessor -""" +"""Tests for the revealhelp preprocessor""" -#----------------------------------------------------------------------------- -# Copyright (c) 2013, the IPython Development Team. -# +# Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- - -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- from IPython.nbformat import current as nbformat @@ -20,10 +9,6 @@ from .base import PreprocessorTestsBase from ..revealhelp import RevealHelpPreprocessor -#----------------------------------------------------------------------------- -# Class -#----------------------------------------------------------------------------- - class Testrevealhelp(PreprocessorTestsBase): """Contains test functions for revealhelp.py""" @@ -41,7 +26,7 @@ class Testrevealhelp(PreprocessorTestsBase): nbformat.new_code_cell(input="", prompt_number=2, outputs=outputs), nbformat.new_text_cell('markdown', source="", metadata=slide_metadata), nbformat.new_text_cell('markdown', source="", metadata=subslide_metadata)] - worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)] + worksheets = [nbformat.new_worksheet(cells=cells)] return nbformat.new_notebook(name="notebook1", worksheets=worksheets) diff --git a/IPython/nbformat/v3/nbbase.py b/IPython/nbformat/v3/nbbase.py index 42f9224..8b08b34 100644 --- a/IPython/nbformat/v3/nbbase.py +++ b/IPython/nbformat/v3/nbbase.py @@ -53,7 +53,6 @@ def new_output(output_type, output_text=None, output_png=None, metadata = {} if not isinstance(metadata, dict): raise TypeError("metadata must be dict") - output.metadata = metadata if output_type != 'pyerr': if output_text is not None: @@ -87,6 +86,8 @@ def new_output(output_type, output_text=None, output_png=None, if output_type == u'stream': output.stream = 'stdout' if stream is None else cast_unicode(stream) + else: + output.metadata = metadata return output @@ -121,21 +122,17 @@ def new_text_cell(cell_type, source=None, rendered=None, metadata=None): cell_type = 'raw' if source is not None: cell.source = cast_unicode(source) - if rendered is not None: - cell.rendered = cast_unicode(rendered) cell.metadata = NotebookNode(metadata or {}) cell.cell_type = cell_type return cell -def new_heading_cell(source=None, rendered=None, level=1, metadata=None): +def new_heading_cell(source=None, level=1, rendered=None, metadata=None): """Create a new section cell with a given integer level.""" cell = NotebookNode() cell.cell_type = u'heading' if source is not None: cell.source = cast_unicode(source) - if rendered is not None: - cell.rendered = cast_unicode(rendered) cell.level = int(level) cell.metadata = NotebookNode(metadata or {}) return cell @@ -144,8 +141,6 @@ def new_heading_cell(source=None, rendered=None, level=1, metadata=None): def new_worksheet(name=None, cells=None, metadata=None): """Create a worksheet by name with with a list of cells.""" ws = NotebookNode() - if name is not None: - ws.name = cast_unicode(name) if cells is None: ws.cells = [] else: diff --git a/IPython/nbformat/v3/nbformat.v3.schema.json b/IPython/nbformat/v3/nbformat.v3.schema.json index 87e9615..946a89e 100644 --- a/IPython/nbformat/v3/nbformat.v3.schema.json +++ b/IPython/nbformat/v3/nbformat.v3.schema.json @@ -117,7 +117,7 @@ "properties": { "cell_type": { "description": "String identifying the type of cell.", - "enum": ["markdown"] + "enum": ["markdown", "html"] }, "metadata": { "description": "Cell-level metadata.", 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/v3/tests/test_nbbase.py b/IPython/nbformat/v3/tests/test_nbbase.py index 351fe5d..0526657 100644 --- a/IPython/nbformat/v3/tests/test_nbbase.py +++ b/IPython/nbformat/v3/tests/test_nbbase.py @@ -41,45 +41,37 @@ class TestCell(TestCase): tc = new_text_cell(u'html') self.assertEqual(tc.cell_type, u'html') self.assertEqual(u'source' not in tc, True) - self.assertEqual(u'rendered' not in tc, True) def test_html_cell(self): - tc = new_text_cell(u'html', 'hi', 'hi') + tc = new_text_cell(u'html', 'hi') self.assertEqual(tc.source, u'hi') - self.assertEqual(tc.rendered, u'hi') def test_empty_markdown_cell(self): tc = new_text_cell(u'markdown') self.assertEqual(tc.cell_type, u'markdown') self.assertEqual(u'source' not in tc, True) - self.assertEqual(u'rendered' not in tc, True) def test_markdown_cell(self): - tc = new_text_cell(u'markdown', 'hi', 'hi') + tc = new_text_cell(u'markdown', 'hi') self.assertEqual(tc.source, u'hi') - self.assertEqual(tc.rendered, u'hi') def test_empty_raw_cell(self): tc = new_text_cell(u'raw') self.assertEqual(tc.cell_type, u'raw') self.assertEqual(u'source' not in tc, True) - self.assertEqual(u'rendered' not in tc, True) def test_raw_cell(self): - tc = new_text_cell(u'raw', 'hi', 'hi') + tc = new_text_cell(u'raw', 'hi') self.assertEqual(tc.source, u'hi') - self.assertEqual(tc.rendered, u'hi') def test_empty_heading_cell(self): tc = new_heading_cell() self.assertEqual(tc.cell_type, u'heading') self.assertEqual(u'source' not in tc, True) - self.assertEqual(u'rendered' not in tc, True) def test_heading_cell(self): - tc = new_heading_cell(u'hi', u'hi', level=2) + tc = new_heading_cell(u'hi', level=2) self.assertEqual(tc.source, u'hi') - self.assertEqual(tc.rendered, u'hi') self.assertEqual(tc.level, 2) @@ -92,9 +84,8 @@ class TestWorksheet(TestCase): def test_worksheet(self): cells = [new_code_cell(), new_text_cell(u'html')] - ws = new_worksheet(cells=cells,name=u'foo') + ws = new_worksheet(cells=cells) self.assertEqual(ws.cells,cells) - self.assertEqual(ws.name,u'foo') class TestNotebook(TestCase):