Completed
Pull Request — master (#294)
by Björn
21:36
created

Nvim.__init__()   B

Complexity

Conditions 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 26
ccs 25
cts 25
cp 1
crap 2
rs 8.8571
c 1
b 0
f 0
1
"""Main Nvim interface."""
2 5
import functools
3 5
import os
4 5
import sys
5
from traceback import format_stack
6 5
7
from msgpack import ExtType
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...
8 5
9
from .buffer import Buffer
10 5
from .common import (Remote, RemoteApi, RemoteMap, RemoteSequence,
11 5
                     decode_if_bytes, walk)
12
from .tabpage import Tabpage
13 5
from .window import Window
14 5
from ..compat import IS_PYTHON3
15 5
from ..util import Version, format_exc_skip
16 5
17
__all__ = ('Nvim')
18 5
19
20
os_chdir = os.chdir
21 5
22
23
class Nvim(object):
24 5
25
    """Class that represents a remote Nvim instance.
26
27
    This class is main entry point to Nvim remote API, it is a wrapper
28
    around Session instances.
29
30
    The constructor of this class must not be called directly. Instead, the
31
    `from_session` class method should be used to create the first instance
32
    from a raw `Session` instance.
33
34
    Subsequent instances for the same session can be created by calling the
35
    `with_decode` instance method to change the decoding behavior or
36
    `SubClass.from_nvim(nvim)` where `SubClass` is a subclass of `Nvim`, which
37
    is useful for having multiple `Nvim` objects that behave differently
38
    without one affecting the other.
39
40
    When this library is used on python3.4+, asyncio event loop is guaranteed
41
    to be used. It is available as the "loop" attribute of this closs. Note
42 5
    that asyncio callbacks cannot make blocking requests, which which includes
43
    accessing state-dependent attributes. They should instead schedule another
44
    callback using nvim.async_call, which will not have this restriction.
45
    """
46
47
    @classmethod
48
    def from_session(cls, session):
49
        """Create a new Nvim instance for a Session instance.
50 5
51 5
        This method must be called to create the first Nvim instance, since it
52
        queries Nvim metadata for type information and sets a SessionHook for
53 5
        creating specialized objects from Nvim remote handles.
54
        """
55 5
        session.error_wrapper = lambda e: NvimError(e[1])
56
        channel_id, metadata = session.request(b'vim_get_api_info')
57 5
58
        if IS_PYTHON3:
59
            # decode all metadata strings for python3
60
            metadata = walk(decode_if_bytes, metadata)
61
62
        types = {
63 5
            metadata['types']['Buffer']['id']: Buffer,
64
            metadata['types']['Window']['id']: Window,
65 5
            metadata['types']['Tabpage']['id']: Tabpage,
66
        }
67
68
        return cls(session, channel_id, metadata, types)
69
70
    @classmethod
71 5
    def from_nvim(cls, nvim):
72
        """Create a new Nvim instance from an existing instance."""
73
        return cls(nvim._session, nvim.channel_id, nvim.metadata,
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _session was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
74 5
                   nvim.types, nvim._decode, nvim._err_cb)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _decode was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
Coding Style Best Practice introduced by
It seems like _err_cb was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
75 5
76 5
    def __init__(self, session, channel_id, metadata, types,
77 5
                 decode=False, err_cb=None):
78 5
        """Initialize a new Nvim instance. This method is module-private."""
79 5
        self._session = session
80 5
        self.channel_id = channel_id
81 5
        self.metadata = metadata
82 5
        version = metadata.get("version", {"api_level": 0})
83 5
        self.version = Version(**version)
84 5
        self.types = types
85 5
        self.api = RemoteApi(self, 'nvim_')
86 5
        self.vars = RemoteMap(self, 'nvim_get_var', 'nvim_set_var')
87 5
        self.vvars = RemoteMap(self, 'nvim_get_vvar', None)
88 5
        self.options = RemoteMap(self, 'nvim_get_option', 'nvim_set_option')
89 5
        self.buffers = Buffers(self)
90 5
        self.windows = RemoteSequence(self, 'nvim_list_wins')
91 5
        self.tabpages = RemoteSequence(self, 'nvim_list_tabpages')
92 5
        self.current = Current(self)
93
        self.session = CompatibilitySession(self)
94 5
        self.funcs = Funcs(self)
95 5
        self.error = NvimError
96 5
        self._decode = decode
97 5
        self._err_cb = err_cb
98 5
99 5
        # only on python3.4+ we expose asyncio
100 5
        if IS_PYTHON3:
101 3
            self.loop = self._session.loop._loop
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _loop was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
102 5
103
    def _from_nvim(self, obj, decode=None):
104 5
        if decode is None:
105 5
            decode = self._decode
106 5
        if type(obj) is ExtType:
107 5
            cls = self.types[obj.code]
108
            return cls(self, (obj.code, obj.data))
109 5
        if decode:
110
            obj = decode_if_bytes(obj, decode)
111
        return obj
112
113
    def _to_nvim(self, obj):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
114
        if isinstance(obj, Remote):
115
            return ExtType(*obj.code_data)
116
        return obj
117
118
    def request(self, name, *args, **kwargs):
119
        r"""Send an API request or notification to nvim.
120
121
        It is rarely needed to call this function directly, as most API
122
        functions have python wrapper functions. The `api` object can
123
        be also be used to call API functions as methods:
124
125
            vim.api.err_write('ERROR\n', async=True)
126
            vim.current.buffer.api.get_mark('.')
127
128
        is equivalent to
129 5
130 5
            vim.request('nvim_err_write', 'ERROR\n', async=True)
131 5
            vim.request('nvim_buf_get_mark', vim.current.buffer, '.')
132 5
133
134 5
        Normally a blocking request will be sent.  If the `async` flag is
135
        present and True, a asynchronous notification is sent instead. This
136
        will never block, and the return value or error is ignored.
137
        """
138
        decode = kwargs.pop('decode', self._decode)
139
        args = walk(self._to_nvim, args)
140 5
        res = self._session.request(name, *args, **kwargs)
141 5
        return walk(self._from_nvim, res, decode=decode)
142 5
143
    def next_message(self):
144 5
        """Block until a message(request or notification) is available.
145
146
        If any messages were previously enqueued, return the first in queue.
147
        If not, run the event loop until one is received.
148
        """
149
        msg = self._session.next_message()
150
        if msg:
151 5
            return walk(self._from_nvim, msg)
152 5
153 5
    def run_loop(self, request_cb, notification_cb,
154
                 setup_cb=None, err_cb=None):
155 5
        """Run the event loop to receive requests and notifications from Nvim.
156 5
157 5
        This should not be called from a plugin running in the host, which
158 5
        already runs the loop and dispatches events to plugins.
159 5
        """
160
        if err_cb is None:
161
            err_cb = sys.stderr.write
162
        self._err_cb = err_cb
163
164
        def filter_request_cb(name, args):
165 5
            name = self._from_nvim(name)
166
            args = walk(self._from_nvim, args)
167 5
            try:
168
                result = request_cb(name, args)
169
            except Exception:
170
                msg = ("error caught in request handler '{} {}'\n{}\n\n"
171
                       .format(name, args, format_exc_skip(1)))
172
                self._err_cb(msg)
173
                raise
174
            return walk(self._to_nvim, result)
175
176
        def filter_notification_cb(name, args):
177
            name = self._from_nvim(name)
178 5
            args = walk(self._from_nvim, args)
179
            try:
180 5
                notification_cb(name, args)
181
            except Exception:
182 5
                msg = ("error caught in notification handler '{} {}'\n{}\n\n"
183
                       .format(name, args, format_exc_skip(1)))
184 5
                self._err_cb(msg)
185
                raise
186 5
187
        self._session.run(filter_request_cb, filter_notification_cb, setup_cb)
188
189 5
    def stop_loop(self):
190
        """Stop the event loop being started with `run_loop`."""
191
        self._session.stop()
192
193
    def with_decode(self, decode=True):
194
        """Initialize a new Nvim instance."""
195
        return Nvim(self._session, self.channel_id,
196
                    self.metadata, self.types, decode, self._err_cb)
197 5
198
    def ui_attach(self, width, height, rgb):
199
        """Register as a remote UI.
200
201 5
        After this method is called, the client will receive redraw
202
        notifications.
203
        """
204
        return self.request('ui_attach', width, height, rgb)
205
206
    def ui_detach(self):
207
        """Unregister as a remote UI."""
208 5
        return self.request('ui_detach')
209
210 5
    def ui_try_resize(self, width, height):
211
        """Notify nvim that the client window has resized.
212 5
213
        If possible, nvim will send a redraw request to resize.
214 5
        """
215
        return self.request('ui_try_resize', width, height)
216 5
217
    def subscribe(self, event):
218 5
        """Subscribe to a Nvim event."""
219
        return self.request('nvim_subscribe', event)
220 5
221
    def unsubscribe(self, event):
222
        """Unsubscribe to a Nvim event."""
223
        return self.request('nvim_unsubscribe', event)
224 5
225
    def command(self, string, **kwargs):
226 5
        """Execute a single ex command."""
227
        return self.request('nvim_command', string, **kwargs)
228 5
229
    def command_output(self, string):
230 5
        """Execute a single ex command and return the output."""
231
        return self.request('nvim_command_output', string)
232 5
233
    def eval(self, string, **kwargs):
234
        """Evaluate a vimscript expression."""
235
        return self.request('nvim_eval', string, **kwargs)
236
237 5
    def call(self, name, *args, **kwargs):
238
        """Call a vimscript function."""
239 5
        return self.request('nvim_call_function', name, args, **kwargs)
240
241
    def strwidth(self, string):
242
        """Return the number of display cells `string` occupies.
243 5
244
        Tab is counted as one cell.
245
        """
246
        return self.request('nvim_strwidth', string)
247
248
    def list_runtime_paths(self):
249
        """Return a list of paths contained in the 'runtimepath' option."""
250
        return self.request('nvim_list_runtime_paths')
251
252
    def foreach_rtp(self, cb):
253
        """Invoke `cb` for each path in 'runtimepath'.
254
255
        Call the given callable for each path in 'runtimepath' until either
256
        callable returns something but None, the exception is raised or there
257
        are no longer paths. If stopped in case callable returned non-None,
258 5
        vim.foreach_rtp function returns the value returned by callable.
259
        """
260 5
        for path in self.request('nvim_list_runtime_paths'):
261 5
            try:
262
                if cb(path) is not None:
263 5
                    break
264
            except Exception:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
265
                break
266
267
    def chdir(self, dir_path):
268
        """Run os.chdir, then all appropriate vim stuff."""
269
        os_chdir(dir_path)
270
        return self.request('nvim_set_current_dir', dir_path)
271
272
    def feedkeys(self, keys, options='', escape_csi=True):
273
        """Push `keys` to Nvim user input buffer.
274 5
275
        Options can be a string with the following character flags:
276
        - 'm': Remap keys. This is default.
277
        - 'n': Do not remap keys.
278
        - 't': Handle keys as if typed; otherwise they are handled as if coming
279
               from a mapping. This matters for undo, opening folds, etc.
280
        """
281
        return self.request('nvim_feedkeys', keys, options, escape_csi)
282 5
283
    def input(self, bytes):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in bytes.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
284 5
        """Push `bytes` to Nvim low level input buffer.
285
286
        Unlike `feedkeys()`, this uses the lowest level input buffer and the
287
        call is not deferred. It returns the number of bytes actually
288
        written(which can be less than what was requested if the buffer is
289
        full).
290
        """
291
        return self.request('nvim_input', bytes)
292
293
    def replace_termcodes(self, string, from_part=False, do_lt=True,
294
                          special=True):
295
        r"""Replace any terminal code strings by byte sequences.
296
297
        The returned sequences are Nvim's internal representation of keys,
298
        for example:
299
300
        <esc> -> '\x1b'
301 5
        <cr>  -> '\r'
302
        <c-l> -> '\x0c'
303
        <up>  -> '\x80ku'
304
305 5
        The returned sequences can be used as input to `feedkeys`.
306
        """
307 2
        return self.request('nvim_replace_termcodes', string,
308
                            from_part, do_lt, special)
309 5
310
    def out_write(self, msg, **kwargs):
311
        """Print `msg` as a normal message."""
312
        return self.request('nvim_out_write', msg, **kwargs)
313
314
    def err_write(self, msg, **kwargs):
315
        """Print `msg` as an error message."""
316
        return self.request('nvim_err_write', msg, **kwargs)
317
318
    def quit(self, quit_command='qa!'):
319
        """Send a quit command to Nvim.
320
321
        By default, the quit command is 'qa!' which will make Nvim quit without
322
        saving anything.
323 5
        """
324
        try:
325
            self.command(quit_command)
326
        except IOError:
0 ignored issues
show
Unused Code introduced by
This except handler seems to be unused and could be removed.

Except handlers which only contain pass and do not have an else clause can usually simply be removed:

try:
    raises_exception()
except:  # Could be removed
    pass
Loading history...
327 5
            # sending a quit command will raise an IOError because the
328
            # connection is closed before a response is received. Safe to
329
            # ignore it.
330
            pass
331
332
    def new_highlight_source(self):
333
        """Return new src_id for use with Buffer.add_highlight."""
334
        return self.current.buffer.add_highlight("", 0, src_id=0)
335
336
    def async_call(self, fn, *args, **kwargs):
337 5
        """Schedule `fn` to be called by the event loop soon.
338
339 5
        This function is thread-safe, and is the only way code not
340 5
        on the main thread could interact with nvim api objects.
341 5
342
        This function can also be called in a synchronous
343 1
        event handler, just before it returns, to defer execution
344
        that shouldn't block neovim.
345
        """
346 1
        call_point = ''.join(format_stack(None, 5)[:-1])
347
348 5
        def handler():
349
            try:
350
                fn(*args, **kwargs)
351 5
            except Exception as err:
352
                msg = ("error caught while executing async callback:\n"
353
                       "{!r}\n{}\n \nthe call was requested at\n{}"
354
                       .format(err, format_exc_skip(1), call_point))
355
                self._err_cb(msg)
356
                raise
357
        self._session.threadsafe_call(handler)
358
359
360
class Buffers(object):
361
362 5
    """Remote NVim buffers.
363
364 5
    Currently the interface for interacting with remote NVim buffers is the
365
    `nvim_list_bufs` msgpack-rpc function. Most methods fetch the list of
366 5
    buffers from NVim.
367
368 5
    Conforms to *python-buffers*.
369
    """
370 5
371
    def __init__(self, nvim):
372 5
        """Initialize a Buffers object with Nvim object `nvim`."""
373 5
        self._fetch_buffers = nvim.api.list_bufs
374 5
375
    def __len__(self):
376
        """Return the count of buffers."""
377 5
        return len(self._fetch_buffers())
378
379 5
    def __getitem__(self, number):
380
        """Return the Buffer object matching buffer number `number`."""
381 5
        for b in self._fetch_buffers():
382
            if b.number == number:
383 5
                return b
384
        raise KeyError(number)
385
386 5
    def __contains__(self, b):
387
        """Return whether Buffer `b` is a known valid buffer."""
388
        return isinstance(b, Buffer) and b.valid
389
390 5
    def __iter__(self):
391 5
        """Return an iterator over the list of buffers."""
392
        return iter(self._fetch_buffers())
393
394 5
395
class CompatibilitySession(object):
396
397
    """Helper class for API compatibility."""
398 5
399 5
    def __init__(self, nvim):
400 5
        self.threadsafe_call = nvim.async_call
401
402 5
403
class Current(object):
404 5
405
    """Helper class for emulating vim.current from python-vim."""
406 5
407
    def __init__(self, session):
408 5
        self._session = session
409
        self.range = None
410 5
411
    @property
412 5
    def line(self):
413
        return self._session.request('nvim_get_current_line')
414 5
415
    @line.setter
416 5
    def line(self, line):
417
        return self._session.request('nvim_set_current_line', line)
418 5
419
    @property
420 5
    def buffer(self):
421
        return self._session.request('nvim_get_current_buf')
422 5
423
    @buffer.setter
424 5
    def buffer(self, buffer):
425
        return self._session.request('nvim_set_current_buf', buffer)
426 5
427
    @property
428 5
    def window(self):
429
        return self._session.request('nvim_get_current_win')
430 5
431
    @window.setter
432 5
    def window(self, window):
433
        return self._session.request('nvim_set_current_win', window)
434
435 5
    @property
436
    def tabpage(self):
437
        return self._session.request('nvim_get_current_tabpage')
438
439 5
    @tabpage.setter
440 5
    def tabpage(self, tabpage):
441
        return self._session.request('nvim_set_current_tabpage', tabpage)
442 5
443 5
444
class Funcs(object):
445
446 5
    """Helper class for functional vimscript interface."""
447 5
448
    def __init__(self, nvim):
449
        self._nvim = nvim
450
451
    def __getattr__(self, name):
452
        return functools.partial(self._nvim.call, name)
453
454
455
class NvimError(Exception):
456
    pass
457