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