Skip to content

Commit 7c8dcf5

Browse files
committed
Merge pull request #96 from captainsafia/add-kernelspec-to-connection-file
Add kernel spec name to connection file
2 parents a476b0d + c307c6f commit 7c8dcf5

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

jupyter_client/connect.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0, hb_port=0,
3535
control_port=0, ip='', key=b'', transport='tcp',
36-
signature_scheme='hmac-sha256',
36+
signature_scheme='hmac-sha256', kernel_name=''
3737
):
3838
"""Generates a JSON config file, including the selection of random ports.
3939
@@ -72,6 +72,8 @@ def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0,
7272
Currently, 'hmac' is the only supported digest scheme,
7373
and 'sha256' is the default hash function.
7474
75+
kernel_name : str, optional
76+
The name of the kernel currently connected to.
7577
"""
7678
if not ip:
7779
ip = localhost()
@@ -127,6 +129,7 @@ def write_connection_file(fname=None, shell_port=0, iopub_port=0, stdin_port=0,
127129
cfg['key'] = bytes_to_str(key)
128130
cfg['transport'] = transport
129131
cfg['signature_scheme'] = signature_scheme
132+
cfg['kernel_name'] = kernel_name
130133

131134
with open(fname, 'w') as f:
132135
f.write(json.dumps(cfg, indent=2))
@@ -379,6 +382,7 @@ def write_connection_file(self):
379382
shell_port=self.shell_port, hb_port=self.hb_port,
380383
control_port=self.control_port,
381384
signature_scheme=self.session.signature_scheme,
385+
kernel_name=self.kernel_name
382386
)
383387
# write_connection_file also sets default ports:
384388
for name in port_names:

jupyter_client/tests/test_connect.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ def initialize(self, argv=[]):
2424

2525
sample_info = dict(ip='1.2.3.4', transport='ipc',
2626
shell_port=1, hb_port=2, iopub_port=3, stdin_port=4, control_port=5,
27-
key=b'abc123', signature_scheme='hmac-md5',
27+
key=b'abc123', signature_scheme='hmac-md5', kernel_name='python'
28+
)
29+
30+
sample_info_kn = dict(ip='1.2.3.4', transport='ipc',
31+
shell_port=1, hb_port=2, iopub_port=3, stdin_port=4, control_port=5,
32+
key=b'abc123', signature_scheme='hmac-md5', kernel_name='test'
2833
)
2934

3035
def test_write_connection_file():
@@ -55,6 +60,23 @@ def test_load_connection_file_session():
5560
nt.assert_equal(session.signature_scheme, sample_info['signature_scheme'])
5661

5762

63+
def test_load_connection_file_session_with_kn():
64+
"""test load_connection_file() after """
65+
session = Session()
66+
app = DummyConsoleApp(session=Session())
67+
app.initialize(argv=[])
68+
session = app.session
69+
70+
with TemporaryDirectory() as d:
71+
cf = os.path.join(d, 'kernel.json')
72+
connect.write_connection_file(cf, **sample_info_kn)
73+
app.connection_file = cf
74+
app.load_connection_file()
75+
76+
nt.assert_equal(session.key, sample_info_kn['key'])
77+
nt.assert_equal(session.signature_scheme, sample_info_kn['signature_scheme'])
78+
79+
5880
def test_app_load_connection_file():
5981
"""test `ipython console --existing` loads a connection file"""
6082
with TemporaryDirectory() as d:

0 commit comments

Comments
 (0)