handlers.py
127 lines
| 4.3 KiB
| text/x-python
|
PythonLexer
Zachary Sailer
|
r13011 | """Tornado handlers for the sessions web service. | ||
Zachary Sailer
|
r12985 | |||
Authors: | ||||
* Zach Sailer | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
Zachary Sailer
|
r13011 | # Copyright (C) 2013 The IPython Development Team | ||
Zachary Sailer
|
r12985 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
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 | ||
Zachary Sailer
|
r12985 | |||
#----------------------------------------------------------------------------- | ||||
# Session web service handlers | ||||
#----------------------------------------------------------------------------- | ||||
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): | ||
Zachary Sailer
|
r13036 | # Creates a new session | ||
#(unless a session already exists for the named nb) | ||||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
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: | ||||
Zachary Sailer
|
r13058 | raise web.HTTPError(400, "Missing field in JSON data: name") | ||
Zachary Sailer
|
r13057 | try: | ||
path = model['notebook']['path'] | ||||
except KeyError: | ||||
Zachary Sailer
|
r13058 | raise web.HTTPError(400, "Missing field in JSON data: path") | ||
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: | ||||
MinRK
|
r15420 | kernel_id = km.start_kernel(path=path) | ||
MinRK
|
r15400 | model = sm.create_session(name=name, path=path, kernel_id=kernel_id) | ||
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 | ||
km = self.kernel_manager | ||||
MinRK
|
r13101 | session = sm.get_session(session_id=session_id) | ||
sm.delete_session(session_id) | ||||
Zachary Sailer
|
r13035 | km.shutdown_kernel(session['kernel']['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 | ] | ||