handlers.py
286 lines
| 9.6 KiB
| text/x-python
|
PythonLexer
MinRK
|
r17524 | """Tornado handlers for the contents web service.""" | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r17524 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r10641 | |||
Zachary Sailer
|
r13045 | import json | ||
Brian E. Granger
|
r10641 | |||
Zachary Sailer
|
r13045 | from tornado import web | ||
Brian E. Granger
|
r10641 | |||
MinRK
|
r13132 | from IPython.html.utils import url_path_join, url_escape | ||
Brian E. Granger
|
r10641 | from IPython.utils.jsonutil import date_default | ||
Thomas Kluyver
|
r13916 | from IPython.html.base.handlers import (IPythonHandler, json_errors, | ||
MinRK
|
r17525 | file_path_regex, path_regex, | ||
file_name_regex) | ||||
Brian E. Granger
|
r10641 | |||
Zachary Sailer
|
r12984 | |||
MinRK
|
r17529 | def sort_key(model): | ||
"""key function for case-insensitive sort by name and type""" | ||||
iname = model['name'].lower() | ||||
type_key = { | ||||
'directory' : '0', | ||||
'notebook' : '1', | ||||
'file' : '2', | ||||
}.get(model['type'], '9') | ||||
return u'%s%s' % (type_key, iname) | ||||
MinRK
|
r17524 | class ContentsHandler(IPythonHandler): | ||
Paul Ivanov
|
r13042 | |||
Zachary Sailer
|
r13045 | SUPPORTED_METHODS = (u'GET', u'PUT', u'PATCH', u'POST', u'DELETE') | ||
MinRK
|
r17525 | def location_url(self, name, path): | ||
MinRK
|
r17524 | """Return the full URL location of a file. | ||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13045 | Parameters | ||
---------- | ||||
name : unicode | ||||
MinRK
|
r17524 | The base name of the file, such as "foo.ipynb". | ||
Zachary Sailer
|
r13045 | path : unicode | ||
MinRK
|
r17524 | The API path of the file, such as "foo/bar". | ||
Zachary Sailer
|
r13045 | """ | ||
MinRK
|
r13132 | return url_escape(url_path_join( | ||
MinRK
|
r17524 | self.base_url, 'api', 'contents', path, name | ||
MinRK
|
r13132 | )) | ||
Paul Ivanov
|
r13042 | |||
MinRK
|
r13129 | def _finish_model(self, model, location=True): | ||
"""Finish a JSON request with a model, setting relevant headers, etc.""" | ||||
if location: | ||||
MinRK
|
r17524 | location = self.location_url(model['name'], model['path']) | ||
MinRK
|
r13129 | self.set_header('Location', location) | ||
self.set_header('Last-Modified', model['last_modified']) | ||||
self.finish(json.dumps(model, default=date_default)) | ||||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def get(self, path='', name=None): | ||
MinRK
|
r17535 | """Return a model for a file or directory. | ||
Brian E. Granger
|
r13113 | |||
MinRK
|
r17535 | A directory model contains a list of models (without content) | ||
of the files and directories it contains. | ||||
MinRK
|
r13067 | """ | ||
MinRK
|
r17525 | path = path or '' | ||
model = self.contents_manager.get_model(name=name, path=path) | ||||
if model['type'] == 'directory': | ||||
MinRK
|
r17529 | # group listing by type, then by name (case-insensitive) | ||
MinRK
|
r17535 | # FIXME: sorting should be done in the frontends | ||
MinRK
|
r17529 | model['content'].sort(key=sort_key) | ||
MinRK
|
r13129 | self._finish_model(model, location=False) | ||
Brian E. Granger
|
r10641 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
MinRK
|
r13067 | @json_errors | ||
def patch(self, path='', name=None): | ||||
MinRK
|
r13074 | """PATCH renames a notebook without re-uploading content.""" | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
Zachary Sailer
|
r13045 | if name is None: | ||
MinRK
|
r17524 | raise web.HTTPError(400, u'Filename missing') | ||
Zachary Sailer
|
r13045 | model = self.get_json_body() | ||
if model is None: | ||||
raise web.HTTPError(400, u'JSON body missing') | ||||
MinRK
|
r17524 | model = cm.update(model, name, path) | ||
MinRK
|
r13129 | self._finish_model(model) | ||
MinRK
|
r17523 | |||
MinRK
|
r17524 | def _copy(self, copy_from, path, copy_to=None): | ||
MinRK
|
r17535 | """Copy a file, optionally specifying the new name. | ||
MinRK
|
r13129 | """ | ||
MinRK
|
r17535 | self.log.info(u"Copying {copy_from} to {path}/{copy_to}".format( | ||
copy_from=copy_from, | ||||
path=path, | ||||
copy_to=copy_to or '', | ||||
)) | ||||
MinRK
|
r17524 | model = self.contents_manager.copy(copy_from, copy_to, path) | ||
MinRK
|
r13129 | self.set_status(201) | ||
self._finish_model(model) | ||||
MinRK
|
r17523 | |||
MinRK
|
r17524 | def _upload(self, model, path, name=None): | ||
MinRK
|
r17535 | """Handle upload of a new file | ||
MinRK
|
r17523 | |||
MinRK
|
r17535 | If name specified, create it in path/name, | ||
otherwise create a new untitled file in path. | ||||
MinRK
|
r13129 | """ | ||
MinRK
|
r17524 | self.log.info(u"Uploading file to %s/%s", path, name or '') | ||
MinRK
|
r13129 | if name: | ||
model['name'] = name | ||||
MinRK
|
r17523 | |||
MinRK
|
r17527 | model = self.contents_manager.create_file(model, path) | ||
MinRK
|
r13129 | self.set_status(201) | ||
self._finish_model(model) | ||||
MinRK
|
r17523 | |||
MinRK
|
r17527 | def _create_empty_file(self, path, name=None, ext='.ipynb'): | ||
"""Create an empty file in path | ||||
MinRK
|
r17523 | |||
MinRK
|
r13129 | If name specified, create it in path/name. | ||
""" | ||||
MinRK
|
r17527 | self.log.info(u"Creating new file in %s/%s", path, name or '') | ||
MinRK
|
r13129 | model = {} | ||
if name: | ||||
model['name'] = name | ||||
MinRK
|
r17527 | model = self.contents_manager.create_file(model, path=path, ext=ext) | ||
MinRK
|
r13129 | self.set_status(201) | ||
self._finish_model(model) | ||||
MinRK
|
r17523 | |||
MinRK
|
r17524 | def _save(self, model, path, name): | ||
"""Save an existing file.""" | ||||
self.log.info(u"Saving file at %s/%s", path, name) | ||||
model = self.contents_manager.save(model, name, path) | ||||
MinRK
|
r13129 | if model['path'] != path.strip('/') or model['name'] != name: | ||
# a rename happened, set Location header | ||||
location = True | ||||
else: | ||||
location = False | ||||
self._finish_model(model, location) | ||||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def post(self, path='', name=None): | ||
MinRK
|
r17529 | """Create a new file or directory in the specified path. | ||
MinRK
|
r17523 | |||
MinRK
|
r17529 | POST creates new files or directories. The server always decides on the name. | ||
MinRK
|
r17523 | |||
MinRK
|
r17524 | POST /api/contents/path | ||
Thomas Kluyver
|
r13597 | New untitled notebook in path. If content specified, upload a | ||
notebook, otherwise start empty. | ||||
MinRK
|
r17527 | POST /api/contents/path | ||
with body {"copy_from" : "OtherNotebook.ipynb"} | ||||
Thomas Kluyver
|
r13597 | New copy of OtherNotebook in path | ||
MinRK
|
r13074 | """ | ||
MinRK
|
r17523 | |||
MinRK
|
r13129 | if name is not None: | ||
MinRK
|
r17525 | path = u'{}/{}'.format(path, name) | ||
cm = self.contents_manager | ||||
if cm.file_exists(path): | ||||
MinRK
|
r17535 | raise web.HTTPError(400, "Cannot POST to existing files, use PUT instead.") | ||
MinRK
|
r17523 | |||
MinRK
|
r17525 | if not cm.path_exists(path): | ||
raise web.HTTPError(404, "No such directory: %s" % path) | ||||
MinRK
|
r13137 | model = self.get_json_body() | ||
MinRK
|
r17523 | |||
MinRK
|
r13137 | if model is not None: | ||
copy_from = model.get('copy_from') | ||||
MinRK
|
r17527 | ext = model.get('ext', '.ipynb') | ||
if model.get('content') is not None: | ||||
if copy_from: | ||||
MinRK
|
r13137 | raise web.HTTPError(400, "Can't upload and copy at the same time.") | ||
MinRK
|
r17527 | self._upload(model, path) | ||
elif copy_from: | ||||
MinRK
|
r17524 | self._copy(copy_from, path) | ||
MinRK
|
r13137 | else: | ||
MinRK
|
r17527 | self._create_empty_file(path, ext=ext) | ||
MinRK
|
r13129 | else: | ||
MinRK
|
r17527 | self._create_empty_file(path) | ||
Zachary Sailer
|
r12997 | |||
@web.authenticated | ||||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def put(self, path='', name=None): | ||
MinRK
|
r17524 | """Saves the file in the location specified by name and path. | ||
MinRK
|
r17523 | |||
Thomas Kluyver
|
r13597 | PUT is very similar to POST, but the requester specifies the name, | ||
whereas with POST, the server picks the name. | ||||
MinRK
|
r17523 | |||
MinRK
|
r17524 | PUT /api/contents/path/Name.ipynb | ||
Thomas Kluyver
|
r13597 | Save notebook at ``path/Name.ipynb``. Notebook structure is specified | ||
in `content` key of JSON request body. If content is not specified, | ||||
create a new empty notebook. | ||||
MinRK
|
r17535 | PUT /api/contents/path/Name.ipynb | ||
with JSON body:: | ||||
{ | ||||
"copy_from" : "[path/to/]OtherNotebook.ipynb" | ||||
} | ||||
Thomas Kluyver
|
r13597 | Copy OtherNotebook to Name | ||
MinRK
|
r13129 | """ | ||
if name is None: | ||||
MinRK
|
r17535 | raise web.HTTPError(400, "name must be specified with PUT.") | ||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13045 | model = self.get_json_body() | ||
MinRK
|
r13137 | if model: | ||
copy_from = model.get('copy_from') | ||||
if copy_from: | ||||
if model.get('content'): | ||||
raise web.HTTPError(400, "Can't upload and copy at the same time.") | ||||
MinRK
|
r17524 | self._copy(copy_from, path, name) | ||
elif self.contents_manager.file_exists(name, path): | ||||
self._save(model, path, name) | ||||
MinRK
|
r13129 | else: | ||
MinRK
|
r17524 | self._upload(model, path, name) | ||
MinRK
|
r13129 | else: | ||
MinRK
|
r17527 | self._create_empty_file(path, name) | ||
Brian E. Granger
|
r10641 | |||
@web.authenticated | ||||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def delete(self, path='', name=None): | ||
MinRK
|
r17524 | """delete a file in the given path""" | ||
cm = self.contents_manager | ||||
MinRK
|
r17525 | self.log.warn('delete %s:%s', path, name) | ||
MinRK
|
r17524 | cm.delete(name, path) | ||
Brian E. Granger
|
r10641 | self.set_status(204) | ||
self.finish() | ||||
Paul Ivanov
|
r13042 | |||
MinRK
|
r17524 | class CheckpointsHandler(IPythonHandler): | ||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | SUPPORTED_METHODS = ('GET', 'POST') | ||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13015 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def get(self, path='', name=None): | ||
MinRK
|
r17524 | """get lists checkpoints for a file""" | ||
cm = self.contents_manager | ||||
checkpoints = cm.list_checkpoints(name, path) | ||||
Zachary Sailer
|
r13045 | data = json.dumps(checkpoints, default=date_default) | ||
Paul Ivanov
|
r13042 | self.finish(data) | ||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13015 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def post(self, path='', name=None): | ||
Paul Ivanov
|
r13042 | """post creates a new checkpoint""" | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
checkpoint = cm.create_checkpoint(name, path) | ||||
Zachary Sailer
|
r13045 | data = json.dumps(checkpoint, default=date_default) | ||
MinRK
|
r17524 | location = url_path_join(self.base_url, 'api/contents', | ||
MinRK
|
r13122 | path, name, 'checkpoints', checkpoint['id']) | ||
MinRK
|
r13132 | self.set_header('Location', url_escape(location)) | ||
Thomas Kluyver
|
r13110 | self.set_status(201) | ||
Paul Ivanov
|
r13042 | self.finish(data) | ||
MinRK
|
r17524 | class ModifyCheckpointsHandler(IPythonHandler): | ||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | SUPPORTED_METHODS = ('POST', 'DELETE') | ||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def post(self, path, name, checkpoint_id): | ||
MinRK
|
r17524 | """post restores a file from a checkpoint""" | ||
cm = self.contents_manager | ||||
cm.restore_checkpoint(checkpoint_id, name, path) | ||||
Paul Ivanov
|
r13042 | self.set_status(204) | ||
self.finish() | ||||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r13067 | def delete(self, path, name, checkpoint_id): | ||
MinRK
|
r17524 | """delete clears a checkpoint for a given file""" | ||
cm = self.contents_manager | ||||
cm.delete_checkpoint(checkpoint_id, name, path) | ||||
Paul Ivanov
|
r13042 | self.set_status(204) | ||
self.finish() | ||||
MinRK
|
r17523 | |||
Brian E. Granger
|
r10647 | #----------------------------------------------------------------------------- | ||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
Paul Ivanov
|
r13042 | |||
_checkpoint_id_regex = r"(?P<checkpoint_id>[\w-]+)" | ||||
Brian E. Granger
|
r10647 | |||
default_handlers = [ | ||||
MinRK
|
r17525 | (r"/api/contents%s/checkpoints" % file_path_regex, CheckpointsHandler), | ||
(r"/api/contents%s/checkpoints/%s" % (file_path_regex, _checkpoint_id_regex), | ||||
MinRK
|
r17524 | ModifyCheckpointsHandler), | ||
MinRK
|
r17525 | (r"/api/contents%s" % file_path_regex, ContentsHandler), | ||
MinRK
|
r17524 | (r"/api/contents%s" % path_regex, ContentsHandler), | ||
Brian E. Granger
|
r10647 | ] | ||