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