##// END OF EJS Templates
Don't use nbformat.current in nbconvert
MinRK -
Show More
@@ -5,7 +5,7 b''
5
5
6 from functools import wraps
6 from functools import wraps
7
7
8 from IPython.nbformat.current import NotebookNode
8 from IPython.nbformat.v4 import NotebookNode
9 from IPython.utils.decorators import undoc
9 from IPython.utils.decorators import undoc
10 from IPython.utils.py3compat import string_types
10 from IPython.utils.py3compat import string_types
11
11
@@ -2,39 +2,22 b''
2 see templateexporter.py.
2 see templateexporter.py.
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
7 #
8 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
5
17 from __future__ import print_function, absolute_import
6 from __future__ import print_function, absolute_import
18
7
19 # Stdlib imports
20 import io
8 import io
21 import os
9 import os
22 import copy
10 import copy
23 import collections
11 import collections
24 import datetime
12 import datetime
25
13
26
27 # IPython imports
28 from IPython.config.configurable import LoggingConfigurable
14 from IPython.config.configurable import LoggingConfigurable
29 from IPython.config import Config
15 from IPython.config import Config
30 from IPython.nbformat import current as nbformat
16 from IPython import nbformat
31 from IPython.utils.traitlets import MetaHasTraits, Unicode, List
17 from IPython.utils.traitlets import MetaHasTraits, Unicode, List
32 from IPython.utils.importstring import import_item
18 from IPython.utils.importstring import import_item
33 from IPython.utils import text, py3compat
19 from IPython.utils import text, py3compat
34
20
35 #-----------------------------------------------------------------------------
36 # Class
37 #-----------------------------------------------------------------------------
38
21
39 class ResourcesDict(collections.defaultdict):
22 class ResourcesDict(collections.defaultdict):
40 def __missing__(self, key):
23 def __missing__(self, key):
@@ -107,7 +90,7 b' class Exporter(LoggingConfigurable):'
107 Parameters
90 Parameters
108 ----------
91 ----------
109 nb : :class:`~IPython.nbformat.current.NotebookNode`
92 nb : :class:`~IPython.nbformat.current.NotebookNode`
110 Notebook node
93 Notebook node (dict-like with attr-access)
111 resources : dict
94 resources : dict
112 Additional resources that can be accessed read/write by
95 Additional resources that can be accessed read/write by
113 preprocessors and filters.
96 preprocessors and filters.
@@ -149,7 +132,7 b' class Exporter(LoggingConfigurable):'
149 resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
132 resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
150
133
151 with io.open(filename, encoding='utf-8') as f:
134 with io.open(filename, encoding='utf-8') as f:
152 return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw)
135 return self.from_notebook_node(nbformat.read(f, as_version=4), resources=resources, **kw)
153
136
154
137
155 def from_file(self, file_stream, resources=None, **kw):
138 def from_file(self, file_stream, resources=None, **kw):
@@ -161,7 +144,7 b' class Exporter(LoggingConfigurable):'
161 file_stream : file-like object
144 file_stream : file-like object
162 Notebook file-like object to convert.
145 Notebook file-like object to convert.
163 """
146 """
164 return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)
147 return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
165
148
166
149
167 def register_preprocessor(self, preprocessor, enabled=False):
150 def register_preprocessor(self, preprocessor, enabled=False):
@@ -4,13 +4,13 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from .exporter import Exporter
6 from .exporter import Exporter
7 from IPython.nbformat import current as nbformat
7 from IPython import nbformat
8 from IPython.utils.traitlets import Enum
8 from IPython.utils.traitlets import Enum
9
9
10 class NotebookExporter(Exporter):
10 class NotebookExporter(Exporter):
11 """Exports to an IPython notebook."""
11 """Exports to an IPython notebook."""
12
12
13 nbformat_version = Enum(list(range(2, nbformat.current_nbformat + 1)),
13 nbformat_version = Enum(list(nbformat.versions),
14 default_value=nbformat.current_nbformat,
14 default_value=nbformat.current_nbformat,
15 config=True,
15 config=True,
16 help="""The nbformat version to write.
16 help="""The nbformat version to write.
@@ -24,7 +24,7 b' class NotebookExporter(Exporter):'
24
24
25 def from_notebook_node(self, nb, resources=None, **kw):
25 def from_notebook_node(self, nb, resources=None, **kw):
26 nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
26 nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
27 if self.nbformat_version != nbformat.current_nbformat:
27 if self.nbformat_version != nb_copy.nbformat:
28 resources['output_suffix'] = '.v%i' % self.nbformat_version
28 resources['output_suffix'] = '.v%i' % self.nbformat_version
29 else:
29 else:
30 resources['output_suffix'] = '.nbconvert'
30 resources['output_suffix'] = '.nbconvert'
@@ -2,29 +2,17 b''
2 Module with tests for export.py
2 Module with tests for export.py
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 # Copyright (c) IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
8 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
7
17 import os
8 import os
18
9
19 from IPython.nbformat import current as nbformat
10 from IPython import nbformat
20
11
21 from .base import ExportersTestsBase
12 from .base import ExportersTestsBase
22 from ..export import *
13 from ..export import *
23 from ..python import PythonExporter
14 from ..python import PythonExporter
24
15
25 #-----------------------------------------------------------------------------
26 # Class
27 #-----------------------------------------------------------------------------
28
16
29 class TestExport(ExportersTestsBase):
17 class TestExport(ExportersTestsBase):
30 """Contains test functions for export.py"""
18 """Contains test functions for export.py"""
@@ -53,7 +41,7 b' class TestExport(ExportersTestsBase):'
53 Can a notebook be exported by a notebook node handle?
41 Can a notebook be exported by a notebook node handle?
54 """
42 """
55 with open(self._get_notebook(), 'r') as f:
43 with open(self._get_notebook(), 'r') as f:
56 notebook = nbformat.read(f, 'json')
44 notebook = nbformat.read(f, 4)
57 (output, resources) = export_by_name('python', notebook)
45 (output, resources) = export_by_name('python', notebook)
58 assert len(output) > 0
46 assert len(output) > 0
59
47
@@ -9,7 +9,8 b' import re'
9
9
10 from .base import ExportersTestsBase
10 from .base import ExportersTestsBase
11 from ..latex import LatexExporter
11 from ..latex import LatexExporter
12 from IPython.nbformat import current
12 from IPython.nbformat import write
13 from IPython.nbformat import v4
13 from IPython.testing.decorators import onlyif_cmds_exist
14 from IPython.testing.decorators import onlyif_cmds_exist
14 from IPython.utils.tempdir import TemporaryDirectory
15 from IPython.utils.tempdir import TemporaryDirectory
15
16
@@ -85,18 +86,18 b' class TestLatexExporter(ExportersTestsBase):'
85 large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000)
86 large_lorem_ipsum_text = "".join([lorem_ipsum_text]*3000)
86
87
87 notebook_name = "lorem_ipsum_long.ipynb"
88 notebook_name = "lorem_ipsum_long.ipynb"
88 nb = current.new_notebook(
89 nb = v4.new_notebook(
89 cells=[
90 cells=[
90 current.new_markdown_cell(source=large_lorem_ipsum_text)
91 v4.new_markdown_cell(source=large_lorem_ipsum_text)
91 ]
92 ]
92 )
93 )
93
94
94 with TemporaryDirectory() as td:
95 with TemporaryDirectory() as td:
95 nbfile = os.path.join(td, notebook_name)
96 nbfile = os.path.join(td, notebook_name)
96 with open(nbfile, 'w') as f:
97 with open(nbfile, 'w') as f:
97 current.write(nb, f, 'ipynb')
98 write(f, nb, 4)
98
99
99 (output, resources) = LatexExporter(template_file='article').from_filename(nbfile)
100 (output, resources) = LatexExporter(template_file='article').from_filename(nbfile)
100 assert len(output) > 0
101 assert len(output) > 0
101
102
102 @onlyif_cmds_exist('pandoc')
103 @onlyif_cmds_exist('pandoc')
@@ -8,7 +8,7 b' import json'
8 from .base import ExportersTestsBase
8 from .base import ExportersTestsBase
9 from ..notebook import NotebookExporter
9 from ..notebook import NotebookExporter
10
10
11 from IPython.nbformat.current import validate
11 from IPython.nbformat import validate
12 from IPython.testing.tools import assert_big_text_equal
12 from IPython.testing.tools import assert_big_text_equal
13
13
14 class TestNotebookExporter(ExportersTestsBase):
14 class TestNotebookExporter(ExportersTestsBase):
@@ -1,20 +1,12 b''
1 """Tests for RSTExporter"""
1 """Tests for RSTExporter"""
2
2
3 #-----------------------------------------------------------------------------
3 # Copyright (c) IPython Development Team.
4 # Copyright (c) 2013, the IPython Development Team.
5 #
6 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
11 #-----------------------------------------------------------------------------
12 # Imports
13 #-----------------------------------------------------------------------------
14
5
15 import io
6 import io
16
7
17 from IPython.nbformat import current
8 from IPython import nbformat
9 from IPython.nbformat import v4
18
10
19 from .base import ExportersTestsBase
11 from .base import ExportersTestsBase
20 from ..rst import RSTExporter
12 from ..rst import RSTExporter
@@ -47,14 +39,14 b' class TestRSTExporter(ExportersTestsBase):'
47 """No empty code cells in rst"""
39 """No empty code cells in rst"""
48 nbname = self._get_notebook()
40 nbname = self._get_notebook()
49 with io.open(nbname, encoding='utf8') as f:
41 with io.open(nbname, encoding='utf8') as f:
50 nb = current.read(f, 'json')
42 nb = nbformat.read(f, 4)
51
43
52 exporter = self.exporter_class()
44 exporter = self.exporter_class()
53
45
54 (output, resources) = exporter.from_notebook_node(nb)
46 (output, resources) = exporter.from_notebook_node(nb)
55 # add an empty code cell
47 # add an empty code cell
56 nb.cells.append(
48 nb.cells.append(
57 current.new_code_cell(source="")
49 v4.new_code_cell(source="")
58 )
50 )
59 (output2, resources) = exporter.from_notebook_node(nb)
51 (output2, resources) = exporter.from_notebook_node(nb)
60 # adding an empty code cell shouldn't change output
52 # adding an empty code cell shouldn't change output
@@ -4,7 +4,6 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import os
6 import os
7 import sys
8
7
9 try:
8 try:
10 from queue import Empty # Py 3
9 from queue import Empty # Py 3
@@ -13,10 +12,11 b' except ImportError:'
13
12
14 from IPython.utils.traitlets import List, Unicode
13 from IPython.utils.traitlets import List, Unicode
15
14
16 from IPython.nbformat.current import reads, writes, output_from_msg
15 from IPython.nbformat.v4 import output_from_msg
17 from .base import Preprocessor
16 from .base import Preprocessor
18 from IPython.utils.traitlets import Integer
17 from IPython.utils.traitlets import Integer
19
18
19
20 class ExecutePreprocessor(Preprocessor):
20 class ExecutePreprocessor(Preprocessor):
21 """
21 """
22 Executes all the cells in a notebook
22 Executes all the cells in a notebook
@@ -3,7 +3,7 b''
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from IPython.nbformat import current as nbformat
6 from IPython.nbformat import v4 as nbformat
7
7
8 from ...tests.base import TestsBase
8 from ...tests.base import TestsBase
9 from ...exporters.exporter import ResourcesDict
9 from ...exporters.exporter import ResourcesDict
@@ -5,8 +5,6 b' Module with tests for the clearoutput preprocessor.'
5 # Copyright (c) IPython Development Team.
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8 from IPython.nbformat import current as nbformat
9
10 from .base import PreprocessorTestsBase
8 from .base import PreprocessorTestsBase
11 from ..clearoutput import ClearOutputPreprocessor
9 from ..clearoutput import ClearOutputPreprocessor
12
10
@@ -3,7 +3,7 b''
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from IPython.nbformat import current as nbformat
6 from IPython.nbformat import v4 as nbformat
7
7
8 from .base import PreprocessorTestsBase
8 from .base import PreprocessorTestsBase
9 from ..coalescestreams import coalesce_streams
9 from ..coalescestreams import coalesce_streams
@@ -7,10 +7,11 b' Module with tests for the execute preprocessor.'
7
7
8 import copy
8 import copy
9 import glob
9 import glob
10 import io
10 import os
11 import os
11 import re
12 import re
12
13
13 from IPython.nbformat import current as nbformat
14 from IPython import nbformat
14
15
15 from .base import PreprocessorTestsBase
16 from .base import PreprocessorTestsBase
16 from ..execute import ExecutePreprocessor
17 from ..execute import ExecutePreprocessor
@@ -78,8 +79,8 b' class TestExecute(PreprocessorTestsBase):'
78 current_dir = os.path.dirname(__file__)
79 current_dir = os.path.dirname(__file__)
79 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
80 input_files = glob.glob(os.path.join(current_dir, 'files', '*.ipynb'))
80 for filename in input_files:
81 for filename in input_files:
81 with open(os.path.join(current_dir, 'files', filename)) as f:
82 with io.open(os.path.join(current_dir, 'files', filename)) as f:
82 input_nb = nbformat.read(f, 'ipynb')
83 input_nb = nbformat.read(f, 4)
83 res = self.build_resources()
84 res = self.build_resources()
84 preprocessor = self.build_preprocessor()
85 preprocessor = self.build_preprocessor()
85 cleaned_input_nb = copy.deepcopy(input_nb)
86 cleaned_input_nb = copy.deepcopy(input_nb)
@@ -3,7 +3,7 b''
3 # Copyright (c) IPython Development Team.
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from IPython.nbformat import current as nbformat
6 from IPython.nbformat import v4 as nbformat
7
7
8 from .base import PreprocessorTestsBase
8 from .base import PreprocessorTestsBase
9 from ..revealhelp import RevealHelpPreprocessor
9 from ..revealhelp import RevealHelpPreprocessor
@@ -4,7 +4,7 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 from IPython.testing import decorators as dec
6 from IPython.testing import decorators as dec
7 from IPython.nbformat import current as nbformat
7 from IPython.nbformat import v4 as nbformat
8
8
9 from .base import PreprocessorTestsBase
9 from .base import PreprocessorTestsBase
10 from ..svg2pdf import SVG2PDFPreprocessor
10 from ..svg2pdf import SVG2PDFPreprocessor
@@ -10,7 +10,7 b' import shutil'
10 import unittest
10 import unittest
11
11
12 import IPython
12 import IPython
13 from IPython.nbformat import current
13 from IPython.nbformat import v4, write
14 from IPython.utils.tempdir import TemporaryWorkingDirectory
14 from IPython.utils.tempdir import TemporaryWorkingDirectory
15 from IPython.utils.path import get_ipython_package_dir
15 from IPython.utils.path import get_ipython_package_dir
16 from IPython.utils.process import get_output_error_code
16 from IPython.utils.process import get_output_error_code
@@ -101,10 +101,9 b' class TestsBase(unittest.TestCase):'
101 return temp_dir
101 return temp_dir
102
102
103 def create_empty_notebook(self, path):
103 def create_empty_notebook(self, path):
104 nb = current.new_notebook()
104 nb = v4.new_notebook()
105 with io.open(path, 'w', encoding='utf-8') as f:
105 with io.open(path, 'w', encoding='utf-8') as f:
106 current.write(nb, f, 'json')
106 write(f, nb, 4)
107
108
107
109 def copy_files_to(self, copy_filenames, dest='.'):
108 def copy_files_to(self, copy_filenames, dest='.'):
110 "Copy test files into the destination directory"
109 "Copy test files into the destination directory"
General Comments 0
You need to be logged in to leave comments. Login now