nbmanager.py
223 lines
| 8.0 KiB
| text/x-python
|
PythonLexer
|
r8180 | """A base class notebook manager. | ||
Authors: | ||||
* Brian Granger | ||||
|
r13046 | * Zach Sailer | ||
|
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 | ||||
#----------------------------------------------------------------------------- | ||||
|
r8181 | import os | ||
|
r8180 | import uuid | ||
|
r13046 | from urllib import quote, unquote | ||
|
r8180 | |||
from tornado import web | ||||
|
r13046 | from IPython.html.utils import url_path_join | ||
|
r8180 | from IPython.config.configurable import LoggingConfigurable | ||
from IPython.nbformat import current | ||||
|
r8181 | from IPython.utils.traitlets import List, Dict, Unicode, TraitError | ||
|
r8180 | |||
#----------------------------------------------------------------------------- | ||||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
|
r8194 | class NotebookManager(LoggingConfigurable): | ||
|
r8180 | |||
|
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 | ||||
|
r10497 | # we are going to have to disentangle all of this. | ||
|
r8181 | notebook_dir = Unicode(os.getcwdu(), config=True, help=""" | ||
|
r12984 | The directory to use for notebooks. | ||
""") | ||||
|
r13026 | |||
|
r13046 | filename_ext = Unicode(u'.ipynb') | ||
|
r12984 | def named_notebook_path(self, notebook_path): | ||
|
r13046 | """Given notebook_path (*always* a URL path to notebook), returns a | ||
(name, path) tuple, where name is a .ipynb file, and path is the | ||||
URL path that describes the file system path for the file. | ||||
It *always* starts *and* ends with a '/' character. | ||||
|
r13026 | |||
Parameters | ||||
---------- | ||||
notebook_path : string | ||||
A path that may be a .ipynb name or a directory | ||||
Returns | ||||
------- | ||||
name : string or None | ||||
|
r13027 | the filename of the notebook, or None if not a .ipynb extension | ||
path : string | ||||
|
r13026 | the path to the directory which contains the notebook | ||
""" | ||||
|
r12984 | names = notebook_path.split('/') | ||
|
r13028 | names = [n for n in names if n != ''] # remove duplicate splits | ||
|
r13027 | |||
|
r13028 | names = [''] + names | ||
if names and names[-1].endswith(".ipynb"): | ||||
name = names[-1] | ||||
|
r13027 | path = "/".join(names[:-1]) + '/' | ||
|
r12984 | else: | ||
|
r13027 | name = None | ||
path = "/".join(names) + '/' | ||||
|
r12984 | return name, path | ||
|
r13041 | |||
def get_os_path(self, fname=None, path='/'): | ||||
|
r13046 | """Given a notebook name and a URL path, return its file system | ||
|
r13041 | path. | ||
Parameters | ||||
---------- | ||||
fname : string | ||||
The name of a notebook file with the .ipynb extension | ||||
path : string | ||||
The relative URL path (with '/' as separator) to the named | ||||
notebook. | ||||
Returns | ||||
------- | ||||
path : string | ||||
A file system path that combines notebook_dir (location where | ||||
server started), the relative path, and the filename with the | ||||
current operating system's url. | ||||
""" | ||||
parts = path.split('/') | ||||
parts = [p for p in parts if p != ''] # remove duplicate splits | ||||
if fname is not None: | ||||
parts += [fname] | ||||
path = os.path.join(self.notebook_dir, *parts) | ||||
return path | ||||
|
r13027 | |||
|
r13012 | def url_encode(self, path): | ||
|
r13046 | """Takes a URL path with special characters and returns | ||
the path with all these characters URL encoded""" | ||||
parts = path.split('/') | ||||
return '/'.join([quote(p) for p in parts]) | ||||
|
r13012 | |||
def url_decode(self, path): | ||||
|
r13046 | """Takes a URL path with encoded special characters and | ||
returns the URL with special characters decoded""" | ||||
parts = path.split('/') | ||||
return '/'.join([unquote(p) for p in parts]) | ||||
|
r13012 | |||
|
r13046 | def _notebook_dir_changed(self, name, old, new): | ||
"""Do a bit of validation of the notebook dir.""" | ||||
|
r8453 | if not os.path.isabs(new): | ||
# If we receive a non-absolute path, make it absolute. | ||||
|
r13066 | self.notebook_dir = os.path.abspath(new) | ||
|
r8453 | return | ||
|
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) | ||||
|
r8180 | |||
|
r13046 | # Main notebook API | ||
|
r8180 | |||
|
r13046 | def increment_filename(self, basename, path='/'): | ||
"""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 | ||||
|
r8180 | """ | ||
|
r13046 | return basename | ||
|
r8180 | |||
def list_notebooks(self): | ||||
|
r13046 | """Return a list of notebook dicts without content. | ||
|
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') | ||||
|
r13046 | def get_notebook_model(self, name, path='/', content=True): | ||
"""Get the notebook model with or without content.""" | ||||
|
r8180 | raise NotImplementedError('must be implemented in a subclass') | ||
|
r13046 | def save_notebook_model(self, model, name, path='/'): | ||
"""Save the notebook model and return the model with no content.""" | ||||
raise NotImplementedError('must be implemented in a subclass') | ||||
|
r8180 | |||
|
r13046 | def update_notebook_model(self, model, name, path='/'): | ||
"""Update the notebook model and return the model with no content.""" | ||||
|
r8180 | raise NotImplementedError('must be implemented in a subclass') | ||
|
r13046 | def delete_notebook_model(self, name, path): | ||
"""Delete notebook by name and path.""" | ||||
|
r8180 | raise NotImplementedError('must be implemented in a subclass') | ||
|
r13046 | def create_notebook_model(self, model=None, path='/'): | ||
"""Create a new untitled notebook and return its model with no content.""" | ||||
name = self.increment_filename('Untitled', path) | ||||
if model is None: | ||||
model = {} | ||||
metadata = current.new_metadata(name=u'') | ||||
nb = current.new_notebook(metadata=metadata) | ||||
model['content'] = nb | ||||
model['name'] = name | ||||
model['path'] = path | ||||
model = self.save_notebook_model(model, name, path) | ||||
return model | ||||
|
r8180 | |||
|
r13046 | def copy_notebook(self, name, path='/', content=False): | ||
"""Copy an existing notebook and return its new model.""" | ||||
model = self.get_notebook_model(name, path) | ||||
name = os.path.splitext(name)[0] + '-Copy' | ||||
name = self.increment_filename(name, path) + self.filename_ext | ||||
model['name'] = name | ||||
model = self.save_notebook_model(model, name, path, content=content) | ||||
return model | ||||
|
r10497 | |||
# Checkpoint-related | ||||
|
r13046 | def create_checkpoint(self, name, path='/'): | ||
|
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") | ||||
|
r13046 | def list_checkpoints(self, name, path='/'): | ||
|
r10497 | """Return a list of checkpoints for a given notebook""" | ||
return [] | ||||
|
r13046 | def restore_checkpoint(self, checkpoint_id, name, path='/'): | ||
|
r10497 | """Restore a notebook from one of its checkpoints""" | ||
raise NotImplementedError("must be implemented in a subclass") | ||||
|
r8181 | |||
|
r13046 | def delete_checkpoint(self, checkpoint_id, name, path='/'): | ||
|
r10497 | """delete a checkpoint for a notebook""" | ||
raise NotImplementedError("must be implemented in a subclass") | ||||
|
r8181 | def log_info(self): | ||
|
r10019 | self.log.info(self.info_string()) | ||
def info_string(self): | ||||
|
r13051 | return "Serving notebooks" | ||