From a50bffae890f52295e91603178597023140438ca Mon Sep 17 00:00:00 2001 From: mathieui Date: Sun, 6 Feb 2022 14:48:04 +0100 Subject: [PATCH] docs: remove yield from and fix some old changes --- docs/howto/guide_xep_0030.rst | 22 +++++++++++----------- docs/using_asyncio.rst | 35 ++++++++++++++--------------------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/docs/howto/guide_xep_0030.rst b/docs/howto/guide_xep_0030.rst index a3af4857..4b795c93 100644 --- a/docs/howto/guide_xep_0030.rst +++ b/docs/howto/guide_xep_0030.rst @@ -172,14 +172,14 @@ the `XEP-0059 `_ plug-in. .. code-block:: python - info = yield from self['xep_0030'].get_info(jid='foo@example.com', - node='bar', - ifrom='baz@mycomponent.example.com', - timeout=30) + info = await self['xep_0030'].get_info(jid='foo@example.com', + node='bar', + ifrom='baz@mycomponent.example.com', + timeout=30) - items = self['xep_0030'].get_info(jid='foo@example.com', - node='bar', - iterator=True) + items = await self['xep_0030'].get_items(jid='foo@example.com', + node='bar', + iterator=True) For more examples on how to use basic disco queries, check the ``disco_browser.py`` example in the ``examples`` directory. @@ -194,7 +194,7 @@ a full Iq stanza. .. code-block:: python - info = self['xep_0030'].get_info(node='foo', local=True) - items = self['xep_0030'].get_items(jid='somejid@mycomponent.example.com', - node='bar', - local=True) + info = await self['xep_0030'].get_info(node='foo', local=True) + items = await self['xep_0030'].get_items(jid='somejid@mycomponent.example.com', + node='bar', + local=True) diff --git a/docs/using_asyncio.rst b/docs/using_asyncio.rst index 55ed7679..ca85b7c6 100644 --- a/docs/using_asyncio.rst +++ b/docs/using_asyncio.rst @@ -11,7 +11,7 @@ Block on IQ sending .. code-block:: python - result = yield from iq.send() + result = await iq.send() .. warning:: @@ -28,13 +28,7 @@ The same changes from the SleekXMPP API apply, so you can do: .. code-block:: python - iq_info = yield from self.xmpp['xep_0030'].get_info(jid) - -But the following will only return a Future: - -.. code-block:: python - - iq_info = self.xmpp['xep_0030'].get_info(jid) + iq_info = await self.xmpp['xep_0030'].get_info(jid) Callbacks, Event Handlers, and Stream Handlers @@ -42,7 +36,7 @@ Callbacks, Event Handlers, and Stream Handlers IQ callbacks and :term:`Event Handlers ` can be coroutine functions; in this case, they will be scheduled in the event loop using -:meth:`.asyncio.async` and not ran immediately. +:meth:`.asyncio.ensure_future` and not ran immediately. A :class:`.CoroutineCallback` class has been added as well for :term:`Stream Handlers `, which will use @@ -94,18 +88,18 @@ a simple . .. code-block:: python - import asyncio, aiohttp, slixmpp + import aiohttp, slixmpp - @asyncio.coroutine - def get_pythonorg(event): - req = yield from aiohttp.request('get', 'http://www.python.org') - text = yield from req.text + async def get_pythonorg(event): + async with aiohttp.ClientSession() as session: + async with session.get('http://www.python.org') as resp: + text = await req.text() client.send_message(mto='jid2@example', mbody=text) - @asyncio.coroutine - def get_asyncioorg(event): - req = yield from aiohttp.request('get', 'http://www.asyncio.org') - text = yield from req.text + async def get_asyncioorg(event): + async with aiohttp.ClientSession() as session: + async with session.get('http://www.asyncio.org') as resp: + text = await req.text() client.send_message(mto='jid3@example', mbody=text) client = slixmpp.ClientXMPP('jid@example', 'password') @@ -132,11 +126,10 @@ JID indicating its findings. self.register_plugin('xep_0092') self.add_event_handler('message', self.on_message) - @asyncio.coroutine - def on_message(self, event): + async def on_message(self, event): # You should probably handle IqError and IqTimeout exceptions here # but this is an example. - version = yield from self['xep_0092'].get_version(message['from']) + version = await self['xep_0092'].get_version(message['from']) text = "%s sent me a message, he runs %s" % (message['from'], version['software_version']['name']) self.send_message(mto='master@example.tld', mbody=text)