diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2018-07-18 08:58:03 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2018-07-26 10:36:16 -0700 |
commit | 0bbf12232e86da57ce24703ff0ea82780dc27e31 (patch) | |
tree | ec5dea197e08ff277ffbd7e859ca8ec022a9bde5 | |
parent | 5293665df4c7747e7126d724bb53502184e958c7 (diff) |
account: Convert send_mail function to coroutine
-rw-r--r-- | alot/account.py | 40 | ||||
-rw-r--r-- | alot/commands/envelope.py | 4 | ||||
-rw-r--r-- | tests/commands/envelope_test.py | 13 |
3 files changed, 26 insertions, 31 deletions
diff --git a/alot/account.py b/alot/account.py index f080fc88..f8438564 100644 --- a/alot/account.py +++ b/alot/account.py @@ -314,14 +314,13 @@ class Account(object): return self.store_mail(self.draft_box, mail) @abc.abstractmethod - def send_mail(self, mail): + async def send_mail(self, mail): """ sends given mail :param mail: the mail to send :type mail: :class:`email.message.Message` or string - :returns: a `Deferred` that errs back with a class:`SendingMailFailed`, - containing a reason string if an error occurred. + :raises SendingMailFailed: if sending fails """ pass @@ -338,35 +337,20 @@ class SendmailAccount(Account): super(SendmailAccount, self).__init__(**kwargs) self.cmd = cmd - def send_mail(self, mail): + async def send_mail(self, mail): """Pipe the given mail to the configured sendmail command. Display a short message on success or a notification on error. :param mail: the mail to send out :type mail: :class:`email.message.Message` or string - :returns: the deferred that calls the sendmail command - :rtype: `twisted.internet.defer.Deferred` + :raises: class:`SendingMailFailed` if sending failes """ cmdlist = split_commandstring(self.cmd) - def cb(out): - """The callback used on success.""" - logging.info('sent mail successfully') - logging.info(out) - - def errb(failure): - """The callback used on error.""" - termobj = failure.value - errmsg = '%s failed with code %s:\n%s\nProcess stderr:\n%s' % \ - (self.cmd, termobj.exitCode, str(failure.value), - failure.value.stderr) - logging.error(errmsg) - logging.error(failure.getTraceback()) - raise SendingMailFailed(errmsg) - - # make sure self.mail is a string - mail = str(mail) - - d = call_cmd_async(cmdlist, stdin=mail) - d.addCallback(cb) - d.addErrback(errb) - return d + try: + # make sure self.mail is a string + out, *_ = await call_cmd_async(cmdlist, stdin=str(mail)) + except Exception as e: + logging.error(str(e)) + raise SendingMailFailed(str(e)) + logging.info('sent mail successfully') + logging.info(out) diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index e6d139d5..1c13044c 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -11,6 +11,8 @@ import re import tempfile import textwrap +from twisted.internet.defer import ensureDeferred + from . import Command, registerCommand from . import globals from . import utils @@ -279,7 +281,7 @@ class SendCommand(Command): clearme = ui.notify('sending..', timeout=-1) if self.envelope is not None: self.envelope.sending = True - d = account.send_mail(self.mail) + d = ensureDeferred(account.send_mail(self.mail)) d.addCallback(afterwards) d.addErrback(send_errb) d.addErrback(store_errb) diff --git a/tests/commands/envelope_test.py b/tests/commands/envelope_test.py index 8b57e2dc..8261da34 100644 --- a/tests/commands/envelope_test.py +++ b/tests/commands/envelope_test.py @@ -30,6 +30,7 @@ from alot.db.envelope import Envelope from alot.errors import GPGProblem from alot.settings.errors import NoMatchingAccount from alot.settings.manager import SettingsManager +from alot.account import Account from .. import utilities @@ -352,10 +353,18 @@ class TestSendCommand(unittest.TestCase): Foo Bar Baz """) + class MockedAccount(Account): + + def __init__(self): + super().__init__('foo@example.com') + + async def send_mail(self, mail): + pass + @utilities.async_test async def test_get_account_by_address_with_str(self): cmd = envelope.SendCommand(mail=self.mail) - account = mock.Mock() + account = mock.Mock(wraps=self.MockedAccount()) with mock.patch( 'alot.commands.envelope.settings.get_account_by_address', mock.Mock(return_value=account)) as get_account_by_address: @@ -369,7 +378,7 @@ class TestSendCommand(unittest.TestCase): async def test_get_account_by_address_with_email_message(self): mail = email.message_from_string(self.mail) cmd = envelope.SendCommand(mail=mail) - account = mock.Mock() + account = mock.Mock(wraps=self.MockedAccount()) with mock.patch( 'alot.commands.envelope.settings.get_account_by_address', mock.Mock(return_value=account)) as get_account_by_address: |