filenbmanager.py
333 lines
| 12.6 KiB
| text/x-python
|
PythonLexer
|
r4609 | """A notebook manager that uses the local file system for storage. | ||
Authors: | ||||
* Brian Granger | ||||
|
r13046 | * Zach Sailer | ||
|
r4609 | """ | ||
|
r4484 | #----------------------------------------------------------------------------- | ||
|
r8180 | # Copyright (C) 2011 The IPython Development Team | ||
|
r4484 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
|
r4609 | # the file COPYING, distributed as part of this software. | ||
|
r4484 | #----------------------------------------------------------------------------- | ||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
import datetime | ||||
|
r6030 | import io | ||
|
r4484 | import os | ||
|
r4624 | import glob | ||
|
r10497 | import shutil | ||
|
r12984 | |||
|
r10777 | from unicodedata import normalize | ||
|
r4484 | |||
from tornado import web | ||||
|
r8194 | from .nbmanager import NotebookManager | ||
|
r4484 | from IPython.nbformat import current | ||
|
r8180 | from IPython.utils.traitlets import Unicode, Dict, Bool, TraitError | ||
|
r11145 | from IPython.utils import tz | ||
|
r4484 | |||
#----------------------------------------------------------------------------- | ||||
|
r5758 | # Classes | ||
#----------------------------------------------------------------------------- | ||||
|
r4484 | |||
|
r8194 | class FileNotebookManager(NotebookManager): | ||
|
r5653 | |||
save_script = Bool(False, config=True, | ||||
|
r5760 | help="""Automatically create a Python script when saving the notebook. | ||
|
r5653 | |||
|
r6765 | For easier use of import, %run and %load across notebooks, a | ||
|
r5758 | <notebook-name>.py script will be created next to any | ||
<notebook-name>.ipynb on each save. This can also be set with the | ||||
short `--script` flag. | ||||
|
r5653 | """ | ||
) | ||||
|
r10497 | checkpoint_dir = Unicode(config=True, | ||
help="""The location in which to keep notebook checkpoints | ||||
By default, it is notebook-dir/.ipynb_checkpoints | ||||
""" | ||||
) | ||||
def _checkpoint_dir_default(self): | ||||
return os.path.join(self.notebook_dir, '.ipynb_checkpoints') | ||||
def _checkpoint_dir_changed(self, name, old, new): | ||||
"""do a bit of validation of the checkpoint dir""" | ||||
if not os.path.isabs(new): | ||||
# If we receive a non-absolute path, make it absolute. | ||||
abs_new = os.path.abspath(new) | ||||
self.checkpoint_dir = abs_new | ||||
return | ||||
if os.path.exists(new) and not os.path.isdir(new): | ||||
raise TraitError("checkpoint dir %r is not a directory" % new) | ||||
if not os.path.exists(new): | ||||
self.log.info("Creating checkpoint dir %s", new) | ||||
try: | ||||
os.mkdir(new) | ||||
except: | ||||
raise TraitError("Couldn't create checkpoint dir %r" % new) | ||||
|
r4484 | filename_ext = Unicode(u'.ipynb') | ||
|
r12984 | def get_notebook_names(self, path): | ||
|
r8180 | """List all notebook names in the notebook dir.""" | ||
|
r13033 | names = glob.glob(self.get_os_path('*'+self.filename_ext, path)) | ||
|
r12984 | names = [os.path.basename(name) | ||
|
r4623 | for name in names] | ||
|
r8180 | return names | ||
|
r12997 | |||
|
r13046 | def increment_filename(self, basename, path='/'): | ||
"""Return a non-used filename of the form basename<int>. | ||||
This searches through the filenames (basename0, basename1, ...) | ||||
until is find one that is not already being used. It is used to | ||||
create Untitled and Copy names that are unique. | ||||
""" | ||||
i = 0 | ||||
while True: | ||||
name = u'%s%i.ipynb' % (basename,i) | ||||
os_path = self.get_os_path(name, path) | ||||
if not os.path.isfile(os_path): | ||||
break | ||||
else: | ||||
i = i+1 | ||||
return name | ||||
|
r4484 | |||
|
r13032 | def notebook_exists(self, name, path): | ||
"""Returns a True if the notebook exists. Else, returns False. | ||||
|
r13046 | |||
|
r13032 | Parameters | ||
---------- | ||||
name : string | ||||
The name of the notebook you are checking. | ||||
path : string | ||||
The relative path to the notebook (with '/' as separator) | ||||
|
r13046 | |||
|
r13032 | Returns | ||
------- | ||||
bool | ||||
""" | ||||
path = self.get_os_path(name, path) | ||||
return os.path.isfile(path) | ||||
|
r12984 | |||
|
r13046 | def list_notebooks(self, path): | ||
"""List all notebooks in the notebook dir.""" | ||||
notebook_names = self.get_notebook_names(path) | ||||
notebooks = [] | ||||
for name in notebook_names: | ||||
model = self.get_notebook_model(name, path, content=False) | ||||
notebooks.append(model) | ||||
notebooks = sorted(notebooks, key=lambda item: item['name']) | ||||
return notebooks | ||||
def get_notebook_model(self, name, path='/', content=True): | ||||
|
r10497 | """read a notebook object from a path""" | ||
|
r13046 | os_path = self.get_os_path(name, path) | ||
if not os.path.isfile(os_path): | ||||
raise web.HTTPError(404, u'Notebook does not exist: %s' % name) | ||||
info = os.stat(os_path) | ||||
|
r11145 | last_modified = tz.utcfromtimestamp(info.st_mtime) | ||
|
r13046 | # Create the notebook model. | ||
model ={} | ||||
model['name'] = name | ||||
model['path'] = path | ||||
model['last_modified'] = last_modified.ctime() | ||||
if content is True: | ||||
with open(os_path,'r') as f: | ||||
s = f.read() | ||||
try: | ||||
# v1 and v2 and json in the .ipynb files. | ||||
nb = current.reads(s, u'json') | ||||
except ValueError as e: | ||||
raise web.HTTPError(400, u"Unreadable Notebook: %s" % e) | ||||
model['content'] = nb | ||||
return model | ||||
|
r12997 | |||
|
r13046 | def save_notebook_model(self, model, name, path='/'): | ||
"""Save the notebook model and return the model with no content.""" | ||||
if 'content' not in model: | ||||
raise web.HTTPError(400, u'No notebook JSON data provided') | ||||
new_path = model.get('path', path) | ||||
new_name = model.get('name', name) | ||||
if path != new_path or name != new_name: | ||||
self.rename_notebook(name, path, new_name, new_path) | ||||
|
r11052 | |||
|
r13046 | # Save the notebook file | ||
ospath = self.get_os_path(new_name, new_path) | ||||
nb = model['content'] | ||||
if 'name' in nb['metadata']: | ||||
nb['metadata']['name'] = u'' | ||||
|
r4484 | try: | ||
|
r13046 | self.log.debug("Autosaving notebook %s", ospath) | ||
with open(ospath,'w') as f: | ||||
|
r4633 | current.write(nb, f, u'json') | ||
|
r5709 | except Exception as e: | ||
|
r13046 | #raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s' % ospath) | ||
raise e | ||||
|
r8180 | |||
|
r13046 | # Save .py script as well | ||
|
r5653 | if self.save_script: | ||
pypath = os.path.splitext(path)[0] + '.py' | ||||
|
r10497 | self.log.debug("Writing script %s", pypath) | ||
|
r5653 | try: | ||
|
r13046 | with io.open(pypath, 'w', encoding='utf-8') as f: | ||
current.write(model, f, u'py') | ||||
|
r5709 | except Exception as e: | ||
|
r13046 | raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % pypath) | ||
model = self.get_notebook_model(name, path, content=False) | ||||
return model | ||||
def update_notebook_model(self, model, name, path='/'): | ||||
"""Update the notebook's path and/or name""" | ||||
new_name = model.get('name', name) | ||||
new_path = model.get('path', path) | ||||
if path != new_path or name != new_name: | ||||
self.rename_notebook(name, path, new_name, new_path) | ||||
model = self.get_notebook_model(new_name, new_path, content=False) | ||||
return model | ||||
def delete_notebook_model(self, name, path='/'): | ||||
"""Delete notebook by name and path.""" | ||||
nb_path = self.get_os_path(name, path) | ||||
|
r10518 | if not os.path.isfile(nb_path): | ||
|
r13046 | raise web.HTTPError(404, u'Notebook does not exist: %s' % nb_path) | ||
|
r10497 | |||
# clear checkpoints | ||||
|
r13046 | for checkpoint in self.list_checkpoints(name): | ||
|
r10518 | checkpoint_id = checkpoint['checkpoint_id'] | ||
|
r13046 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | ||
self.log.debug(cp_path) | ||||
if os.path.isfile(cp_path): | ||||
self.log.debug("Unlinking checkpoint %s", cp_path) | ||||
os.unlink(cp_path) | ||||
|
r10518 | |||
|
r13046 | self.log.debug("Unlinking notebook %s", nb_path) | ||
|
r10518 | os.unlink(nb_path) | ||
|
r4484 | |||
|
r13046 | def rename_notebook(self, old_name, old_path, new_name, new_path): | ||
"""Rename a notebook.""" | ||||
if new_name == old_name and new_path == old_path: | ||||
return | ||||
|
r5877 | |||
|
r13046 | new_full_path = self.get_os_path(new_name, new_path) | ||
old_full_path = self.get_os_path(old_name, old_path) | ||||
# Should we proceed with the move? | ||||
if os.path.isfile(new_full_path): | ||||
raise web.HTTPError(409, u'Notebook with name already exists: ' % new_full_path) | ||||
if self.save_script: | ||||
old_pypath = os.path.splitext(old_full_path)[0] + '.py' | ||||
new_pypath = os.path.splitext(new_full_path)[0] + '.py' | ||||
if os.path.isfile(new_pypath): | ||||
raise web.HTTPError(409, u'Python script with name already exists: %s' % new_pypath) | ||||
# Move the notebook file | ||||
try: | ||||
os.rename(old_full_path, new_full_path) | ||||
except: | ||||
raise web.HTTPError(400, u'Unknown error renaming notebook: %s' % old_full_path) | ||||
# Move the checkpoints | ||||
old_checkpoints = self.list_checkpoints(old_name, old_path) | ||||
for cp in old_checkpoints: | ||||
checkpoint_id = cp['checkpoint_id'] | ||||
old_cp_path = self.get_checkpoint_path(checkpoint_id, old_name, path) | ||||
new_cp_path = self.get_checkpoint_path(checkpoint_id, new_name, path) | ||||
if os.path.isfile(old_cp_path): | ||||
self.log.debug("Renaming checkpoint %s -> %s", old_cp_path, new_cp_path) | ||||
os.rename(old_cp_path, new_cp_path) | ||||
# Move the .py script | ||||
if self.save_script: | ||||
os.rename(old_pypath, new_pypath) | ||||
|
r10497 | # Checkpoint-related utilities | ||
|
r13046 | def get_checkpoint_path(self, checkpoint_id, name, path='/'): | ||
"""find the path to a checkpoint""" | ||||
|
r10777 | filename = u"{name}-{checkpoint_id}{ext}".format( | ||
|
r10497 | name=name, | ||
checkpoint_id=checkpoint_id, | ||||
ext=self.filename_ext, | ||||
) | ||||
|
r13046 | cp_path = os.path.join(path, self.checkpoint_dir, filename) | ||
return cp_path | ||||
def get_checkpoint_model(self, checkpoint_id, name, path='/'): | ||||
|
r10500 | """construct the info dict for a given checkpoint""" | ||
|
r13046 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | ||
stats = os.stat(cp_path) | ||||
|
r11145 | last_modified = tz.utcfromtimestamp(stats.st_mtime) | ||
|
r10500 | info = dict( | ||
checkpoint_id = checkpoint_id, | ||||
last_modified = last_modified, | ||||
) | ||||
return info | ||||
|
r10497 | # public checkpoint API | ||
|
r13046 | def create_checkpoint(self, name, path='/'): | ||
|
r10497 | """Create a checkpoint from the current state of a notebook""" | ||
|
r13046 | nb_path = self.get_os_path(name, path) | ||
|
r10500 | # only the one checkpoint ID: | ||
|
r10777 | checkpoint_id = u"checkpoint" | ||
|
r13046 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | ||
self.log.debug("creating checkpoint for notebook %s", name) | ||||
|
r10497 | if not os.path.exists(self.checkpoint_dir): | ||
os.mkdir(self.checkpoint_dir) | ||||
shutil.copy2(nb_path, cp_path) | ||||
|
r10500 | |||
# return the checkpoint info | ||||
|
r13046 | return self.get_checkpoint_model(checkpoint_id, name, path) | ||
|
r10497 | |||
|
r13046 | def list_checkpoints(self, name, path='/'): | ||
|
r10497 | """list the checkpoints for a given notebook | ||
|
r10019 | |||
|
r10497 | This notebook manager currently only supports one checkpoint per notebook. | ||
""" | ||||
|
r12984 | checkpoint_id = "checkpoint" | ||
|
r13046 | path = self.get_checkpoint_path(checkpoint_id, name, path) | ||
|
r10500 | if not os.path.exists(path): | ||
|
r10497 | return [] | ||
|
r10500 | else: | ||
|
r13046 | return [self.get_checkpoint_model(checkpoint_id, name, path)] | ||
|
r10500 | |||
|
r10497 | |||
|
r13046 | def restore_checkpoint(self, checkpoint_id, name, path='/'): | ||
|
r10497 | """restore a notebook to a checkpointed state""" | ||
|
r13046 | self.log.info("restoring Notebook %s from checkpoint %s", name, checkpoint_id) | ||
nb_path = self.get_os_path(name, path) | ||||
cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | ||||
|
r10497 | if not os.path.isfile(cp_path): | ||
|
r10500 | self.log.debug("checkpoint file does not exist: %s", cp_path) | ||
|
r10497 | raise web.HTTPError(404, | ||
|
r13046 | u'Notebook checkpoint does not exist: %s-%s' % (name, checkpoint_id) | ||
|
r10497 | ) | ||
# ensure notebook is readable (never restore from an unreadable notebook) | ||||
|
r13046 | with file(cp_path, 'r') as f: | ||
nb = current.read(f, u'json') | ||||
|
r10497 | shutil.copy2(cp_path, nb_path) | ||
self.log.debug("copying %s -> %s", cp_path, nb_path) | ||||
|
r13046 | def delete_checkpoint(self, checkpoint_id, name, path='/'): | ||
|
r10497 | """delete a notebook's checkpoint""" | ||
|
r13046 | cp_path = self.get_checkpoint_path(checkpoint_id, name, path) | ||
if not os.path.isfile(cp_path): | ||||
|
r10497 | raise web.HTTPError(404, | ||
|
r13046 | u'Notebook checkpoint does not exist: %s-%s' % (name, checkpoint_id) | ||
|
r10497 | ) | ||
|
r13046 | self.log.debug("unlinking %s", cp_path) | ||
os.unlink(cp_path) | ||||
|
r10497 | |||
|
r10019 | def info_string(self): | ||
return "Serving notebooks from local directory: %s" % self.notebook_dir | ||||