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