##// END OF EJS Templates
refactor of get_output_cell
refactor of get_output_cell

File last commit:

r13123:0bdca148
r13299:b4062264
Show More
nbmanager.py
173 lines | 6.2 KiB | text/x-python | PythonLexer
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180 """A base class notebook manager.
Authors:
* Brian Granger
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 * Zach Sailer
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180 """
#-----------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181 import os
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
from IPython.config.configurable import LoggingConfigurable
from IPython.nbformat import current
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181 from IPython.utils.traitlets import List, Dict, Unicode, TraitError
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Brian Granger
Renaming BaseNotebookManager->NotebookManager to preserve config.
r8194 class NotebookManager(LoggingConfigurable):
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181 # Todo:
# The notebook_dir attribute is used to mean a couple of different things:
# 1. Where the notebooks are stored if FileNotebookManager is used.
# 2. The cwd of the kernel for a project.
# Right now we use this attribute in a number of different places and
MinRK
add checkpoint API to FileNBManager
r10497 # we are going to have to disentangle all of this.
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181 notebook_dir = Unicode(os.getcwdu(), config=True, help="""
Zachary Sailer
manual rebase notebooks web services
r12984 The directory to use for notebooks.
""")
Paul Ivanov
cleaning up named_notebook_path
r13026
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 filename_ext = Unicode(u'.ipynb')
MinRK
adjust definition of 'path' in notebooks...
r13067
def path_exists(self, path):
"""Does the API-style path (directory) actually exist?
MinRK
move os_path to FileNBMan...
r13070 Override this method in subclasses.
MinRK
adjust definition of 'path' in notebooks...
r13067
Paul Ivanov
cleaning up named_notebook_path
r13026 Parameters
----------
MinRK
adjust definition of 'path' in notebooks...
r13067 path : string
The
Paul Ivanov
cleaning up named_notebook_path
r13026 Returns
-------
MinRK
adjust definition of 'path' in notebooks...
r13067 exists : bool
Whether the path does indeed exist.
Paul Ivanov
cleaning up named_notebook_path
r13026 """
MinRK
move os_path to FileNBMan...
r13070 raise NotImplementedError
MinRK
adjust definition of 'path' in notebooks...
r13067
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 def _notebook_dir_changed(self, name, old, new):
"""Do a bit of validation of the notebook dir."""
Ohad Ravid
Answer Issue #2366...
r8453 if not os.path.isabs(new):
# If we receive a non-absolute path, make it absolute.
MinRK
fix `--notebook-dir` configurable when there is no trailing slash
r13066 self.notebook_dir = os.path.abspath(new)
Ohad Ravid
Answer Issue #2366...
r8453 return
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181 if os.path.exists(new) and not os.path.isdir(new):
raise TraitError("notebook dir %r is not a directory" % new)
if not os.path.exists(new):
self.log.info("Creating notebook dir %s", new)
try:
os.mkdir(new)
except:
raise TraitError("Couldn't create notebook dir %r" % new)
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 # Main notebook API
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
MinRK
adjust definition of 'path' in notebooks...
r13067 def increment_filename(self, basename, path=''):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Increment a notebook filename without the .ipynb to make it unique.
Parameters
----------
basename : unicode
The name of a notebook without the ``.ipynb`` file extension.
path : unicode
The URL path of the notebooks directory
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180 """
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return basename
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
MinRK
update upload and copy...
r13074 def list_notebooks(self, path=''):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Return a list of notebook dicts without content.
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
This returns a list of dicts, each of the form::
dict(notebook_id=notebook,name=name)
This list of dicts should be sorted by name::
data = sorted(data, key=lambda item: item['name'])
"""
raise NotImplementedError('must be implemented in a subclass')
MinRK
adjust definition of 'path' in notebooks...
r13067 def get_notebook_model(self, name, path='', content=True):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Get the notebook model with or without content."""
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180 raise NotImplementedError('must be implemented in a subclass')
MinRK
adjust definition of 'path' in notebooks...
r13067 def save_notebook_model(self, model, name, path=''):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Save the notebook model and return the model with no content."""
raise NotImplementedError('must be implemented in a subclass')
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
MinRK
adjust definition of 'path' in notebooks...
r13067 def update_notebook_model(self, model, name, path=''):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Update the notebook model and return the model with no content."""
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180 raise NotImplementedError('must be implemented in a subclass')
MinRK
update upload and copy...
r13074 def delete_notebook_model(self, name, path=''):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Delete notebook by name and path."""
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180 raise NotImplementedError('must be implemented in a subclass')
MinRK
adjust definition of 'path' in notebooks...
r13067 def create_notebook_model(self, model=None, path=''):
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 """Create a new untitled notebook and return its model with no content."""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 if model is None:
model = {}
MinRK
update upload and copy...
r13074 if 'content' not in model:
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 metadata = current.new_metadata(name=u'')
MinRK
update upload and copy...
r13074 model['content'] = current.new_notebook(metadata=metadata)
if 'name' not in model:
model['name'] = self.increment_filename('Untitled', path)
model['path'] = path
model = self.save_notebook_model(model, model['name'], model['path'])
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return model
Brian Granger
Refactoring notebook managers and adding Azure backed storage....
r8180
MinRK
allow specifying destination in copy_notebook
r13123 def copy_notebook(self, from_name, to_name=None, path=''):
"""Copy an existing notebook and return its new model.
If to_name not specified, increment `from_name-Copy#.ipynb`.
"""
MinRK
ensure 'path' never has leading or trailing slash in nbmanager...
r13078 path = path.strip('/')
MinRK
allow specifying destination in copy_notebook
r13123 model = self.get_notebook_model(from_name, path)
if not to_name:
base = os.path.splitext(from_name)[0] + '-Copy'
to_name = self.increment_filename(base, path)
model['name'] = to_name
model = self.save_notebook_model(model, to_name, path)
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 return model
MinRK
add checkpoint API to FileNBManager
r10497
# Checkpoint-related
MinRK
update upload and copy...
r13074 def create_checkpoint(self, name, path=''):
MinRK
add checkpoint API to FileNBManager
r10497 """Create a checkpoint of the current state of a notebook
Returns a checkpoint_id for the new checkpoint.
"""
raise NotImplementedError("must be implemented in a subclass")
MinRK
update upload and copy...
r13074 def list_checkpoints(self, name, path=''):
MinRK
add checkpoint API to FileNBManager
r10497 """Return a list of checkpoints for a given notebook"""
return []
MinRK
update upload and copy...
r13074 def restore_checkpoint(self, checkpoint_id, name, path=''):
MinRK
add checkpoint API to FileNBManager
r10497 """Restore a notebook from one of its checkpoints"""
raise NotImplementedError("must be implemented in a subclass")
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181
MinRK
update upload and copy...
r13074 def delete_checkpoint(self, checkpoint_id, name, path=''):
MinRK
add checkpoint API to FileNBManager
r10497 """delete a checkpoint for a notebook"""
raise NotImplementedError("must be implemented in a subclass")
Brian Granger
Fixing minor things for the Azure backed nb storage.
r8181 def log_info(self):
Paul Ivanov
print info string on interrupt, log it on startup
r10019 self.log.info(self.info_string())
def info_string(self):
Brian E. Granger
Review and refactoring of notebooks web service.
r13051 return "Serving notebooks"