handlers.py
104 lines
| 3.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 | ||||
#----------------------------------------------------------------------------- | ||||
from tornado import web | ||||
from zmq.utils import jsonapi | ||||
from IPython.utils.jsonutil import date_default | ||||
from ...base.handlers import IPythonHandler, authenticate_unless_readonly | ||||
#----------------------------------------------------------------------------- | ||||
# Session web service handlers | ||||
#----------------------------------------------------------------------------- | ||||
class SessionRootHandler(IPythonHandler): | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @authenticate_unless_readonly | ||
def get(self): | ||||
sm = self.session_manager | ||||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
sessions = sm.list_sessions() | ||||
self.finish(jsonapi.dumps(sessions)) | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @web.authenticated | ||
def post(self): | ||||
sm = self.session_manager | ||||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
notebook_path = self.get_argument('notebook_path', default=None) | ||||
notebook_name, path = nbm.named_notebook_path(notebook_path) | ||||
session_id, model = sm.get_session(notebook_name, path) | ||||
if model == None: | ||||
kernel_id = km.start_kernel() | ||||
kernel = km.kernel_model(kernel_id, self.ws_url) | ||||
model = sm.session_model(session_id, notebook_name, path, kernel) | ||||
self.finish(jsonapi.dumps(model)) | ||||
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
|
r12997 | @authenticate_unless_readonly | ||
Zachary Sailer
|
r12985 | def get(self, session_id): | ||
sm = self.session_manager | ||||
model = sm.get_session_from_id(session_id) | ||||
self.finish(jsonapi.dumps(model)) | ||||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12997 | @web.authenticated | ||
def patch(self, session_id): | ||||
Zachary Sailer
|
r12985 | sm = self.session_manager | ||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
Zachary Sailer
|
r12997 | notebook_path = self.request.body | ||
Zachary Sailer
|
r12985 | notebook_name, path = nbm.named_notebook_path(notebook_path) | ||
kernel_id = sm.get_kernel_from_session(session_id) | ||||
kernel = km.kernel_model(kernel_id, self.ws_url) | ||||
sm.delete_mapping_for_session(session_id) | ||||
model = sm.session_model(session_id, notebook_name, path, kernel) | ||||
Zachary Sailer
|
r12996 | self.finish(jsonapi.dumps(model)) | ||
Zachary Sailer
|
r13009 | |||
Zachary Sailer
|
r12985 | @web.authenticated | ||
def delete(self, session_id): | ||||
sm = self.session_manager | ||||
nbm = self.notebook_manager | ||||
km = self.kernel_manager | ||||
kernel_id = sm.get_kernel_from_session(session_id) | ||||
km.shutdown_kernel(kernel_id) | ||||
sm.delete_mapping_for_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 = [ | ||||
(r"api/sessions/%s" % _session_id_regex, SessionHandler), | ||||
(r"api/sessions", SessionRootHandler) | ||||
] | ||||