Skip to content

Commit f4a4675

Browse files
committedNov 28, 2019
Upgrade to prompt_toolkit 3.0.
1 parent 3f66e62 commit f4a4675

File tree

8 files changed

+38
-39
lines changed

8 files changed

+38
-39
lines changed
 

‎pymux/arrangement.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from __future__ import unicode_literals
1111

1212
from ptterm import Terminal
13-
from prompt_toolkit.application.current import get_app, set_app
13+
from prompt_toolkit.application.current import get_app, get_app_or_none, set_app
1414
from prompt_toolkit.buffer import Buffer
1515

1616
import math
@@ -647,7 +647,7 @@ def create_window(self, pane, name=None, set_active=True):
647647
# Sort windows by index.
648648
self.windows = sorted(self.windows, key=lambda w: w.index)
649649

650-
app = get_app(return_none=True)
650+
app = get_app_or_none()
651651

652652
if app is not None and set_active:
653653
self.set_active_window(w)

‎pymux/client/posix.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import unicode_literals
22

3-
from prompt_toolkit.eventloop.select import select_fds
3+
from select import select
44
from prompt_toolkit.input.posix_utils import PosixStdinReader
55
from prompt_toolkit.input.vt100 import raw_mode, cooked_mode
66
from prompt_toolkit.output.vt100 import _get_size, Vt100_Output
@@ -88,7 +88,7 @@ def winch_handler(signum, frame):
8888

8989
signal.signal(signal.SIGWINCH, winch_handler)
9090
while True:
91-
r = select_fds([stdin_fd, socket_fd], current_timeout)
91+
r, _, _ = select([stdin_fd, socket_fd], [], [], current_timeout)
9292

9393
if socket_fd in r:
9494
# Received packet from server.
@@ -120,7 +120,7 @@ def winch_handler(signum, frame):
120120
else:
121121
# Timeout. (Tell the server to flush the vt100 Escape.)
122122
self._send_packet({'cmd': 'flush-input'})
123-
current_timeout = None
123+
current_timeout = 0
124124
finally:
125125
signal.signal(signal.SIGWINCH, signal.SIG_IGN)
126126

‎pymux/client/windows.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from __future__ import unicode_literals
22

3+
from asyncio import ensure_future, get_event_loop
34
from ctypes import byref, windll
45
from ctypes.wintypes import DWORD
5-
from prompt_toolkit.eventloop import ensure_future, From
6-
from prompt_toolkit.eventloop import get_event_loop
76
from prompt_toolkit.input.win32 import Win32Input
87
from prompt_toolkit.output import ColorDepth
98
from prompt_toolkit.output.win32 import Win32Output
@@ -49,12 +48,12 @@ def attach(self, detach_other_clients=False, color_depth=ColorDepth.DEPTH_8_BIT)
4948
# Run as long as we have a connection with the server.
5049
get_event_loop().run_until_complete(f) # Run forever.
5150

52-
def _start_reader(self):
51+
async def _start_reader(self):
5352
"""
5453
Read messages from the Win32 pipe server and handle them.
5554
"""
5655
while True:
57-
message = yield From(self.pipe.read_message())
56+
message = await self.pipe.read_message()
5857
self._process(message)
5958

6059
def _process(self, data_buffer):

‎pymux/main.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
from __future__ import unicode_literals
22

3+
from asyncio import Future, get_event_loop
4+
35
from prompt_toolkit.application import Application
46
from prompt_toolkit.application.current import get_app, set_app
57
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
68
from prompt_toolkit.buffer import Buffer
9+
from prompt_toolkit.data_structures import Size
710
from prompt_toolkit.enums import EditingMode
8-
from prompt_toolkit.eventloop import Future
9-
from prompt_toolkit.eventloop import get_event_loop
10-
from prompt_toolkit.eventloop.context import context
1111
from prompt_toolkit.filters import Condition
1212
from prompt_toolkit.input.defaults import create_input
1313
from prompt_toolkit.key_binding.vi_state import InputMode
1414
from prompt_toolkit.layout.layout import Layout
15-
from prompt_toolkit.layout.screen import Size
1615
from prompt_toolkit.output.defaults import create_output
1716
from prompt_toolkit.styles import ConditionalStyleTransformation, SwapLightAndDarkStyleTransformation
1817

@@ -31,6 +30,7 @@
3130
from .utils import get_default_shell
3231
from ptterm import Terminal
3332

33+
import contextvars
3434
import os
3535
import signal
3636
import six
@@ -572,8 +572,8 @@ def listen_on_socket(self, socket_name=None):
572572
def connection_cb(pipe_connection):
573573
# We have to create a new `context`, because this will be the scope for
574574
# a new prompt_toolkit.Application to become active.
575-
with context():
576-
connection = ServerConnection(self, pipe_connection)
575+
context = contextvars.copy_context()
576+
connection = context.run(lambda: ServerConnection(self, pipe_connection))
577577

578578
self.connections.append(connection)
579579

‎pymux/pipes/posix.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import socket
66
import tempfile
77

8-
from prompt_toolkit.eventloop import From, Return, Future, get_event_loop
8+
from asyncio import get_event_loop, Future
99

1010
from ..log import logger
1111
from .base import PipeConnection, BrokenPipeError
@@ -91,23 +91,22 @@ def __init__(self, socket):
9191
self._fd = socket.fileno()
9292
self._recv_buffer = b''
9393

