From e2d88e4befddcab50450ff716b00b373cf047d03 2014-05-08 23:18:23 From: Thomas Kluyver Date: 2014-05-08 23:18:23 Subject: [PATCH] Create REST API for kernel specs --- diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py index 03070c0..56772f9 100644 --- a/IPython/html/base/handlers.py +++ b/IPython/html/base/handlers.py @@ -159,6 +159,10 @@ class IPythonHandler(AuthenticatedHandler): return self.settings['session_manager'] @property + def kernel_spec_manager(self): + return self.settings['kernel_spec_manager'] + + @property def project_dir(self): return self.notebook_manager.notebook_dir diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py index fc08f6b..c36d901 100644 --- a/IPython/html/notebookapp.py +++ b/IPython/html/notebookapp.py @@ -67,12 +67,13 @@ from IPython.core.application import ( ) from IPython.core.profiledir import ProfileDir from IPython.kernel import KernelManager +from IPython.kernel.kernelspec import KernelSpecManager from IPython.kernel.zmq.session import default_secure, Session from IPython.nbformat.sign import NotebookNotary from IPython.utils.importstring import import_item from IPython.utils import submodule from IPython.utils.traitlets import ( - Dict, Unicode, Integer, List, Bool, Bytes, + Dict, Unicode, Integer, List, Bool, Bytes, Instance, DottedObjectName, TraitError, ) from IPython.utils import py3compat @@ -118,19 +119,21 @@ def load_handlers(name): class NotebookWebApplication(web.Application): def __init__(self, ipython_app, kernel_manager, notebook_manager, - cluster_manager, session_manager, log, base_url, - settings_overrides, jinja_env_options): + cluster_manager, session_manager, kernel_spec_manager, log, + base_url, settings_overrides, jinja_env_options): settings = self.init_settings( ipython_app, kernel_manager, notebook_manager, cluster_manager, - session_manager, log, base_url, settings_overrides, jinja_env_options) + session_manager, kernel_spec_manager, log, base_url, + settings_overrides, jinja_env_options) handlers = self.init_handlers(settings) super(NotebookWebApplication, self).__init__(handlers, **settings) def init_settings(self, ipython_app, kernel_manager, notebook_manager, - cluster_manager, session_manager, log, base_url, - settings_overrides, jinja_env_options=None): + cluster_manager, session_manager, kernel_spec_manager, + log, base_url, settings_overrides, + jinja_env_options=None): # Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and # base_url will always be unicode, which will in turn # make the patterns unicode, and ultimately result in unicode @@ -162,6 +165,7 @@ class NotebookWebApplication(web.Application): notebook_manager=notebook_manager, cluster_manager=cluster_manager, session_manager=session_manager, + kernel_spec_manager=kernel_spec_manager, # IPython stuff nbextensions_path = ipython_app.nbextensions_path, @@ -188,6 +192,7 @@ class NotebookWebApplication(web.Application): handlers.extend(load_handlers('services.clusters.handlers')) handlers.extend(load_handlers('services.sessions.handlers')) handlers.extend(load_handlers('services.nbconvert.handlers')) + handlers.extend(load_handlers('services.kernelspecs.handlers')) # FIXME: /files/ should be handled by the Contents service when it exists nbm = settings['notebook_manager'] if hasattr(nbm, 'notebook_dir'): @@ -510,6 +515,11 @@ class NotebookApp(BaseIPythonApplication): help='The cluster manager class to use.' ) + kernel_spec_manager = Instance(KernelSpecManager) + + def _kernel_spec_manager_default(self): + return KernelSpecManager(ipython_dir=self.ipython_dir) + trust_xheaders = Bool(False, config=True, help=("Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For headers" "sent by the upstream reverse proxy. Necessary if the proxy handles SSL") @@ -616,7 +626,7 @@ class NotebookApp(BaseIPythonApplication): """initialize tornado webapp and httpserver""" self.web_app = NotebookWebApplication( self, self.kernel_manager, self.notebook_manager, - self.cluster_manager, self.session_manager, + self.cluster_manager, self.session_manager, self.kernel_spec_manager, self.log, self.base_url, self.webapp_settings, self.jinja_environment_options ) diff --git a/IPython/html/services/kernelspecs/__init__.py b/IPython/html/services/kernelspecs/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/IPython/html/services/kernelspecs/__init__.py diff --git a/IPython/html/services/kernelspecs/handlers.py b/IPython/html/services/kernelspecs/handlers.py new file mode 100644 index 0000000..1840cf4 --- /dev/null +++ b/IPython/html/services/kernelspecs/handlers.py @@ -0,0 +1,75 @@ +"""Tornado handlers for kernel specifications.""" + +# Copyright (c) IPython Development Team. +# Distributed under the terms of the Modified BSD License. + +import logging +from tornado import web + +from zmq.utils import jsonapi + +from ...base.handlers import IPythonHandler, json_errors, path_regex + + +class MainKernelSpecHandler(IPythonHandler): + SUPPORTED_METHODS = ('GET',) + + @web.authenticated + @json_errors + def get(self): + ksm = self.kernel_spec_manager + results = [] + for kernel_name in ksm.find_kernel_specs(): + results.append(dict(name=kernel_name, + display_name=ksm.get_kernel_spec(kernel_name).display_name)) + + self.set_header("Content-Type", 'application/json') + self.finish(jsonapi.dumps(results)) + + +class KernelSpecHandler(IPythonHandler): + SUPPORTED_METHODS = ('GET',) + + @web.authenticated + @json_errors + def get(self, kernel_name): + ksm = self.kernel_spec_manager + kernelspec = ksm.get_kernel_spec(kernel_name) + self.set_header("Content-Type", 'application/json') + 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 + self.root = ksm.get_kernel_spec(kernel_name).resource_dir + self.log.warn("Set root: %s", self.root) + return web.StaticFileHandler.get(self, path, include_body=include_body) + +# @classmethod +# def get_absolute_path(cls, root, path): +# res = web.StaticFileHandler.get_absolute_path(cls, root, path) +# self.log.warn("Full path: %s", res) +# return res + + def head(self, kernel_name, path): + self.get(kernel_name, path, include_body=False) + + +#----------------------------------------------------------------------------- +# URL to handler mappings +#----------------------------------------------------------------------------- + + +_kernel_name_regex = r"(?P\w+)" + +default_handlers = [ + (r"/api/kernelspecs", MainKernelSpecHandler), + (r"/api/kernelspecs/%s" % _kernel_name_regex, KernelSpecHandler), + (r"/api/kernelspecs/%s/(?P.*)" % _kernel_name_regex, KernelSpecResourceHandler), +] diff --git a/IPython/kernel/kernelspec.py b/IPython/kernel/kernelspec.py index 0db302c..1028b66 100644 --- a/IPython/kernel/kernelspec.py +++ b/IPython/kernel/kernelspec.py @@ -48,6 +48,14 @@ class KernelSpec(HasTraits): kernel_dict = json.load(f) return cls(resource_dir=resource_dir, **kernel_dict) + def to_json(self): + return json.dumps(dict(argv=self.argv, + env=self.env, + display_name=self.display_name, + language=self.language, + codemirror_mode=self.codemirror_mode, + )) + def _is_kernel_dir(path): """Is ``path`` a kernel directory?""" return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json'))