##// END OF EJS Templates
NotebookManager API: rename *notebook_model methods to *notebook
Konrad Hinsen -
Show More
@@ -231,11 +231,11 b' class FileNotebookManager(NotebookManager):'
231 """
231 """
232 path = path.strip('/')
232 path = path.strip('/')
233 notebook_names = self.get_notebook_names(path)
233 notebook_names = self.get_notebook_names(path)
234 notebooks = [self.get_notebook_model(name, path, content=False) for name in notebook_names]
234 notebooks = [self.get_notebook(name, path, content=False) for name in notebook_names]
235 notebooks = sorted(notebooks, key=lambda item: item['name'])
235 notebooks = sorted(notebooks, key=lambda item: item['name'])
236 return notebooks
236 return notebooks
237
237
238 def get_notebook_model(self, name, path='', content=True):
238 def get_notebook(self, name, path='', content=True):
239 """ Takes a path and name for a notebook and returns its model
239 """ Takes a path and name for a notebook and returns its model
240
240
241 Parameters
241 Parameters
@@ -276,7 +276,7 b' class FileNotebookManager(NotebookManager):'
276 model['content'] = nb
276 model['content'] = nb
277 return model
277 return model
278
278
279 def save_notebook_model(self, model, name='', path=''):
279 def save_notebook(self, model, name='', path=''):
280 """Save the notebook model and return the model with no content."""
280 """Save the notebook model and return the model with no content."""
281 path = path.strip('/')
281 path = path.strip('/')
282
282
@@ -318,20 +318,20 b' class FileNotebookManager(NotebookManager):'
318 except Exception as e:
318 except Exception as e:
319 raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s %s' % (py_path, e))
319 raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s %s' % (py_path, e))
320
320
321 model = self.get_notebook_model(new_name, new_path, content=False)
321 model = self.get_notebook(new_name, new_path, content=False)
322 return model
322 return model
323
323
324 def update_notebook_model(self, model, name, path=''):
324 def update_notebook(self, model, name, path=''):
325 """Update the notebook's path and/or name"""
325 """Update the notebook's path and/or name"""
326 path = path.strip('/')
326 path = path.strip('/')
327 new_name = model.get('name', name)
327 new_name = model.get('name', name)
328 new_path = model.get('path', path).strip('/')
328 new_path = model.get('path', path).strip('/')
329 if path != new_path or name != new_name:
329 if path != new_path or name != new_name:
330 self.rename_notebook(name, path, new_name, new_path)
330 self.rename_notebook(name, path, new_name, new_path)
331 model = self.get_notebook_model(new_name, new_path, content=False)
331 model = self.get_notebook(new_name, new_path, content=False)
332 return model
332 return model
333
333
334 def delete_notebook_model(self, name, path=''):
334 def delete_notebook(self, name, path=''):
335 """Delete notebook by name and path."""
335 """Delete notebook by name and path."""
336 path = path.strip('/')
336 path = path.strip('/')
337 os_path = self.get_os_path(name, path)
337 os_path = self.get_os_path(name, path)
@@ -84,7 +84,7 b' class NotebookHandler(IPythonHandler):'
84 self.finish(json.dumps(notebooks, default=date_default))
84 self.finish(json.dumps(notebooks, default=date_default))
85 return
85 return
86 # get and return notebook representation
86 # get and return notebook representation
87 model = nbm.get_notebook_model(name, path)
87 model = nbm.get_notebook(name, path)
88 self._finish_model(model, location=False)
88 self._finish_model(model, location=False)
89
89
90 @web.authenticated
90 @web.authenticated
@@ -97,7 +97,7 b' class NotebookHandler(IPythonHandler):'
97 model = self.get_json_body()
97 model = self.get_json_body()
98 if model is None:
98 if model is None:
99 raise web.HTTPError(400, u'JSON body missing')
99 raise web.HTTPError(400, u'JSON body missing')
100 model = nbm.update_notebook_model(model, name, path)
100 model = nbm.update_notebook(model, name, path)
101 self._finish_model(model)
101 self._finish_model(model)
102
102
103 def _copy_notebook(self, copy_from, path, copy_to=None):
103 def _copy_notebook(self, copy_from, path, copy_to=None):
@@ -122,7 +122,7 b' class NotebookHandler(IPythonHandler):'
122 if name:
122 if name:
123 model['name'] = name
123 model['name'] = name
124
124
125 model = self.notebook_manager.create_notebook_model(model, path)
125 model = self.notebook_manager.create_notebook(model, path)
126 self.set_status(201)
126 self.set_status(201)
127 self._finish_model(model)
127 self._finish_model(model)
128
128
@@ -135,14 +135,14 b' class NotebookHandler(IPythonHandler):'
135 model = {}
135 model = {}
136 if name:
136 if name:
137 model['name'] = name
137 model['name'] = name
138 model = self.notebook_manager.create_notebook_model(model, path=path)
138 model = self.notebook_manager.create_notebook(model, path=path)
139 self.set_status(201)
139 self.set_status(201)
140 self._finish_model(model)
140 self._finish_model(model)
141
141
142 def _save_notebook(self, model, path, name):
142 def _save_notebook(self, model, path, name):
143 """Save an existing notebook."""
143 """Save an existing notebook."""
144 self.log.info(u"Saving notebook at %s/%s", path, name)
144 self.log.info(u"Saving notebook at %s/%s", path, name)
145 model = self.notebook_manager.save_notebook_model(model, name, path)
145 model = self.notebook_manager.save_notebook(model, name, path)
146 if model['path'] != path.strip('/') or model['name'] != name:
146 if model['path'] != path.strip('/') or model['name'] != name:
147 # a rename happened, set Location header
147 # a rename happened, set Location header
148 location = True
148 location = True
@@ -217,7 +217,7 b' class NotebookHandler(IPythonHandler):'
217 def delete(self, path='', name=None):
217 def delete(self, path='', name=None):
218 """delete the notebook in the given notebook path"""
218 """delete the notebook in the given notebook path"""
219 nbm = self.notebook_manager
219 nbm = self.notebook_manager
220 nbm.delete_notebook_model(name, path)
220 nbm.delete_notebook(name, path)
221 self.set_status(204)
221 self.set_status(204)
222 self.finish()
222 self.finish()
223
223
@@ -162,23 +162,23 b' class NotebookManager(LoggingConfigurable):'
162 """
162 """
163 raise NotImplementedError('must be implemented in a subclass')
163 raise NotImplementedError('must be implemented in a subclass')
164
164
165 def get_notebook_model(self, name, path='', content=True):
165 def get_notebook(self, name, path='', content=True):
166 """Get the notebook model with or without content."""
166 """Get the notebook model with or without content."""
167 raise NotImplementedError('must be implemented in a subclass')
167 raise NotImplementedError('must be implemented in a subclass')
168
168
169 def save_notebook_model(self, model, name, path=''):
169 def save_notebook(self, model, name, path=''):
170 """Save the notebook model and return the model with no content."""
170 """Save the notebook and return the model with no content."""
171 raise NotImplementedError('must be implemented in a subclass')
171 raise NotImplementedError('must be implemented in a subclass')
172
172
173 def update_notebook_model(self, model, name, path=''):
173 def update_notebook(self, model, name, path=''):
174 """Update the notebook model and return the model with no content."""
174 """Update the notebook and return the model with no content."""
175 raise NotImplementedError('must be implemented in a subclass')
175 raise NotImplementedError('must be implemented in a subclass')
176
176
177 def delete_notebook_model(self, name, path=''):
177 def delete_notebook(self, name, path=''):
178 """Delete notebook by name and path."""
178 """Delete notebook by name and path."""
179 raise NotImplementedError('must be implemented in a subclass')
179 raise NotImplementedError('must be implemented in a subclass')
180
180
181 def create_notebook_model(self, model=None, path=''):
181 def create_notebook(self, model=None, path=''):
182 """Create a new notebook and return its model with no content."""
182 """Create a new notebook and return its model with no content."""
183 path = path.strip('/')
183 path = path.strip('/')
184 if model is None:
184 if model is None:
@@ -190,7 +190,7 b' class NotebookManager(LoggingConfigurable):'
190 model['name'] = self.increment_filename('Untitled', path)
190 model['name'] = self.increment_filename('Untitled', path)
191
191
192 model['path'] = path
192 model['path'] = path
193 model = self.save_notebook_model(model, model['name'], model['path'])
193 model = self.save_notebook(model, model['name'], model['path'])
194 return model
194 return model
195
195
196 def copy_notebook(self, from_name, to_name=None, path=''):
196 def copy_notebook(self, from_name, to_name=None, path=''):
@@ -199,12 +199,12 b' class NotebookManager(LoggingConfigurable):'
199 If to_name not specified, increment `from_name-Copy#.ipynb`.
199 If to_name not specified, increment `from_name-Copy#.ipynb`.
200 """
200 """
201 path = path.strip('/')
201 path = path.strip('/')
202 model = self.get_notebook_model(from_name, path)
202 model = self.get_notebook(from_name, path)
203 if not to_name:
203 if not to_name:
204 base = os.path.splitext(from_name)[0] + '-Copy'
204 base = os.path.splitext(from_name)[0] + '-Copy'
205 to_name = self.increment_filename(base, path)
205 to_name = self.increment_filename(base, path)
206 model['name'] = to_name
206 model['name'] = to_name
207 model = self.save_notebook_model(model, to_name, path)
207 model = self.save_notebook(model, to_name, path)
208 return model
208 return model
209
209
210 # Checkpoint-related
210 # Checkpoint-related
@@ -70,11 +70,11 b' class TestNotebookManager(TestCase):'
70 except OSError:
70 except OSError:
71 print("Directory already exists: %r" % os_path)
71 print("Directory already exists: %r" % os_path)
72
72
73 def test_create_notebook_model(self):
73 def test_create_notebook(self):
74 with TemporaryDirectory() as td:
74 with TemporaryDirectory() as td:
75 # Test in root directory
75 # Test in root directory
76 nm = FileNotebookManager(notebook_dir=td)
76 nm = FileNotebookManager(notebook_dir=td)
77 model = nm.create_notebook_model()
77 model = nm.create_notebook()
78 assert isinstance(model, dict)
78 assert isinstance(model, dict)
79 self.assertIn('name', model)
79 self.assertIn('name', model)
80 self.assertIn('path', model)
80 self.assertIn('path', model)
@@ -84,24 +84,24 b' class TestNotebookManager(TestCase):'
84 # Test in sub-directory
84 # Test in sub-directory
85 sub_dir = '/foo/'
85 sub_dir = '/foo/'
86 self.make_dir(nm.notebook_dir, 'foo')
86 self.make_dir(nm.notebook_dir, 'foo')
87 model = nm.create_notebook_model(None, sub_dir)
87 model = nm.create_notebook(None, sub_dir)
88 assert isinstance(model, dict)
88 assert isinstance(model, dict)
89 self.assertIn('name', model)
89 self.assertIn('name', model)
90 self.assertIn('path', model)
90 self.assertIn('path', model)
91 self.assertEqual(model['name'], 'Untitled0.ipynb')
91 self.assertEqual(model['name'], 'Untitled0.ipynb')
92 self.assertEqual(model['path'], sub_dir.strip('/'))
92 self.assertEqual(model['path'], sub_dir.strip('/'))
93
93
94 def test_get_notebook_model(self):
94 def test_get_notebook(self):
95 with TemporaryDirectory() as td:
95 with TemporaryDirectory() as td:
96 # Test in root directory
96 # Test in root directory
97 # Create a notebook
97 # Create a notebook
98 nm = FileNotebookManager(notebook_dir=td)
98 nm = FileNotebookManager(notebook_dir=td)
99 model = nm.create_notebook_model()
99 model = nm.create_notebook()
100 name = model['name']
100 name = model['name']
101 path = model['path']
101 path = model['path']
102
102
103 # Check that we 'get' on the notebook we just created
103 # Check that we 'get' on the notebook we just created
104 model2 = nm.get_notebook_model(name, path)
104 model2 = nm.get_notebook(name, path)
105 assert isinstance(model2, dict)
105 assert isinstance(model2, dict)
106 self.assertIn('name', model2)
106 self.assertIn('name', model2)
107 self.assertIn('path', model2)
107 self.assertIn('path', model2)
@@ -111,8 +111,8 b' class TestNotebookManager(TestCase):'
111 # Test in sub-directory
111 # Test in sub-directory
112 sub_dir = '/foo/'
112 sub_dir = '/foo/'
113 self.make_dir(nm.notebook_dir, 'foo')
113 self.make_dir(nm.notebook_dir, 'foo')
114 model = nm.create_notebook_model(None, sub_dir)
114 model = nm.create_notebook(None, sub_dir)
115 model2 = nm.get_notebook_model(name, sub_dir)
115 model2 = nm.get_notebook(name, sub_dir)
116 assert isinstance(model2, dict)
116 assert isinstance(model2, dict)
117 self.assertIn('name', model2)
117 self.assertIn('name', model2)
118 self.assertIn('path', model2)
118 self.assertIn('path', model2)
@@ -120,37 +120,37 b' class TestNotebookManager(TestCase):'
120 self.assertEqual(model2['name'], 'Untitled0.ipynb')
120 self.assertEqual(model2['name'], 'Untitled0.ipynb')
121 self.assertEqual(model2['path'], sub_dir.strip('/'))
121 self.assertEqual(model2['path'], sub_dir.strip('/'))
122
122
123 def test_update_notebook_model(self):
123 def test_update_notebook(self):
124 with TemporaryDirectory() as td:
124 with TemporaryDirectory() as td:
125 # Test in root directory
125 # Test in root directory
126 # Create a notebook
126 # Create a notebook
127 nm = FileNotebookManager(notebook_dir=td)
127 nm = FileNotebookManager(notebook_dir=td)
128 model = nm.create_notebook_model()
128 model = nm.create_notebook()
129 name = model['name']
129 name = model['name']
130 path = model['path']
130 path = model['path']
131
131
132 # Change the name in the model for rename
132 # Change the name in the model for rename
133 model['name'] = 'test.ipynb'
133 model['name'] = 'test.ipynb'
134 model = nm.update_notebook_model(model, name, path)
134 model = nm.update_notebook(model, name, path)
135 assert isinstance(model, dict)
135 assert isinstance(model, dict)
136 self.assertIn('name', model)
136 self.assertIn('name', model)
137 self.assertIn('path', model)
137 self.assertIn('path', model)
138 self.assertEqual(model['name'], 'test.ipynb')
138 self.assertEqual(model['name'], 'test.ipynb')
139
139
140 # Make sure the old name is gone
140 # Make sure the old name is gone
141 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
141 self.assertRaises(HTTPError, nm.get_notebook, name, path)
142
142
143 # Test in sub-directory
143 # Test in sub-directory
144 # Create a directory and notebook in that directory
144 # Create a directory and notebook in that directory
145 sub_dir = '/foo/'
145 sub_dir = '/foo/'
146 self.make_dir(nm.notebook_dir, 'foo')
146 self.make_dir(nm.notebook_dir, 'foo')
147 model = nm.create_notebook_model(None, sub_dir)
147 model = nm.create_notebook(None, sub_dir)
148 name = model['name']
148 name = model['name']
149 path = model['path']
149 path = model['path']
150
150
151 # Change the name in the model for rename
151 # Change the name in the model for rename
152 model['name'] = 'test_in_sub.ipynb'
152 model['name'] = 'test_in_sub.ipynb'
153 model = nm.update_notebook_model(model, name, path)
153 model = nm.update_notebook(model, name, path)
154 assert isinstance(model, dict)
154 assert isinstance(model, dict)
155 self.assertIn('name', model)
155 self.assertIn('name', model)
156 self.assertIn('path', model)
156 self.assertIn('path', model)
@@ -158,22 +158,22 b' class TestNotebookManager(TestCase):'
158 self.assertEqual(model['path'], sub_dir.strip('/'))
158 self.assertEqual(model['path'], sub_dir.strip('/'))
159
159
160 # Make sure the old name is gone
160 # Make sure the old name is gone
161 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
161 self.assertRaises(HTTPError, nm.get_notebook, name, path)
162
162
163 def test_save_notebook_model(self):
163 def test_save_notebook(self):
164 with TemporaryDirectory() as td:
164 with TemporaryDirectory() as td:
165 # Test in the root directory
165 # Test in the root directory
166 # Create a notebook
166 # Create a notebook
167 nm = FileNotebookManager(notebook_dir=td)
167 nm = FileNotebookManager(notebook_dir=td)
168 model = nm.create_notebook_model()
168 model = nm.create_notebook()
169 name = model['name']
169 name = model['name']
170 path = model['path']
170 path = model['path']
171
171
172 # Get the model with 'content'
172 # Get the model with 'content'
173 full_model = nm.get_notebook_model(name, path)
173 full_model = nm.get_notebook(name, path)
174
174
175 # Save the notebook
175 # Save the notebook
176 model = nm.save_notebook_model(full_model, name, path)
176 model = nm.save_notebook(full_model, name, path)
177 assert isinstance(model, dict)
177 assert isinstance(model, dict)
178 self.assertIn('name', model)
178 self.assertIn('name', model)
179 self.assertIn('path', model)
179 self.assertIn('path', model)
@@ -184,13 +184,13 b' class TestNotebookManager(TestCase):'
184 # Create a directory and notebook in that directory
184 # Create a directory and notebook in that directory
185 sub_dir = '/foo/'
185 sub_dir = '/foo/'
186 self.make_dir(nm.notebook_dir, 'foo')
186 self.make_dir(nm.notebook_dir, 'foo')
187 model = nm.create_notebook_model(None, sub_dir)
187 model = nm.create_notebook(None, sub_dir)
188 name = model['name']
188 name = model['name']
189 path = model['path']
189 path = model['path']
190 model = nm.get_notebook_model(name, path)
190 model = nm.get_notebook(name, path)
191
191
192 # Change the name in the model for rename
192 # Change the name in the model for rename
193 model = nm.save_notebook_model(model, name, path)
193 model = nm.save_notebook(model, name, path)
194 assert isinstance(model, dict)
194 assert isinstance(model, dict)
195 self.assertIn('name', model)
195 self.assertIn('name', model)
196 self.assertIn('path', model)
196 self.assertIn('path', model)
@@ -202,34 +202,34 b' class TestNotebookManager(TestCase):'
202 # Create a notebook
202 # Create a notebook
203 nm = FileNotebookManager(notebook_dir=td)
203 nm = FileNotebookManager(notebook_dir=td)
204 nm.save_script = True
204 nm.save_script = True
205 model = nm.create_notebook_model()
205 model = nm.create_notebook()
206 name = model['name']
206 name = model['name']
207 path = model['path']
207 path = model['path']
208
208
209 # Get the model with 'content'
209 # Get the model with 'content'
210 full_model = nm.get_notebook_model(name, path)
210 full_model = nm.get_notebook(name, path)
211
211
212 # Save the notebook
212 # Save the notebook
213 model = nm.save_notebook_model(full_model, name, path)
213 model = nm.save_notebook(full_model, name, path)
214
214
215 # Check that the script was created
215 # Check that the script was created
216 py_path = os.path.join(td, os.path.splitext(name)[0]+'.py')
216 py_path = os.path.join(td, os.path.splitext(name)[0]+'.py')
217 assert os.path.exists(py_path), py_path
217 assert os.path.exists(py_path), py_path
218
218
219 def test_delete_notebook_model(self):
219 def test_delete_notebook(self):
220 with TemporaryDirectory() as td:
220 with TemporaryDirectory() as td:
221 # Test in the root directory
221 # Test in the root directory
222 # Create a notebook
222 # Create a notebook
223 nm = FileNotebookManager(notebook_dir=td)
223 nm = FileNotebookManager(notebook_dir=td)
224 model = nm.create_notebook_model()
224 model = nm.create_notebook()
225 name = model['name']
225 name = model['name']
226 path = model['path']
226 path = model['path']
227
227
228 # Delete the notebook
228 # Delete the notebook
229 nm.delete_notebook_model(name, path)
229 nm.delete_notebook(name, path)
230
230
231 # Check that a 'get' on the deleted notebook raises and error
231 # Check that a 'get' on the deleted notebook raises and error
232 self.assertRaises(HTTPError, nm.get_notebook_model, name, path)
232 self.assertRaises(HTTPError, nm.get_notebook, name, path)
233
233
234 def test_copy_notebook(self):
234 def test_copy_notebook(self):
235 with TemporaryDirectory() as td:
235 with TemporaryDirectory() as td:
@@ -239,7 +239,7 b' class TestNotebookManager(TestCase):'
239 path = u'å b'
239 path = u'å b'
240 name = u'nb √.ipynb'
240 name = u'nb √.ipynb'
241 os.mkdir(os.path.join(td, path))
241 os.mkdir(os.path.join(td, path))
242 orig = nm.create_notebook_model({'name' : name}, path=path)
242 orig = nm.create_notebook({'name' : name}, path=path)
243
243
244 # copy with unspecified name
244 # copy with unspecified name
245 copy = nm.copy_notebook(name, path=path)
245 copy = nm.copy_notebook(name, path=path)
General Comments 0
You need to be logged in to leave comments. Login now