Skip to content

Commit 79e4413

Browse files
committed
Improve python API: Add façade function to create python api sessions.
Add a utility function to make creating client session more comfortable. For example, instead of: ``` from neovim import socket_session, Nvim session = tcp_session(address=<address>, port=<port>) nvim = Nvim.from_session(session) ``` you can do now: ``` from neovim import attach nvim = attach('tcp', address=<address>, port=<port>) ```
1 parent 01c1da1 commit 79e4413

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

neovim/__init__.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'spawn_session',
1717
'start_host', 'autocmd', 'command', 'encoding', 'function',
1818
'plugin', 'rpc_export', 'Host', 'DecodeHook', 'Nvim',
19-
'SessionHook', 'shutdown_hook')
19+
'SessionHook', 'shutdown_hook', 'attach')
2020

2121

2222
def start_host(session=None):
@@ -65,6 +65,35 @@ def start_host(session=None):
6565
host.start(plugins)
6666

6767

68+
def attach(session_type, address=None, port=None, path=None, argv=None):
69+
"""Provide a nicer interface to create python api sessions.
70+
71+
Previous machinery to create python api sessions is still there. This only
72+
creates a facade function to make things easier for the most usual cases.
73+
Thus, instead of:
74+
from neovim import socket_session, Nvim
75+
session = tcp_session(address=<address>, port=<port>)
76+
nvim = Nvim.from_session(session)
77+
You can now do:
78+
from neovim import attach
79+
nvim = attach('tcp', address=<address>, port=<port>)
80+
And also:
81+
nvim = attach('socket', path=<path>)
82+
nvim = attach('child', argv=<argv>)
83+
nvim = attach('stdio')
84+
"""
85+
session = (tcp_session(address, port) if session_type == 'tcp' else
86+
socket_session(path) if session_type == 'socket' else
87+
stdio_session() if session_type == 'stdio' else
88+
spawn_session(argv) if session_type == 'child'else
89+
None)
90+
91+
if not session:
92+
raise Exception('Unknown session type "%s"' % session_type)
93+
94+
return Nvim.from_session(session)
95+
96+
6897
# Required for python 2.6
6998
class NullHandler(logging.Handler):
7099
def emit(self, record):

0 commit comments

Comments
 (0)