Skip to content

Commit d374df1

Browse files
committed
Merge pull request #58 from elmart/session-creation-facade
Improve python API.
2 parents 01c1da1 + 4af3197 commit d374df1

File tree

8 files changed

+54
-28
lines changed

8 files changed

+54
-28
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ install:
3636
script:
3737
- if [ $CI_TARGET = tests ]; then
3838
./scripts/run-api-tests.exp "nosetests" "nvim -u NONE";
39-
NVIM_SPAWN_ARGV='["nvim", "-u", "NONE", "--embed"]' nosetests;
39+
NVIM_CHILD_ARGV='["nvim", "-u", "NONE", "--embed"]' nosetests;
4040
else
4141
flake8 neovim;
4242
fi

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ similar to the one exposed by the [python-vim
2727
bridge](http://vimdoc.sourceforge.net/htmldoc/if_pyth.html#python-vim))
2828

2929
```python
30-
>>> from neovim import socket_session, Nvim
31-
# Create a msgpack-rpc session to the unix domain socket created by Nvim:
32-
>>> session = socket_session('/tmp/nvim')
33-
# Create a Nvim instance from the session(don't call Nvim constructor!):
34-
>>> nvim = Nvim.from_session(session)
30+
>>> from neovim import attach
31+
# Create a python API session attached to unix domain socket created above:
32+
>>> nvim = attach('socket', path='/tmp/nvim')
3533
# Now do some work.
3634
>>> buffer = nvim.buffers[0] # Get the first buffer
3735
>>> buffer[0] = 'replace first line'

neovim/__init__.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
import sys
88

99
from .api import DecodeHook, Nvim, SessionHook
10-
from .msgpack_rpc import (socket_session, spawn_session, stdio_session,
10+
from .msgpack_rpc import (child_session, socket_session, stdio_session,
1111
tcp_session)
1212
from .plugin import (Host, autocmd, command, encoding, function, plugin,
1313
rpc_export, shutdown_hook)
1414

1515

16-
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'spawn_session',
16+
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'child_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+
child_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):

neovim/msgpack_rpc/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .session import Session
1111

1212

13-
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'spawn_session')
13+
__all__ = ('tcp_session', 'socket_session', 'stdio_session', 'child_session')
1414

1515

1616
def session(transport_type='stdio', *args, **kwargs):
@@ -36,6 +36,6 @@ def stdio_session():
3636
return session('stdio')
3737

3838

39-
def spawn_session(argv):
39+
def child_session(argv):
4040
"""Create a msgpack-rpc session from a new Nvim instance."""
41-
return session('spawn', argv)
41+
return session('child', argv)

neovim/msgpack_rpc/event_loop/asyncio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _connect_stdio(self):
8989
coroutine = self._loop.connect_write_pipe(self._fact, sys.stdout)
9090
self._loop.run_until_complete(coroutine)
9191

92-
def _connect_spawn(self, argv):
92+
def _connect_child(self, argv):
9393
coroutine = self._loop.subprocess_exec(self._fact, *argv)
9494
self._loop.run_until_complete(coroutine)
9595

neovim/msgpack_rpc/event_loop/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BaseEventLoop(object):
3030
- `_connect_socket(path)`: Same as tcp, but use a UNIX domain socket or
3131
or named pipe.
3232
- `_connect_stdio()`: Use stdin/stdout as the connection to Nvim
33-
- `_connect_spawn(argv)`: Use the argument vector `argv` to spawn an
33+
- `_connect_child(argv)`: Use the argument vector `argv` to spawn an
3434
embedded Nvim that has it's stdin/stdout connected to the event loop.
3535
- `_start_reading()`: Called after any of _connect_* methods. Can be used
3636
to perform any post-connection setup or validation.
@@ -68,7 +68,7 @@ def __init__(self, transport_type, *args):
6868
Traceback (most recent call last):
6969
...
7070
AttributeError: 'BaseEventLoop' object has no attribute '_init'
71-
>>> BaseEventLoop('spawn', ['nvim', '--embed', '-u', 'NONE'])
71+
>>> BaseEventLoop('child', ['nvim', '--embed', '-u', 'NONE'])
7272
Traceback (most recent call last):
7373
...
7474
AttributeError: 'BaseEventLoop' object has no attribute '_init'
@@ -101,10 +101,10 @@ def connect_stdio(self):
101101
info('Preparing stdin/stdout for streaming data')
102102
self._connect_stdio()
103103

104-
def connect_spawn(self, argv):
105-
"""Connect a new Nvim instance. Delegated to `_connect_spawn`."""
104+
def connect_child(self, argv):
105+
"""Connect a new Nvim instance. Delegated to `_connect_child`."""
106106
info('Spawning a new nvim instance')
107-
self._connect_spawn(argv)
107+
self._connect_child(argv)
108108

109109
def send(self, data):
110110
"""Queue `data` for sending to Nvim."""

neovim/msgpack_rpc/event_loop/uv.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _connect_stdio(self):
6363
self._write_stream = pyuv.Pipe(self._loop)
6464
self._write_stream.open(sys.stdout.fileno())
6565

66-
def _connect_spawn(self, argv):
66+
def _connect_child(self, argv):
6767
self._write_stream = pyuv.Pipe(self._loop)
6868
self._read_stream = pyuv.Pipe(self._loop)
6969
self._error_stream = pyuv.Pipe(self._loop)
@@ -108,7 +108,10 @@ def _on_async(self, handle):
108108

109109
def _setup_signals(self, signals):
110110
self._signal_handles = []
111-
handler = lambda h, signum: self._on_signal(signum)
111+
112+
def handler(h, signum):
113+
self._on_signal(signum)
114+
112115
for signum in signals:
113116
handle = pyuv.Signal(self._loop)
114117
handle.start(handler, signum)

test/common.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@
77
from nose.tools import eq_ as eq
88

99

10-
session = None
11-
vim = None
12-
if 'NVIM_SPAWN_ARGV' in os.environ:
13-
argv = json.loads(os.environ['NVIM_SPAWN_ARGV'])
14-
session = neovim.spawn_session(argv)
10+
if 'NVIM_CHILD_ARGV' in os.environ:
11+
vim = neovim.attach('child', argv=json.loads(os.environ['NVIM_CHILD_ARGV']))
1512
else:
16-
session = neovim.socket_session(os.environ['NVIM_LISTEN_ADDRESS'])
17-
18-
vim = neovim.Nvim.from_session(session)
13+
vim = neovim.attach('socket', path=os.environ['NVIM_LISTEN_ADDRESS'])
1914

2015
if sys.version_info >= (3, 0):
2116
# For Python3 we decode binary strings as Unicode for compatibility
@@ -63,6 +58,7 @@
6358

6459
vim.input(cleanup_func)
6560

61+
6662
def cleanup():
6763
# cleanup nvim
6864
vim.command('call BeforeEachTest()')

0 commit comments

Comments
 (0)