Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aioimap: using await/async syntax #59

Merged
merged 3 commits into from Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 22 additions & 25 deletions README.rst
Expand Up @@ -16,7 +16,7 @@ This library is inspired by imaplib_ and imaplib2_ from Piers Lauder, Nicolas Se

The aim is to port the imaplib with asyncio_, to benefit from the sleep or treat model.

It runs with python 3.4 and 3.5.
It runs with python 3.5, 3.6, 3.7, 3.8.

Example
-------
Expand All @@ -27,17 +27,16 @@ Example
from aioimaplib import aioimaplib


@asyncio.coroutine
def check_mailbox(host, user, password):
async def check_mailbox(host, user, password):
imap_client = aioimaplib.IMAP4_SSL(host=host)
yield from imap_client.wait_hello_from_server()
await imap_client.wait_hello_from_server()

yield from imap_client.login(user, password)
await imap_client.login(user, password)

res, data = yield from imap_client.select()
res, data = await imap_client.select()
print('there is %s messages INBOX' % data[0])

yield from imap_client.logout()
await imap_client.logout()


if __name__ == '__main__':
Expand All @@ -55,23 +54,22 @@ The RFC2177_ is implemented, to be able to wait for new mail messages without us

::

@asyncio.coroutine
def wait_for_new_message(host, user, password):
async def wait_for_new_message(host, user, password):
imap_client = aioimaplib.IMAP4_SSL(host=host)
yield from imap_client.wait_hello_from_server()
await imap_client.wait_hello_from_server()

yield from imap_client.login(user, password)
yield from imap_client.select()
await imap_client.login(user, password)
await imap_client.select()

idle = yield from imap_client.idle_start(timeout=10)
idle = await imap_client.idle_start(timeout=10)
while imap_client.has_pending_idle():
msg = yield from imap_client.wait_server_push()
msg = await imap_client.wait_server_push()
print(msg)
if msg == STOP_WAIT_SERVER_PUSH:
imap_client.idle_done()
yield from asyncio.wait_for(idle, 1)
await asyncio.wait_for(idle, 1)

yield from imap_client.logout()
await imap_client.logout()

if __name__ == '__main__':
loop = asyncio.get_event_loop()
Expand All @@ -81,22 +79,21 @@ Or in a more event based style (the IDLE command is closed at each message from

::

@asyncio.coroutine
def idle_loop(host, user, password):
async def idle_loop(host, user, password):
imap_client = aioimaplib.IMAP4_SSL(host=host, timeout=30)
yield from imap_client.wait_hello_from_server()
await imap_client.wait_hello_from_server()

yield from imap_client.login(user, password)
yield from imap_client.select()
await imap_client.login(user, password)
await imap_client.select()

while True:
print((yield from imap_client.uid('fetch', '1:*', 'FLAGS')))
print((await imap_client.uid('fetch', '1:*', 'FLAGS')))

idle = yield from imap_client.idle_start(timeout=60)
print((yield from imap_client.wait_server_push()))
idle = await imap_client.idle_start(timeout=60)
print((await imap_client.wait_server_push()))

imap_client.idle_done()
yield from asyncio.wait_for(idle, 30)
await asyncio.wait_for(idle, 30)

Threading
---------
Expand Down