handlers.py
265 lines
| 8.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 | ||
MinRK
|
r18749 | from IPython.html.base.handlers import ( | ||
IPythonHandler, json_errors, path_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
|
r18749 | def location_url(self, path): | ||
MinRK
|
r17524 | """Return the full URL location of a file. | ||
MinRK
|
r17523 | |||
Zachary Sailer
|
r13045 | Parameters | ||
---------- | ||||
path : unicode | ||||
MinRK
|
r18749 | The API path of the file, such as "foo/bar.txt". | ||
Zachary Sailer
|
r13045 | """ | ||
MinRK
|
r13132 | return url_escape(url_path_join( | ||
MinRK
|
r18749 | self.base_url, 'api', 'contents', path | ||
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
|
r18749 | location = self.location_url(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
|
r18749 | def get(self, path=''): | ||
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 '' | ||
Thomas Kluyver
|
r18781 | type_ = self.get_query_argument('type', default=None) | ||
if type_ not in {None, 'directory', 'file', 'notebook'}: | ||||
raise web.HTTPError(400, u'Type %r is invalid' % type_) | ||||
Thomas Kluyver
|
r18788 | format = self.get_query_argument('format', default=None)# | ||
if format not in {None, 'text', 'base64'}: | ||||
raise web.HTTPError(400, u'Format %r is invalid' % format) | ||||
Thomas Kluyver
|
r18791 | model = self.contents_manager.get(path=path, type_=type_, format=format) | ||
MinRK
|
r17525 | 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 | ||
MinRK
|
r18749 | def patch(self, path=''): | ||
"""PATCH renames a file or directory without re-uploading content.""" | ||||
MinRK
|
r17524 | cm = self.contents_manager | ||
Zachary Sailer
|
r13045 | model = self.get_json_body() | ||
if model is None: | ||||
raise web.HTTPError(400, u'JSON body missing') | ||||
MinRK
|
r18749 | model = cm.update(model, path) | ||
MinRK
|
r13129 | self._finish_model(model) | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def _copy(self, copy_from, copy_to=None): | ||
Min RK
|
r18758 | """Copy a file, optionally specifying a target directory.""" | ||
MinRK
|
r18749 | self.log.info(u"Copying {copy_from} to {copy_to}".format( | ||
MinRK
|
r17535 | copy_from=copy_from, | ||
copy_to=copy_to or '', | ||||
)) | ||||
MinRK
|
r18749 | model = self.contents_manager.copy(copy_from, copy_to) | ||
MinRK
|
r13129 | self.set_status(201) | ||
self._finish_model(model) | ||||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def _upload(self, model, path): | ||
"""Handle upload of a new file to path""" | ||||
self.log.info(u"Uploading file to %s", path) | ||||
Min RK
|
r18753 | model = self.contents_manager.new(model, path) | ||
MinRK
|
r13129 | self.set_status(201) | ||
self._finish_model(model) | ||||
Min RK
|
r18758 | |||
Min RK
|
r18759 | def _new_untitled(self, path, type='', ext=''): | ||
"""Create a new, empty untitled entity""" | ||||
Min RK
|
r18758 | self.log.info(u"Creating new %s in %s", type or 'file', path) | ||
Min RK
|
r18759 | model = self.contents_manager.new_untitled(path=path, type=type, ext=ext) | ||
MinRK
|
r13129 | self.set_status(201) | ||
self._finish_model(model) | ||||
MinRK
|
r17523 | |||
MinRK
|
r18749 | def _save(self, model, path): | ||
MinRK
|
r17524 | """Save an existing file.""" | ||
MinRK
|
r18749 | self.log.info(u"Saving file at %s", path) | ||
model = self.contents_manager.save(model, path) | ||||
self._finish_model(model) | ||||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r18749 | def post(self, path=''): | ||
Min RK
|
r18758 | """Create a new file in the specified path. | ||
MinRK
|
r17523 | |||
Min RK
|
r18758 | POST creates new files. The server always decides on the name. | ||
MinRK
|
r17523 | |||
MinRK
|
r17524 | POST /api/contents/path | ||
MinRK
|
r18749 | New untitled, empty file or directory. | ||
MinRK
|
r17527 | POST /api/contents/path | ||
MinRK
|
r18749 | with body {"copy_from" : "/path/to/OtherNotebook.ipynb"} | ||
Thomas Kluyver
|
r13597 | New copy of OtherNotebook in path | ||
MinRK
|
r13074 | """ | ||
MinRK
|
r17523 | |||
MinRK
|
r17525 | cm = self.contents_manager | ||
if cm.file_exists(path): | ||||
Min RK
|
r18758 | raise web.HTTPError(400, "Cannot POST to files, use PUT instead.") | ||
MinRK
|
r17523 | |||
MinRK
|
r18749 | if not cm.dir_exists(path): | ||
MinRK
|
r17525 | 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') | ||||
Min RK
|
r18758 | ext = model.get('ext', '') | ||
Min RK
|
r18759 | type = model.get('type', '') | ||
MinRK
|
r18749 | if copy_from: | ||
MinRK
|
r17524 | self._copy(copy_from, path) | ||
MinRK
|
r13137 | else: | ||
Min RK
|
r18759 | self._new_untitled(path, type=type, ext=ext) | ||
MinRK
|
r13129 | else: | ||
Min RK
|
r18759 | self._new_untitled(path) | ||
Zachary Sailer
|
r12997 | |||
@web.authenticated | ||||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r18749 | def put(self, path=''): | ||
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
|
r13129 | """ | ||
Zachary Sailer
|
r13045 | model = self.get_json_body() | ||
MinRK
|
r13137 | if model: | ||
Min RK
|
r18750 | if model.get('copy_from'): | ||
raise web.HTTPError(400, "Cannot copy with PUT, only POST") | ||||
if self.contents_manager.file_exists(path): | ||||
MinRK
|
r18749 | self._save(model, path) | ||
MinRK
|
r13129 | else: | ||
MinRK
|
r18749 | self._upload(model, path) | ||
MinRK
|
r13129 | else: | ||
Min RK
|
r18759 | self._new_untitled(path) | ||
Brian E. Granger
|
r10641 | |||
@web.authenticated | ||||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r18749 | def delete(self, path=''): | ||
MinRK
|
r17524 | """delete a file in the given path""" | ||
cm = self.contents_manager | ||||
MinRK
|
r18749 | self.log.warn('delete %s', path) | ||
cm.delete(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
|
r18749 | def get(self, path=''): | ||
MinRK
|
r17524 | """get lists checkpoints for a file""" | ||
cm = self.contents_manager | ||||
MinRK
|
r18749 | checkpoints = cm.list_checkpoints(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
|
r18749 | def post(self, path=''): | ||
Paul Ivanov
|
r13042 | """post creates a new checkpoint""" | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
MinRK
|
r18749 | checkpoint = cm.create_checkpoint(path) | ||
Zachary Sailer
|
r13045 | data = json.dumps(checkpoint, default=date_default) | ||
MinRK
|
r17524 | location = url_path_join(self.base_url, 'api/contents', | ||
MinRK
|
r18749 | path, '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
|
r18749 | def post(self, path, checkpoint_id): | ||
MinRK
|
r17524 | """post restores a file from a checkpoint""" | ||
cm = self.contents_manager | ||||
MinRK
|
r18749 | cm.restore_checkpoint(checkpoint_id, path) | ||
Paul Ivanov
|
r13042 | self.set_status(204) | ||
self.finish() | ||||
MinRK
|
r17523 | |||
Paul Ivanov
|
r13042 | @web.authenticated | ||
Zachary Sailer
|
r13045 | @json_errors | ||
MinRK
|
r18749 | def delete(self, path, checkpoint_id): | ||
MinRK
|
r17524 | """delete clears a checkpoint for a given file""" | ||
cm = self.contents_manager | ||||
MinRK
|
r18749 | cm.delete_checkpoint(checkpoint_id, path) | ||
Paul Ivanov
|
r13042 | self.set_status(204) | ||
self.finish() | ||||
MinRK
|
r17523 | |||
MinRK
|
r18262 | |||
class NotebooksRedirectHandler(IPythonHandler): | ||||
"""Redirect /api/notebooks to /api/contents""" | ||||
SUPPORTED_METHODS = ('GET', 'PUT', 'PATCH', 'POST', 'DELETE') | ||||
def get(self, path): | ||||
self.log.warn("/api/notebooks is deprecated, use /api/contents") | ||||
self.redirect(url_path_join( | ||||
self.base_url, | ||||
'api/contents', | ||||
path | ||||
)) | ||||
put = patch = post = delete = get | ||||
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
|
r18749 | (r"/api/contents%s/checkpoints" % path_regex, CheckpointsHandler), | ||
(r"/api/contents%s/checkpoints/%s" % (path_regex, _checkpoint_id_regex), | ||||
MinRK
|
r17524 | ModifyCheckpointsHandler), | ||
(r"/api/contents%s" % path_regex, ContentsHandler), | ||||
MinRK
|
r18262 | (r"/api/notebooks/?(.*)", NotebooksRedirectHandler), | ||
Brian E. Granger
|
r10647 | ] | ||