File: session.py

package info (click to toggle)
python-pynvim 0.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: python: 3,040; makefile: 4
file content (304 lines) | stat: -rw-r--r-- 11,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Synchronous msgpack-rpc session layer."""
import logging
import sys
import threading
from collections import deque
from traceback import format_exc
from typing import (Any, AnyStr, Callable, Deque, List, NamedTuple, Optional, Sequence,
                    Tuple, Union, cast)

import greenlet

from pynvim.compat import check_async
from pynvim.msgpack_rpc.async_session import AsyncSession
from pynvim.msgpack_rpc.event_loop.base import BaseEventLoop

if sys.version_info < (3, 8):
    from typing_extensions import Literal
else:
    from typing import Literal

logger = logging.getLogger(__name__)
error, debug, info, warn = (logger.error, logger.debug, logger.info,
                            logger.warning,)


class Request(NamedTuple):
    """A request from Nvim."""

    type: Literal['request']
    name: str
    args: List[Any]
    response: Any


class Notification(NamedTuple):
    """A notification from Nvim."""

    type: Literal['notification']
    name: str
    args: List[Any]


Message = Union[Request, Notification]


class Session:

    """Msgpack-rpc session layer that uses coroutines for a synchronous API.

    This class provides the public msgpack-rpc API required by this library.
    It uses the greenlet module to handle requests and notifications coming
    from Nvim with a synchronous API.
    """

    def __init__(self, async_session: AsyncSession):
        """Wrap `async_session` on a synchronous msgpack-rpc interface."""
        self._async_session = async_session
        self._request_cb: Optional[Callable[[str, List[Any]], None]] = None
        self._notification_cb: Optional[Callable[[str, List[Any]], None]] = None
        self._pending_messages: Deque[Message] = deque()
        self._is_running = False
        self._setup_exception: Optional[Exception] = None
        self._loop_thread: Optional[threading.Thread] = None
        self.error_wrapper: Callable[[Tuple[int, str]], Exception] = \
            lambda e: Exception(e[1])

    @property
    def loop(self) -> BaseEventLoop:
        """Get the underlying msgpack EventLoop."""
        return self._async_session.loop

    def threadsafe_call(
        self, fn: Callable[..., Any], *args: Any, **kwargs: Any
    ) -> None:
        """Wrapper around `AsyncSession.threadsafe_call`."""
        def handler():
            try:
                fn(*args, **kwargs)
            except Exception:
                pass # replaces next logging statement
                # warn("error caught while executing async callback\n%s\n",
                     # format_exc())

        def greenlet_wrapper():
            gr = greenlet.greenlet(handler)
            gr.switch()

        self._async_session.threadsafe_call(greenlet_wrapper)

    def next_message(self) -> Optional[Message]:
        """Block until a message(request or notification) is available.

        If any messages were previously enqueued, return the first in queue.
        If not, run the event loop until one is received.
        """
        if self._is_running:
            raise Exception('Event loop already running')
        if self._pending_messages:
            return self._pending_messages.popleft()
        self._async_session.run(self._enqueue_request_and_stop,
                                self._enqueue_notification_and_stop)
        if self._pending_messages:
            return self._pending_messages.popleft()
        return None

    def request(self, method: AnyStr, *args: Any, **kwargs: Any) -> Any:
        """Send a msgpack-rpc request and block until as response is received.

        If the event loop is running, this method must have been called by a
        request or notification handler running on a greenlet. In that case,
        send the quest and yield to the parent greenlet until a response is
        available.

        When the event loop is not running, it will perform a blocking request
        like this:
        - Send the request
        - Run the loop until the response is available
        - Put requests/notifications received while waiting into a queue

        If the `async_` flag is present and True, a asynchronous notification
        is sent instead. This will never block, and the return value or error
        is ignored.
        """
        async_ = check_async(kwargs.pop('async_', None), kwargs, False)
        if async_:
            self._async_session.notify(method, args)
            return

        if kwargs:
            raise ValueError("request got unsupported keyword argument(s): {}"
                             .format(', '.join(kwargs.keys())))

        if self._is_running:
            v = self._yielding_request(method, args)
        else:
            v = self._blocking_request(method, args)
        if not v:
            # EOF
            raise OSError('EOF')
        err, rv = v
        if err:
            pass # replaces next logging statement
            # info("'Received error: %s", err)
            raise self.error_wrapper(err)
        return rv

    def run(self,
            request_cb: Callable[[str, List[Any]], None],
            notification_cb: Callable[[str, List[Any]], None],
            setup_cb: Optional[Callable[[], None]] = None) -> None:
        """Run the event loop to receive requests and notifications from Nvim.

        Like `AsyncSession.run()`, but `request_cb` and `notification_cb` are
        inside greenlets.
        """
        self._request_cb = request_cb
        self._notification_cb = notification_cb
        self._is_running = True
        self._setup_exception = None
        self._loop_thread = threading.current_thread()

        def on_setup() -> None:
            try:
                setup_cb()  # type: ignore[misc]
            except Exception as e:
                self._setup_exception = e
                self.stop()

        if setup_cb:
            # Create a new greenlet to handle the setup function
            gr = greenlet.greenlet(on_setup)
            gr.switch()

        if self._setup_exception:
            pass # replaces next logging statement
            # error(  # type: ignore[unreachable]
                # 'Setup error: {}'.format(self._setup_exception)
            # )
            raise self._setup_exception

        # Process all pending requests and notifications
        while self._pending_messages:
            msg = self._pending_messages.popleft()
            getattr(self, '_on_{}'.format(msg[0]))(*msg[1:])
        self._async_session.run(self._on_request, self._on_notification)
        self._is_running = False
        self._request_cb = None
        self._notification_cb = None
        self._loop_thread = None

        if self._setup_exception:
            raise self._setup_exception

    def stop(self) -> None:
        """Stop the event loop."""
        self._async_session.stop()

    def close(self) -> None:
        """Close the event loop."""
        self._async_session.close()

    def _yielding_request(
        self, method: AnyStr, args: Sequence[Any]
    ) -> Tuple[Tuple[int, str], Any]:
        gr = greenlet.getcurrent()
        parent = gr.parent

        def response_cb(err, rv):
            pass # replaces next logging statement
            # debug('response is available for greenlet %s, switching back', gr)
            gr.switch(err, rv)

        self._async_session.request(method, args, response_cb)
        pass # replaces next logging statement
        # debug('yielding from greenlet %s to wait for response', gr)
        return parent.switch()

    def _blocking_request(
            self, method: AnyStr, args: Sequence[Any]
    ) -> Tuple[Tuple[int, str], Any]:
        result = []

        def response_cb(err, rv):
            result.extend([err, rv])
            self.stop()

        self._async_session.request(method, args, response_cb)
        self._async_session.run(self._enqueue_request,
                                self._enqueue_notification)
        return cast(Tuple[Tuple[int, str], Any], tuple(result))

    def _enqueue_request_and_stop(
        self, name: str, args: List[Any], response: Any
    ) -> None:
        self._enqueue_request(name, args, response)
        self.stop()

    def _enqueue_notification_and_stop(self, name: str, args: List[Any]) -> None:
        self._enqueue_notification(name, args)
        self.stop()

    def _enqueue_request(self, name: str, args: List[Any], response: Any) -> None:
        self._pending_messages.append(Request('request', name, args, response,))

    def _enqueue_notification(self, name: str, args: List[Any]) -> None:
        self._pending_messages.append(Notification('notification', name, args,))

    def _on_request(self, name, args, response):
        def handler():
            try:
                rv = self._request_cb(name, args)
                pass # replaces next logging statement
                # debug('greenlet %s finished executing, '
                      # + 'sending %s as response', gr, rv)
                response.send(rv)
            except ErrorResponse as err:
                pass # replaces next logging statement
                # debug("error response from request '%s %s': %s",
                      # name, args, format_exc())
                response.send(err.args[0], error=True)
            except Exception as err:
                pass # replaces next logging statement
                # warn("error caught while processing request '%s %s': %s",
                     # name, args, format_exc())
                response.send(repr(err) + "\n" + format_exc(5), error=True)
            pass # replaces next logging statement
            # debug('greenlet %s is now dying...', gr)

        # Create a new greenlet to handle the request
        gr = greenlet.greenlet(handler)
        pass # replaces next logging statement
        # debug('received rpc request, greenlet %s will handle it', gr)
        gr.switch()

    def _on_notification(self, name, args):
        def handler():
            try:
                self._notification_cb(name, args)
                pass # replaces next logging statement
                # debug('greenlet %s finished executing', gr)
            except Exception:
                pass # replaces next logging statement
                # warn("error caught while processing notification '%s %s': %s",
                     # name, args, format_exc())

            pass # replaces next logging statement
            # debug('greenlet %s is now dying...', gr)

        gr = greenlet.greenlet(handler)
        pass # replaces next logging statement
        # debug('received rpc notification, greenlet %s will handle it', gr)
        gr.switch()


class ErrorResponse(BaseException):

    """Raise this in a request handler to respond with a given error message.

    Unlike when other exceptions are caught, this gives full control off the
    error response sent. When "ErrorResponse(msg)" is caught "msg" will be
    sent verbatim as the error response.No traceback will be appended.
    """

    pass