Skip to content

Commit 720c664

Browse files
committed
wip
1 parent 9908ada commit 720c664

File tree

7 files changed

+32
-36
lines changed

7 files changed

+32
-36
lines changed

irc3/__init__.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ def __init__(self, *ini, **config):
128128
self.log.fatal('realname = %(userinfo)s', self.config)
129129
import sys
130130
sys.exit(-1)
131-
self.queue = None
132-
if self.config.asynchronous:
133-
self.queue = Queue(loop=self.loop)
134-
self.awaiting_queue = self.create_task(self.process_queue())
131+
self.queue = Queue(loop=self.loop)
132+
self.awaiting_queue = self.create_task(self.process_queue())
135133
self._ip = self._dcc = None
136134
# auto include the sasl plugin if needed
137135
if 'sasl_username' in self.config and \
@@ -373,29 +371,25 @@ def dcc(self):
373371
self._dcc = DCCManager(self)
374372
return self._dcc
375373

376-
@asyncio.coroutine
377-
def dcc_chat(self, mask, host=None, port=None):
374+
async def dcc_chat(self, mask, host=None, port=None):
378375
"""Open a DCC CHAT whith mask. If host/port are specified then connect
379376
to a server. Else create a server"""
380377
return self.dcc.create(
381378
'chat', mask, host=host, port=port).ready
382379

383-
@asyncio.coroutine
384-
def dcc_get(self, mask, host, port, filepath, filesize=None):
380+
async def dcc_get(self, mask, host, port, filepath, filesize=None):
385381
"""DCC GET a file from mask. filepath must be an absolute path with an
386382
existing directory. filesize is the expected file size."""
387383
return self.dcc.create(
388384
'get', mask, filepath=filepath, filesize=filesize,
389385
host=host, port=port).ready
390386

391-
@asyncio.coroutine
392-
def dcc_send(self, mask, filepath):
387+
async def dcc_send(self, mask, filepath):
393388
"""DCC SEND a file to mask. filepath must be an absolute path to
394389
existing file"""
395390
return self.dcc.create('send', mask, filepath=filepath).ready
396391

397-
@asyncio.coroutine
398-
def dcc_accept(self, mask, filepath, port, pos):
392+
async def dcc_accept(self, mask, filepath, port, pos):
399393
"""accept a DCC RESUME for an axisting DCC SEND. filepath is the
400394
filename to sent. port is the port opened on the server.
401395
pos is the expected offset"""
@@ -420,6 +414,11 @@ def cancel(self):
420414
except asyncio.CancelledError:
421415
pass
422416

417+
def close(self):
418+
self.cancel()
419+
super().close()
420+
421+
423422
def run(argv=None):
424423
bots = {}
425424
bot = IrcBot.from_argv(argv, botnet=bots)

irc3/base.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ class IrcObject:
7171
port=6667,
7272
timeout=320,
7373
max_lag=60,
74-
asynchronous=True,
7574
max_length=512,
7675
testing=False,
7776
ssl=False,
@@ -100,11 +99,12 @@ def __init__(self, *ini, **config):
10099
self.encoding = self.config['encoding']
101100

102101
self.loop = self.config.loop
102+
self._own_loop = None
103103
if self.loop is None:
104104
try:
105105
self.loop = asyncio.get_event_loop()
106106
except RuntimeError:
107-
self.loop = asyncio.new_event_loop()
107+
self.loop = self._own_loop = asyncio.new_event_loop()
108108
asyncio.set_event_loop(self.loop)
109109

110110
# python 3.4.1 do not have a create_task method. check for it
@@ -273,7 +273,7 @@ def notify(self, event, exc=None, client=None):
273273
def dispatch(self, data, iotype='in', client=None):
274274
str = utils.IrcString
275275
create_task = self.create_task
276-
call_soon = self.loop.call_soon
276+
tasks = []
277277
for match, events in self.registry.get_event_matches(data, iotype):
278278
match = match.groupdict()
279279
for key, value in match.items():
@@ -288,9 +288,10 @@ def dispatch(self, data, iotype='in', client=None):
288288
match['client'] = client
289289
for e in events:
290290
if e.iscoroutine is True:
291-
create_task(e.callback(**match))
291+
tasks.append(create_task(e.callback(**match)))
292292
else:
293-
call_soon(e.async_callback, match)
293+
tasks.append(create_task(e.async_callback(match)))
294+
return tasks
294295

