filenbmanager.py
345 lines
| 14.3 KiB
| text/x-python
|
PythonLexer
|
r4609 | """A notebook manager that uses the local file system for storage. | ||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
|
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 | ||
|
r12997 | import ast | ||
|
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') | ||
|
r10497 | |||
|
r12984 | def get_notebook_names(self, path): | ||
|
r8180 | """List all notebook names in the notebook dir.""" | ||
|
r12984 | names = glob.glob(os.path.join(self.notebook_dir, path, | ||
|
r4623 | '*' + self.filename_ext)) | ||
|
r12984 | names = [os.path.basename(name) | ||
|
r4623 | for name in names] | ||
|
r8180 | return names | ||
|
r12984 | |||
def list_notebooks(self, path): | ||||
|
r8180 | """List all notebooks in the notebook dir.""" | ||
|
r12997 | notebook_names = self.get_notebook_names(path) | ||
notebook_mapping = [] | ||||
for name in notebook_names: | ||||
model = self.notebook_model(name, path) | ||||
notebook_mapping.append(model) | ||||
return notebook_mapping | ||||
def change_notebook(self, data, notebook_name, notebook_path=None): | ||||
"""Changes notebook""" | ||||
changes = data.keys() | ||||
for change in changes: | ||||
|
r12998 | full_path = self.get_path(notebook_name, notebook_path) | ||
|
r12997 | if change == "notebook_name": | ||
|
r12998 | os.rename(full_path, | ||
self.get_path(data['notebook_name'], notebook_path)) | ||||
|
r12997 | notebook_name = data['notebook_name'] | ||
if change == "notebook_path": | ||||
new_path = self.get_path(data['notebook_name'], data['notebook_path']) | ||||
|
r12998 | stutil.move(full_path, new_path) | ||
|
r12997 | notebook_path = data['notebook_path'] | ||
if change == "content": | ||||
self.save_notebook(data, notebook_name, notebook_path) | ||||
model = self.notebook_model(notebook_name, notebook_path) | ||||
return model | ||||
|
r4484 | |||
|
r13006 | def notebook_exists(self, notebook_path): | ||
|
r4484 | """Does a notebook exist?""" | ||
|
r13006 | return os.path.isfile(notebook_path) | ||
|
r10497 | |||
|
r12984 | def get_path(self, notebook_name, notebook_path=None): | ||
"""Return a full path to a notebook given its notebook_name.""" | ||||
return self.get_path_by_name(notebook_name, notebook_path) | ||||
def get_path_by_name(self, name, notebook_path=None): | ||||
|
r4484 | """Return a full path to a notebook given its name.""" | ||
|
r12984 | filename = name #+ self.filename_ext | ||
if notebook_path == None: | ||||
path = os.path.join(self.notebook_dir, filename) | ||||
else: | ||||
path = os.path.join(self.notebook_dir, notebook_path, filename) | ||||
|
r10497 | return path | ||
|
r4484 | |||
|
r10497 | def read_notebook_object_from_path(self, path): | ||
"""read a notebook object from a path""" | ||||
|
r4484 | info = os.stat(path) | ||
|
r11145 | last_modified = tz.utcfromtimestamp(info.st_mtime) | ||
|
r4633 | with open(path,'r') as f: | ||
s = f.read() | ||||
try: | ||||
# v1 and v2 and json in the .ipynb files. | ||||
nb = current.reads(s, u'json') | ||||
|
r11643 | except ValueError as e: | ||
msg = u"Unreadable Notebook: %s" % e | ||||
raise web.HTTPError(400, msg, reason=msg) | ||||
|
r10497 | return last_modified, nb | ||
|
r12984 | def read_notebook_object(self, notebook_name, notebook_path): | ||
"""Get the Notebook representation of a notebook by notebook_name.""" | ||||
path = self.get_path(notebook_name, notebook_path) | ||||
|
r10497 | if not os.path.isfile(path): | ||
|
r12984 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_name) | ||
|
r10497 | last_modified, nb = self.read_notebook_object_from_path(path) | ||
|
r7231 | # Always use the filename as the notebook name. | ||
|
r11052 | # Eventually we will get rid of the notebook name in the metadata | ||
# but for now, that name is just an empty string. Until the notebooks | ||||
# web service knows about names in URLs we still pass the name | ||||
# back to the web app using the metadata though. | ||||
|
r7615 | nb.metadata.name = os.path.splitext(os.path.basename(path))[0] | ||
|
r4484 | return last_modified, nb | ||
|
r10497 | |||
|
r12997 | def write_notebook_object(self, nb, notebook_name=None, notebook_path=None, new_name= None): | ||
|
r12984 | """Save an existing notebook object by notebook_name.""" | ||
|
r12997 | if new_name == None: | ||
try: | ||||
new_name = normalize('NFC', nb.metadata.name) | ||||
except AttributeError: | ||||
raise web.HTTPError(400, u'Missing notebook name') | ||||
|
r12984 | new_path = notebook_path | ||
old_name = notebook_name | ||||
old_checkpoints = self.list_checkpoints(old_name) | ||||
path = self.get_path_by_name(new_name, new_path) | ||||
|
r11052 | # Right before we save the notebook, we write an empty string as the | ||
# notebook name in the metadata. This is to prepare for removing | ||||
# this attribute entirely post 1.0. The web app still uses the metadata | ||||
# name for now. | ||||
nb.metadata.name = u'' | ||||
|
r4484 | try: | ||
|
r10518 | self.log.debug("Autosaving notebook %s", path) | ||
|
r4484 | with open(path,'w') as f: | ||
|
r4633 | current.write(nb, f, u'json') | ||
|
r5709 | except Exception as e: | ||
|
r10518 | raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s' % e) | ||
|
r8180 | |||
|
r5653 | # save .py script as well | ||
if self.save_script: | ||||
pypath = os.path.splitext(path)[0] + '.py' | ||||
|
r10497 | self.log.debug("Writing script %s", pypath) | ||
|
r5653 | try: | ||
|
r6031 | with io.open(pypath,'w', encoding='utf-8') as f: | ||
|
r5653 | current.write(nb, f, u'py') | ||
|
r5709 | except Exception as e: | ||
raise web.HTTPError(400, u'Unexpected error while saving notebook as script: %s' % e) | ||||
|
r5653 | |||
|
r12984 | if old_name != None: | ||
# remove old files if the name changed | ||||
if old_name != new_name: | ||||
# remove renamed original, if it exists | ||||
old_path = self.get_path_by_name(old_name, notebook_path) | ||||
if os.path.isfile(old_path): | ||||
self.log.debug("unlinking notebook %s", old_path) | ||||
os.unlink(old_path) | ||||
|
r10497 | |||
|
r12984 | # cleanup old script, if it exists | ||
if self.save_script: | ||||
old_pypath = os.path.splitext(old_path)[0] + '.py' | ||||
if os.path.isfile(old_pypath): | ||||
self.log.debug("unlinking script %s", old_pypath) | ||||
os.unlink(old_pypath) | ||||
# rename checkpoints to follow file | ||||
for cp in old_checkpoints: | ||||
checkpoint_id = cp['checkpoint_id'] | ||||
old_cp_path = self.get_checkpoint_path_by_name(old_name, checkpoint_id) | ||||
new_cp_path = self.get_checkpoint_path_by_name(new_name, checkpoint_id) | ||||
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) | ||||
|
r10497 | |||
|
r12984 | return new_name | ||
|
r10497 | |||
|
r12984 | def delete_notebook(self, notebook_name, notebook_path): | ||
"""Delete notebook by notebook_name.""" | ||||
nb_path = self.get_path(notebook_name, notebook_path) | ||||
|
r10518 | if not os.path.isfile(nb_path): | ||
|
r12984 | raise web.HTTPError(404, u'Notebook does not exist: %s' % notebook_name) | ||
|
r10497 | |||
# clear checkpoints | ||||
|
r12984 | for checkpoint in self.list_checkpoints(notebook_name): | ||
|
r10518 | checkpoint_id = checkpoint['checkpoint_id'] | ||
|
r12984 | path = self.get_checkpoint_path(notebook_name, checkpoint_id) | ||
|
r10518 | self.log.debug(path) | ||
|
r10497 | if os.path.isfile(path): | ||
|
r10518 | self.log.debug("unlinking checkpoint %s", path) | ||
|
r10497 | os.unlink(path) | ||
|
r10518 | |||
self.log.debug("unlinking notebook %s", nb_path) | ||||
os.unlink(nb_path) | ||||
|
r4484 | |||
|
r12984 | def increment_filename(self, basename, notebook_path=None): | ||
|
r5877 | """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. | ||||
""" | ||||
|
r4484 | i = 0 | ||
while True: | ||||
|
r12984 | name = u'%s%i.ipynb' % (basename,i) | ||
path = self.get_path_by_name(name, notebook_path) | ||||
|
r4484 | if not os.path.isfile(path): | ||
break | ||||
else: | ||||
i = i+1 | ||||
|
r8180 | return name | ||
|
r10497 | |||
# Checkpoint-related utilities | ||||
|
r12984 | def get_checkpoint_path_by_name(self, name, checkpoint_id, notebook_path=None): | ||
|
r10497 | """Return a full path to a notebook checkpoint, given its name and checkpoint id.""" | ||
|
r10777 | filename = u"{name}-{checkpoint_id}{ext}".format( | ||
|
r10497 | name=name, | ||
checkpoint_id=checkpoint_id, | ||||
ext=self.filename_ext, | ||||
) | ||||
|
r12984 | if notebook_path ==None: | ||
path = os.path.join(self.checkpoint_dir, filename) | ||||
else: | ||||
path = os.path.join(notebook_path, self.checkpoint_dir, filename) | ||||
|
r10497 | return path | ||
|
r12984 | def get_checkpoint_path(self, notebook_name, checkpoint_id, notebook_path=None): | ||
|
r10497 | """find the path to a checkpoint""" | ||
|
r12984 | name = notebook_name | ||
return self.get_checkpoint_path_by_name(name, checkpoint_id, notebook_path) | ||||
|
r10497 | |||
|
r12984 | def get_checkpoint_info(self, notebook_name, checkpoint_id, notebook_path=None): | ||
|
r10500 | """construct the info dict for a given checkpoint""" | ||
|
r12984 | path = self.get_checkpoint_path(notebook_name, checkpoint_id, notebook_path) | ||
|
r10500 | stats = os.stat(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 | ||
|
r12984 | def create_checkpoint(self, notebook_name, notebook_path=None): | ||
|
r10497 | """Create a checkpoint from the current state of a notebook""" | ||
|
r12984 | nb_path = self.get_path(notebook_name, notebook_path) | ||
|
r10500 | # only the one checkpoint ID: | ||
|
r10777 | checkpoint_id = u"checkpoint" | ||
|
r12984 | cp_path = self.get_checkpoint_path(notebook_name, checkpoint_id, notebook_path) | ||
self.log.debug("creating checkpoint for notebook %s", notebook_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 | ||||
|
r12984 | return self.get_checkpoint_info(notebook_name, checkpoint_id, notebook_path) | ||
|
r10497 | |||
|
r12984 | def list_checkpoints(self, notebook_name, notebook_path=None): | ||
|
r10497 | """list the checkpoints for a given notebook | ||
|
r10019 | |||
|
r10497 | This notebook manager currently only supports one checkpoint per notebook. | ||
""" | ||||
|
r12984 | checkpoint_id = "checkpoint" | ||
path = self.get_checkpoint_path(notebook_name, checkpoint_id, notebook_path) | ||||
|
r10500 | if not os.path.exists(path): | ||
|
r10497 | return [] | ||
|
r10500 | else: | ||
|
r12984 | return [self.get_checkpoint_info(notebook_name, checkpoint_id, notebook_path)] | ||
|
r10500 | |||
|
r10497 | |||
|
r12984 | def restore_checkpoint(self, notebook_name, checkpoint_id, notebook_path=None): | ||
|
r10497 | """restore a notebook to a checkpointed state""" | ||
|
r12984 | self.log.info("restoring Notebook %s from checkpoint %s", notebook_name, checkpoint_id) | ||
nb_path = self.get_path(notebook_name, notebook_path) | ||||
cp_path = self.get_checkpoint_path(notebook_name, checkpoint_id, notebook_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, | ||
|
r12984 | u'Notebook checkpoint does not exist: %s-%s' % (notebook_name, checkpoint_id) | ||
|
r10497 | ) | ||
# ensure notebook is readable (never restore from an unreadable notebook) | ||||
last_modified, nb = self.read_notebook_object_from_path(cp_path) | ||||
shutil.copy2(cp_path, nb_path) | ||||
self.log.debug("copying %s -> %s", cp_path, nb_path) | ||||
|
r12984 | def delete_checkpoint(self, notebook_name, checkpoint_id, notebook_path=None): | ||
|
r10497 | """delete a notebook's checkpoint""" | ||
|
r12984 | path = self.get_checkpoint_path(notebook_name, checkpoint_id, notebook_path) | ||
|
r10497 | if not os.path.isfile(path): | ||
raise web.HTTPError(404, | ||||
|
r12984 | u'Notebook checkpoint does not exist: %s-%s' % (notebook_name, checkpoint_id) | ||
|
r10497 | ) | ||
self.log.debug("unlinking %s", path) | ||||
os.unlink(path) | ||||
|
r10019 | def info_string(self): | ||
return "Serving notebooks from local directory: %s" % self.notebook_dir | ||||