Added query method to Account and backend classes to ask if account supports

notifications. Currently only IMAP supports notifications, but only if idle parameter 
is true.
This commit is contained in:
Timo Kankare
2016-12-19 22:56:58 +02:00
parent 82258cf1ca
commit d76367d933
4 changed files with 34 additions and 1 deletions

View File

@@ -67,6 +67,11 @@ class MailboxBackend(object):
"""
raise NotImplementedError
def supports_notifications(self):
"""Returns True if mailbox supports notifications."""
# Default implementation
return False
@abstractmethod
def notify_next_change(self, callback=None, timeout=None):
"""Asks mailbox to notify next change.

View File

@@ -40,7 +40,7 @@ class IMAPMailboxBackend(MailboxBackend):
"""Implementation of IMAP mail boxes."""
def __init__(self, name = '', user = '', password = '', oauth2string = '',
server = '', port = '', ssl = True, folders = [], **kw):
server = '', port = '', ssl = True, folders = [], idle=True, **kw):
self.name = name
self.user = user
self.password = password
@@ -49,6 +49,7 @@ class IMAPMailboxBackend(MailboxBackend):
self.port = port
self.ssl = ssl # bool
self.folders = [encode_mutf7(folder) for folder in folders]
self.idle = idle
self._conn = None
@@ -130,6 +131,11 @@ class IMAPMailboxBackend(MailboxBackend):
return lst
def supports_notifications(self):
"""Returns True if mailbox supports notifications.
IMAP mailbox supports notifications if idle parameter is True"""
return self.idle
def notify_next_change(self, callback=None, timeout=None):
self._ensure_open()

View File

@@ -116,6 +116,11 @@ class Account:
return self._get_backend().list_messages()
def supports_notifications(self):
"""Returns True if account supports notifications."""
return self._get_backend().supports_notifications()
def notify_next_change(self, callback=None, timeout=None):
"""Asks mailbox to notify next change.
Callback is called when new mail arrives or removed.

View File

@@ -22,6 +22,8 @@
"""Test cases for Account."""
import pytest
from Mailnag.common.accounts import Account
@@ -109,3 +111,18 @@ def test_account_should_configurable_with_any_parameters():
assert config['weird'] == 'odd'
assert config['odd'] == 'weird'
@pytest.mark.parametrize("config,should_support", [
({'mailbox_type': 'imap', 'idle': True}, True),
({'mailbox_type': 'imap', 'idle': False}, False),
({'mailbox_type': 'pop3'}, False),
({'mailbox_type': 'mbox'}, False),
({'mailbox_type': 'maildir'}, False),
])
def test_account_supports_notifications(config, should_support):
account = Account(**config)
if should_support:
assert account.supports_notifications()
else:
assert not account.supports_notifications()