1
|
|
|
"""Synchronous msgpack-rpc session layer.""" |
2
|
6 |
|
import logging |
3
|
6 |
|
from collections import deque |
4
|
|
|
|
5
|
6 |
|
from traceback import format_exc |
6
|
|
|
|
7
|
6 |
|
import greenlet |
|
|
|
|
8
|
|
|
|
9
|
6 |
|
logger = logging.getLogger(__name__) |
10
|
6 |
|
error, debug, info, warn = (logger.error, logger.debug, logger.info, |
11
|
|
|
logger.warning,) |
12
|
|
|
|
13
|
|
|
|
14
|
6 |
|
class Session(object): |
15
|
|
|
|
16
|
|
|
"""Msgpack-rpc session layer that uses coroutines for a synchronous API. |
17
|
|
|
|
18
|
|
|
This class provides the public msgpack-rpc API required by this library. |
19
|
|
|
It uses the greenlet module to handle requests and notifications coming |
20
|
|
|
from Nvim with a synchronous API. |
21
|
|
|
""" |
22
|
|
|
|
23
|
6 |
|
def __init__(self, async_session): |
24
|
|
|
"""Wrap `async_session` on a synchronous msgpack-rpc interface.""" |
25
|
6 |
|
self._async_session = async_session |
26
|
6 |
|
self._request_cb = self._notification_cb = None |
27
|
6 |
|
self._pending_messages = deque() |
28
|
6 |
|
self._is_running = False |
29
|
6 |
|
self._setup_exception = None |
30
|
|
|
|
31
|
6 |
|
def threadsafe_call(self, fn, *args, **kwargs): |
32
|
|
|
"""Wrapper around `AsyncSession.threadsafe_call`.""" |
33
|
6 |
|
def handler(): |
34
|
6 |
|
try: |
35
|
6 |
|
fn(*args, **kwargs) |
36
|
|
|
except Exception: |
|
|
|
|
37
|
|
|
warn("error caught while excecuting async callback\n%s\n", |
38
|
|
|
format_exc()) |
39
|
|
|
|
40
|
6 |
|
def greenlet_wrapper(): |
41
|
6 |
|
gr = greenlet.greenlet(handler) |
42
|
6 |
|
gr.switch() |
43
|
|
|
|
44
|
6 |
|
self._async_session.threadsafe_call(greenlet_wrapper) |
45
|
|
|
|
46
|
6 |
|
def next_message(self): |
47
|
|
|
"""Block until a message(request or notification) is available. |
48
|
|
|
|
49
|
|
|
If any messages were previously enqueued, return the first in queue. |
50
|
|
|
If not, run the event loop until one is received. |
51
|
|
|
""" |
52
|
6 |
|
if self._is_running: |
53
|
6 |
|
raise Exception('Event loop already running') |
54
|
6 |
|
if self._pending_messages: |
55
|
6 |
|
return self._pending_messages.popleft() |
56
|
6 |
|
self._async_session.run(self._enqueue_request_and_stop, |
57
|
|
|
self._enqueue_notification_and_stop) |
58
|
6 |
|
if self._pending_messages: |
59
|
6 |
|
return self._pending_messages.popleft() |
60
|
|
|
|
61
|
6 |
|
def request(self, method, *args, **kwargs): |
62
|
|
|
"""Send a msgpack-rpc request and block until as response is received. |
63
|
|
|
|
64
|
|
|
If the event loop is running, this method must have been called by a |
65
|
|
|
request or notification handler running on a greenlet. In that case, |
66
|
|
|
send the quest and yield to the parent greenlet until a response is |
67
|
|
|
available. |
68
|
|
|
|
69
|
|
|
When the event loop is not running, it will perform a blocking request |
70
|
|
|
like this: |
71
|
|
|
- Send the request |
72
|
|
|
- Run the loop until the response is available |
73
|
|
|
- Put requests/notifications received while waiting into a queue |
74
|
|
|
|
75
|
|
|
If the `async` flag is present and True, a asynchronous notification is |
76
|
|
|
sent instead. This will never block, and the return value or error is |
77
|
|
|
ignored. |
78
|
|
|
""" |
79
|
6 |
|
async = kwargs.pop('async', False) |
80
|
6 |
|
if async: |
81
|
6 |
|
self._async_session.notify(method, args) |
82
|
6 |
|
return |
83
|
|
|
|
84
|
6 |
|
if kwargs: |
85
|
|
|
raise ValueError("request got unsupported keyword argument(s): {0}" |
86
|
|
|
.format(', '.join(kwargs.keys()))) |
87
|
|
|
|
88
|
6 |
|
if self._is_running: |
89
|
6 |
|
v = self._yielding_request(method, args) |
90
|
|
|
else: |
91
|
6 |
|
v = self._blocking_request(method, args) |
92
|
6 |
|
if not v: |
93
|
|
|
# EOF |
94
|
|
|
raise IOError('EOF') |
95
|
6 |
|
err, rv = v |
|
|
|
|
96
|
6 |
|
if err: |
97
|
6 |
|
info("'Received error: %s", err) |
98
|
6 |
|
raise self.error_wrapper(err) |
|
|
|
|
99
|
6 |
|
return rv |
100
|
|
|
|
101
|
6 |
|
def run(self, request_cb, notification_cb, setup_cb=None): |
102
|
|
|
"""Run the event loop to receive requests and notifications from Nvim. |
103
|
|
|
|
104
|
|
|
Like `AsyncSession.run()`, but `request_cb` and `notification_cb` are |
105
|
|
|
inside greenlets. |
106
|
|
|
""" |
107
|
6 |
|
self._request_cb = request_cb |
108
|
6 |
|
self._notification_cb = notification_cb |
109
|
6 |
|
self._is_running = True |
110
|
6 |
|
self._setup_exception = None |
111
|
|
|
|
112
|
6 |
|
def on_setup(): |
113
|
6 |
|
try: |
114
|
6 |
|
setup_cb() |
115
|
|
|
except Exception as e: |
|
|
|
|
116
|
|
|
self._setup_exception = e |
117
|
|
|
self.stop() |
118
|
|
|
|
119
|
6 |
|
if setup_cb: |
120
|
|
|
# Create a new greenlet to handle the setup function |
121
|
6 |
|
gr = greenlet.greenlet(on_setup) |
122
|
6 |
|
gr.switch() |
123
|
|
|
|
124
|
6 |
|
if self._setup_exception: |
125
|
|
|
error('Setup error: {0}'.format(self._setup_exception)) |
126
|
|
|
raise self._setup_exception |
|
|
|
|
127
|
|
|
|
128
|
|
|
# Process all pending requests and notifications |
129
|
6 |
|
while self._pending_messages: |
130
|
|
|
msg = self._pending_messages.popleft() |
131
|
1 |
|
getattr(self, '_on_{0}'.format(msg[0]))(*msg[1:]) |
132
|
6 |
|
self._async_session.run(self._on_request, self._on_notification) |
133
|
6 |
|
self._is_running = False |
134
|
6 |
|
self._request_cb = None |
135
|
6 |
|
self._notification_cb = None |
136
|
|
|
|
137
|
6 |
|
if self._setup_exception: |
138
|
|
|
raise self._setup_exception |
|
|
|
|
139
|
|
|
|
140
|
6 |
|
def stop(self): |
141
|
|
|
"""Stop the event loop.""" |
142
|
6 |
|
self._async_session.stop() |
143
|
|
|
|
144
|
6 |
|
def _yielding_request(self, method, args): |
145
|
6 |
|
gr = greenlet.getcurrent() |
146
|
6 |
|
parent = gr.parent |
147
|
|
|
|
148
|
6 |
|
def response_cb(err, rv): |
149
|
6 |
|
debug('response is available for greenlet %s, switching back', gr) |
150
|
6 |
|
gr.switch(err, rv) |
151
|
|
|
|
152
|
6 |
|
self._async_session.request(method, args, response_cb) |
153
|
6 |
|
debug('yielding from greenlet %s to wait for response', gr) |
154
|
6 |
|
return parent.switch() |
155
|
|
|
|
156
|
6 |
|
def _blocking_request(self, method, args): |
157
|
6 |
|
result = [] |
158
|
|
|
|
159
|
6 |
|
def response_cb(err, rv): |
160
|
6 |
|
result.extend([err, rv]) |
161
|
6 |
|
self.stop() |
162
|
|
|
|
163
|
6 |
|
self._async_session.request(method, args, response_cb) |
164
|
6 |
|
self._async_session.run(self._enqueue_request, |
165
|
|
|
self._enqueue_notification) |
166
|
6 |
|
return result |
167
|
|
|
|
168
|
6 |
|
def _enqueue_request_and_stop(self, name, args, response): |
169
|
|
|
self._enqueue_request(name, args, response) |
170
|
|
|
self.stop() |
171
|
|
|
|
172
|
6 |
|
def _enqueue_notification_and_stop(self, name, args): |
173
|
6 |
|
self._enqueue_notification(name, args) |
174
|
6 |
|
self.stop() |
175
|
|
|
|
176
|
6 |
|
def _enqueue_request(self, name, args, response): |
177
|
|
|
self._pending_messages.append(('request', name, args, response,)) |
178
|
|
|
|
179
|
6 |
|
def _enqueue_notification(self, name, args): |
180
|
6 |
|
self._pending_messages.append(('notification', name, args,)) |
181
|
|
|
|
182
|
6 |
|
def _on_request(self, name, args, response): |
183
|
6 |
|
def handler(): |
184
|
6 |
|
try: |
185
|
6 |
|
rv = self._request_cb(name, args) |
186
|
6 |
|
debug('greenlet %s finished executing, ' + |
187
|
|
|
'sending %s as response', gr, rv) |
188
|
6 |
|
response.send(rv) |
189
|
|
|
except ErrorResponse as err: |
190
|
|
|
warn("error response from request '%s %s': %s", name, |
191
|
|
|
args, format_exc()) |
192
|
|
|
response.send(err.args[0], error=True) |
193
|
|
|
except Exception as err: |
|
|
|
|
194
|
|
|
warn("error caught while processing request '%s %s': %s", name, |
195
|
|
|
args, format_exc()) |
196
|
|
|
response.send(repr(err) + "\n" + format_exc(5), error=True) |
197
|
6 |
|
debug('greenlet %s is now dying...', gr) |
198
|
|
|
|
199
|
|
|
# Create a new greenlet to handle the request |
200
|
6 |
|
gr = greenlet.greenlet(handler) |
201
|
6 |
|
debug('received rpc request, greenlet %s will handle it', gr) |
202
|
6 |
|
gr.switch() |
203
|
|
|
|
204
|
6 |
|
def _on_notification(self, name, args): |
205
|
|
|
def handler(): |
206
|
|
|
try: |
207
|
|
|
self._notification_cb(name, args) |
208
|
|
|
debug('greenlet %s finished executing', gr) |
209
|
|
|
except Exception: |
|
|
|
|
210
|
|
|
warn("error caught while processing notification '%s %s': %s", |
211
|
|
|
name, args, format_exc()) |
212
|
|
|
|
213
|
|
|
debug('greenlet %s is now dying...', gr) |
214
|
|
|
|
215
|
|
|
gr = greenlet.greenlet(handler) |
216
|
|
|
debug('received rpc notification, greenlet %s will handle it', gr) |
217
|
|
|
gr.switch() |
218
|
|
|
|
219
|
|
|
|
220
|
6 |
|
class ErrorResponse(BaseException): |
221
|
|
|
|
222
|
|
|
"""Raise this in a request handler to respond with a given error message. |
223
|
|
|
|
224
|
|
|
Unlike when other exceptions are caught, this gives full control off the |
225
|
|
|
error response sent. When "ErrorResponse(msg)" is caught "msg" will be |
226
|
|
|
sent verbatim as the error response.No traceback will be appended. |
227
|
|
|
""" |
228
|
|
|
|
229
|
|
|
pass |
230
|
|
|
|
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.
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.