handlers.py
118 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
MinRK
|
r17524 | """Tornado handlers for the sessions web service.""" | ||
Zachary Sailer
|
r12985 | |||
MinRK
|
r17524 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Zachary Sailer
|
r12985 | |||
Zachary Sailer
|
r13057 | import json | ||
Zachary Sailer
|
r12985 | |||
Zachary Sailer
|
r13057 | from tornado import web | ||
Brian E. Granger
|
r13116 | |||
Zachary Sailer
|
r13057 | from ...base.handlers import IPythonHandler, json_errors | ||
Brian E. Granger
|
r13116 | from IPython.utils.jsonutil import date_default | ||
MinRK
|
r13132 | from IPython.html.utils import url_path_join, url_escape | ||
Jessica B. Hamrick
|
r18059 | from IPython.kernel.kernelspec import NoSuchKernel | ||
Zachary Sailer
|
r12985 | |||
class SessionRootHandler(IPythonHandler): | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r13014 | @web.authenticated | ||
Zachary Sailer
|
r13057 | @json_errors | ||
Zachary Sailer
|
r12985 | def get(self): | ||
Zachary Sailer
|
r13036 | # Return a list of running sessions | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
sessions = sm.list_sessions() | ||||
Zachary Sailer
|
r13057 | self.finish(json.dumps(sessions, default=date_default)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @web.authenticated | ||
Zachary Sailer
|
r13057 | @json_errors | ||
Zachary Sailer
|
r12985 | def post(self): | ||
Dale Jung
|
r16052 | # Creates a new session | ||
Zachary Sailer
|
r13036 | #(unless a session already exists for the named nb) | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
MinRK
|
r17524 | cm = self.contents_manager | ||
km = self.kernel_manager | ||||
Thomas Kluyver
|
r17222 | |||
Zachary Sailer
|
r13057 | model = self.get_json_body() | ||
if model is None: | ||||
Zachary Sailer
|
r13058 | raise web.HTTPError(400, "No JSON data provided") | ||
Zachary Sailer
|
r13057 | try: | ||
name = model['notebook']['name'] | ||||
except KeyError: | ||||
Thomas Kluyver
|
r17222 | raise web.HTTPError(400, "Missing field in JSON data: notebook.name") | ||
Zachary Sailer
|
r13057 | try: | ||
path = model['notebook']['path'] | ||||
except KeyError: | ||||
Thomas Kluyver
|
r17222 | raise web.HTTPError(400, "Missing field in JSON data: notebook.path") | ||
try: | ||||
kernel_name = model['kernel']['name'] | ||||
except KeyError: | ||||
MinRK
|
r18022 | self.log.debug("No kernel name specified, using default kernel") | ||
kernel_name = None | ||||
Thomas Kluyver
|
r17222 | |||
Zachary Sailer
|
r13036 | # Check to see if session exists | ||
Zachary Sailer
|
r13035 | if sm.session_exists(name=name, path=path): | ||
model = sm.get_session(name=name, path=path) | ||||
else: | ||||
Jessica B. Hamrick
|
r18059 | try: | ||
model = sm.create_session(name=name, path=path, kernel_name=kernel_name) | ||||
except NoSuchKernel: | ||||
raise web.HTTPError(400, "No such kernel: %s" % kernel_name) | ||||
MinRK
|
r15310 | location = url_path_join(self.base_url, 'api', 'sessions', model['id']) | ||
MinRK
|
r13132 | self.set_header('Location', url_escape(location)) | ||
Thomas Kluyver
|
r13095 | self.set_status(201) | ||
Zachary Sailer
|
r13057 | self.finish(json.dumps(model, default=date_default)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | class SessionHandler(IPythonHandler): | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12997 | SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE') | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r13014 | @web.authenticated | ||
Zachary Sailer
|
r13057 | @json_errors | ||
Zachary Sailer
|
r12985 | def get(self, session_id): | ||
Zachary Sailer
|
r13036 | # Returns the JSON model for a single session | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
MinRK
|
r13101 | model = sm.get_session(session_id=session_id) | ||
Zachary Sailer
|
r13057 | self.finish(json.dumps(model, default=date_default)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12997 | @web.authenticated | ||
Zachary Sailer
|
r13057 | @json_errors | ||
Zachary Sailer
|
r12997 | def patch(self, session_id): | ||
Zachary Sailer
|
r13035 | # Currently, this handler is strictly for renaming notebooks | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
Zachary Sailer
|
r13057 | model = self.get_json_body() | ||
if model is None: | ||||
Thomas Kluyver
|
r13096 | raise web.HTTPError(400, "No JSON data provided") | ||
Zachary Sailer
|
r13057 | changes = {} | ||
if 'notebook' in model: | ||||
notebook = model['notebook'] | ||||
if 'name' in notebook: | ||||
changes['name'] = notebook['name'] | ||||
if 'path' in notebook: | ||||
changes['path'] = notebook['path'] | ||||
MinRK
|
r13101 | |||
Zachary Sailer
|
r13057 | sm.update_session(session_id, **changes) | ||
MinRK
|
r13101 | model = sm.get_session(session_id=session_id) | ||
Zachary Sailer
|
r13057 | self.finish(json.dumps(model, default=date_default)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @web.authenticated | ||
Zachary Sailer
|
r13057 | @json_errors | ||
Zachary Sailer
|
r12985 | def delete(self, session_id): | ||
Zachary Sailer
|
r13036 | # Deletes the session with given session_id | ||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
MinRK
|
r13101 | sm.delete_session(session_id) | ||
Zachary Sailer
|
r12996 | self.set_status(204) | ||
self.finish() | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | |||
#----------------------------------------------------------------------------- | ||||
# URL to handler mappings | ||||
#----------------------------------------------------------------------------- | ||||
_session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)" | ||||
default_handlers = [ | ||||
Zachary Sailer
|
r13059 | (r"/api/sessions/%s" % _session_id_regex, SessionHandler), | ||
(r"/api/sessions", SessionRootHandler) | ||||
Zachary Sailer
|
r12985 | ] | ||