Refactor kernelspec resource handler to separate URL prefix

This commit is contained in:
Thomas Kluyver 2014-05-10 12:22:20 -07:00
parent 1dabf3c547
commit f6628b7006
4 changed files with 32 additions and 26 deletions

View File

@ -0,0 +1,25 @@
from tornado import web
from ..base.handlers import IPythonHandler
from ..services.kernelspecs.handlers import kernel_name_regex
class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
SUPPORTED_METHODS = ('GET', 'HEAD')
def initialize(self):
web.StaticFileHandler.initialize(self, path='')
def get(self, kernel_name, path, include_body=True):
ksm = self.kernel_spec_manager
try:
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
except KeyError:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
self.log.debug("Serving kernel resource from: %s", self.root)
return web.StaticFileHandler.get(self, path, include_body=include_body)
def head(self, kernel_name, path):
self.get(kernel_name, path, include_body=False)
default_handlers = [
(r"/kernelspecs/%s/(?P<path>.*)" % kernel_name_regex, KernelSpecResourceHandler),
]

View File

@ -187,6 +187,7 @@ class NotebookWebApplication(web.Application):
handlers.extend(load_handlers('auth.logout'))
handlers.extend(load_handlers('notebook.handlers'))
handlers.extend(load_handlers('nbconvert.handlers'))
handlers.extend(load_handlers('kernelspecs.handlers'))
handlers.extend(load_handlers('services.kernels.handlers'))
handlers.extend(load_handlers('services.notebooks.handlers'))
handlers.extend(load_handlers('services.clusters.handlers'))

View File

@ -41,31 +41,11 @@ class KernelSpecHandler(IPythonHandler):
self.finish(kernelspec.to_json())
class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
SUPPORTED_METHODS = ('GET', 'HEAD')
def initialize(self):
web.StaticFileHandler.initialize(self, path='')
def get(self, kernel_name, path, include_body=True):
ksm = self.kernel_spec_manager
try:
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
except KeyError:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
self.log.debug("Serving kernel resource from: %s", self.root)
return web.StaticFileHandler.get(self, path, include_body=include_body)
def head(self, kernel_name, path):
self.get(kernel_name, path, include_body=False)
# URL to handler mappings
_kernel_name_regex = r"(?P<kernel_name>\w+)"
kernel_name_regex = r"(?P<kernel_name>\w+)"
default_handlers = [
(r"/api/kernelspecs", MainKernelSpecHandler),
(r"/api/kernelspecs/%s" % _kernel_name_regex, KernelSpecHandler),
(r"/api/kernelspecs/%s/(?P<path>.*)" % _kernel_name_regex, KernelSpecResourceHandler),
(r"/api/kernelspecs/%s" % kernel_name_regex, KernelSpecHandler),
]

View File

@ -30,20 +30,20 @@ class KernelSpecAPI(object):
def _req(self, verb, path, body=None):
response = requests.request(verb,
url_path_join(self.base_url, 'api/kernelspecs', path),
url_path_join(self.base_url, path),
data=body,
)
response.raise_for_status()
return response
def list(self):
return self._req('GET', '')
return self._req('GET', 'api/kernelspecs')
def kernel_spec_info(self, name):
return self._req('GET', name)
return self._req('GET', url_path_join('api/kernelspecs', name))
def kernel_resource(self, name, path):
return self._req('GET', url_path_join(name, path))
return self._req('GET', url_path_join('kernelspecs', name, path))
class APITest(NotebookTestBase):
"""Test the kernelspec web service API"""