##// END OF EJS Templates
refactoring of nbmanager and filenbmanager...
refactoring of nbmanager and filenbmanager major clean up of the two managers. We make sure to follow the standard models described in IPEP 16

File last commit:

r13046:116db313
r13046:116db313
Show More
handlers.py
115 lines | 4.0 KiB | text/x-python | PythonLexer
Zachary Sailer
standard model changes
r13011 """Tornado handlers for the sessions web service.
Zachary Sailer
manual rebase - add sessions web service
r12985
Authors:
* Zach Sailer
"""
#-----------------------------------------------------------------------------
Zachary Sailer
standard model changes
r13011 # Copyright (C) 2013 The IPython Development Team
Zachary Sailer
manual rebase - add sessions web service
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
Zachary Sailer
rebase master- eliminate read-only
r13014 from ...base.handlers import IPythonHandler
Zachary Sailer
manual rebase - add sessions web service
r12985
#-----------------------------------------------------------------------------
# Session web service handlers
#-----------------------------------------------------------------------------
class SessionRootHandler(IPythonHandler):
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
rebase master- eliminate read-only
r13014 @web.authenticated
Zachary Sailer
manual rebase - add sessions web service
r12985 def get(self):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Return a list of running sessions
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
nbm = self.notebook_manager
km = self.kernel_manager
sessions = sm.list_sessions()
self.finish(jsonapi.dumps(sessions))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985 @web.authenticated
def post(self):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Creates a new session
#(unless a session already exists for the named nb)
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
nbm = self.notebook_manager
km = self.kernel_manager
notebook_path = self.get_argument('notebook_path', default=None)
Zachary Sailer
session manager restructuring...
r13035 name, path = nbm.named_notebook_path(notebook_path)
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Check to see if session exists
Zachary Sailer
session manager restructuring...
r13035 if sm.session_exists(name=name, path=path):
model = sm.get_session(name=name, path=path)
kernel_id = model['kernel']['id']
km.start_kernel(kernel_id, cwd=nbm.notebook_dir)
else:
session_id = sm.get_session_id()
sm.save_session(session_id=session_id, name=name, path=path)
kernel_id = km.start_kernel(cwd=nbm.notebook_dir)
Zachary Sailer
manual rebase - add sessions web service
r12985 kernel = km.kernel_model(kernel_id, self.ws_url)
Zachary Sailer
session manager restructuring...
r13035 sm.update_session(session_id, kernel=kernel_id)
model = sm.get_session(id=session_id)
self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id))
Zachary Sailer
manual rebase - add sessions web service
r12985 self.finish(jsonapi.dumps(model))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985 class SessionHandler(IPythonHandler):
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
rebase master- eliminate read-only
r13014 @web.authenticated
Zachary Sailer
manual rebase - add sessions web service
r12985 def get(self, session_id):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Returns the JSON model for a single session
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
Zachary Sailer
session manager restructuring...
r13035 model = sm.get_session(id=session_id)
Zachary Sailer
manual rebase - add sessions web service
r12985 self.finish(jsonapi.dumps(model))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
Add 'patch' to session & notebook, rename working
r12997 @web.authenticated
def patch(self, session_id):
Zachary Sailer
session manager restructuring...
r13035 # Currently, this handler is strictly for renaming notebooks
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
nbm = self.notebook_manager
km = self.kernel_manager
Zachary Sailer
refactoring of nbmanager and filenbmanager...
r13046 data = self.request.body
Zachary Sailer
add tests for session api
r13044 data = jsonapi.loads(self.request.body)
name, path = nbm.named_notebook_path(data['notebook_path'])
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 sm.update_session(session_id, name=name)
Zachary Sailer
session manager restructuring...
r13035 model = sm.get_session(id=session_id)
Zachary Sailer
fixed shutdown button refresh on dashboard
r12996 self.finish(jsonapi.dumps(model))
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985 @web.authenticated
def delete(self, session_id):
Zachary Sailer
cleaning nb handlers, adding doc-strings/comments
r13036 # Deletes the session with given session_id
Zachary Sailer
manual rebase - add sessions web service
r12985 sm = self.session_manager
nbm = self.notebook_manager
km = self.kernel_manager
Zachary Sailer
session manager restructuring...
r13035 session = sm.get_session(id=session_id)
sm.delete_session(session_id)
km.shutdown_kernel(session['kernel']['id'])
Zachary Sailer
fixed shutdown button refresh on dashboard
r12996 self.set_status(204)
self.finish()
Zachary Sailer
removing debug logs
r13009
Zachary Sailer
manual rebase - add sessions web service
r12985
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
_session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
default_handlers = [
Zachary Sailer
add tests for session api
r13044 (r"api/sessions/%s/" % _session_id_regex, SessionHandler),
Zachary Sailer
manual rebase - add sessions web service
r12985 (r"api/sessions/%s" % _session_id_regex, SessionHandler),
Zachary Sailer
add tests for session api
r13044 (r"api/sessions/", SessionRootHandler),
Zachary Sailer
manual rebase - add sessions web service
r12985 (r"api/sessions", SessionRootHandler)
]