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