Completed
Pull Request — master (#303)
by Björn
24:34
created

MsgpackStream.run()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
"""Msgpack handling in the event loop pipeline."""
2 5
import logging
3
4 5
from msgpack import Packer, Unpacker
0 ignored issues
show
Configuration introduced by
The import msgpack could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6 5
from ..compat import unicode_errors_default
7
8 5
logger = logging.getLogger(__name__)
9 5
debug, info, warn = (logger.debug, logger.info, logger.warning,)
10
11
12 5
class MsgpackStream(object):
13
14
    """Two-way msgpack stream that wraps a event loop byte stream.
15
16
    This wraps the event loop interface for reading/writing bytes and
17
    exposes an interface for reading/writing msgpack documents.
18
    """
19
20 5
    def __init__(self, event_loop):
21
        """Wrap `event_loop` on a msgpack-aware interface."""
22 5
        self._event_loop = event_loop
23 5
        self._packer = Packer(encoding='utf-8',
24 5
                              unicode_errors=unicode_errors_default)
25 5
        self._unpacker = Unpacker()
26
        self._message_cb = None
27 5
28
    def threadsafe_call(self, fn):
29 5
        """Wrapper around `BaseEventLoop.threadsafe_call`."""
30
        self._event_loop.threadsafe_call(fn)
31 5
32
    def send(self, msg):
33 5
        """Queue `msg` for sending to Nvim."""
34 5
        debug('sent %s', msg)
35
        self._event_loop.send(self._packer.pack(msg))
36 5
37
    def run(self, message_cb):
38
        """Run the event loop to receive messages from Nvim.
39
40
        While the event loop is running, `message_cb` will be called whenever
41
        a message has been successfully parsed from the input stream.
42 5
        """
43 5
        self._message_cb = message_cb
44 5
        self._event_loop.run(self._on_data)
45
        self._message_cb = None
46 5
47
    def stop(self):
48 5
        """Stop the event loop."""
49
        self._event_loop.stop()
50 5
51 5
    def close(self):
52 5
        """Close the event loop."""
53 5
        self._event_loop.close()
54 5
55 5
    def _on_data(self, data):
56 5
        self._unpacker.feed(data)
57 5
        while True:
58 5
            try:
59 5
                debug('waiting for message...')
60 5
                msg = next(self._unpacker)
61
                debug('received message: %s', msg)
62
                self._message_cb(msg)
63
            except StopIteration:
64
                debug('unpacker needs more data...')
65
                break
66