Completed
Pull Request — master (#179)
by Björn
02:36
created

neovim.api.DecodeHook   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 24
ccs 10
cts 10
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 1
A decode_if_bytes() 0 4 2
A walk() 0 6 1
1
"""Code shared between the API classes."""
2 6
import functools
3
4
5 6
class Remote(object):
6
7
    """Base class for Nvim objects(buffer/window/tabpage).
8
9
    Each type of object has it's own specialized class with API wrappers around
10
    the msgpack-rpc session. This implements equality which takes the remote
11
    object handle into consideration.
12
    """
13
14 6
    def __eq__(self, other):
15
        """Return True if `self` and `other` are the same object."""
16 6
        return (hasattr(other, 'code_data') and
17
                other.code_data == self.code_data)
0 ignored issues
show
Bug introduced by
The Instance of Remote does not seem to have a member named code_data.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
18
19 6
    def __hash__(self):
20
        """Return hash based on remote object id."""
21 6
        return self.code_data.__hash__()
0 ignored issues
show
Bug introduced by
The Instance of Remote does not seem to have a member named code_data.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
22
23 6
class RemoteApi(object):
24 6
    def __init__(self, session, prefix, args=()):
25 6
        self._session = session
26 6
        self._prefix = prefix
27 6
        self._args = args
28
29 6
    def __getattr__(self, name):
30
        return functools.partial(self._session.request,self._prefix + name, *self._args)
31
32 6
class RemoteMap(object):
33
34
    """Represents a string->object map stored in Nvim.
35
36
    This is the dict counterpart to the `RemoteSequence` class, but it is used
37
    as a generic way of retrieving values from the various map-like data
38
    structures present in Nvim.
39
40
    It is used to provide a dict-like API to vim variables and options.
41
    """
42
43 6
    def __init__(self, session, get_method, set_method, self_obj=None):
44
        """Initialize a RemoteMap with session, getter/setter and self_obj."""
45 6
        self._get = _wrap(session, get_method, self_obj)
46 6
        self._set = None
47 6
        if set_method:
48 6
            self._set = _wrap(session, set_method, self_obj)
49
50 6
    def __getitem__(self, key):
51
        """Return a map value by key."""
52 6
        return self._get(key)
53
54 6
    def __setitem__(self, key, value):
55
        """Set a map value by key(if the setter was provided)."""
56 6
        if not self._set:
57 6
            raise TypeError('This dict is read-only')
58 6
        self._set(key, value)
59
60 6
    def __delitem__(self, key):
61
        """Delete a map value by associating None with the key."""
62
        if not self._set:
63
            raise TypeError('This dict is read-only')
64
        return self._set(key, None)
65
66 6
    def __contains__(self, key):
67
        """Check if key is present in the map."""
68
        try:
69
            self._get(key)
70
            return True
71
        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...
72
            return False
73
74 6
    def get(self, key, default=None):
75
        """Return value for key if present, else a default value."""
76
        try:
77
            return self._get(key)
78
        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...
79
            return default
80
81
82 6
class RemoteSequence(object):
83
84
    """Represents a sequence of objects stored in Nvim.
85
86
    This class is used to wrap msgapck-rpc functions that work on Nvim
87
    sequences(of lines, buffers, windows and tabpages) with an API that
88
    is similar to the one provided by the python-vim interface.
89
90
    For example, the 'buffers' property of the `Nvim class is a RemoteSequence
91
    sequence instance, and the expression `nvim.buffers[0]` is translated to
92
    session.request('vim_get_buffers')[0].
93
94
    It can also receive an optional self_obj that will be passed as first
95
    argument of the request. For example, `tabpage.windows[0]` is translated
96
    to: session.request('tabpage_get_windows', tabpage_instance)[0].
97
98
    One important detail about this class is that all methods will fetch the
99
    sequence into a list and perform the necessary manipulation
100
    locally(iteration, indexing, counting, etc).
101
    """
102
103 6
    def __init__(self, session, method, self_obj=None):
104
        """Initialize a RemoteSequence with session, method and self_obj."""
105 6
        self._fetch = _wrap(session, method, self_obj)
106
107 6
    def __len__(self):
108
        """Return the length of the remote sequence."""
109 6
        return len(self._fetch())
110
111 6
    def __getitem__(self, idx):
112
        """Return a sequence item by index."""
113 6
        if not isinstance(idx, slice):
114 6
            return self._fetch()[idx]
115
        return self._fetch()[idx.start:idx.stop]
116
117 6
    def __iter__(self):
118
        """Return an iterator for the sequence."""
119 6
        items = self._fetch()
120 6
        for item in items:
121 6
            yield item
122
123 6
    def __contains__(self, item):
124
        """Check if an item is present in the sequence."""
125 6
        return item in self._fetch()
126
127
128 6
def _identity(obj, session, method, kind):
0 ignored issues
show
Unused Code introduced by
The argument session seems to be unused.
Loading history...
Unused Code introduced by
The argument kind seems to be unused.
Loading history...
Unused Code introduced by
The argument method seems to be unused.
Loading history...
129
    return obj
130
131
132
133 6
class DecodeHook(object):
134
135
    """SessionHook subclass that decodes utf-8 strings coming from Nvim.
136
137
    This class is useful for python3, where strings are now unicode by
138
    default(byte strings need to be prefixed with "b").
139
    """
140
141 6
    def __init__(self, encoding='utf-8', encoding_errors='strict'):
142
        """Initialize with encoding and encoding errors policy."""
143 5
        self.encoding = encoding
144 5
        self.encoding_errors = encoding_errors
145
146 6
    def decode_if_bytes(self, obj):
147 3
        if isinstance(obj, bytes):
148 4
            return obj.decode(self.encoding, errors=self.encoding_errors)
149 3
        return obj
150
151 6
    def walk(self, obj):
152
        """Decode bytes found in obj (any msgpack object).
153
154
        Uses encoding and policy specified in constructor.
155
        """
156 3
        return walk(self.decode_if_bytes, obj)
157
158 6
def walk(fn, obj, *args):
159
    """Recursively walk an object graph applying `fn`/`args` to objects."""
160 6
    if type(obj) in [list, tuple]:
161 6
        return list(walk(fn, o, *args) for o in obj)
162 6
    if type(obj) is dict:
163 6
        return dict((walk(fn, k, *args), walk(fn, v, *args)) for k, v in
164
                    obj.items())
165 6
    return fn(obj, *args)
166
167
168 6
def _wrap(session, method, self_obj):
169 6
    if self_obj is not None:
170 6
        return lambda *args: session.request(method, self_obj, *args)
171
    else:
172
        return lambda *args: session.request(method, *args)
173