From 9867311c03dc803dd254935233109e53c609d593 2014-11-01 23:41:10 From: MinRK Date: 2014-11-01 23:41:10 Subject: [PATCH] Don't use nbformat.current in nbconvert --- diff --git a/IPython/nbconvert/exporters/export.py b/IPython/nbconvert/exporters/export.py index d6110e2..e6df098 100644 --- a/IPython/nbconvert/exporters/export.py +++ b/IPython/nbconvert/exporters/export.py @@ -5,7 +5,7 @@ from functools import wraps -from IPython.nbformat.current import NotebookNode +from IPython.nbformat.v4 import NotebookNode from IPython.utils.decorators import undoc from IPython.utils.py3compat import string_types diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index 775fe39..77973ba 100644 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -2,39 +2,22 @@ see templateexporter.py. """ -#----------------------------------------------------------------------------- -# Copyright (c) 2013, the 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 __future__ import print_function, absolute_import -# Stdlib imports import io import os import copy import collections import datetime - -# IPython imports from IPython.config.configurable import LoggingConfigurable from IPython.config import Config -from IPython.nbformat import current as nbformat +from IPython import nbformat from IPython.utils.traitlets import MetaHasTraits, Unicode, List from IPython.utils.importstring import import_item from IPython.utils import text, py3compat -#----------------------------------------------------------------------------- -# Class -#----------------------------------------------------------------------------- class ResourcesDict(collections.defaultdict): def __missing__(self, key): @@ -107,7 +90,7 @@ class Exporter(LoggingConfigurable): Parameters ---------- nb : :class:`~IPython.nbformat.current.NotebookNode` - Notebook node + Notebook node (dict-like with attr-access) resources : dict Additional resources that can be accessed read/write by preprocessors and filters. @@ -149,7 +132,7 @@ class Exporter(LoggingConfigurable): resources['metadata']['modified_date'] = modified_date.strftime(text.date_format) with io.open(filename, encoding='utf-8') as f: - return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw) + return self.from_notebook_node(nbformat.read(f, as_version=4), resources=resources, **kw) def from_file(self, file_stream, resources=None, **kw): @@ -161,7 +144,7 @@ class Exporter(LoggingConfigurable): file_stream : file-like object Notebook file-like object to convert. """ - return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw) + return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw) def register_preprocessor(self, preprocessor, enabled=False): diff --git a/IPython/nbconvert/exporters/notebook.py b/IPython/nbconvert/exporters/notebook.py index 65250a3..61c9bc6 100644 --- a/IPython/nbconvert/exporters/notebook.py +++ b/IPython/nbconvert/exporters/notebook.py @@ -4,13 +4,13 @@ # Distributed under the terms of the Modified BSD License. from .exporter import Exporter -from IPython.nbformat import current as nbformat +from IPython import nbformat from IPython.utils.traitlets import Enum class NotebookExporter(Exporter): """Exports to an IPython notebook.""" - nbformat_version = Enum(list(range(2, nbformat.current_nbformat + 1)), + nbformat_version = Enum(list(nbformat.versions), default_value=nbformat.current_nbformat, config=True, help="""The nbformat version to write. @@ -24,7 +24,7 @@ class NotebookExporter(Exporter): def from_notebook_node(self, nb, resources=None, **kw): nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw) - if self.nbformat_version != nbformat.current_nbformat: + if self.nbformat_version != nb_copy.nbformat: resources['output_suffix'] = '.v%i' % self.nbformat_version else: resources['output_suffix'] = '.nbconvert' diff --git a/IPython/nbconvert/exporters/tests/test_export.py b/IPython/nbconvert/exporters/tests/test_export.py index ccf7de5..1b7292a 100644 --- a/IPython/nbconvert/exporters/tests/test_export.py +++ b/IPython/nbconvert/exporters/tests/test_export.py @@ -2,29 +2,17 @@ Module with tests for export.py """ -#----------------------------------------------------------------------------- -# 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 -#----------------------------------------------------------------------------- import os -from IPython.nbformat import current as nbformat +from IPython import nbformat from .base import ExportersTestsBase from ..export import * from ..python import PythonExporter -#----------------------------------------------------------------------------- -# Class -#----------------------------------------------------------------------------- class TestExport(ExportersTestsBase): """Contains test functions for export.py""" @@ -53,7 +41,7 @@ class TestExport(ExportersTestsBase): Can a notebook be exported by a notebook node handle? """ with open(self._get_notebook(), 'r') as f: - notebook = nbformat.read(f, 'json') + notebook = nbformat.read(f, 4) (output, resources) = export_by_name('python', notebook) assert len(output) > 0 diff --git a/IPython/nbconvert/exporters/tests/test_latex.py b/IPython/nbconvert/exporters/tests/test_latex.py index 2ccd201..da368dd 100644 --- a/IPython/nbconvert/exporters/tests/test_latex.py +++ b/IPython/nbconvert/exporters/tests/test_latex.py @@ -9,7 +9,8 @@ import re from .base import ExportersTestsBase from ..latex import LatexExporter -from IPython.nbformat import current +from IPython.nbformat import write +from IPython.nbformat import v4 from IPython.testing.decorators import onlyif_cmds_exist from IPython.utils.tempdir import TemporaryDirectory @@ -85,18 +86,18 @@ class TestLatexExporter(ExportersTestsBase): large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000) notebook_name = "lorem_ipsum_long.ipynb" - nb = current.new_notebook( + nb = v4.new_notebook( cells=[ - current.new_markdown_cell(source=large_lorem_ipsum_text) + v4.new_markdown_cell(source=large_lorem_ipsum_text) ] ) with TemporaryDirectory() as td: nbfile = os.path.join(td, notebook_name) with open(nbfile, 'w') as f: - current.write(nb, f, 'ipynb') + write(f, nb, 4) - (output, resources) = LatexExporter(template_file='article').from_filename(nbfile) + (output, resources) = LatexExporter(template_file='article').from_filename(nbfile) assert len(output) > 0 @onlyif_cmds_exist('pandoc') diff --git a/IPython/nbconvert/exporters/tests/test_notebook.py b/IPython/nbconvert/exporters/tests/test_notebook.py index 2af5491..4fefb1c 100644 --- a/IPython/nbconvert/exporters/tests/test_notebook.py +++ b/IPython/nbconvert/exporters/tests/test_notebook.py @@ -8,7 +8,7 @@ import json from .base import ExportersTestsBase from ..notebook import NotebookExporter -from IPython.nbformat.current import validate +from IPython.nbformat import validate from IPython.testing.tools import assert_big_text_equal class TestNotebookExporter(ExportersTestsBase): diff --git a/IPython/nbconvert/exporters/tests/test_rst.py b/IPython/nbconvert/exporters/tests/test_rst.py index 8d35844..1ccdb80 100644 --- a/IPython/nbconvert/exporters/tests/test_rst.py +++ b/IPython/nbconvert/exporters/tests/test_rst.py @@ -1,20 +1,12 @@ """Tests for RSTExporter""" -#----------------------------------------------------------------------------- -# 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 -#----------------------------------------------------------------------------- import io -from IPython.nbformat import current +from IPython import nbformat +from IPython.nbformat import v4 from .base import ExportersTestsBase from ..rst import RSTExporter @@ -47,14 +39,14 @@ class TestRSTExporter(ExportersTestsBase): """No empty code cells in rst""" nbname = self._get_notebook() with io.open(nbname, encoding='utf8') as f: - nb = current.read(f, 'json') + nb = nbformat.read(f, 4) exporter = self.exporter_class() (output, resources) = exporter.from_notebook_node(nb) # add an empty code cell nb.cells.append( - current.new_code_cell(source="") + v4.new_code_cell(source="") ) (output2, resources) = exporter.from_notebook_node(nb) # adding an empty code cell shouldn't change output diff --git a/IPython/nbconvert/preprocessors/execute.py b/IPython/nbconvert/preprocessors/execute.py index 12077c5..4360b6b 100644 --- a/IPython/nbconvert/preprocessors/execute.py +++ b/IPython/nbconvert/preprocessors/execute.py @@ -4,7 +4,6 @@ # Distributed under the terms of the Modified BSD License. import os -import sys try: from queue import Empty # Py 3 @@ -13,10 +12,11 @@ except ImportError: from IPython.utils.traitlets import List, Unicode -from IPython.nbformat.current import reads, writes, output_from_msg +from IPython.nbformat.v4 import output_from_msg from .base import Preprocessor from IPython.utils.traitlets import Integer + class ExecutePreprocessor(Preprocessor): """ Executes all the cells in a notebook diff --git a/IPython/nbconvert/preprocessors/tests/base.py b/IPython/nbconvert/preprocessors/tests/base.py index 2e1bfa1..e27cd57 100644 --- a/IPython/nbconvert/preprocessors/tests/base.py +++ b/IPython/nbconvert/preprocessors/tests/base.py @@ -3,7 +3,7 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from IPython.nbformat import current as nbformat +from IPython.nbformat import v4 as nbformat from ...tests.base import TestsBase from ...exporters.exporter import ResourcesDict diff --git a/IPython/nbconvert/preprocessors/tests/test_clearoutput.py b/IPython/nbconvert/preprocessors/tests/test_clearoutput.py index 21f4c2e..78db651 100644 --- a/IPython/nbconvert/preprocessors/tests/test_clearoutput.py +++ b/IPython/nbconvert/preprocessors/tests/test_clearoutput.py @@ -5,8 +5,6 @@ Module with tests for the clearoutput preprocessor. # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from IPython.nbformat import current as nbformat - from .base import PreprocessorTestsBase from ..clearoutput import ClearOutputPreprocessor diff --git a/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py b/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py index f65a7ff..bded340 100644 --- a/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py +++ b/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py @@ -3,7 +3,7 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from IPython.nbformat import current as nbformat +from IPython.nbformat import v4 as nbformat from .base import PreprocessorTestsBase from ..coalescestreams import coalesce_streams diff --git a/IPython/nbconvert/preprocessors/tests/test_execute.py b/IPython/nbconvert/preprocessors/tests/test_execute.py index 2a62144..2cf426f 100644 --- a/IPython/nbconvert/preprocessors/tests/test_execute.py +++ b/IPython/nbconvert/preprocessors/tests/test_execute.py @@ -7,10 +7,11 @@ Module with tests for the execute preprocessor. import copy import glob +import io import os import re -from IPython.nbformat import current as nbformat +from IPython import nbformat from .base import PreprocessorTestsBase from ..execute import ExecutePreprocessor @@ -78,8 +79,8 @@ class TestExecute(PreprocessorTestsBase): current_dir = os.path.dirname(__file__) input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb')) for filename in input_files: - with open(os.path.join(current_dir, 'files', filename)) as f: - input_nb = nbformat.read(f, 'ipynb') + with io.open(os.path.join(current_dir, 'files', filename)) as f: + input_nb = nbformat.read(f, 4) res = self.build_resources() preprocessor = self.build_preprocessor() cleaned_input_nb = copy.deepcopy(input_nb) diff --git a/IPython/nbconvert/preprocessors/tests/test_revealhelp.py b/IPython/nbconvert/preprocessors/tests/test_revealhelp.py index 49c8894..07fb23a 100644 --- a/IPython/nbconvert/preprocessors/tests/test_revealhelp.py +++ b/IPython/nbconvert/preprocessors/tests/test_revealhelp.py @@ -3,7 +3,7 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -from IPython.nbformat import current as nbformat +from IPython.nbformat import v4 as nbformat from .base import PreprocessorTestsBase from ..revealhelp import RevealHelpPreprocessor diff --git a/IPython/nbconvert/preprocessors/tests/test_svg2pdf.py b/IPython/nbconvert/preprocessors/tests/test_svg2pdf.py index d3bd16c..e69e926 100644 --- a/IPython/nbconvert/preprocessors/tests/test_svg2pdf.py +++ b/IPython/nbconvert/preprocessors/tests/test_svg2pdf.py @@ -4,7 +4,7 @@ # Distributed under the terms of the Modified BSD License. from IPython.testing import decorators as dec -from IPython.nbformat import current as nbformat +from IPython.nbformat import v4 as nbformat from .base import PreprocessorTestsBase from ..svg2pdf import SVG2PDFPreprocessor diff --git a/IPython/nbconvert/tests/base.py b/IPython/nbconvert/tests/base.py index c4affc8..61664d6 100644 --- a/IPython/nbconvert/tests/base.py +++ b/IPython/nbconvert/tests/base.py @@ -10,7 +10,7 @@ import shutil import unittest import IPython -from IPython.nbformat import current +from IPython.nbformat import v4, write from IPython.utils.tempdir import TemporaryWorkingDirectory from IPython.utils.path import get_ipython_package_dir from IPython.utils.process import get_output_error_code @@ -101,10 +101,9 @@ class TestsBase(unittest.TestCase): return temp_dir def create_empty_notebook(self, path): - nb = current.new_notebook() + nb = v4.new_notebook() with io.open(path, 'w', encoding='utf-8') as f: - current.write(nb, f, 'json') - + write(f, nb, 4) def copy_files_to(self, copy_filenames, dest='.'): "Copy test files into the destination directory"