Skip to content

Commit

Permalink
Merge pull request #149 from MattBroach/master
Browse files Browse the repository at this point in the history
making AioConnection.connect a coroutine and other small fixes
  • Loading branch information
jaraco authored Aug 1, 2018
2 parents c8b8bc8 + 98c5ca0 commit 4268620
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
9 changes: 8 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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
======
Expand Down
18 changes: 11 additions & 7 deletions irc/client_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

from .client import ServerConnection, ServerNotConnectedError, Reactor,\
SimpleIRCClient, Event, _ping_ponger
import jaraco.functools

log = logging.getLogger(__name__)

Expand All @@ -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):
"""
Expand All @@ -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
):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
)
2 changes: 1 addition & 1 deletion irc/tests/test_client_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions scripts/irccat-aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion scripts/irccat2-aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4268620

Please sign in to comment.