Integrated mbox mailbox to the set of backends.

This commit is contained in:
Timo Kankare
2016-09-20 22:54:31 +03:00
parent 84abf559e9
commit cdbd3f0a94
4 changed files with 22 additions and 16 deletions

View File

@@ -28,6 +28,7 @@ import re
from Mailnag.backends.imap import IMAPMailboxBackend
from Mailnag.backends.pop3 import POP3MailboxBackend
from Mailnag.backends.local import MBoxBackend
from Mailnag.common.utils import splitstr
@@ -76,12 +77,15 @@ _backends = {
Param('imap', 'imap', _str_to_bool, _bool_to_str, False),
Param('idle', 'idle', _str_to_bool, _bool_to_str, False),
]),
'mbox' : Backend(MBoxBackend, [
Param('path', 'path', str, str, ''),
]),
}
def create_backend(mailbox_type, name, **kw):
"""Create mailbox backend of specified type, name and other parameters."""
return _backends[mailbox_type].backend_class(name, **kw)
def create_backend(mailbox_type, **kw):
"""Create mailbox backend of specified type and parameters."""
return _backends[mailbox_type].backend_class(**kw)
def get_mailbox_parameter_specs(mailbox_type):

View File

@@ -27,10 +27,12 @@ import mailbox
import logging
import os.path
class MBoxBackend:
from Mailnag.backends.base import MailboxBackend
class MBoxBackend(MailboxBackend):
"""Implementation of mbox mail boxes."""
def __init__(self, name = '', path=None):
def __init__(self, name = '', path=None, **kw):
"""Initialize mbox mailbox backens with a name and path."""
self.name = name
self.path = path

View File

@@ -48,7 +48,7 @@ CREDENTIAL_KEY = 'Mailnag password for %s://%s@%s'
#
class Account:
def __init__(self, enabled = False, name = '', user = '', \
password = '', oauth2string = '', server = '', port = '', ssl = True, imap = True, idle = True, folders = [], mailbox_type = None, **kw):
password = '', oauth2string = '', server = '', port = '', ssl = True, imap = True, idle = False, folders = [], mailbox_type = None, **kw):
self.enabled = enabled # bool
if mailbox_type:

View File

@@ -25,23 +25,23 @@
import mailbox
import pytest
from Mailnag.backends.local import MBoxBackend
from Mailnag.backends import create_backend
def test_create_mbox_backend():
be = MBoxBackend()
be = create_backend('mbox')
assert be is not None
def test_initially_mailbox_should_be_closed():
be = MBoxBackend()
be = create_backend('mbox')
assert not be.is_open()
def test_when_opened_mailbox_should_be_open(tmpdir):
tmpdir.join('sample').write('')
path = str(tmpdir.join('sample'))
be = MBoxBackend(path=path)
be = create_backend('mbox', path=path)
be.open()
assert be.is_open()
@@ -49,7 +49,7 @@ def test_when_opened_mailbox_should_be_open(tmpdir):
def test_closed_mailbox_should_be_closed(tmpdir):
tmpdir.join('sample').write('')
path = str(tmpdir.join('sample'))
be = MBoxBackend(path=path)
be = create_backend('mbox', path=path)
be.open()
be.close()
assert not be.is_open()
@@ -59,7 +59,7 @@ def test_mbox_lists_no_messages_from_empty_mailbox(tmpdir):
path = str(tmpdir.join('sample'))
sample_mbox = mailbox.mbox(path, create=True)
be = MBoxBackend(name='sample', path=path)
be = create_backend('mbox', name='sample', path=path)
be.open()
try:
msgs = list(be.list_messages())
@@ -76,7 +76,7 @@ def test_mbox_lists_two_messages_from_mailbox(tmpdir):
add_mbox_message(sample_mbox, 'blaa-blaa-3', 'RO')
sample_mbox.close()
be = MBoxBackend(name='sample', path=path)
be = create_backend('mbox', name='sample', path=path)
be.open()
try:
msgs = list(be.list_messages())
@@ -92,7 +92,7 @@ def test_mbox_lists_two_messages_from_mailbox(tmpdir):
def test_mbox_should_not_have_folders(tmpdir):
tmpdir.join('sample').write('')
path = str(tmpdir.join('sample'))
be = MBoxBackend(path=path)
be = create_backend('mbox', path=path)
be.open()
assert be.request_folders() == []
@@ -100,7 +100,7 @@ def test_mbox_should_not_have_folders(tmpdir):
def test_mbox_does_not_support_notifications(tmpdir): # for now
tmpdir.join('sample').write('')
path = str(tmpdir.join('sample'))
be = MBoxBackend(path=path)
be = create_backend('mbox', path=path)
be.open()
with pytest.raises(NotImplementedError):
be.notify_next_change()
@@ -111,7 +111,7 @@ def test_mbox_does_not_support_notifications(tmpdir): # for now
def test_mbox_open_should_fail_if_mailbox_does_not_exist(tmpdir):
path = str(tmpdir.join('not-exist'))
be = MBoxBackend(path=path)
be = create_backend('mbox', path=path)
with pytest.raises(IOError):
be.open()