Skip to content

Commit 4902927

Browse files
authored
Merge pull request #20 from gateway-experiments/convey-config
Give kernel providers opportunity to load configuration
2 parents 5cc31f6 + 9e37f57 commit 4902927

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

jupyter_kernel_mgmt/discovery.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import six
55

6+
from traitlets.config import Application
67
try:
78
from json import JSONDecodeError
89
except ImportError:
@@ -44,6 +45,14 @@ def launch_async(self, name, cwd=None):
4445
"""
4546
raise NotImplementedError()
4647

48+
def load_config(self, config=None):
49+
"""Loads the configuration corresponding to the hosting application. This method
50+
is called during KernelFinder initialization prior to any other methods.
51+
Provider is responsible for interpreting the `config` parameter (when present) which will be
52+
an instance of Config: https://traitlets.readthedocs.io/en/stable/config.html#the-main-concepts
53+
"""
54+
pass
55+
4756

4857
class KernelSpecProvider(KernelProviderBase):
4958
"""Offers kernel types from installed kernelspec directories.
@@ -140,6 +149,15 @@ class KernelFinder(object):
140149
def __init__(self, providers):
141150
self.providers = providers
142151

152+
# If there's an application singleton, pass its configurables to the provider. If
153+
# no application, still give provider a chance to handle configuration loading.
154+
config = None
155+
if Application.initialized():
156+
config = Application.instance().config
157+
158+
for provider in providers:
159+
provider.load_config(config=config)
160+
143161
@classmethod
144162
def from_entrypoints(cls):
145163
"""Load all kernel providers advertised by entry points.

jupyter_kernel_mgmt/tests/test_discovery.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from jupyter_kernel_mgmt.managerabc import KernelManagerABC
77
from jupyter_kernel_mgmt.subproc.manager import KernelManager
88
from jupyter_core import paths
9+
from traitlets import List, Unicode
10+
from traitlets.config import Application, SingletonConfigurable
911
from .utils import test_env
1012
from .test_kernelspec import install_sample_kernel
1113

@@ -63,6 +65,35 @@ def get_connection_info(self):
6365
return {}
6466

6567

68+
class ProviderApplication(Application):
69+
name = 'ProviderApplication'
70+
my_app = Unicode('my_app', config=True,)
71+
72+
73+
class ProviderConfig(SingletonConfigurable):
74+
my_argv = List(Unicode(), ['default_argv'], config=True,)
75+
my_foo = Unicode('foo.bar', config=True,)
76+
77+
78+
class TestConfigKernelProvider(DummyKernelProvider):
79+
"""A dummy kernel provider for testing KernelFinder with configuration loading"""
80+
id = 'config'
81+
82+
config = None
83+
argv = ['dummy_config_kernel'] # will be replace by config item
84+
85+
def find_kernels(self):
86+
argv = self.argv
87+
if self.config:
88+
argv = self.config.my_argv
89+
assert self.config.my_foo == 'foo.bar' # verify default config value
90+
91+
yield 'sample', {'argv': argv}
92+
93+
def load_config(self, config=None):
94+
self.config = ProviderConfig.instance(config=config)
95+
96+
6697
class KernelDiscoveryTests(unittest.TestCase):
6798

6899
def setUp(self):
@@ -134,3 +165,21 @@ def test_kernel_spec_provider_subclass():
134165

135166
conn_info, manager = kf.launch('dummy_kspec/dummy_kspec1')
136167
assert isinstance(manager, DummyKernelManager)
168+
169+
def test_load_config(self):
170+
# create fake application
171+
app = ProviderApplication()
172+
app.launch_instance(argv=["--ProviderConfig.my_argv=['xxx','yyy']"])
173+
174+
kf = discovery.KernelFinder(providers=[TestConfigKernelProvider()])
175+
dummy_kspecs = list(kf.find_kernels())
176+
177+
count = 0
178+
found_argv = []
179+
for name, spec in dummy_kspecs:
180+
if name == 'config/sample':
181+
found_argv = spec['argv']
182+
count += 1
183+
184+
assert count == 1
185+
assert found_argv == ['xxx', 'yyy']

0 commit comments

Comments
 (0)