manual rebase - add sessions web service

This commit is contained in:
Zachary Sailer 2013-07-03 10:37:55 -07:00 committed by MinRK
parent 98e67bfa22
commit 0559df1b8a
2 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,105 @@
"""Tornado handlers for the notebooks web service.
Authors:
* Zach Sailer
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2011 The IPython Development Team
#
# 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):
@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))
@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))
class SessionHandler(IPythonHandler):
@web.authenticated
def get(self, session_id):
sm = self.session_manager
model = sm.get_session_from_id(session_id)
self.finish(jsonapi.dumps(model))
@authenticate_unless_readonly
def put(self, session_id):
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)
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)
return model
@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)
#-----------------------------------------------------------------------------
# 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)
]

View File

@ -0,0 +1,95 @@
"""A base class session manager.
Authors:
* Zach Sailer
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2011 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import uuid
from tornado import web
from IPython.config.configurable import LoggingConfigurable
from IPython.nbformat import current
from IPython.utils.traitlets import List, Dict, Unicode, TraitError
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
class SessionManager(LoggingConfigurable):
# Use session_ids to map notebook names to kernel_ids
sessions = List()
def get_session(self, nb_name, nb_path=None):
"""Get an existing session or create a new one"""
model = None
for session in self.sessions:
if session['notebook_name'] == nb_name and session['notebook_path'] == nb_path:
session_id = session['session_id']
model = session
if model != None:
return session_id, model
else:
session_id = unicode(uuid.uuid4())
return session_id, model
def session_model(self, session_id, notebook_name=None, notebook_path=None, kernel=None):
""" Create a session that links notebooks with kernels """
model = dict(session_id=session_id,
notebook_name=notebook_name,
notebook_path=notebook_path,
kernel=kernel)
self.sessions.append(model)
return model
def list_sessions(self):
"""List all sessions and their information"""
return self.sessions
def set_kernel_for_sessions(self, session_id, kernel_id):
"""Maps the kernel_ids to the session_id in session_mapping"""
for session in self.sessions:
if session['session_id'] == session_id:
session['kernel_id'] = kernel_id
return self.sessions
def delete_mapping_for_session(self, session_id):
"""Delete the session from session_mapping with the given session_id"""
i = 0
for session in self.sessions:
if session['session_id'] == session_id:
del self.sessions[i]
i = i + 1
return self.sessions
def get_session_from_id(self, session_id):
for session in self.sessions:
if session['session_id'] == session_id:
return session
def get_notebook_from_session(self, session_id):
"""Returns the notebook_path for the given session_id"""
for session in self.sessions:
if session['session_id'] == session_id:
return session['notebook_name']
def get_kernel_from_session(self, session_id):
"""Returns the kernel_id for the given session_id"""
for session in self.sessions:
if session['session_id'] == session_id:
return session['kernel']['kernel_id']