Skip to content

Commit

Permalink
Upgrade to prompt_toolkit 3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanslenders committed Nov 28, 2019
1 parent 3f66e62 commit 0857332
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 39 deletions.
4 changes: 2 additions & 2 deletions pymux/arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from __future__ import unicode_literals

from ptterm import Terminal
from prompt_toolkit.application.current import get_app, set_app
from prompt_toolkit.application.current import get_app, get_app_or_none, set_app
from prompt_toolkit.buffer import Buffer

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

app = get_app(return_none=True)
app = get_app_or_none()

if app is not None and set_active:
self.set_active_window(w)
Expand Down
6 changes: 3 additions & 3 deletions pymux/client/posix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import unicode_literals

from prompt_toolkit.eventloop.select import select_fds
from select import select
from prompt_toolkit.input.posix_utils import PosixStdinReader
from prompt_toolkit.input.vt100 import raw_mode, cooked_mode
from prompt_toolkit.output.vt100 import _get_size, Vt100_Output
Expand Down Expand Up @@ -88,7 +88,7 @@ def winch_handler(signum, frame):

signal.signal(signal.SIGWINCH, winch_handler)
while True:
r = select_fds([stdin_fd, socket_fd], current_timeout)
r, _, _ = select([stdin_fd, socket_fd], [], [], current_timeout)

if socket_fd in r:
# Received packet from server.
Expand Down Expand Up @@ -120,7 +120,7 @@ def winch_handler(signum, frame):
else:
# Timeout. (Tell the server to flush the vt100 Escape.)
self._send_packet({'cmd': 'flush-input'})
current_timeout = None
current_timeout = 0
finally:
signal.signal(signal.SIGWINCH, signal.SIG_IGN)

Expand Down
7 changes: 3 additions & 4 deletions pymux/client/windows.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import unicode_literals

from asyncio import ensure_future, get_event_loop
from ctypes import byref, windll
from ctypes.wintypes import DWORD
from prompt_toolkit.eventloop import ensure_future, From
from prompt_toolkit.eventloop import get_event_loop
from prompt_toolkit.input.win32 import Win32Input
from prompt_toolkit.output import ColorDepth
from prompt_toolkit.output.win32 import Win32Output
Expand Down Expand Up @@ -49,12 +48,12 @@ def attach(self, detach_other_clients=False, color_depth=ColorDepth.DEPTH_8_BIT)
# Run as long as we have a connection with the server.
get_event_loop().run_until_complete(f) # Run forever.

def _start_reader(self):
async def _start_reader(self):
"""
Read messages from the Win32 pipe server and handle them.
"""
while True:
message = yield From(self.pipe.read_message())
message = await self.pipe.read_message()
self._process(message)

def _process(self, data_buffer):
Expand Down
12 changes: 6 additions & 6 deletions pymux/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from __future__ import unicode_literals

from asyncio import Future, get_event_loop

from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app, set_app
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.data_structures import Size
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.eventloop import Future
from prompt_toolkit.eventloop import get_event_loop
from prompt_toolkit.eventloop.context import context
from prompt_toolkit.filters import Condition
from prompt_toolkit.input.defaults import create_input
from prompt_toolkit.key_binding.vi_state import InputMode
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout.screen import Size
from prompt_toolkit.output.defaults import create_output
from prompt_toolkit.styles import ConditionalStyleTransformation, SwapLightAndDarkStyleTransformation

Expand All @@ -31,6 +30,7 @@
from .utils import get_default_shell
from ptterm import Terminal

import contextvars
import os
import signal
import six
Expand Down Expand Up @@ -572,8 +572,8 @@ def listen_on_socket(self, socket_name=None):
def connection_cb(pipe_connection):
# We have to create a new `context`, because this will be the scope for
# a new prompt_toolkit.Application to become active.
with context():
connection = ServerConnection(self, pipe_connection)
context = contextvars.copy_context()
connection = context.run(lambda: ServerConnection(self, pipe_connection))

self.connections.append(connection)

Expand Down
13 changes: 7 additions & 6 deletions pymux/pipes/posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import socket
import tempfile

from prompt_toolkit.eventloop import From, Return, Future, get_event_loop
from asyncio import get_event_loop, Future

