don't use nbformat.current in IPython.html

use top-level nbformat.read/write, v4 directly for compose
This commit is contained in:
MinRK 2014-10-23 15:35:01 -07:00 committed by Min RK
parent ae0b46aa3f
commit af735018f3
8 changed files with 36 additions and 30 deletions

View File

@ -13,7 +13,7 @@ from ..base.handlers import (
IPythonHandler, FilesRedirectHandler,
notebook_path_regex, path_regex,
)
from IPython.nbformat.current import from_dict
from IPython.nbformat import from_dict
from IPython.utils.py3compat import cast_bytes

View File

@ -10,9 +10,10 @@ import requests
from IPython.html.utils import url_path_join
from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
from IPython.nbformat.current import (new_notebook, write,
new_markdown_cell, new_code_cell,
new_output)
from IPython.nbformat import write
from IPython.nbformat.v4 import (
new_notebook, new_markdown_cell, new_code_cell, new_output,
)
from IPython.testing.decorators import onlyif_cmds_exist
@ -66,7 +67,7 @@ class APITest(NotebookTestBase):
with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
encoding='utf-8') as f:
write(nb, f)
write(f, nb, version=4)
self.nbconvert_api = NbconvertAPI(self.base_url())

View File

@ -12,7 +12,7 @@ import shutil
from tornado import web
from .manager import ContentsManager
from IPython.nbformat import current
from IPython import nbformat
from IPython.utils.io import atomic_writing
from IPython.utils.path import ensure_dir_exists
from IPython.utils.traitlets import Unicode, Bool, TraitError
@ -253,7 +253,7 @@ class FileContentsManager(ContentsManager):
os_path = self._get_os_path(name, path)
with io.open(os_path, 'r', encoding='utf-8') as f:
try:
nb = current.read(f, u'json')
nb = nbformat.read(f, as_version=4)
except Exception as e:
raise web.HTTPError(400, u"Unreadable Notebook: %s %r" % (os_path, e))
self.mark_trusted_cells(nb, name, path)
@ -295,7 +295,7 @@ class FileContentsManager(ContentsManager):
def _save_notebook(self, os_path, model, name='', path=''):
"""save a notebook file"""
# Save the notebook file
nb = current.from_dict(model['content'])
nb = nbformat.from_dict(model['content'])
self.check_and_sign(nb, name, path)
@ -303,7 +303,7 @@ class FileContentsManager(ContentsManager):
nb['metadata']['name'] = u''
with atomic_writing(os_path, encoding='utf-8') as f:
current.write(nb, f, version=nb.nbformat)
nbformat.write(f, nb, version=nbformat.NO_CONVERT)
def _save_file(self, os_path, model, name='', path=''):
"""save a non-notebook file"""
@ -522,7 +522,7 @@ class FileContentsManager(ContentsManager):
# ensure notebook is readable (never restore from an unreadable notebook)
if cp_path.endswith('.ipynb'):
with io.open(cp_path, 'r', encoding='utf-8') as f:
current.read(f, u'json')
nbformat.read(f, as_version=4)
self._copy(cp_path, nb_path)
self.log.debug("copying %s -> %s", cp_path, nb_path)

View File