295296
def call_many(self, callback, args):
296297
"""callback is run with each arg but run a call per second"""
@@ -460,3 +461,8 @@ def from_argv(cls, argv=None, **kwargs):
460461
context.run(forever=not bool(kwargs))
461462
if kwargs or argv:
462463
return context
464+
465+
def close(self):
466+
if self._own_loop is not None:
467+
self._own_loop.stop()
468+
self._own_loop.run_forever()

irc3/dec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, regexp, callback=None, iotype='in',
5454
if callback is not None:
5555
self.iscoroutine = asyncio.iscoroutinefunction(callback)
5656

57-
def async_callback(self, kwargs): # pragma: no cover
57+
async def async_callback(self, kwargs): # pragma: no cover
5858
return self.callback(**kwargs)
5959

6060
def compile(self, config):
@@ -103,7 +103,7 @@ def extend(func):
103103
>>> import sys
104104
>>> sys.path.append('examples')
105105
>>> from irc3 import IrcBot
106-
>>> IrcBot.defaults.update(asynchronous=False, testing=True)
106+
>>> IrcBot.defaults.update(testing=True)
107107
108108
Now you can use those routine in your bot::
109109

irc3/plugins/asynchronious.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def do_whois(self):
6666

6767
class Whois(AsyncEvents):
6868

69-
# the command will fail if we do not have a result after 30s
69+
# the command will fail if we do not have a result after 20s
7070
timeout = 20
7171

7272
# send this line before listening to events

irc3/plugins/command.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
PRIVMSG #chan :!echo <words>...
6767
>>> bot.test(':gawel!user@host PRIVMSG #chan :!help nonexistant')
6868
PRIVMSG #chan :No such command. Try !help for an overview of all commands.
69+
>>> bot.close()
70+
6971
7072
Guard
7173
=====

irc3/testing.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import asyncio
23
from unittest import TestCase
34
import irc3
45
import irc3d
@@ -55,17 +56,7 @@ class IrcBot(irc3.IrcBot):
5556

5657
def __init__(self, **config):
5758
self.check_required()
58-
self._own_loop = None
59-
if 'loop' not in config:
60-
loop = asyncio.new_event_loop()
61-
loop = mock.create_autospec(loop, spec_set=True)
62-
loop.call_later = call_later
63-
loop.call_soon = call_soon
64-
loop.time.return_value = 10
65-
config.update(testing=True, asynchronous=False, level=1000,
66-
loop=loop)
67-
else:
68-
config.update(testing=True, level=1000)
59+
config.update(testing=True, level=1000)
6960
super(IrcBot, self).__init__(**config)
7061
self.protocol = irc3.IrcConnection()
7162
self.protocol.closed = False
@@ -83,7 +74,7 @@ def check_required(self): # pragma: no cover
8374
fd.write(passwd_ini)
8475

8576
def test(self, data, show=True):
86-
self.dispatch(data)
77+
self.loop.run_until_complete(asyncio.wait(self.dispatch(data)))
8778
if show:
8879
for line in self.sent: # pragma: no cover
8980
print(line)
@@ -189,8 +180,7 @@ def __init__(self, **config):
189180
loop.call_soon = call_soon
190181
loop.time = MagicMock()
191182
loop.time.return_value = 10
192-
config.update(testing=True, asynchronous=False, level=1000,
193-
loop=loop)
183+
config.update(testing=True, level=1000, loop=loop)
194184
super(IrcServer, self).__init__(**config)
195185
print(self.clients)
196186

tests/test_commands.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ def test_reconnect_command(self):
201201

202202
@unittest.skip('')
203203
def test_antiflood(self):
204-
bot = self.callFTU(asynchronous=True,
205-
**{self.name: dict(antiflood=True)})
204+
bot = self.callFTU(**{self.name: dict(antiflood=True)})
206205
bot.dispatch(':bar!user@host PRIVMSG #chan :!help ping')
207206
self.assertSent(['PRIVMSG #chan :ping/pong', 'PRIVMSG #chan :!ping'])
208207

0 commit comments

Comments
 (0)