##// END OF EJS Templates
Rename get_model() to get()
Thomas Kluyver -
Show More
@@ -28,7 +28,7 b' class FilesHandler(IPythonHandler):'
28 else:
28 else:
29 name = path
29 name = path
30
30
31 model = cm.get_model(path)
31 model = cm.get(path)
32
32
33 if self.get_argument("download", False):
33 if self.get_argument("download", False):
34 self.set_header('Content-Disposition','attachment; filename="%s"' % name)
34 self.set_header('Content-Disposition','attachment; filename="%s"' % name)
@@ -81,7 +81,7 b' class NbconvertFileHandler(IPythonHandler):'
81 exporter = get_exporter(format, config=self.config, log=self.log)
81 exporter = get_exporter(format, config=self.config, log=self.log)
82
82
83 path = path.strip('/')
83 path = path.strip('/')
84 model = self.contents_manager.get_model(path=path)
84 model = self.contents_manager.get(path=path)
85 name = model['name']
85 name = model['name']
86
86
87 self.set_header('Last-Modified', model['last_modified'])
87 self.set_header('Last-Modified', model['last_modified'])
@@ -200,7 +200,7 b' class FileContentsManager(ContentsManager):'
200 self.log.debug("%s not a regular file", os_path)
200 self.log.debug("%s not a regular file", os_path)
201 continue
201 continue
202 if self.should_list(name) and not is_hidden(os_path, self.root_dir):
202 if self.should_list(name) and not is_hidden(os_path, self.root_dir):
203 contents.append(self.get_model(
203 contents.append(self.get(
204 path='%s/%s' % (path, name),
204 path='%s/%s' % (path, name),
205 content=False)
205 content=False)
206 )
206 )
@@ -266,7 +266,7 b' class FileContentsManager(ContentsManager):'
266 self.validate_notebook_model(model)
266 self.validate_notebook_model(model)
267 return model
267 return model
268
268
269 def get_model(self, path, content=True, type_=None, format=None):
269 def get(self, path, content=True, type_=None, format=None):
270 """ Takes a path for an entity and returns its model
270 """ Takes a path for an entity and returns its model
271
271
272 Parameters
272 Parameters
@@ -380,7 +380,7 b' class FileContentsManager(ContentsManager):'
380 self.validate_notebook_model(model)
380 self.validate_notebook_model(model)
381 validation_message = model.get('message', None)
381 validation_message = model.get('message', None)
382
382
383 model = self.get_model(path, content=False)
383 model = self.get(path, content=False)
384 if validation_message:
384 if validation_message:
385 model['message'] = validation_message
385 model['message'] = validation_message
386 return model
386 return model
@@ -395,7 +395,7 b' class FileContentsManager(ContentsManager):'
395 new_path = model.get('path', path).strip('/')
395 new_path = model.get('path', path).strip('/')
396 if path != new_path:
396 if path != new_path:
397 self.rename(path, new_path)
397 self.rename(path, new_path)
398 model = self.get_model(new_path, content=False)
398 model = self.get(new_path, content=False)
399 return model
399 return model
400
400
401 def delete(self, path):
401 def delete(self, path):
@@ -66,7 +66,7 b' class ContentsHandler(IPythonHandler):'
66 if format not in {None, 'text', 'base64'}:
66 if format not in {None, 'text', 'base64'}:
67 raise web.HTTPError(400, u'Format %r is invalid' % format)
67 raise web.HTTPError(400, u'Format %r is invalid' % format)
68
68
69 model = self.contents_manager.get_model(path=path, type_=type_, format=format)
69 model = self.contents_manager.get(path=path, type_=type_, format=format)
70 if model['type'] == 'directory':
70 if model['type'] == 'directory':
71 # group listing by type, then by name (case-insensitive)
71 # group listing by type, then by name (case-insensitive)
72 # FIXME: sorting should be done in the frontends
72 # FIXME: sorting should be done in the frontends
@@ -135,7 +135,7 b' class ContentsManager(LoggingConfigurable):'
135 """
135 """
136 return self.file_exists(path) or self.dir_exists(path)
136 return self.file_exists(path) or self.dir_exists(path)
137
137
138 def get_model(self, path, content=True, type_=None, format=None):
138 def get(self, path, content=True, type_=None, format=None):
139 """Get the model of a file or directory with or without content."""
139 """Get the model of a file or directory with or without content."""
140 raise NotImplementedError('must be implemented in a subclass')
140 raise NotImplementedError('must be implemented in a subclass')
141
141
@@ -300,7 +300,7 b' class ContentsManager(LoggingConfigurable):'
300 from_dir = ''
300 from_dir = ''
301 from_name = path
301 from_name = path
302
302
303 model = self.get_model(path)
303 model = self.get(path)
304 model.pop('path', None)
304 model.pop('path', None)
305 model.pop('name', None)
305 model.pop('name', None)
306 if model['type'] == 'directory':
306 if model['type'] == 'directory':
@@ -328,7 +328,7 b' class ContentsManager(LoggingConfigurable):'
328 path : string
328 path : string
329 The path of a notebook
329 The path of a notebook
330 """
330 """
331 model = self.get_model(path)
331 model = self.get(path)
332 nb = model['content']
332 nb = model['content']
333 self.log.warn("Trusting notebook %s", path)
333 self.log.warn("Trusting notebook %s", path)
334 self.notary.mark_cells(nb, True)
334 self.notary.mark_cells(nb, True)
@@ -105,7 +105,7 b' class TestContentsManager(TestCase):'
105 name = model['name']
105 name = model['name']
106 path = model['path']
106 path = model['path']
107
107
108 full_model = cm.get_model(path)
108 full_model = cm.get(path)
109 nb = full_model['content']
109 nb = full_model['content']
110 self.add_code_cell(nb)
110 self.add_code_cell(nb)
111
111
@@ -152,27 +152,27 b' class TestContentsManager(TestCase):'
152 path = model['path']
152 path = model['path']
153
153
154 # Check that we 'get' on the notebook we just created
154 # Check that we 'get' on the notebook we just created
155 model2 = cm.get_model(path)
155 model2 = cm.get(path)
156 assert isinstance(model2, dict)
156 assert isinstance(model2, dict)
157 self.assertIn('name', model2)
157 self.assertIn('name', model2)
158 self.assertIn('path', model2)
158 self.assertIn('path', model2)
159 self.assertEqual(model['name'], name)
159 self.assertEqual(model['name'], name)
160 self.assertEqual(model['path'], path)
160 self.assertEqual(model['path'], path)
161
161
162 nb_as_file = cm.get_model(path, content=True, type_='file')
162 nb_as_file = cm.get(path, content=True, type_='file')
163 self.assertEqual(nb_as_file['path'], path)
163 self.assertEqual(nb_as_file['path'], path)
164 self.assertEqual(nb_as_file['type'], 'file')
164 self.assertEqual(nb_as_file['type'], 'file')
165 self.assertEqual(nb_as_file['format'], 'text')
165 self.assertEqual(nb_as_file['format'], 'text')
166 self.assertNotIsInstance(nb_as_file['content'], dict)
166 self.assertNotIsInstance(nb_as_file['content'], dict)
167
167
168 nb_as_bin_file = cm.get_model(path, content=True, type_='file', format='base64')
168 nb_as_bin_file = cm.get(path, content=True, type_='file', format='base64')
169 self.assertEqual(nb_as_bin_file['format'], 'base64')
169 self.assertEqual(nb_as_bin_file['format'], 'base64')
170
170
171 # Test in sub-directory
171 # Test in sub-directory
172 sub_dir = '/foo/'
172 sub_dir = '/foo/'
173 self.make_dir(cm.root_dir, 'foo')
173 self.make_dir(cm.root_dir, 'foo')
174 model = cm.new_untitled(path=sub_dir, ext='.ipynb')
174 model = cm.new_untitled(path=sub_dir, ext='.ipynb')
175 model2 = cm.get_model(sub_dir + name)
175 model2 = cm.get(sub_dir + name)
176 assert isinstance(model2, dict)
176 assert isinstance(model2, dict)
177 self.assertIn('name', model2)
177 self.assertIn('name', model2)
178 self.assertIn('path', model2)
178 self.assertIn('path', model2)
@@ -181,11 +181,11 b' class TestContentsManager(TestCase):'
181 self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name))
181 self.assertEqual(model2['path'], '{0}/{1}'.format(sub_dir.strip('/'), name))
182
182
183 # Test getting directory model
183 # Test getting directory model
184 dirmodel = cm.get_model('foo')
184 dirmodel = cm.get('foo')
185 self.assertEqual(dirmodel['type'], 'directory')
185 self.assertEqual(dirmodel['type'], 'directory')
186
186
187 with self.assertRaises(HTTPError):
187 with self.assertRaises(HTTPError):
188 cm.get_model('foo', type_='file')
188 cm.get('foo', type_='file')
189
189
190
190
191 @dec.skip_win32
191 @dec.skip_win32
@@ -198,7 +198,7 b' class TestContentsManager(TestCase):'
198
198
199 # create a broken symlink
199 # create a broken symlink
200 os.symlink("target", os.path.join(os_path, "bad symlink"))
200 os.symlink("target", os.path.join(os_path, "bad symlink"))
201 model = cm.get_model(path)
201 model = cm.get(path)
202 self.assertEqual(model['content'], [file_model])
202 self.assertEqual(model['content'], [file_model])
203
203
204 @dec.skip_win32
204 @dec.skip_win32
@@ -213,8 +213,8 b' class TestContentsManager(TestCase):'
213
213
214 # create a good symlink
214 # create a good symlink
215 os.symlink(file_model['name'], os.path.join(os_path, name))
215 os.symlink(file_model['name'], os.path.join(os_path, name))
216 symlink_model = cm.get_model(path, content=False)
216 symlink_model = cm.get(path, content=False)
217 dir_model = cm.get_model(parent)
217 dir_model = cm.get(parent)
218 self.assertEqual(
218 self.assertEqual(
219 sorted(dir_model['content'], key=lambda x: x['name']),
219 sorted(dir_model['content'], key=lambda x: x['name']),
220 [symlink_model, file_model],
220 [symlink_model, file_model],
@@ -236,7 +236,7 b' class TestContentsManager(TestCase):'
236 self.assertEqual(model['name'], 'test.ipynb')
236 self.assertEqual(model['name'], 'test.ipynb')
237
237
238 # Make sure the old name is gone
238 # Make sure the old name is gone
239 self.assertRaises(HTTPError, cm.get_model, path)
239 self.assertRaises(HTTPError, cm.get, path)
240
240
241 # Test in sub-directory
241 # Test in sub-directory
242 # Create a directory and notebook in that directory
242 # Create a directory and notebook in that directory
@@ -257,7 +257,7 b' class TestContentsManager(TestCase):'
257 self.assertEqual(model['path'], new_path)
257 self.assertEqual(model['path'], new_path)
258
258
259 # Make sure the old name is gone
259 # Make sure the old name is gone
260 self.assertRaises(HTTPError, cm.get_model, path)
260 self.assertRaises(HTTPError, cm.get, path)
261
261
262 def test_save(self):
262 def test_save(self):
263 cm = self.contents_manager
263 cm = self.contents_manager
@@ -267,7 +267,7 b' class TestContentsManager(TestCase):'
267 path = model['path']
267 path = model['path']
268
268
269 # Get the model with 'content'
269 # Get the model with 'content'
270 full_model = cm.get_model(path)
270 full_model = cm.get(path)
271
271
272 # Save the notebook
272 # Save the notebook
273 model = cm.save(full_model, path)
273 model = cm.save(full_model, path)
@@ -284,7 +284,7 b' class TestContentsManager(TestCase):'
284 model = cm.new_untitled(path=sub_dir, type='notebook')
284 model = cm.new_untitled(path=sub_dir, type='notebook')
285 name = model['name']
285 name = model['name']
286 path = model['path']
286 path = model['path']
287 model = cm.get_model(path)
287 model = cm.get(path)
288
288
289 # Change the name in the model for rename
289 # Change the name in the model for rename
290 model = cm.save(model, path)
290 model = cm.save(model, path)
@@ -303,7 +303,7 b' class TestContentsManager(TestCase):'
303 cm.delete(path)
303 cm.delete(path)
304
304
305 # Check that a 'get' on the deleted notebook raises and error
305 # Check that a 'get' on the deleted notebook raises and error
306 self.assertRaises(HTTPError, cm.get_model, path)
306 self.assertRaises(HTTPError, cm.get, path)
307
307
308 def test_copy(self):
308 def test_copy(self):
309 cm = self.contents_manager
309 cm = self.contents_manager
@@ -326,12 +326,12 b' class TestContentsManager(TestCase):'
326 cm = self.contents_manager
326 cm = self.contents_manager
327 nb, name, path = self.new_notebook()
327 nb, name, path = self.new_notebook()
328
328
329 untrusted = cm.get_model(path)['content']
329 untrusted = cm.get(path)['content']
330 assert not cm.notary.check_cells(untrusted)
330 assert not cm.notary.check_cells(untrusted)
331
331
332 # print(untrusted)
332 # print(untrusted)
333 cm.trust_notebook(path)
333 cm.trust_notebook(path)
334 trusted = cm.get_model(path)['content']
334 trusted = cm.get(path)['content']
335 # print(trusted)
335 # print(trusted)
336 assert cm.notary.check_cells(trusted)
336 assert cm.notary.check_cells(trusted)
337
337
@@ -345,7 +345,7 b' class TestContentsManager(TestCase):'
345 assert not cell.metadata.trusted
345 assert not cell.metadata.trusted
346
346
347 cm.trust_notebook(path)
347 cm.trust_notebook(path)
348 nb = cm.get_model(path)['content']
348 nb = cm.get(path)['content']
349 for cell in nb.cells:
349 for cell in nb.cells:
350 if cell.cell_type == 'code':
350 if cell.cell_type == 'code':
351 assert cell.metadata.trusted
351 assert cell.metadata.trusted
@@ -359,7 +359,7 b' class TestContentsManager(TestCase):'
359 assert not cm.notary.check_signature(nb)
359 assert not cm.notary.check_signature(nb)
360
360
361 cm.trust_notebook(path)
361 cm.trust_notebook(path)
362 nb = cm.get_model(path)['content']
362 nb = cm.get(path)['content']
363 cm.mark_trusted_cells(nb, path)
363 cm.mark_trusted_cells(nb, path)
364 cm.check_and_sign(nb, path)
364 cm.check_and_sign(nb, path)
365 assert cm.notary.check_signature(nb)
365 assert cm.notary.check_signature(nb)
@@ -38,7 +38,7 b' class TreeHandler(IPythonHandler):'
38 cm = self.contents_manager
38 cm = self.contents_manager
39 if cm.file_exists(path):
39 if cm.file_exists(path):
40 # it's not a directory, we have redirecting to do
40 # it's not a directory, we have redirecting to do
41 model = cm.get_model(path, content=False)
41 model = cm.get(path, content=False)
42 # redirect to /api/notebooks if it's a notebook, otherwise /api/files
42 # redirect to /api/notebooks if it's a notebook, otherwise /api/files
43 service = 'notebooks' if model['type'] == 'notebook' else 'files'
43 service = 'notebooks' if model['type'] == 'notebook' else 'files'
44 url = url_escape(url_path_join(
44 url = url_escape(url_path_join(
General Comments 0
You need to be logged in to leave comments. Login now