@ -11,7 +11,8 @@ import os
from tornado.web import HTTPError
from IPython.config.configurable import LoggingConfigurable
from IPython.nbformat import current, sign
from IPython.nbformat import sign, validate, ValidationError
from IPython.nbformat.v4 import new_notebook
from IPython.utils.traitlets import Instance, Unicode, List
@ -220,8 +221,8 @@ class ContentsManager(LoggingConfigurable):
def validate_notebook_model(self, model):
"""Add failed-validation message to model"""
try:
current.validate(model['content'])
except current.ValidationError as e:
validate(model['content'])
except ValidationError as e:
model['message'] = 'Notebook Validation failed: {}:\n{}'.format(
e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'),
)
@ -234,7 +235,7 @@ class ContentsManager(LoggingConfigurable):
model = {}
if 'content' not in model and model.get('type', None) != 'directory':
if ext == '.ipynb':
model['content'] = current.new_notebook()
model['content'] = new_notebook()
model['type'] = 'notebook'
model['format'] = 'json'
else:
@ -308,13 +309,14 @@ class ContentsManager(LoggingConfigurable):
Parameters
----------
nb : dict
The notebook object (in nbformat.current format)
The notebook object (in current nbformat)
name : string
The filename of the notebook (for logging)
path : string
The notebook's directory (for logging)
"""
if nb['nbformat'] != current.nbformat:
# can't sign old notebooks
if nb['nbformat'] != 4:
return
if self.notary.check_cells(nb):
self.notary.sign(nb)
@ -329,7 +331,7 @@ class ContentsManager(LoggingConfigurable):
Parameters
----------
nb : dict
The notebook object (in nbformat.current format)
The notebook object (in current nbformat)
name : string
The filename of the notebook (for logging)
path : string

View File

@ -14,9 +14,10 @@ import requests
from IPython.html.utils import url_path_join, url_escape
from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
from IPython.nbformat import current
from IPython.nbformat.current import (new_notebook, write, read,
new_markdown_cell, from_dict)
from IPython.nbformat import read, write, from_dict
from IPython.nbformat.v4 import (
new_notebook, new_markdown_cell,
)
from IPython.nbformat import v2
from IPython.utils import py3compat
from IPython.utils.data import uniq_stable
@ -143,7 +144,7 @@ class APITest(NotebookTestBase):
with io.open(pjoin(nbdir, d, '%s.ipynb' % name), 'w',
encoding='utf-8') as f:
nb = new_notebook()
write(nb, f, format='ipynb')
write(f, nb, version=4)
# create a text file
with io.open(pjoin(nbdir, d, '%s.txt' % name), 'w',
@ -354,7 +355,7 @@ class APITest(NotebookTestBase):
self._check_created(resp, u'Upload tést.ipynb', u'å b')
resp = self.api.read(u'Upload tést.ipynb', u'å b')
data = resp.json()
self.assertEqual(data['content']['nbformat'], current.nbformat)
self.assertEqual(data['content']['nbformat'], 4)
def test_copy_untitled(self):
resp = self.api.copy_untitled(u'ç d.ipynb', path=u'å b')
@ -422,7 +423,7 @@ class APITest(NotebookTestBase):
nbfile = pjoin(self.notebook_dir.name, 'foo', 'a.ipynb')
with io.open(nbfile, 'r', encoding='utf-8') as f:
newnb = read(f, format='ipynb')
newnb = read(f, as_version=4)
self.assertEqual(newnb.cells[0].source,
u'Created by test ³')
nbcontent = self.api.read('a.ipynb', 'foo').json()['content']

View File

@ -9,7 +9,7 @@ from tornado.web import HTTPError
from unittest import TestCase
from tempfile import NamedTemporaryFile
from IPython.nbformat import current
from IPython.nbformat import v4 as nbformat
from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.traitlets import TraitError
@ -95,8 +95,8 @@ class TestContentsManager(TestCase):
return os_path
def add_code_cell(self, nb):
output = current.new_output("display_data", {'application/javascript': "alert('hi');"})
cell = current.new_code_cell("print('hi')", outputs=[output])
output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"})
cell = nbformat.new_code_cell("print('hi')", outputs=[output])
nb.cells.append(cell)
def new_notebook(self):

View File

@ -11,7 +11,8 @@ pjoin = os.path.join
from IPython.html.utils import url_path_join
from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
from IPython.nbformat.current import new_notebook, write
from IPython.nbformat.v4 import new_notebook
from IPython.nbformat import write
class SessionAPI(object):
"""Wrapper for notebook API calls."""
@ -63,7 +64,7 @@ class SessionAPITest(NotebookTestBase):
with io.open(pjoin(nbdir, 'foo', 'nb1.ipynb'), 'w',
encoding='utf-8') as f:
nb = new_notebook()
write(nb, f, format='ipynb')
write(f, nb, version=4)
self.sess_api = SessionAPI(self.base_url())

View File

@ -10,7 +10,8 @@ pjoin = os.path.join
import requests
import json
from IPython.nbformat.current import (new_notebook, write,
from IPython.nbformat import write
from IPython.nbformat.v4 import (new_notebook,
new_markdown_cell, new_code_cell,
new_output)
@ -73,7 +74,7 @@ class FilesTest(NotebookTestBase):
with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w',
encoding='utf-8') as f:
write(nb, f)
write(f, nb, version=4)
with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
f.write(b'\xff' + os.urandom(5))