##// END OF EJS Templates
ensure 'path' never has leading or trailing slash in nbmanager...
MinRK -
Show More
@@ -75,6 +75,7 b' class FileNotebookManager(NotebookManager):'
75
75
76 def get_notebook_names(self, path=''):
76 def get_notebook_names(self, path=''):
77 """List all notebook names in the notebook dir and path."""
77 """List all notebook names in the notebook dir and path."""
78 path = path.strip('/')
78 names = glob.glob(self.get_os_path('*'+self.filename_ext, path))
79 names = glob.glob(self.get_os_path('*'+self.filename_ext, path))
79 names = [os.path.basename(name)
80 names = [os.path.basename(name)
80 for name in names]
81 for name in names]
@@ -82,6 +83,7 b' class FileNotebookManager(NotebookManager):'
82
83
83 def increment_filename(self, basename, path=''):
84 def increment_filename(self, basename, path=''):
84 """Return a non-used filename of the form basename<int>."""
85 """Return a non-used filename of the form basename<int>."""
86 path = path.strip('/')
85 i = 0
87 i = 0
86 while True:
88 while True:
87 name = u'%s%i.ipynb' % (basename,i)
89 name = u'%s%i.ipynb' % (basename,i)
@@ -106,6 +108,7 b' class FileNotebookManager(NotebookManager):'
106 exists : bool
108 exists : bool
107 Whether the path is indeed a directory.
109 Whether the path is indeed a directory.
108 """
110 """
111 path = path.strip('/')
109 os_path = self.get_os_path(path=path)
112 os_path = self.get_os_path(path=path)
110 return os.path.isdir(os_path)
113 return os.path.isdir(os_path)
111
114
@@ -149,6 +152,7 b' class FileNotebookManager(NotebookManager):'
149 -------
152 -------
150 bool
153 bool
151 """
154 """
155 path = path.strip('/')
152 nbpath = self.get_os_path(name, path=path)
156 nbpath = self.get_os_path(name, path=path)
153 return os.path.isfile(nbpath)
157 return os.path.isfile(nbpath)
154
158
@@ -167,6 +171,7 b' class FileNotebookManager(NotebookManager):'
167 notebooks : list of dicts
171 notebooks : list of dicts
168 a list of the notebook models without 'content'
172 a list of the notebook models without 'content'
169 """
173 """
174 path = path.strip('/')
170 notebook_names = self.get_notebook_names(path)
175 notebook_names = self.get_notebook_names(path)
171 notebooks = []
176 notebooks = []
172 for name in notebook_names:
177 for name in notebook_names:
@@ -192,6 +197,7 b' class FileNotebookManager(NotebookManager):'
192 the notebook model. If contents=True, returns the 'contents'
197 the notebook model. If contents=True, returns the 'contents'
193 dict in the model as well.
198 dict in the model as well.
194 """
199 """
200 path = path.strip('/')
195 if not self.notebook_exists(name=name, path=path):
201 if not self.notebook_exists(name=name, path=path):
196 raise web.HTTPError(404, u'Notebook does not exist: %s' % name)
202 raise web.HTTPError(404, u'Notebook does not exist: %s' % name)
197 os_path = self.get_os_path(name, path)
203 os_path = self.get_os_path(name, path)
@@ -215,6 +221,7 b' class FileNotebookManager(NotebookManager):'
215
221
216 def save_notebook_model(self, model, name='', path=''):
222 def save_notebook_model(self, model, name='', path=''):
217 """Save the notebook model and return the model with no content."""
223 """Save the notebook model and return the model with no content."""
224 path = path.strip('/')
218
225
219 if 'content' not in model:
226 if 'content' not in model:
220 raise web.HTTPError(400, u'No notebook JSON data provided')
227 raise web.HTTPError(400, u'No notebook JSON data provided')
@@ -250,17 +257,19 b' class FileNotebookManager(NotebookManager):'
250 model = self.get_notebook_model(name, path, content=False)
257 model = self.get_notebook_model(name, path, content=False)
251 return model
258 return model
252
259
253 def update_notebook_model(self, model, name, path='/'):
260 def update_notebook_model(self, model, name, path=''):
254 """Update the notebook's path and/or name"""
261 """Update the notebook's path and/or name"""
262 path = path.strip('/')
255 new_name = model.get('name', name)
263 new_name = model.get('name', name)
256 new_path = model.get('path', path)
264 new_path = model.get('path', path).strip('/')
257 if path != new_path or name != new_name:
265 if path != new_path or name != new_name:
258 self.rename_notebook(name, path, new_name, new_path)
266 self.rename_notebook(name, path, new_name, new_path)
259 model = self.get_notebook_model(new_name, new_path, content=False)
267 model = self.get_notebook_model(new_name, new_path, content=False)
260 return model
268 return model
261
269
262 def delete_notebook_model(self, name, path='/'):
270 def delete_notebook_model(self, name, path=''):
263 """Delete notebook by name and path."""
271 """Delete notebook by name and path."""
272 path = path.strip('/')
264 os_path = self.get_os_path(name, path)
273 os_path = self.get_os_path(name, path)
265 if not os.path.isfile(os_path):
274 if not os.path.isfile(os_path):
266 raise web.HTTPError(404, u'Notebook does not exist: %s' % os_path)
275 raise web.HTTPError(404, u'Notebook does not exist: %s' % os_path)
@@ -278,6 +287,8 b' class FileNotebookManager(NotebookManager):'
278
287
279 def rename_notebook(self, old_name, old_path, new_name, new_path):
288 def rename_notebook(self, old_name, old_path, new_name, new_path):
280 """Rename a notebook."""
289 """Rename a notebook."""
290 old_path = old_path.strip('/')
291 new_path = new_path.strip('/')
281 if new_name == old_name and new_path == old_path:
292 if new_name == old_name and new_path == old_path:
282 return
293 return
283
294
@@ -315,8 +326,9 b' class FileNotebookManager(NotebookManager):'
315
326
316 # Checkpoint-related utilities
327 # Checkpoint-related utilities
317
328
318 def get_checkpoint_path(self, checkpoint_id, name, path='/'):
329 def get_checkpoint_path(self, checkpoint_id, name, path=''):
319 """find the path to a checkpoint"""
330 """find the path to a checkpoint"""
331 path = path.strip('/')
320 filename = u"{name}-{checkpoint_id}{ext}".format(
332 filename = u"{name}-{checkpoint_id}{ext}".format(
321 name=name,
333 name=name,
322 checkpoint_id=checkpoint_id,
334 checkpoint_id=checkpoint_id,
@@ -325,8 +337,9 b' class FileNotebookManager(NotebookManager):'
325 cp_path = os.path.join(path, self.checkpoint_dir, filename)
337 cp_path = os.path.join(path, self.checkpoint_dir, filename)
326 return cp_path
338 return cp_path
327
339
328 def get_checkpoint_model(self, checkpoint_id, name, path='/'):
340 def get_checkpoint_model(self, checkpoint_id, name, path=''):
329 """construct the info dict for a given checkpoint"""
341 """construct the info dict for a given checkpoint"""
342 path = path.strip('/')
330 cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
343 cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
331 stats = os.stat(cp_path)
344 stats = os.stat(cp_path)
332 last_modified = tz.utcfromtimestamp(stats.st_mtime)
345 last_modified = tz.utcfromtimestamp(stats.st_mtime)
@@ -338,8 +351,9 b' class FileNotebookManager(NotebookManager):'
338
351
339 # public checkpoint API
352 # public checkpoint API
340
353
341 def create_checkpoint(self, name, path='/'):
354 def create_checkpoint(self, name, path=''):
342 """Create a checkpoint from the current state of a notebook"""
355 """Create a checkpoint from the current state of a notebook"""
356 path = path.strip('/')
343 nb_path = self.get_os_path(name, path)
357 nb_path = self.get_os_path(name, path)
344 # only the one checkpoint ID:
358 # only the one checkpoint ID:
345 checkpoint_id = u"checkpoint"
359 checkpoint_id = u"checkpoint"
@@ -352,11 +366,12 b' class FileNotebookManager(NotebookManager):'
352 # return the checkpoint info
366 # return the checkpoint info
353 return self.get_checkpoint_model(checkpoint_id, name, path)
367 return self.get_checkpoint_model(checkpoint_id, name, path)
354
368
355 def list_checkpoints(self, name, path='/'):
369 def list_checkpoints(self, name, path=''):
356 """list the checkpoints for a given notebook
370 """list the checkpoints for a given notebook
357
371
358 This notebook manager currently only supports one checkpoint per notebook.
372 This notebook manager currently only supports one checkpoint per notebook.
359 """
373 """
374 path = path.strip('/')
360 checkpoint_id = "checkpoint"
375 checkpoint_id = "checkpoint"
361 path = self.get_checkpoint_path(checkpoint_id, name, path)
376 path = self.get_checkpoint_path(checkpoint_id, name, path)
362 if not os.path.exists(path):
377 if not os.path.exists(path):
@@ -365,8 +380,9 b' class FileNotebookManager(NotebookManager):'
365 return [self.get_checkpoint_model(checkpoint_id, name, path)]
380 return [self.get_checkpoint_model(checkpoint_id, name, path)]
366
381
367
382
368 def restore_checkpoint(self, checkpoint_id, name, path='/'):
383 def restore_checkpoint(self, checkpoint_id, name, path=''):
369 """restore a notebook to a checkpointed state"""
384 """restore a notebook to a checkpointed state"""
385 path = path.strip('/')
370 self.log.info("restoring Notebook %s from checkpoint %s", name, checkpoint_id)
386 self.log.info("restoring Notebook %s from checkpoint %s", name, checkpoint_id)
371 nb_path = self.get_os_path(name, path)
387 nb_path = self.get_os_path(name, path)
372 cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
388 cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
@@ -381,8 +397,9 b' class FileNotebookManager(NotebookManager):'
381 shutil.copy2(cp_path, nb_path)
397 shutil.copy2(cp_path, nb_path)
382 self.log.debug("copying %s -> %s", cp_path, nb_path)
398 self.log.debug("copying %s -> %s", cp_path, nb_path)
383
399
384 def delete_checkpoint(self, checkpoint_id, name, path='/'):
400 def delete_checkpoint(self, checkpoint_id, name, path=''):
385 """delete a notebook's checkpoint"""
401 """delete a notebook's checkpoint"""
402 path = path.strip('/')
386 cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
403 cp_path = self.get_checkpoint_path(checkpoint_id, name, path)
387 if not os.path.isfile(cp_path):
404 if not os.path.isfile(cp_path):
388 raise web.HTTPError(404,
405 raise web.HTTPError(404,
@@ -118,6 +118,7 b' class NotebookManager(LoggingConfigurable):'
118
118
119 def create_notebook_model(self, model=None, path=''):
119 def create_notebook_model(self, model=None, path=''):
120 """Create a new untitled notebook and return its model with no content."""
120 """Create a new untitled notebook and return its model with no content."""
121 path = path.strip('/')
121 if model is None:
122 if model is None:
122 model = {}
123 model = {}
123 if 'content' not in model:
124 if 'content' not in model:
@@ -132,6 +133,7 b' class NotebookManager(LoggingConfigurable):'
132
133
133 def copy_notebook(self, name, path=''):
134 def copy_notebook(self, name, path=''):
134 """Copy an existing notebook and return its new model."""
135 """Copy an existing notebook and return its new model."""
136 path = path.strip('/')
135 model = self.get_notebook_model(name, path)
137 model = self.get_notebook_model(name, path)
136 name = os.path.splitext(name)[0] + '-Copy'
138 name = os.path.splitext(name)[0] + '-Copy'
137 name = self.increment_filename(name, path)
139 name = self.increment_filename(name, path)
General Comments 0
You need to be logged in to leave comments. Login now