Correct IMAPAccount populate_handler

IMAP list with wrong delimiter could return [None], so trying with another delimiter in that case

darcs-hash:20080725215323-86b55-f4b7cc15e8b30aaebf2fe5233ab40e4409ebf9a2.gz
This commit is contained in:
David Rousselie
2008-07-25 23:53:23 +02:00
parent 5cdb8e8ba6
commit 56eb2f004c
2 changed files with 38 additions and 11 deletions

View File

@@ -435,7 +435,7 @@ class IMAPAccount(MailAccount):
current_folder = current_folder[folder]
return current_folder.keys()
def populate_handler(self):
def populate_handler(self, try_other_delimiter=True, testing_delimiter="/"):
"""
Handler called when populating account
"""
@@ -445,6 +445,16 @@ class IMAPAccount(MailAccount):
typ, data = self.connection.list(self.mailbox)
if typ == 'OK':
line = data[0]
if line is None:
if try_other_delimiter:
self.mailbox = self.mailbox.replace(testing_delimiter,
".")
self.populate_handler(False, ".")
return
else:
self.disconnect()
raise Exception("Cannot find mailbox " + self.mailbox)
else:
match = self._regexp_list.match(line)
if match is not None:
self.delimiter = match.group(2)
@@ -457,8 +467,9 @@ class IMAPAccount(MailAccount):
raise Exception("Cannot find mailbox " + self.mailbox)
self.disconnect()
# replace any previous delimiter in self.mailbox by "/"
if self.delimiter != "/":
self.mailbox = self.mailbox.replace(self.delimiter, "/")
if self.delimiter != testing_delimiter:
self.mailbox = self.mailbox.replace(testing_delimiter,
self.delimiter)
class POP3Account(MailAccount):

View File

@@ -730,10 +730,10 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
def test_populate_handler(self):
self.assertEquals(".", self.imap_account.delimiter)
self.imap_account.mailbox = "INBOX.dir1.subdir2"
self.imap_account.mailbox = "INBOX/dir1/subdir2"
def call_func(self):
self.imap_account.populate_handler()
self.assertEquals("INBOX/dir1/subdir2", self.imap_account.mailbox)
self.assertEquals("INBOX.dir1.subdir2", self.imap_account.mailbox)
test_func = self.make_test(\
[lambda data: '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \
data.split()[0] + ' OK LIST completed\r\n'],
@@ -741,6 +741,22 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
call_func)
test_func()
def test_populate_handler_wrong_default_delimiter(self):
self.imap_account.delimiter = "/"
self.imap_account.mailbox = "INBOX/dir1/subdir2"
def call_func(self):
self.imap_account.populate_handler()
self.assertEquals("INBOX.dir1.subdir2", self.imap_account.mailbox)
self.assertEquals(".", self.imap_account.delimiter)
test_func = self.make_test(\
[lambda data: data.split()[0] + ' OK LIST completed\r\n',
lambda data: '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \
data.split()[0] + ' OK LIST completed\r\n'],
["^[^ ]* LIST \"?INBOX/dir1/subdir2\"? \*",
"^[^ ]* LIST \"?INBOX.dir1.subdir2\"? \*"],
call_func)
test_func()
def test_populate_handler_wrong_mailbox(self):
self.assertEquals(".", self.imap_account.delimiter)
self.imap_account.mailbox = "INBOX.dir1.subdir2"