From b747ce1bd1e46038bc5e4c3eff1a64175f0908dd Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sat, 21 Nov 2020 20:45:44 -0800 Subject: [PATCH] [#151] Fixup irccat.py example --- pydle/utils/irccat.py | 50 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/pydle/utils/irccat.py b/pydle/utils/irccat.py index 47a857c..495018d 100644 --- a/pydle/utils/irccat.py +++ b/pydle/utils/irccat.py @@ -8,57 +8,59 @@ import logging import asyncio from asyncio.streams import FlowControlMixin -from .. import Client, __version__ +from .. import Client, __version__ from . import _args import asyncio + class IRCCat(Client): """ irccat. Takes raw messages on stdin, dumps raw messages to stdout. Life has never been easier. """ + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.async_stdin = None - @asyncio.coroutine - def _send(self, data): - sys.stdout.write(data) - yield from super()._send(data) + async def _send(self, data): + await super(IRCCat, self)._send(data) - @asyncio.coroutine - def process_stdin(self): + async def process_stdin(self): """ Yes. """ - loop = self.eventloop.loop + loop = asyncio.get_event_loop() self.async_stdin = asyncio.StreamReader() reader_protocol = asyncio.StreamReaderProtocol(self.async_stdin) - yield from loop.connect_read_pipe(lambda: reader_protocol, sys.stdin) + await loop.connect_read_pipe(lambda: reader_protocol, sys.stdin) while True: - line = yield from self.async_stdin.readline() + line = await self.async_stdin.readline() if not line: break - yield from self.raw(line.decode('utf-8')) + await self.raw(line.decode('utf-8')) - yield from self.quit('EOF') + await self.quit('EOF') - @asyncio.coroutine - def on_raw(self, message): + async def on_raw(self, message): print(message._raw) - yield from super().on_raw(message) + await super().on_raw(message) - @asyncio.coroutine - def on_ctcp_version(self, source, target, contents): - self.ctcp_reply(source, 'VERSION', 'pydle-irccat v{}'.format(__version__)) + async def on_ctcp_version(self, source, target, contents): + await self.ctcp_reply(source, 'VERSION', 'pydle-irccat v{}'.format(__version__)) + + +async def _main(): + # Create client. + irccat, connect = _args.client_from_args('irccat', default_nick='irccat', + description='Process raw IRC messages from stdin, dump received IRC messages to stdout.', + cls=IRCCat) + await connect() + while True: + await irccat.process_stdin() def main(): # Setup logging. logging.basicConfig(format='!! %(levelname)s: %(message)s') - - # Create client. - irccat, connect = _args.client_from_args('irccat', default_nick='irccat', description='Process raw IRC messages from stdin, dump received IRC messages to stdout.', cls=IRCCat) - - irccat.eventloop.schedule_async(connect()) - irccat.eventloop.run_with(irccat.process_stdin()) + asyncio.get_event_loop().run_until_complete(_main()) if __name__ == '__main__':