94-
def read(self):
94+
async def read(self):
9595
r"""
9696
Coroutine that reads the next packet.
9797
(Packets are \0 separated.)
9898
"""
9999
# Read until we have a \0 in our buffer.
100100
while b'\0' not in self._recv_buffer:
101-
self._recv_buffer += yield From(_read_chunk_from_socket(self.socket))
101+
self._recv_buffer += await _read_chunk_from_socket(self.socket)
102102

103103
# Split on the first separator.
104104
pos = self._recv_buffer.index(b'\0')
105105

106106
packet = self._recv_buffer[:pos]
107107
self._recv_buffer = self._recv_buffer[pos + 1:]
108108

109-
raise Return(packet)
110-
109+
return packet
111110

112111
def write(self, message):
113112
"""
@@ -119,7 +118,9 @@ def write(self, message):
119118
if not self._closed:
120119
raise BrokenPipeError
121120

122-
return Future.succeed(None)
121+
f = Future()
122+
f.set_result(None)
123+
return f
123124

124125
def close(self):
125126
"""

‎pymux/pipes/win32_server.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import unicode_literals
2+
from asyncio import ensure_future, Future
23
from ctypes import windll, byref
34
from ctypes.wintypes import DWORD
4-
from prompt_toolkit.eventloop import From, Future, Return, ensure_future
55
from ptterm.backends.win32_pipes import OVERLAPPED
66

77
from .win32 import wait_for_event, create_event, read_message_from_pipe, write_message_to_pipe
@@ -63,7 +63,7 @@ def __init__(self, pipe_instance):
6363
self.pipe_instance = pipe_instance
6464
self.done_f = Future()
6565

66-
def read(self):
66+
async def read(self):
6767
"""
6868
(coroutine)
6969
Read a single message from the pipe. (Return as text.)
@@ -72,13 +72,13 @@ def read(self):
7272
raise BrokenPipeError
7373

7474
try:
75-
result = yield From(read_message_from_pipe(self.pipe_instance.pipe_handle))
76-
raise Return(result)
75+
result = await read_message_from_pipe(self.pipe_instance.pipe_handle)
76+
return result
7777
except BrokenPipeError:
7878
self.done_f.set_result(None)
7979
raise
8080

81-
def write(self, message):
81+
async def write(self, message):
8282
"""
8383
(coroutine)
8484
Write a single message into the pipe.
@@ -87,7 +87,7 @@ def write(self, message):
8787
raise BrokenPipeError
8888

8989
try:
90-
yield From(write_message_to_pipe(self.pipe_instance.pipe_handle, message))
90+
await write_message_to_pipe(self.pipe_instance.pipe_handle, message)
9191
except BrokenPipeError:
9292
self.done_f.set_result(None)
9393
raise

‎pymux/server.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import unicode_literals
22
import json
33

4+
from asyncio import ensure_future
45
from prompt_toolkit.application.current import set_app
5-
from prompt_toolkit.eventloop import ensure_future, From
66
from prompt_toolkit.input.vt100_parser import Vt100Parser
7-
from prompt_toolkit.layout.screen import Size
7+
from prompt_toolkit.data_structures import Size
88
from prompt_toolkit.output.vt100 import Vt100_Output
99
from prompt_toolkit.utils import is_windows
1010

@@ -40,10 +40,10 @@ def feed_key(key):
4040

4141
ensure_future(self._start_reading())
4242

43-
def _start_reading(self):
43+
async def _start_reading(self):
4444
while True:
4545
try:
46-
data = yield From(self.pipe_connection.read())
46+
data = await self.pipe_connection.read()
4747
self._process(data)
4848
except BrokenPipeError:
4949
self.detach_and_close()
@@ -105,9 +105,9 @@ def _send_packet(self, data):
105105

106106
data = json.dumps(data)
107107

108-
def send():
108+
async def send():
109109
try:
110-
yield From(self.pipe_connection.write(data))
110+
await self.pipe_connection.write(data)
111111
except BrokenPipeError:
112112
self.detach_and_close()
113113
ensure_future(send())
@@ -147,15 +147,14 @@ def _create_app(self, color_depth, term='xterm'):
147147
self.client_state = self.pymux.add_client(
148148
input=self._pipeinput, output=output, connection=self, color_depth=color_depth)
149149

150-
print('Start running app...')
151-
future = self.client_state.app.run_async()
152-
print('Start running app got future...', future)
150+
async def run():
151+
print('Start running app...')
152+
future = await self.client_state.app.run_async()
153153

154-
@future.add_done_callback
155-
def done(_):
156154
print('APP DONE.........')
157155
print(future.result())
158156
self._close_connection()
157+
ensure_future(run())
159158

160159
def _close_connection(self):
161160
# This is important. If we would forget this, the server will

‎setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
long_description=long_description,
1818
packages=find_packages('.'),
1919
install_requires = [
20-
'prompt_toolkit>=2.0.0,<2.1.0',
20+
'prompt_toolkit>=3.0.0,<3.1.0',
2121
'ptterm',
2222
'six>=1.9.0',
2323
'docopt>=0.6.2',

0 commit comments

Comments
 (0)
Please sign in to comment.