Show More
@@ -73,22 +73,15 b' class FileNotebookManager(NotebookManager):' | |||||
73 | except: |
|
73 | except: | |
74 | raise TraitError("Couldn't create checkpoint dir %r" % new) |
|
74 | raise TraitError("Couldn't create checkpoint dir %r" % new) | |
75 |
|
75 | |||
76 | filename_ext = Unicode(u'.ipynb') |
|
76 | def get_notebook_names(self, path='/'): | |
77 |
|
77 | """List all notebook names in the notebook dir and path.""" | ||
78 | def get_notebook_names(self, path): |
|
|||
79 | """List all notebook names in the notebook dir.""" |
|
|||
80 | names = glob.glob(self.get_os_path('*'+self.filename_ext, path)) |
|
78 | names = glob.glob(self.get_os_path('*'+self.filename_ext, path)) | |
81 | names = [os.path.basename(name) |
|
79 | names = [os.path.basename(name) | |
82 | for name in names] |
|
80 | for name in names] | |
83 | return names |
|
81 | return names | |
84 |
|
82 | |||
85 | def increment_filename(self, basename, path='/'): |
|
83 | def increment_filename(self, basename, path='/'): | |
86 | """Return a non-used filename of the form basename<int>. |
|
84 | """Return a non-used filename of the form basename<int>.""" | |
87 |
|
||||
88 | This searches through the filenames (basename0, basename1, ...) |
|
|||
89 | until is find one that is not already being used. It is used to |
|
|||
90 | create Untitled and Copy names that are unique. |
|
|||
91 | """ |
|
|||
92 | i = 0 |
|
85 | i = 0 | |
93 | while True: |
|
86 | while True: | |
94 | name = u'%s%i.ipynb' % (basename,i) |
|
87 | name = u'%s%i.ipynb' % (basename,i) | |
@@ -99,7 +92,7 b' class FileNotebookManager(NotebookManager):' | |||||
99 | i = i+1 |
|
92 | i = i+1 | |
100 | return name |
|
93 | return name | |
101 |
|
94 | |||
102 | def notebook_exists(self, name, path): |
|
95 | def notebook_exists(self, name, path='/'): | |
103 | """Returns a True if the notebook exists. Else, returns False. |
|
96 | """Returns a True if the notebook exists. Else, returns False. | |
104 |
|
97 | |||
105 | Parameters |
|
98 | Parameters | |
@@ -113,7 +106,7 b' class FileNotebookManager(NotebookManager):' | |||||
113 | ------- |
|
106 | ------- | |
114 | bool |
|
107 | bool | |
115 | """ |
|
108 | """ | |
116 | path = self.get_os_path(name, path) |
|
109 | path = self.get_os_path(name, path='/') | |
117 | return os.path.isfile(path) |
|
110 | return os.path.isfile(path) | |
118 |
|
111 | |||
119 | def list_notebooks(self, path): |
|
112 | def list_notebooks(self, path): | |
@@ -165,15 +158,13 b' class FileNotebookManager(NotebookManager):' | |||||
165 | model ={} |
|
158 | model ={} | |
166 | model['name'] = name |
|
159 | model['name'] = name | |
167 | model['path'] = path |
|
160 | model['path'] = path | |
168 |
model['last_modified'] = last_modified |
|
161 | model['last_modified'] = last_modified | |
169 | if content is True: |
|
162 | if content is True: | |
170 | with open(os_path,'r') as f: |
|
163 | with open(os_path, 'r') as f: | |
171 | s = f.read() |
|
|||
172 | try: |
|
164 | try: | |
173 | # v1 and v2 and json in the .ipynb files. |
|
165 | nb = current.read(f, u'json') | |
174 | nb = current.reads(s, u'json') |
|
166 | except Exception as e: | |
175 | except ValueError as e: |
|
167 | raise web.HTTPError(400, u"Unreadable Notebook: %s %s" % (os_path, e)) | |
176 | raise web.HTTPError(400, u"Unreadable Notebook: %s" % e) |
|
|||
177 | model['content'] = nb |
|
168 | model['content'] = nb | |
178 | return model |
|
169 | return model | |
179 |
|
170 | |||
@@ -190,27 +181,26 b' class FileNotebookManager(NotebookManager):' | |||||
190 | self.rename_notebook(name, path, new_name, new_path) |
|
181 | self.rename_notebook(name, path, new_name, new_path) | |
191 |
|
182 | |||
192 | # Save the notebook file |
|
183 | # Save the notebook file | |
193 | ospath = self.get_os_path(new_name, new_path) |
|
184 | os_path = self.get_os_path(new_name, new_path) | |
194 | nb = model['content'] |
|
185 | nb = current.to_notebook_json(model['content']) | |
195 | if 'name' in nb['metadata']: |
|
186 | if 'name' in nb['metadata']: | |
196 | nb['metadata']['name'] = u'' |
|
187 | nb['metadata']['name'] = u'' | |
197 | try: |
|
188 | try: | |
198 | self.log.debug("Autosaving notebook %s", ospath) |
|
189 | self.log.debug("Autosaving notebook %s", os_path) | |
199 | with open(ospath,'w') as f: |
|
190 | with open(os_path, 'w') as f: | |
200 | current.write(nb, f, u'json') |
|
191 | current.write(nb, f, u'json') | |
201 | except Exception as e: |
|
192 | except Exception as e: | |
202 |
|
|
193 | raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s %s' % (os_path, e)) | |
203 | raise e |
|
|||
204 |
|
194 | |||
205 | # Save .py script as well |
|
195 | # Save .py script as well | |
206 | if self.save_script: |
|
196 | if self.save_script: | |
207 | pypath = os.path.splitext(path)[0] + '.py' |
|
197 | py_path = os.path.splitext(os_path)[0] + '.py' | |
208 | self.log.debug("Writing script %s", pypath) |
|
198 | self.log.debug("Writing script %s", py_path) | |
209 | try: |
|
199 | try: | |
210 | with io.open(pypath, 'w', encoding='utf-8') as f: |
|
200 | with io.open(py_path, 'w', encoding='utf-8') as f: | |
211 | current.write(model, f, u'py') |
|
201 | current.write(model, f, u'py') | |
212 | except Exception as e: |
|
202 | except Exception as e: | |
213 | raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % pypath) |
|
203 | raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s %s' % (py_path, e)) | |
214 |
|
204 | |||
215 | model = self.get_notebook_model(name, path, content=False) |
|
205 | model = self.get_notebook_model(name, path, content=False) | |
216 | return model |
|
206 | return model | |
@@ -226,15 +216,14 b' class FileNotebookManager(NotebookManager):' | |||||
226 |
|
216 | |||
227 | def delete_notebook_model(self, name, path='/'): |
|
217 | def delete_notebook_model(self, name, path='/'): | |
228 | """Delete notebook by name and path.""" |
|
218 | """Delete notebook by name and path.""" | |
229 |
|
|
219 | os_path = self.get_os_path(name, path) | |
230 |
if not os.path.isfile( |
|
220 | if not os.path.isfile(os_path): | |
231 |
raise web.HTTPError(404, u'Notebook does not exist: %s' % |
|
221 | raise web.HTTPError(404, u'Notebook does not exist: %s' % os_path) | |
232 |
|
222 | |||
233 | # clear checkpoints |
|
223 | # clear checkpoints | |
234 | for checkpoint in self.list_checkpoints(name): |
|
224 | for checkpoint in self.list_checkpoints(name, path): | |
235 | checkpoint_id = checkpoint['checkpoint_id'] |
|
225 | checkpoint_id = checkpoint['checkpoint_id'] | |
236 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) |
|
226 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | |
237 | self.log.debug(cp_path) |
|
|||
238 | if os.path.isfile(cp_path): |
|
227 | if os.path.isfile(cp_path): | |
239 | self.log.debug("Unlinking checkpoint %s", cp_path) |
|
228 | self.log.debug("Unlinking checkpoint %s", cp_path) | |
240 | os.unlink(cp_path) |
|
229 | os.unlink(cp_path) | |
@@ -247,23 +236,23 b' class FileNotebookManager(NotebookManager):' | |||||
247 | if new_name == old_name and new_path == old_path: |
|
236 | if new_name == old_name and new_path == old_path: | |
248 | return |
|
237 | return | |
249 |
|
238 | |||
250 |
new_ |
|
239 | new_os_path = self.get_os_path(new_name, new_path) | |
251 |
old_ |
|
240 | old_os_path = self.get_os_path(old_name, old_path) | |
252 |
|
241 | |||
253 | # Should we proceed with the move? |
|
242 | # Should we proceed with the move? | |
254 |
if os.path.isfile(new_ |
|
243 | if os.path.isfile(new_os_path): | |
255 |
raise web.HTTPError(409, u'Notebook with name already exists: ' % new_ |
|
244 | raise web.HTTPError(409, u'Notebook with name already exists: ' % new_os_path) | |
256 | if self.save_script: |
|
245 | if self.save_script: | |
257 |
old_pypath = os.path.splitext(old_ |
|
246 | old_py_path = os.path.splitext(old_os_path)[0] + '.py' | |
258 |
new_pypath = os.path.splitext(new_ |
|
247 | new_py_path = os.path.splitext(new_os_path)[0] + '.py' | |
259 | if os.path.isfile(new_pypath): |
|
248 | if os.path.isfile(new_py_path): | |
260 | raise web.HTTPError(409, u'Python script with name already exists: %s' % new_pypath) |
|
249 | raise web.HTTPError(409, u'Python script with name already exists: %s' % new_py_path) | |
261 |
|
250 | |||
262 | # Move the notebook file |
|
251 | # Move the notebook file | |
263 | try: |
|
252 | try: | |
264 |
os.rename(old_ |
|
253 | os.rename(old_os_path, new_os_path) | |
265 | except: |
|
254 | except Exception as e: | |
266 |
raise web.HTTPError(400, u'Unknown error renaming notebook: %s' % |
|
255 | raise web.HTTPError(400, u'Unknown error renaming notebook: %s %s' % (old_os_path, e)) | |
267 |
|
256 | |||
268 | # Move the checkpoints |
|
257 | # Move the checkpoints | |
269 | old_checkpoints = self.list_checkpoints(old_name, old_path) |
|
258 | old_checkpoints = self.list_checkpoints(old_name, old_path) | |
@@ -277,7 +266,7 b' class FileNotebookManager(NotebookManager):' | |||||
277 |
|
266 | |||
278 | # Move the .py script |
|
267 | # Move the .py script | |
279 | if self.save_script: |
|
268 | if self.save_script: | |
280 | os.rename(old_pypath, new_pypath) |
|
269 | os.rename(old_py_path, new_py_path) | |
281 |
|
270 | |||
282 | # Checkpoint-related utilities |
|
271 | # Checkpoint-related utilities | |
283 |
|
272 | |||
@@ -352,7 +341,7 b' class FileNotebookManager(NotebookManager):' | |||||
352 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) |
|
341 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | |
353 | if not os.path.isfile(cp_path): |
|
342 | if not os.path.isfile(cp_path): | |
354 | raise web.HTTPError(404, |
|
343 | raise web.HTTPError(404, | |
355 | u'Notebook checkpoint does not exist: %s-%s' % (name, checkpoint_id) |
|
344 | u'Notebook checkpoint does not exist: %s%s-%s' % (path, name, checkpoint_id) | |
356 | ) |
|
345 | ) | |
357 | self.log.debug("unlinking %s", cp_path) |
|
346 | self.log.debug("unlinking %s", cp_path) | |
358 | os.unlink(cp_path) |
|
347 | os.unlink(cp_path) |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file |
@@ -29,7 +29,7 b' from IPython.nbformat.v3 import (' | |||||
29 | NotebookNode, |
|
29 | NotebookNode, | |
30 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, |
|
30 | new_code_cell, new_text_cell, new_notebook, new_output, new_worksheet, | |
31 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, |
|
31 | parse_filename, new_metadata, new_author, new_heading_cell, nbformat, | |
32 | nbformat_minor, |
|
32 | nbformat_minor, to_notebook_json | |
33 | ) |
|
33 | ) | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- |
General Comments 0
You need to be logged in to leave comments.
Login now