IMAP DAL enhancements, thanks Alan

This commit is contained in:
Massimo
2013-01-10 13:04:38 -06:00
parent 8eadfe3d79
commit 7614955591
2 changed files with 37 additions and 14 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.4.1-alpha.2+timestamp.2013.01.10.12.59.43 Version 2.4.1-alpha.2+timestamp.2013.01.10.13.04.10
+36 -13
View File
@@ -5670,6 +5670,7 @@ class IMAPAdapter(NoSQLAdapter):
self.driver_args = driver_args self.driver_args = driver_args
self.adapter_args = adapter_args self.adapter_args = adapter_args
self.mailbox_size = None self.mailbox_size = None
self.static_names = None
self.charset = sys.getfilesystemencoding() self.charset = sys.getfilesystemencoding()
# imap class # imap class
self.imap4 = None self.imap4 = None
@@ -5848,11 +5849,17 @@ class IMAPAdapter(NoSQLAdapter):
return charset return charset
def reset_mailboxes(self): def reset_mailboxes(self):
self.connection.mailbox_names = None "Forces the adapter to retrieve mailbox names from the server"
self.connection.mailbox_names = self.static_names = None
self.get_mailboxes() self.get_mailboxes()
def get_mailboxes(self): def get_mailboxes(self):
""" Query the mail database for mailbox names """ """ Query the mail database for mailbox names """
if self.static_names:
# statically defined mailbox names
self.connection.mailbox_names = self.static_names
return self.static_names.keys()
mailboxes_list = self.connection.list() mailboxes_list = self.connection.list()
self.connection.mailbox_names = dict() self.connection.mailbox_names = dict()
mailboxes = list() mailboxes = list()
@@ -5864,7 +5871,8 @@ class IMAPAdapter(NoSQLAdapter):
sub_items = item.split("\"") sub_items = item.split("\"")
sub_items = [sub_item for sub_item in sub_items \ sub_items = [sub_item for sub_item in sub_items \
if len(sub_item.strip()) > 0] if len(sub_item.strip()) > 0]
mailbox = sub_items[len(sub_items) - 1] # mailbox = sub_items[len(sub_items) -1]
mailbox = sub_items[-1]
# remove unwanted characters and store original names # remove unwanted characters and store original names
# Don't allow leading non alphabetic characters # Don't allow leading non alphabetic characters
mailbox_name = re.sub('^[_0-9]*', '', re.sub('[^_\w]','',re.sub('[/ ]','_',mailbox))) mailbox_name = re.sub('^[_0-9]*', '', re.sub('[^_\w]','',re.sub('[/ ]','_',mailbox)))
@@ -5896,7 +5904,7 @@ class IMAPAdapter(NoSQLAdapter):
else: else:
return False return False
def define_tables(self): def define_tables(self, mailbox_names=None):
""" """
Auto create common IMAP fileds Auto create common IMAP fileds
@@ -5908,11 +5916,16 @@ class IMAPAdapter(NoSQLAdapter):
Returns a dictionary with tablename, server native mailbox name Returns a dictionary with tablename, server native mailbox name
pairs. pairs.
""" """
if mailbox_names:
# optional statically declared mailboxes
self.static_names = mailbox_names
if not isinstance(self.connection.mailbox_names, dict): if not isinstance(self.connection.mailbox_names, dict):
self.get_mailboxes() self.get_mailboxes()
mailboxes = self.connection.mailbox_names.keys()
for mailbox_name in mailboxes: names = self.connection.mailbox_names.keys()
self.db.define_table("%s" % mailbox_name,
for name in names:
self.db.define_table("%s" % name,
Field("uid", "string", writable=False), Field("uid", "string", writable=False),
Field("answered", "boolean"), Field("answered", "boolean"),
Field("created", "datetime", writable=False), Field("created", "datetime", writable=False),
@@ -5935,8 +5948,8 @@ class IMAPAdapter(NoSQLAdapter):
# Set a special _mailbox attribute for storing # Set a special _mailbox attribute for storing
# native mailbox names # native mailbox names
self.db[mailbox_name].mailbox = \ self.db[name].mailbox = \
self.connection.mailbox_names[mailbox_name] self.connection.mailbox_names[name]
# Set the db instance mailbox collections # Set the db instance mailbox collections
self.db.mailboxes = self.connection.mailbox_names self.db.mailboxes = self.connection.mailbox_names
@@ -5969,10 +5982,14 @@ class IMAPAdapter(NoSQLAdapter):
if isinstance(query, Query): if isinstance(query, Query):
tablename = self.get_table(query) tablename = self.get_table(query)
mailbox = self.connection.mailbox_names.get(tablename, None) mailbox = self.connection.mailbox_names.get(tablename, None)
if mailbox is not None: if mailbox is None:
raise ValueError("Mailbox name not found: %s" % mailbox)
else:
# select with readonly # select with readonly
selected = self.connection.select(mailbox, True) result, selected = self.connection.select(mailbox, True)
self.mailbox_size = int(selected[1][0]) if result != "OK":
raise Exception("IMAP error: %s" % selected)
self.mailbox_size = int(selected[0])
search_query = "(%s)" % str(query).strip() search_query = "(%s)" % str(query).strip()
search_result = self.connection.uid("search", None, search_query) search_result = self.connection.uid("search", None, search_query)
# Normal IMAP response OK is assumed (change this) # Normal IMAP response OK is assumed (change this)
@@ -6014,10 +6031,12 @@ class IMAPAdapter(NoSQLAdapter):
fetch_results.append(fr) fetch_results.append(fr)
else: else:
# error retrieving the flags for this message # error retrieving the flags for this message
pass raise Exception("IMAP error retrieving flags: %s" % fdata)
else: else:
# error retrieving the message body # error retrieving the message body
pass raise Exception("IMAP error retrieving the body: %s" % data)
else:
raise Exception("IMAP search error: %s" % search_result[1])
elif isinstance(query, (Expression, basestring)): elif isinstance(query, (Expression, basestring)):
raise NotImplementedError() raise NotImplementedError()
else: else:
@@ -6193,6 +6212,8 @@ class IMAPAdapter(NoSQLAdapter):
result, data = self.connection.store(*command) result, data = self.connection.store(*command)
if result == "OK": if result == "OK":
rowcount += 1 rowcount += 1
else:
raise Exception("IMAP storing error: %s" % data)
return rowcount return rowcount
def _count(self, query, distinct=None): def _count(self, query, distinct=None):
@@ -6224,6 +6245,8 @@ class IMAPAdapter(NoSQLAdapter):
result, data = self.connection.store(number, "+FLAGS", "(\\Deleted)") result, data = self.connection.store(number, "+FLAGS", "(\\Deleted)")
if result == "OK": if result == "OK":
counter += 1 counter += 1
else:
raise Exception("IMAP store error: %s" % data)
if counter > 0: if counter > 0:
result, data = self.connection.expunge() result, data = self.connection.expunge()
return counter return counter