from ..log import logger
from .base import PipeConnection, BrokenPipeError
Expand Down Expand Up @@ -91,23 +91,22 @@ def __init__(self, socket):
self._fd = socket.fileno()
self._recv_buffer = b''

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

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

packet = self._recv_buffer[:pos]
self._recv_buffer = self._recv_buffer[pos + 1:]

raise Return(packet)

return packet

def write(self, message):
"""
Expand All @@ -119,7 +118,9 @@ def write(self, message):
if not self._closed:
raise BrokenPipeError

return Future.succeed(None)
f = Future()
f.set_result(None)
return f

def close(self):
"""
Expand Down
12 changes: 6 additions & 6 deletions pymux/pipes/win32_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals
from asyncio import ensure_future, Future
from ctypes import windll, byref
from ctypes.wintypes import DWORD
from prompt_toolkit.eventloop import From, Future, Return, ensure_future
from ptterm.backends.win32_pipes import OVERLAPPED

from .win32 import wait_for_event, create_event, read_message_from_pipe, write_message_to_pipe
Expand Down Expand Up @@ -63,7 +63,7 @@ def __init__(self, pipe_instance):
self.pipe_instance = pipe_instance
self.done_f = Future()

def read(self):
async def read(self):
"""
(coroutine)
Read a single message from the pipe. (Return as text.)
Expand All @@ -72,13 +72,13 @@ def read(self):
raise BrokenPipeError

try:
result = yield From(read_message_from_pipe(self.pipe_instance.pipe_handle))
raise Return(result)
result = await read_message_from_pipe(self.pipe_instance.pipe_handle)
return result
except BrokenPipeError:
self.done_f.set_result(None)
raise

def write(self, message):
async def write(self, message):
"""
(coroutine)
Write a single message into the pipe.
Expand All @@ -87,7 +87,7 @@ def write(self, message):
raise BrokenPipeError

try:
yield From(write_message_to_pipe(self.pipe_instance.pipe_handle, message))
await write_message_to_pipe(self.pipe_instance.pipe_handle, message)
except BrokenPipeError:
self.done_f.set_result(None)
raise
Expand Down
21 changes: 10 additions & 11 deletions pymux/server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import unicode_literals
import json

from asyncio import ensure_future
from prompt_toolkit.application.current import set_app
from prompt_toolkit.eventloop import ensure_future, From
from prompt_toolkit.input.vt100_parser import Vt100Parser
from prompt_toolkit.layout.screen import Size
from prompt_toolkit.data_structures import Size
from prompt_toolkit.output.vt100 import Vt100_Output
from prompt_toolkit.utils import is_windows

Expand Down Expand Up @@ -40,10 +40,10 @@ def feed_key(key):

ensure_future(self._start_reading())

def _start_reading(self):
async def _start_reading(self):
while True:
try:
data = yield From(self.pipe_connection.read())
data = await self.pipe_connection.read()
self._process(data)
except BrokenPipeError:
self.detach_and_close()
Expand Down Expand Up @@ -105,9 +105,9 @@ def _send_packet(self, data):

data = json.dumps(data)

def send():
async def send():
try:
yield From(self.pipe_connection.write(data))
await self.pipe_connection.write(data)
except BrokenPipeError:
self.detach_and_close()
ensure_future(send())
Expand Down Expand Up @@ -147,15 +147,14 @@ def _create_app(self, color_depth, term='xterm'):
self.client_state = self.pymux.add_client(
input=self._pipeinput, output=output, connection=self, color_depth=color_depth)

print('Start running app...')
future = self.client_state.app.run_async()
print('Start running app got future...', future)
async def run():
print('Start running app...')
future = await self.client_state.app.run_async()

@future.add_done_callback
def done(_):
print('APP DONE.........')
print(future.result())
self._close_connection()
ensure_future(run())

def _close_connection(self):
# This is important. If we would forget this, the server will
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description=long_description,
packages=find_packages('.'),
install_requires = [
'prompt_toolkit>=2.0.0,<2.1.0',
'prompt_toolkit>=2.0.0,<3.1.0',
'ptterm',
'six>=1.9.0',
'docopt>=0.6.2',
Expand Down

0 comments on commit 0857332

Please sign in to comment.