Skip to content

Commit

Permalink
aioimap: using await/async syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bamthomas committed Apr 10, 2021
1 parent c59cf4f commit 0235f84
Show file tree
Hide file tree
Showing 9 changed files with 553 additions and 727 deletions.
304 changes: 125 additions & 179 deletions aioimaplib/aioimaplib.py

Large diffs are not rendered by default.

26 changes: 12 additions & 14 deletions aioimaplib/tests/example.py
Expand Up @@ -4,36 +4,34 @@
from aioimaplib import aioimaplib


@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()

asyncio.async(imap_client.idle())
await imap_client.idle()
while True:
msg = yield from imap_client.wait_server_push()
msg = await imap_client.wait_server_push()
print('--> received from server: %s' % msg)
if 'EXISTS' in msg:
imap_client.idle_done()
break

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


@asyncio.coroutine
def fetch_mail(host, user, password):
async def fetch_mail(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)

response = yield from imap_client.select()
response = await imap_client.select()
print('there is %s messages INBOX' % aioimaplib.extract_exists(response))

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


if __name__ == '__main__':
Expand Down
18 changes: 8 additions & 10 deletions aioimaplib/tests/imapserver.py
Expand Up @@ -168,9 +168,8 @@ def _reindex(self, user, mailbox):


def critical_section(next_state):
@asyncio.coroutine
def execute_section(self, state, critical_func, *args, **kwargs):
with (yield from self.state_condition):
async def execute_section(self, state, critical_func, *args, **kwargs):
with await self.state_condition:
critical_func(self, *args, **kwargs)
self.state = state
log.debug('state -> %s' % state)
Expand Down Expand Up @@ -310,10 +309,9 @@ def close(self, tag, *args):
self.user_mailbox = None
self.send_tagged_line(tag, 'OK CLOSE completed.')

@asyncio.coroutine
def wait(self, state):
with (yield from self.state_condition):
yield from self.state_condition.wait_for(lambda: self.state == state)
async def wait(self, state):
with await self.state_condition:
await self.state_condition.wait_for(lambda: self.state == state)

def examine(self, tag, *args):
mailbox_name = args[0]
Expand Down Expand Up @@ -663,16 +661,15 @@ def receive(self, mail, imap_user=None, mailbox='INBOX'):
uids.append(self._server_state.imap_receive(to, mail, mailbox))
return uids

@asyncio.coroutine
def wait_state(self, state, user):
async def wait_state(self, state, user):
user_connections = [connection for connection in self._connections if connection.user_login == user]
if len(user_connections) == 0:
other_users = list(map(lambda c: c.user_login, self._connections))
raise ValueError("wait_state didn't find a connection to user %s among %s" % (user, other_users))
if len(user_connections) > 1:
raise ValueError("wait_state can't handle %d connections for user %s" % (len(user_connections), user))

yield from user_connections[0].wait(state)
await user_connections[0].wait(state)

def get_connection(self, user):
return self._server_state.get_connection(user)
Expand Down Expand Up @@ -760,6 +757,7 @@ def create(to, mail_from='', subject='', content='',

return Mail(msg, date=date)


if __name__ == '__main__':
loop = asyncio.get_event_loop()
server = MockImapServer().run_server()
Expand Down
12 changes: 5 additions & 7 deletions aioimaplib/tests/test_acceptance_aioimaplib.py
Expand Up @@ -29,19 +29,17 @@ class TestAioimaplibAcceptance(AioWithImapServer, TestCase):
def setUp(self):
self._init_server(self.loop)

@asyncio.coroutine
def tearDown(self):
yield from self._shutdown_server()
async def tearDown(self):
await self._shutdown_server()

@asyncio.coroutine
def test_file_with_attachement(self):
async def test_file_with_attachement(self):
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/test_attachment.eml'), mode='br') as msg:
imap_client = yield from self.login_user('user@mail', 'pass', select=True)
imap_client = await self.login_user('user@mail', 'pass', select=True)
mail = Mail(email.message_from_binary_file(msg))

self.imapserver.receive(mail, imap_user='user@mail')

result, data = yield from imap_client.fetch('1', '(RFC822)')
result, data = await imap_client.fetch('1', '(RFC822)')

self.assertEqual('OK', result)
self.assertEqual(['1 FETCH (RFC822 {418898}', mail.as_bytes(), ')', 'FETCH completed.'], data)

0 comments on commit 0235f84

Please sign in to comment.