Skip to content

Commit 052f56f

Browse files
committed
support connection_file-based IPython connection
1 parent eac94f6 commit 052f56f

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

ftplugin/python/ipy.vim

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,43 @@ except NameError:
5151

5252
def km_from_string(s):
5353
"""create kernel manager from IPKernelApp string
54-
such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668'
54+
such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668' for 0.11
55+
or just 'kernel-12345.json' for 0.12
5556
"""
57+
from os.path import join as pjoin
5658
from IPython.zmq.blockingkernelmanager import BlockingKernelManager, Empty
5759
from IPython.config.loader import KeyValueConfigLoader
5860
from IPython.zmq.kernelapp import kernel_aliases
5961
global km,send,Empty
60-
# vim interface currently only deals with existing kernels
61-
s = s.replace('--existing','')
62-
loader = KeyValueConfigLoader(s.split(), aliases=kernel_aliases)
63-
cfg = loader.load_config()['KernelApp']
64-
try:
65-
km = BlockingKernelManager(
66-
shell_address=(ip, cfg['shell_port']),
67-
sub_address=(ip, cfg['iopub_port']),
68-
stdin_address=(ip, cfg['stdin_port']),
69-
hb_address=(ip, cfg['hb_port']))
70-
except KeyError,e:
71-
echo(":IPython " +s + " failed", "Info")
72-
echo("^-- failed --"+e.message.replace('_port','')+" not specified", "Error")
73-
return
62+
63+
if 'connection_file' in BlockingKernelManager.class_trait_names():
64+
from IPython.lib.kernel import find_connection_file
65+
# 0.12 uses files instead of a collection of ports
66+
# include default IPython search path
67+
# filefind also allows for absolute paths, in which case the search
68+
# is ignored
69+
try:
70+
fullpath = find_connection_file(s)
71+
except IOError,e:
72+
echo(":IPython " + s + " failed", "Info")
73+
echo("^-- failed --" + s + " not found", "Error")
74+
return
75+
km = BlockingKernelManager(connection_file = fullpath)
76+
km.load_connection_file()
77+
else:
78+
s = s.replace('--existing', '')
79+
loader = KeyValueConfigLoader(s.split(), aliases=kernel_aliases)
80+
cfg = loader.load_config()['KernelApp']
81+
try:
82+
km = BlockingKernelManager(
83+
shell_address=(ip, cfg['shell_port']),
84+
sub_address=(ip, cfg['iopub_port']),
85+
stdin_address=(ip, cfg['stdin_port']),
86+
hb_address=(ip, cfg['hb_port']))
87+
except KeyError,e:
88+
echo(":IPython " +s + " failed", "Info")
89+
echo("^-- failed --"+e.message.replace('_port','')+" not specified", "Error")
90+
return
7491
km.start_channels()
7592
send = km.shell_channel.execute
7693
return km
@@ -170,7 +187,8 @@ def update_subchannel_msgs(debug=False):
170187
db = vim.current.buffer
171188
else:
172189
db = []
173-
startedin_vimipython = vim.current.buffer.name.endswith('vim-ipython')
190+
b = vim.current.buffer
191+
startedin_vimipython = (b.name is not None and b.name.endswith('vim-ipython'))
174192
if not startedin_vimipython:
175193
vim.command("pcl")
176194
vim.command("silent pedit vim-ipython")

0 commit comments

Comments
 (0)