##// END OF EJS Templates
More descriptive short message
Jessica B. Hamrick -
Show More
@@ -1,124 +1,124 b''
1 1 """Tornado handlers for the sessions web service."""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import json
7 7
8 8 from tornado import web
9 9
10 10 from ...base.handlers import IPythonHandler, json_errors
11 11 from IPython.utils.jsonutil import date_default
12 12 from IPython.html.utils import url_path_join, url_escape
13 13 from IPython.kernel.kernelspec import NoSuchKernel
14 14
15 15
16 16 class SessionRootHandler(IPythonHandler):
17 17
18 18 @web.authenticated
19 19 @json_errors
20 20 def get(self):
21 21 # Return a list of running sessions
22 22 sm = self.session_manager
23 23 sessions = sm.list_sessions()
24 24 self.finish(json.dumps(sessions, default=date_default))
25 25
26 26 @web.authenticated
27 27 @json_errors
28 28 def post(self):
29 29 # Creates a new session
30 30 #(unless a session already exists for the named nb)
31 31 sm = self.session_manager
32 32 cm = self.contents_manager
33 33 km = self.kernel_manager
34 34
35 35 model = self.get_json_body()
36 36 if model is None:
37 37 raise web.HTTPError(400, "No JSON data provided")
38 38 try:
39 39 name = model['notebook']['name']
40 40 except KeyError:
41 41 raise web.HTTPError(400, "Missing field in JSON data: notebook.name")
42 42 try:
43 43 path = model['notebook']['path']
44 44 except KeyError:
45 45 raise web.HTTPError(400, "Missing field in JSON data: notebook.path")
46 46 try:
47 47 kernel_name = model['kernel']['name']
48 48 except KeyError:
49 49 self.log.debug("No kernel name specified, using default kernel")
50 50 kernel_name = None
51 51
52 52 # Check to see if session exists
53 53 if sm.session_exists(name=name, path=path):
54 54 model = sm.get_session(name=name, path=path)
55 55 else:
56 56 try:
57 57 model = sm.create_session(name=name, path=path, kernel_name=kernel_name)
58 58 except NoSuchKernel:
59 59 msg = ("The '%s' kernel is not available. Please pick another "
60 60 "suitable kernel instead, or install that kernel." % kernel_name)
61 status_msg = 'Kernel not found'
61 status_msg = '%s not found' % kernel_name
62 62 self.log.warn('Kernel not found: %s' % kernel_name)
63 63 self.set_status(501)
64 64 self.finish(json.dumps(dict(message=msg, short_message=status_msg)))
65 65 return
66 66
67 67 location = url_path_join(self.base_url, 'api', 'sessions', model['id'])
68 68 self.set_header('Location', url_escape(location))
69 69 self.set_status(201)
70 70 self.finish(json.dumps(model, default=date_default))
71 71
72 72 class SessionHandler(IPythonHandler):
73 73
74 74 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
75 75
76 76 @web.authenticated
77 77 @json_errors
78 78 def get(self, session_id):
79 79 # Returns the JSON model for a single session
80 80 sm = self.session_manager
81 81 model = sm.get_session(session_id=session_id)
82 82 self.finish(json.dumps(model, default=date_default))
83 83
84 84 @web.authenticated
85 85 @json_errors
86 86 def patch(self, session_id):
87 87 # Currently, this handler is strictly for renaming notebooks
88 88 sm = self.session_manager
89 89 model = self.get_json_body()
90 90 if model is None:
91 91 raise web.HTTPError(400, "No JSON data provided")
92 92 changes = {}
93 93 if 'notebook' in model:
94 94 notebook = model['notebook']
95 95 if 'name' in notebook:
96 96 changes['name'] = notebook['name']
97 97 if 'path' in notebook:
98 98 changes['path'] = notebook['path']
99 99
100 100 sm.update_session(session_id, **changes)
101 101 model = sm.get_session(session_id=session_id)
102 102 self.finish(json.dumps(model, default=date_default))
103 103
104 104 @web.authenticated
105 105 @json_errors
106 106 def delete(self, session_id):
107 107 # Deletes the session with given session_id
108 108 sm = self.session_manager
109 109 sm.delete_session(session_id)
110 110 self.set_status(204)
111 111 self.finish()
112 112
113 113
114 114 #-----------------------------------------------------------------------------
115 115 # URL to handler mappings
116 116 #-----------------------------------------------------------------------------
117 117
118 118 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
119 119
120 120 default_handlers = [
121 121 (r"/api/sessions/%s" % _session_id_regex, SessionHandler),
122 122 (r"/api/sessions", SessionRootHandler)
123 123 ]
124 124
General Comments 0
You need to be logged in to leave comments. Login now