mirror of
https://github.com/jupyter/notebook.git
synced 2025-02-11 12:30:51 +08:00
Rename get_model() to get()
This commit is contained in:
parent
6f48b58b18
commit
f1f81e2efa
@ -28,7 +28,7 @@ class FilesHandler(IPythonHandler):
|
|||||||
else:
|
else:
|
||||||
name = path
|
name = path
|
||||||
|
|
||||||
model = cm.get_model(path)
|
model = cm.get(path)
|
||||||
|
|
||||||
if self.get_argument("download", False):
|
if self.get_argument("download", False):
|
||||||
self.set_header('Content-Disposition','attachment; filename="%s"' % name)
|
self.set_header('Content-Disposition','attachment; filename="%s"' % name)
|
||||||
|
@ -81,7 +81,7 @@ class NbconvertFileHandler(IPythonHandler):
|
|||||||
exporter = get_exporter(format, config=self.config, log=self.log)
|
exporter = get_exporter(format, config=self.config, log=self.log)
|
||||||
|
|
||||||
path = path.strip('/')
|
path = path.strip('/')
|
||||||
model = self.contents_manager.get_model(path=path)
|
model = self.contents_manager.get(path=path)
|
||||||
name = model['name']
|
name = model['name']
|
||||||
|
|
||||||
self.set_header('Last-Modified', model['last_modified'])
|
self.set_header('Last-Modified', model['last_modified'])
|
||||||
|
@ -200,7 +200,7 @@ class FileContentsManager(ContentsManager):
|
|||||||
self.log.debug("%s not a regular file", os_path)
|
self.log.debug("%s not a regular file", os_path)
|
||||||
continue
|
continue
|
||||||
if self.should_list(name) and not is_hidden(os_path, self.root_dir):
|
if self.should_list(name) and not is_hidden(os_path, self.root_dir):
|
||||||
contents.append(self.get_model(
|
contents.append(self.get(
|
||||||
path='%s/%s' % (path, name),
|
path='%s/%s' % (path, name),
|
||||||
content=False)
|
content=False)
|
||||||
)
|
)
|
||||||
@ -266,7 +266,7 @@ class FileContentsManager(ContentsManager):
|
|||||||
self.validate_notebook_model(model)
|
self.validate_notebook_model(model)
|
||||||
return model
|
return model
|
||||||
|
|
||||||
def get_model(self, path, content=True, type_=None, format=None):
|
def get(self, path, content=True, type_=None, format=None):
|
||||||
""" Takes a path for an entity and returns its model
|
""" Takes a path for an entity and returns its model
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@ -380,7 +380,7 @@ class FileContentsManager(ContentsManager):
|
|||||||
self.validate_notebook_model(model)
|
self.validate_notebook_model(model)
|
||||||
validation_message = model.get('message', None)
|
validation_message = model.get('message', None)
|
||||||
|
|
||||||
model = self.get_model(path, content=False)
|
model = self.get(path, content=False)
|
||||||
if validation_message:
|
if validation_message:
|
||||||
model['message'] = validation_message
|
model['message'] = validation_message
|
||||||
return model
|
return model
|
||||||
@ -395,7 +395,7 @@ class FileContentsManager(ContentsManager):
|
|||||||
new_path = model.get('path', path).strip('/')
|
new_path = model.get('path', path).strip('/')
|
||||||
if path != new_path:
|
if path != new_path:
|
||||||
self.rename(path, new_path)
|
self.rename(path, new_path)
|
||||||
model = self.get_model(new_path, content=False)
|
model = self.get(new_path, content=False)
|
||||||
return model
|
return model
|
||||||
|
|
||||||
def delete(self, path):
|
def delete(self, path):
|
||||||
|
@ -66,7 +66,7 @@ class ContentsHandler(IPythonHandler):
|
|||||||
if format not in {None, 'text', 'base64'}:
|
if format not in {None, 'text', 'base64'}:
|
||||||
raise web.HTTPError(400, u'Format %r is invalid' % format)
|
raise web.HTTPError(400, u'Format %r is invalid' % format)
|
||||||
|
|
||||||
model = self.contents_manager.get_model(path=path, type_=type_, format=format)
|
model = self.contents_manager.get(path=path, type_=type_, format=format)
|
||||||
if model['type'] == 'directory':
|
if model['type'] == 'directory':
|
||||||
# group listing by type, then by name (case-insensitive)
|
# group listing by type, then by name (case-insensitive)
|
||||||
# FIXME: sorting should be done in the frontends
|
# FIXME: sorting should be done in the frontends
|
||||||
|
@ -135,7 +135,7 @@ class ContentsManager(LoggingConfigurable):
|
|||||||
"""
|
"""
|
||||||
return self.file_exists(path) or self.dir_exists(path)
|
return self.file_exists(path) or self.dir_exists(path)
|
||||||
|
|
||||||
def get_model(self, path, content=True, type_=None, format=None):
|
def get(self, path, content=True, type_=None, format=None):
|
||||||
"""Get the model of a file or directory with or without content."""
|
"""Get the model of a file or directory with or without content."""
|
||||||
raise NotImplementedError('must be implemented in a subclass')
|
raise NotImplementedError('must be implemented in a subclass')
|
||||||
|
|
||||||
@ -300,7 +300,7 @@ class ContentsManager(LoggingConfigurable):
|
|||||||
from_dir = ''
|
from_dir = ''
|
||||||
from_name = path
|
from_name = path
|
||||||
|
|
||||||
model = self.get_model(path)
|
model = self.get(path)
|
||||||
model.pop('path', None)
|
model.pop('path', None)
|
||||||
model.pop('name', None)
|
model.pop('name', None)
|
||||||
if model['type'] == 'directory':
|
if model['type'] == 'directory':
|
||||||
@ -328,7 +328,7 @@ class ContentsManager(LoggingConfigurable):
|
|||||||
path : string
|
path : string
|
||||||
The path of a notebook
|
The path of a notebook
|
||||||
"""
|
"""
|
||||||
model = self.get_model(path)
|
model = self.get(path)
|
||||||
nb = model['content']
|
nb = model['content']
|
||||||
self.log.warn("Trusting notebook %s", path)
|
self.log.warn("Trusting notebook %s", path)
|
||||||
self.notary.mark_cells(nb, True)
|
self.notary.mark_cells(nb, True)
|
||||||
|
@ -105,7 +105,7 @@ class TestContentsManager(TestCase):
|
|||||||
name = model['name']
|
name = model['name']
|
||||||
path = model['path']
|
path = model['path']
|
||||||
|
|
||||||
full_model = cm.get_model(path)
|
full_model = cm.get(path)
|
||||||
nb = full_model['content']
|
nb = full_model['content']
|
||||||
self.add_code_cell(nb)
|
self.add_code_cell(nb)
|
||||||
|
|
||||||
@ -152,27 +152,27 @@ class TestContentsManager(TestCase):
|
|||||||
path = model['path']
|
path = model['path']
|
||||||
|
|
||||||
# Check that we 'get' on the notebook we just created
|
# Check that we 'get' on the notebook we just created
|
||||||
model2 = cm.get_model(path)
|
model2 = cm.get(path)
|
||||||
assert isinstance(model2, dict)
|
assert isinstance(model2, dict)
|
||||||
self.assertIn('name', model2)
|
self.assertIn('name', model2)
|
||||||
self.assertIn('path', model2)
|
self.assertIn('path', model2)
|
||||||
self.assertEqual(model['name'], name)
|
self.assertEqual(model['name'], name)
|
||||||
self.assertEqual(model['path'], path)
|
self.assertEqual(model['path'], path)
|
||||||
|
|
||||||
nb_as_file = cm.get_model(path, content=True, type_='file')
|
nb_as_file = cm.get(path, content=True, type_='file')
|
||||||
self.assertEqual(nb_as_file['path'], path)
|
self.assertEqual(nb_as_file['path'], path)
|
||||||
self.assertEqual(nb_as_file['type'], 'file')
|
self.assertEqual(nb_as_file['type'], 'file')
|
||||||
self.assertEqual(nb_as_file['format'], 'text')
|
self.assertEqual(nb_as_file['format'], 'text')
|
||||||
self.assertNotIsInstance(nb_as_file['content'], dict)
|
self.assertNotIsInstance(nb_as_file['content'], dict)
|
||||||
|
|
||||||
nb_as_bin_file = cm.get_model(path, content=True, type_='file', format='base64')
|
nb_as_bin_file = cm.get(path, content=True, type_='file', format='base64')
|
||||||
self.assertEqual(nb_as_bin_file['format'], 'base64')
|
self.assertEqual(nb_as_bin_file['format'], 'base64')
|
||||||
|
|
||||||
# Test in sub-directory
|
# Test in sub-directory
|
||||||
sub_dir = '/foo/'
|
sub_dir = '/foo/'
|
||||||
self.make_dir(cm.root_dir, 'foo')
|
self.make_dir(cm.root_dir, 'foo')
|
||||||
model = cm.new_untitled(path=sub_dir, ext='.ipynb')
|
model = cm.new_untitled(path=sub_dir, ext='.ipynb')
|
||||||
model2 = cm.get_model(sub_dir + name)
|
model2 = cm.get(sub_dir + name)
|
||||||
assert isinstance(model2, dict)
|
assert isinstance(model2, dict)
|
||||||
self.assertIn('name', model2)
|
self.assertIn('name', model2)
|
||||||
self.assertIn('path', model2)
|
self.assertIn('path', model2)
|
||||||
@ -181,11 +181,11 @@ class TestContentsManager(TestCase):
|
|||||||
self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name))
|
self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name))
|
||||||
|
|
||||||
# Test getting directory model
|
# Test getting directory model
|
||||||
dirmodel = cm.get_model('foo')
|
dirmodel = cm.get('foo')
|
||||||
self.assertEqual(dirmodel['type'], 'directory')
|
self.assertEqual(dirmodel['type'], 'directory')
|
||||||
|
|
||||||
with self.assertRaises(HTTPError):
|
with self.assertRaises(HTTPError):
|
||||||
cm.get_model('foo', type_='file')
|
cm.get('foo', type_='file')
|
||||||
|
|
||||||
|
|
||||||
@dec.skip_win32
|
@dec.skip_win32
|
||||||
@ -198,7 +198,7 @@ class TestContentsManager(TestCase):
|
|||||||
|
|
||||||
# create a broken symlink
|
# create a broken symlink
|
||||||
os.symlink("target", os.path.join(os_path, "bad symlink"))
|
os.symlink("target", os.path.join(os_path, "bad symlink"))
|
||||||
model = cm.get_model(path)
|
model = cm.get(path)
|
||||||
self.assertEqual(model['content'], [file_model])
|
self.assertEqual(model['content'], [file_model])
|
||||||
|
|
||||||
@dec.skip_win32
|
@dec.skip_win32
|
||||||
@ -213,8 +213,8 @@ class TestContentsManager(TestCase):
|
|||||||
|
|
||||||
# create a good symlink
|
# create a good symlink
|
||||||
os.symlink(file_model['name'], os.path.join(os_path, name))
|
os.symlink(file_model['name'], os.path.join(os_path, name))
|
||||||
symlink_model = cm.get_model(path, content=False)
|
symlink_model = cm.get(path, content=False)
|
||||||
dir_model = cm.get_model(parent)
|
dir_model = cm.get(parent)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(dir_model['content'], key=lambda x: x['name']),
|
sorted(dir_model['content'], key=lambda x: x['name']),
|
||||||
[symlink_model, file_model],
|
[symlink_model, file_model],
|
||||||
@ -236,7 +236,7 @@ class TestContentsManager(TestCase):
|
|||||||
self.assertEqual(model['name'], 'test.ipynb')
|
self.assertEqual(model['name'], 'test.ipynb')
|
||||||
|
|
||||||
# Make sure the old name is gone
|
# Make sure the old name is gone
|
||||||
self.assertRaises(HTTPError, cm.get_model, path)
|
self.assertRaises(HTTPError, cm.get, path)
|
||||||
|
|
||||||
# Test in sub-directory
|
# Test in sub-directory
|
||||||
# Create a directory and notebook in that directory
|
# Create a directory and notebook in that directory
|
||||||
@ -257,7 +257,7 @@ class TestContentsManager(TestCase):
|
|||||||
self.assertEqual(model['path'], new_path)
|
self.assertEqual(model['path'], new_path)
|
||||||
|
|
||||||
# Make sure the old name is gone
|
# Make sure the old name is gone
|
||||||
self.assertRaises(HTTPError, cm.get_model, path)
|
self.assertRaises(HTTPError, cm.get, path)
|
||||||
|
|
||||||
def test_save(self):
|
def test_save(self):
|
||||||
cm = self.contents_manager
|
cm = self.contents_manager
|
||||||
@ -267,7 +267,7 @@ class TestContentsManager(TestCase):
|
|||||||
path = model['path']
|
path = model['path']
|
||||||
|
|
||||||
# Get the model with 'content'
|
# Get the model with 'content'
|
||||||
full_model = cm.get_model(path)
|
full_model = cm.get(path)
|
||||||
|
|
||||||
# Save the notebook
|
# Save the notebook
|
||||||
model = cm.save(full_model, path)
|
model = cm.save(full_model, path)
|
||||||
@ -284,7 +284,7 @@ class TestContentsManager(TestCase):
|
|||||||
model = cm.new_untitled(path=sub_dir, type='notebook')
|
model = cm.new_untitled(path=sub_dir, type='notebook')
|
||||||
name = model['name']
|
name = model['name']
|
||||||
path = model['path']
|
path = model['path']
|
||||||
model = cm.get_model(path)
|
model = cm.get(path)
|
||||||
|
|
||||||
# Change the name in the model for rename
|
# Change the name in the model for rename
|
||||||
model = cm.save(model, path)
|
model = cm.save(model, path)
|
||||||
@ -303,7 +303,7 @@ class TestContentsManager(TestCase):
|
|||||||
cm.delete(path)
|
cm.delete(path)
|
||||||
|
|
||||||
# Check that a 'get' on the deleted notebook raises and error
|
# Check that a 'get' on the deleted notebook raises and error
|
||||||
self.assertRaises(HTTPError, cm.get_model, path)
|
self.assertRaises(HTTPError, cm.get, path)
|
||||||
|
|
||||||
def test_copy(self):
|
def test_copy(self):
|
||||||
cm = self.contents_manager
|
cm = self.contents_manager
|
||||||
@ -326,12 +326,12 @@ class TestContentsManager(TestCase):
|
|||||||
cm = self.contents_manager
|
cm = self.contents_manager
|
||||||
nb, name, path = self.new_notebook()
|
nb, name, path = self.new_notebook()
|
||||||
|
|
||||||
untrusted = cm.get_model(path)['content']
|
untrusted = cm.get(path)['content']
|
||||||
assert not cm.notary.check_cells(untrusted)
|
assert not cm.notary.check_cells(untrusted)
|
||||||
|
|
||||||
# print(untrusted)
|
# print(untrusted)
|
||||||
cm.trust_notebook(path)
|
cm.trust_notebook(path)
|
||||||
trusted = cm.get_model(path)['content']
|
trusted = cm.get(path)['content']
|
||||||
# print(trusted)
|
# print(trusted)
|
||||||
assert cm.notary.check_cells(trusted)
|
assert cm.notary.check_cells(trusted)
|
||||||
|
|
||||||
@ -345,7 +345,7 @@ class TestContentsManager(TestCase):
|
|||||||
assert not cell.metadata.trusted
|
assert not cell.metadata.trusted
|
||||||
|
|
||||||
cm.trust_notebook(path)
|
cm.trust_notebook(path)
|
||||||
nb = cm.get_model(path)['content']
|
nb = cm.get(path)['content']
|
||||||
for cell in nb.cells:
|
for cell in nb.cells:
|
||||||
if cell.cell_type == 'code':
|
if cell.cell_type == 'code':
|
||||||
assert cell.metadata.trusted
|
assert cell.metadata.trusted
|
||||||
@ -359,7 +359,7 @@ class TestContentsManager(TestCase):
|
|||||||
assert not cm.notary.check_signature(nb)
|
assert not cm.notary.check_signature(nb)
|
||||||
|
|
||||||
cm.trust_notebook(path)
|
cm.trust_notebook(path)
|
||||||
nb = cm.get_model(path)['content']
|
nb = cm.get(path)['content']
|
||||||
cm.mark_trusted_cells(nb, path)
|
cm.mark_trusted_cells(nb, path)
|
||||||
cm.check_and_sign(nb, path)
|
cm.check_and_sign(nb, path)
|
||||||
assert cm.notary.check_signature(nb)
|
assert cm.notary.check_signature(nb)
|
||||||
|
@ -38,7 +38,7 @@ class TreeHandler(IPythonHandler):
|
|||||||
cm = self.contents_manager
|
cm = self.contents_manager
|
||||||
if cm.file_exists(path):
|
if cm.file_exists(path):
|
||||||
# it's not a directory, we have redirecting to do
|
# it's not a directory, we have redirecting to do
|
||||||
model = cm.get_model(path, content=False)
|
model = cm.get(path, content=False)
|
||||||
# redirect to /api/notebooks if it's a notebook, otherwise /api/files
|
# redirect to /api/notebooks if it's a notebook, otherwise /api/files
|
||||||
service = 'notebooks' if model['type'] == 'notebook' else 'files'
|
service = 'notebooks' if model['type'] == 'notebook' else 'files'
|
||||||
url = url_escape(url_path_join(
|
url = url_escape(url_path_join(
|
||||||
|
Loading…
Reference in New Issue
Block a user