From b4b58c467f374d40fdaa4799f55fe32b68884412 Mon Sep 17 00:00:00 2001 From: Matt Broach Date: Fri, 20 Jul 2018 17:43:12 -0400 Subject: [PATCH 1/2] making AioConnection.connect a coroutine and other small fixes --- irc/client_aio.py | 18 +++++++++++------- irc/tests/test_client_aio.py | 2 +- scripts/irccat-aio.py | 5 ++--- scripts/irccat2-aio.py | 1 - 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/irc/client_aio.py b/irc/client_aio.py index 2d342b9..7504ad2 100644 --- a/irc/client_aio.py +++ b/irc/client_aio.py @@ -45,7 +45,6 @@ from .client import ServerConnection, ServerNotConnectedError, Reactor,\ SimpleIRCClient, Event, _ping_ponger -import jaraco.functools log = logging.getLogger(__name__) @@ -70,6 +69,10 @@ def __init__(self, connection, loop): def data_received(self, data): self.connection.process_data(data) + def connection_lost(self, exc): + log.debug("connection lost: {}".format(exc)) + self.connection.disconnect() + class AioConnection(ServerConnection): """ @@ -95,8 +98,7 @@ class AioConnection(ServerConnection): """ protocol_class = IrcProtocol - @jaraco.functools.save_method_args - def connect( + async def connect( self, server, port, nickname, password=None, username=None, ircname=None ): @@ -137,9 +139,7 @@ def connect( self.server, self.port, ) - print('HELLO') - print(connection) - transport, protocol = self.reactor.loop.run_until_complete(connection) + transport, protocol = await connection self.transport = transport self.protocol = protocol @@ -164,7 +164,6 @@ def process_data(self, new_data): # process each non-empty line after logging all lines for line in self.buffer: - print(line) log.debug("FROM SERVER: %s", line) if not line: continue @@ -275,3 +274,8 @@ class AioSimpleIRCClient(SimpleIRCClient): on irc.client.SimpleIRCClient """ reactor_class = AioReactor + + def connect(self, *args, **kwargs): + self.reactor.loop.run_until_complete( + self.connection.connect(*args, **kwargs) + ) diff --git a/irc/tests/test_client_aio.py b/irc/tests/test_client_aio.py index 3ef08df..9b7e4ed 100644 --- a/irc/tests/test_client_aio.py +++ b/irc/tests/test_client_aio.py @@ -18,7 +18,7 @@ def test_privmsg_sends_msg(create_connection_mock): # connect to dummy server loop = asyncio.get_event_loop() server = client_aio.AioReactor(loop=loop).server() - server.connect('foo', 6667, 'my_irc_nick') + loop.run_until_complete(server.connect('foo', 6667, 'my_irc_nick')) server.privmsg('#best-channel', 'You are great') mock_transport.write.assert_called_with( diff --git a/scripts/irccat-aio.py b/scripts/irccat-aio.py index 0268bf1..49a4f69 100644 --- a/scripts/irccat-aio.py +++ b/scripts/irccat-aio.py @@ -36,7 +36,6 @@ def get_lines(): async def main_loop(connection): for line in itertools.takewhile(bool, get_lines()): - print(line) connection.privmsg(target, line) # Allow pause in the stdin loop to not block the asyncio event loop @@ -70,9 +69,9 @@ def main(): reactor = irc.client_aio.AioReactor(loop=loop) try: - c = reactor.server().connect( + c = loop.run_until_complete(reactor.server().connect( args.server, args.port, args.nickname, password=args.password - ) + )) except irc.client.ServerConnectionError: print(sys.exc_info()[1]) raise SystemExit(1) diff --git a/scripts/irccat2-aio.py b/scripts/irccat2-aio.py index a63e234..c593dc1 100644 --- a/scripts/irccat2-aio.py +++ b/scripts/irccat2-aio.py @@ -70,7 +70,6 @@ def main(): args.server, args.port, args.nickname, password=args.password ) except irc.client.ServerConnectionError as x: - print(x) sys.exit(1) try: From 98c5ca018f0531f98bd81de84f1d2c5b729cc217 Mon Sep 17 00:00:00 2001 From: Matt Broach Date: Wed, 1 Aug 2018 15:26:18 -0400 Subject: [PATCH 2/2] updating CHANGES.rst --- CHANGES.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 9826bba..57a0abe 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,8 +1,15 @@ +16.4 +==== + +* #149: ``AioConnection.connect`` moved to coroutine, added + disconnect handling for AsyncIO. + 16.3 ==== * #140: Methods now use 'connection' and 'event' for parameter names. -# #135 via #144: Added AsyncIO implementation. + +* #135 via #144: Added AsyncIO implementation. 16.2.1 ======