handlers.py
122 lines
| 4.1 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: | ||
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 | ||
Min RK
|
r18751 | if sm.session_exists(path=path): | ||
model = sm.get_session(path=path) | ||||
Zachary Sailer
|
r13035 | else: | ||
Jessica B. Hamrick
|
r18059 | try: | ||
Min RK
|
r18751 | model = sm.create_session(path=path, kernel_name=kernel_name) | ||
Jessica B. Hamrick
|
r18059 | except NoSuchKernel: | ||
Jessica B. Hamrick
|
r18063 | msg = ("The '%s' kernel is not available. Please pick another " | ||
"suitable kernel instead, or install that kernel." % kernel_name) | ||||
Jessica B. Hamrick
|
r18069 | status_msg = '%s not found' % kernel_name | ||
Jessica B. Hamrick
|
r18065 | self.log.warn('Kernel not found: %s' % kernel_name) | ||
self.set_status(501) | ||||
self.finish(json.dumps(dict(message=msg, short_message=status_msg))) | ||||
return | ||||
Jessica B. Hamrick
|
r18059 | |||
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 '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 | ||
Jessica B. Hamrick
|
r18214 | try: | ||
sm.delete_session(session_id) | ||||
except KeyError: | ||||
# the kernel was deleted but the session wasn't! | ||||
raise web.HTTPError(410, "Kernel deleted before session") | ||||
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 | ] | ||