From 3502480384bd7d9f4e4eb1a3b92e8df08f4e487c Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 1 Jul 2018 18:46:33 +0200 Subject: Switch from @asyncio.coroutine to async def everywhere. --- slixmpp/plugins/xep_0047/stream.py | 15 ++++++-------- slixmpp/plugins/xep_0065/proxy.py | 29 ++++++++++++--------------- slixmpp/plugins/xep_0065/socks5.py | 4 ++-- slixmpp/plugins/xep_0115/caps.py | 10 ++++----- slixmpp/plugins/xep_0153/vcard_avatar.py | 5 ++--- slixmpp/plugins/xep_0198/stream_management.py | 7 +++---- slixmpp/plugins/xep_0199/ping.py | 10 ++++----- 7 files changed, 34 insertions(+), 46 deletions(-) (limited to 'slixmpp/plugins') diff --git a/slixmpp/plugins/xep_0047/stream.py b/slixmpp/plugins/xep_0047/stream.py index 3be894eb..b3767265 100644 --- a/slixmpp/plugins/xep_0047/stream.py +++ b/slixmpp/plugins/xep_0047/stream.py @@ -31,8 +31,7 @@ class IBBytestream(object): self.recv_queue = asyncio.Queue() - @asyncio.coroutine - def send(self, data, timeout=None): + async def send(self, data, timeout=None): if not self.stream_started or self.stream_out_closed: raise socket.error if len(data) > self.block_size: @@ -56,22 +55,20 @@ class IBBytestream(object): iq['ibb_data']['sid'] = self.sid iq['ibb_data']['seq'] = seq iq['ibb_data']['data'] = data - yield from iq.send(timeout=timeout) + await iq.send(timeout=timeout) return len(data) - @asyncio.coroutine - def sendall(self, data, timeout=None): + async def sendall(self, data, timeout=None): sent_len = 0 while sent_len < len(data): - sent_len += yield from self.send(data[sent_len:self.block_size], timeout=timeout) + sent_len += await self.send(data[sent_len:self.block_size], timeout=timeout) - @asyncio.coroutine - def sendfile(self, file, timeout=None): + async def sendfile(self, file, timeout=None): while True: data = file.read(self.block_size) if not data: break - yield from self.send(data, timeout=timeout) + await self.send(data, timeout=timeout) def _recv_data(self, stanza): new_seq = stanza['ibb_data']['seq'] diff --git a/slixmpp/plugins/xep_0065/proxy.py b/slixmpp/plugins/xep_0065/proxy.py index a4be2717..e1ca4096 100644 --- a/slixmpp/plugins/xep_0065/proxy.py +++ b/slixmpp/plugins/xep_0065/proxy.py @@ -55,18 +55,17 @@ class XEP_0065(BasePlugin): """Returns the socket associated to the SID.""" return self._sessions.get(sid, None) - @asyncio.coroutine - def handshake(self, to, ifrom=None, sid=None, timeout=None): + async def handshake(self, to, ifrom=None, sid=None, timeout=None): """ Starts the handshake to establish the socks5 bytestreams connection. """ if not self._proxies: - self._proxies = yield from self.discover_proxies() + self._proxies = await self.discover_proxies() if sid is None: sid = uuid4().hex - used = yield from self.request_stream(to, sid=sid, ifrom=ifrom, timeout=timeout) + used = await self.request_stream(to, sid=sid, ifrom=ifrom, timeout=timeout) proxy = used['socks']['streamhost_used']['jid'] if proxy not in self._proxies: @@ -74,16 +73,16 @@ class XEP_0065(BasePlugin): return try: - self._sessions[sid] = (yield from self._connect_proxy( + self._sessions[sid] = (await self._connect_proxy( self._get_dest_sha1(sid, self.xmpp.boundjid, to), self._proxies[proxy][0], self._proxies[proxy][1]))[1] except socket.error: return None - addr, port = yield from self._sessions[sid].connected + addr, port = await self._sessions[sid].connected # Request that the proxy activate the session with the target. - yield from self.activate(proxy, sid, to, timeout=timeout) + await self.activate(proxy, sid, to, timeout=timeout) sock = self.get_socket(sid) self.xmpp.event('stream:%s:%s' % (sid, to), sock) return sock @@ -105,8 +104,7 @@ class XEP_0065(BasePlugin): iq['socks'].add_streamhost(proxy, host, port) return iq.send(timeout=timeout, callback=callback) - @asyncio.coroutine - def discover_proxies(self, jid=None, ifrom=None, timeout=None): + async def discover_proxies(self, jid=None, ifrom=None, timeout=None): """Auto-discover the JIDs of SOCKS5 proxies on an XMPP server.""" if jid is None: if self.xmpp.is_component: @@ -116,7 +114,7 @@ class XEP_0065(BasePlugin): discovered = set() - disco_items = yield from self.xmpp['xep_0030'].get_items(jid, timeout=timeout) + disco_items = await self.xmpp['xep_0030'].get_items(jid, timeout=timeout) disco_items = {item[0] for item in disco_items['disco_items']['items']} disco_info_futures = {} @@ -125,7 +123,7 @@ class XEP_0065(BasePlugin): for item in disco_items: try: - disco_info = yield from disco_info_futures[item] + disco_info = await disco_info_futures[item] except XMPPError: continue else: @@ -137,7 +135,7 @@ class XEP_0065(BasePlugin): for jid in discovered: try: - addr = yield from self.get_network_address(jid, ifrom=ifrom, timeout=timeout) + addr = await self.get_network_address(jid, ifrom=ifrom, timeout=timeout) self._proxies[jid] = (addr['socks']['streamhost']['host'], addr['socks']['streamhost']['port']) except XMPPError: @@ -182,9 +180,8 @@ class XEP_0065(BasePlugin): streamhost['host'], streamhost['port'])) - @asyncio.coroutine - def gather(futures, iq, streamhosts): - proxies = yield from asyncio.gather(*futures, return_exceptions=True) + async def gather(futures, iq, streamhosts): + proxies = await asyncio.gather(*futures, return_exceptions=True) for streamhost, proxy in zip(streamhosts, proxies): if isinstance(proxy, ValueError): continue @@ -194,7 +191,7 @@ class XEP_0065(BasePlugin): proxy = proxy[1] # TODO: what if the future never happens? try: - addr, port = yield from proxy.connected + addr, port = await proxy.connected except socket.error: log.exception('Socket error while connecting to the proxy.') continue diff --git a/slixmpp/plugins/xep_0065/socks5.py b/slixmpp/plugins/xep_0065/socks5.py index 54267b32..f390e313 100644 --- a/slixmpp/plugins/xep_0065/socks5.py +++ b/slixmpp/plugins/xep_0065/socks5.py @@ -137,8 +137,8 @@ class Socks5Protocol(asyncio.Protocol): def resume_writing(self): self.paused.set_result(None) - def write(self, data): - yield from self.paused + async def write(self, data): + await self.paused self.transport.write(data) def _send_methods(self): diff --git a/slixmpp/plugins/xep_0115/caps.py b/slixmpp/plugins/xep_0115/caps.py index 9b2d499e..749b74bd 100644 --- a/slixmpp/plugins/xep_0115/caps.py +++ b/slixmpp/plugins/xep_0115/caps.py @@ -137,8 +137,7 @@ class XEP_0115(BasePlugin): self.xmpp.event('entity_caps', p) - @asyncio.coroutine - def _process_caps(self, pres): + async def _process_caps(self, pres): if not pres['caps']['hash']: log.debug("Received unsupported legacy caps: %s, %s, %s", pres['caps']['node'], @@ -169,7 +168,7 @@ class XEP_0115(BasePlugin): log.debug("New caps verification string: %s", ver) try: node = '%s#%s' % (pres['caps']['node'], ver) - caps = yield from self.xmpp['xep_0030'].get_info(pres['from'], node, + caps = await self.xmpp['xep_0030'].get_info(pres['from'], node, coroutine=True) if isinstance(caps, Iq): @@ -285,10 +284,9 @@ class XEP_0115(BasePlugin): binary = hash(S.encode('utf8')).digest() return base64.b64encode(binary).decode('utf-8') - @asyncio.coroutine - def update_caps(self, jid=None, node=None, preserve=False): + async def update_caps(self, jid=None, node=None, preserve=False): try: - info = yield from self.xmpp['xep_0030'].get_info(jid, node, local=True) + info = await self.xmpp['xep_0030'].get_info(jid, node, local=True) if isinstance(info, Iq): info = info['disco_info'] ver = self.generate_verstring(info, self.hash) diff --git a/slixmpp/plugins/xep_0153/vcard_avatar.py b/slixmpp/plugins/xep_0153/vcard_avatar.py index bbbbd2ad..6430e8d6 100644 --- a/slixmpp/plugins/xep_0153/vcard_avatar.py +++ b/slixmpp/plugins/xep_0153/vcard_avatar.py @@ -98,10 +98,9 @@ class XEP_0153(BasePlugin): first_future.add_done_callback(propagate_timeout_exception) return future - @asyncio.coroutine - def _start(self, event): + async def _start(self, event): try: - vcard = yield from self.xmpp['xep_0054'].get_vcard(self.xmpp.boundjid.bare) + vcard = await self.xmpp['xep_0054'].get_vcard(self.xmpp.boundjid.bare) data = vcard['vcard_temp']['PHOTO']['BINVAL'] if not data: new_hash = '' diff --git a/slixmpp/plugins/xep_0198/stream_management.py b/slixmpp/plugins/xep_0198/stream_management.py index fbc9e023..759e82e1 100644 --- a/slixmpp/plugins/xep_0198/stream_management.py +++ b/slixmpp/plugins/xep_0198/stream_management.py @@ -174,8 +174,7 @@ class XEP_0198(BasePlugin): req = stanza.RequestAck(self.xmpp) self.xmpp.send_raw(str(req)) - @asyncio.coroutine - def _handle_sm_feature(self, features): + async def _handle_sm_feature(self, features): """ Enable or resume stream management. @@ -203,7 +202,7 @@ class XEP_0198(BasePlugin): MatchXPath(stanza.Enabled.tag_name()), MatchXPath(stanza.Failed.tag_name())])) self.xmpp.register_handler(waiter) - result = yield from waiter.wait() + result = await waiter.wait() elif self.sm_id and self.allow_resume and 'bind' not in self.xmpp.features: self.enabled = True resume = stanza.Resume(self.xmpp) @@ -219,7 +218,7 @@ class XEP_0198(BasePlugin): MatchXPath(stanza.Resumed.tag_name()), MatchXPath(stanza.Failed.tag_name())])) self.xmpp.register_handler(waiter) - result = yield from waiter.wait() + result = await waiter.wait() if result is not None and result.name == 'resumed': return True return False diff --git a/slixmpp/plugins/xep_0199/ping.py b/slixmpp/plugins/xep_0199/ping.py index 2ca71f7d..1153389b 100644 --- a/slixmpp/plugins/xep_0199/ping.py +++ b/slixmpp/plugins/xep_0199/ping.py @@ -104,11 +104,10 @@ class XEP_0199(BasePlugin): def disable_keepalive(self, event=None): self.xmpp.cancel_schedule('Ping keepalive') - @asyncio.coroutine - def _keepalive(self, event=None): + async def _keepalive(self, event=None): log.debug("Keepalive ping...") try: - rtt = yield from self.ping(self.xmpp.boundjid.host, timeout=self.timeout) + rtt = await self.ping(self.xmpp.boundjid.host, timeout=self.timeout) except IqTimeout: log.debug("Did not receive ping back in time." + \ "Requesting Reconnect.") @@ -145,8 +144,7 @@ class XEP_0199(BasePlugin): return iq.send(timeout=timeout, callback=callback, timeout_callback=timeout_callback) - @asyncio.coroutine - def ping(self, jid=None, ifrom=None, timeout=None): + async def ping(self, jid=None, ifrom=None, timeout=None): """Send a ping request and calculate RTT. This is a coroutine. @@ -174,7 +172,7 @@ class XEP_0199(BasePlugin): log.debug('Pinging %s' % jid) try: - yield from self.send_ping(jid, ifrom=ifrom, timeout=timeout) + await self.send_ping(jid, ifrom=ifrom, timeout=timeout) except IqError as e: if own_host: rtt = time.time() - start -- cgit v1.2.3