Make `Client.disconnect` async

This commit is contained in:
theunkn0wn1 2018-12-30 15:57:59 -08:00 committed by Joshua Salzedo
parent 0dcbf42c33
commit 781fd56d1d
2 changed files with 12 additions and 15 deletions

View File

@ -1,7 +1,8 @@
## client.py
# Basic IRC client implementation.
import asyncio
import logging
from asyncio import ensure_future, new_event_loop, gather, get_event_loop, sleep
from asyncio import new_event_loop, gather, get_event_loop, sleep
from . import connection, protocol
@ -35,7 +36,7 @@ class BasicClient:
RECONNECT_ON_ERROR = True
RECONNECT_MAX_ATTEMPTS = 3
RECONNECT_DELAYED = True
RECONNECT_DELAYS = [0, 5, 10, 30, 120, 600]
RECONNECT_DELAYS = [5, 5, 10, 30, 120, 600]
def __init__(self, nickname, fallback_nicknames=[], username=None, realname=None,
eventloop=None, **kwargs):
@ -98,7 +99,7 @@ class BasicClient:
# Disconnect from current connection.
if self.connected:
self.disconnect(expected=True)
await self.disconnect(expected=True)
# Reset attributes and connect.
if not reconnect:
@ -108,7 +109,6 @@ class BasicClient:
# Set logger name.
if self.server_tag:
self.logger = logging.getLogger(self.__class__.__name__ + ':' + self.server_tag)
ensure_future(self.handle_forever(), loop=self.eventloop)
def disconnect(self, expected=True):
""" Disconnect from server. """
@ -117,7 +117,7 @@ class BasicClient:
if self._ping_checker_handle:
self._ping_checker_handle.cancel()
# Schedule disconnect.
ensure_future(self._disconnect(expected), loop=self.eventloop)
await self._disconnect(expected)
async def _disconnect(self, expected):
# Shutdown connection.
@ -365,7 +365,7 @@ class BasicClient:
data = await self.connection.recv()
if not data:
if self.connected:
self.disconnect(expected=False)
await self.disconnect(expected=False)
break
await self.on_data(data)
@ -391,7 +391,7 @@ class BasicClient:
""" Handle error. """
self.logger.error('Encountered error on socket.',
exc_info=(type(exception), exception, None))
self.disconnect(expected=False)
await self.disconnect(expected=False)
async def on_raw(self, message):
""" Handle a single message. """

View File

@ -6,8 +6,7 @@ import ipaddress
import itertools
from pydle.client import BasicClient, NotInChannel, AlreadyInChannel
from . import parsing
from . import protocol
from . import parsing, protocol
class RFC1459Support(BasicClient):
@ -194,7 +193,6 @@ class RFC1459Support(BasicClient):
# And initiate the IRC connection.
await self._register()
async def _register(self):
""" Perform IRC connection registration. """
if self.registered:
@ -271,14 +269,13 @@ class RFC1459Support(BasicClient):
else:
await self.rawmsg('PART', channel)
async def kick(self, channel, target, reason=None):
""" Kick user from channel. """
if not self.in_channel(channel):
raise NotInChannel(channel)
if reason:
await self.rawmsg('KICK', channel, target, reason)
await self.rawmsg('KICK', channel, target, reason)
else:
await self.rawmsg('KICK', channel, target)
@ -325,7 +322,7 @@ class RFC1459Support(BasicClient):
message = self.DEFAULT_QUIT_MESSAGE
await self.rawmsg('QUIT', message)
self.disconnect(expected=True)
await self.disconnect(expected=True)
async def cycle(self, channel):
""" Rejoin channel. """
@ -613,7 +610,7 @@ class RFC1459Support(BasicClient):
await self.on_kill(target, by, reason)
if self.is_same_nick(self.nickname, target):
self.disconnect(expected=False)
await self.disconnect(expected=False)
else:
self._destroy_user(target)
@ -725,7 +722,7 @@ class RFC1459Support(BasicClient):
self._destroy_user(nick)
# Else, we quit.
elif self.connected:
self.disconnect(expected=True)
await self.disconnect(expected=True)
async def on_raw_topic(self, message):
""" TOPIC command. """