|
16 | 16 | __all__ = ('tcp_session', 'socket_session', 'stdio_session', 'spawn_session',
|
17 | 17 | 'start_host', 'autocmd', 'command', 'encoding', 'function',
|
18 | 18 | 'plugin', 'rpc_export', 'Host', 'DecodeHook', 'Nvim',
|
19 |
| - 'SessionHook', 'shutdown_hook') |
| 19 | + 'SessionHook', 'shutdown_hook', 'attach') |
20 | 20 |
|
21 | 21 |
|
22 | 22 | def start_host(session=None):
|
@@ -65,6 +65,35 @@ def start_host(session=None):
|
65 | 65 | host.start(plugins)
|
66 | 66 |
|
67 | 67 |
|
| 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 | + |
68 | 97 | # Required for python 2.6
|
69 | 98 | class NullHandler(logging.Handler):
|
70 | 99 | def emit(self, record):
|
|
0 commit comments