From 8532a69458f5256ba73be2d46dc5200dd81dcb17 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 18 Aug 2011 09:08:54 +0200 Subject: [PATCH] Clean up the maxsize maxage code Do away with the wrapping of this code in a try...except KeyError, as this code cannot conceivably throw a KeyError. Even if it could, it should be documented why we should simply return() in this case. Shorten some of the variable names and minor code cleanup while taking the git blame anyway. Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/folder/IMAP.py | 57 ++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index f930bc5..fa2c377 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -107,51 +107,48 @@ class IMAPFolder(BaseFolder): # TODO: Make this so that it can define a date that would be the oldest messages etc. def cachemessagelist(self): - imapobj = self.imapserver.acquireconnection() + maxage = self.config.getdefaultint("Account %s" % self.accountname, + "maxage", -1) + maxsize = self.config.getdefaultint("Account %s" % self.accountname, + "maxsize", -1) self.messagelist = {} + imapobj = self.imapserver.acquireconnection() + try: # Primes untagged_responses imaptype, imapdata = imapobj.select(self.getfullname(), readonly = 1, force = 1) - maxage = self.config.getdefaultint("Account " + self.accountname, "maxage", -1) - maxsize = self.config.getdefaultint("Account " + self.accountname, "maxsize", -1) - if (maxage != -1) | (maxsize != -1): - try: - search_condition = "("; + search_cond = "("; - if(maxage != -1): - #find out what the oldest message is that we should look at - oldest_time_struct = time.gmtime(time.time() - (60*60*24*maxage)) + if(maxage != -1): + #find out what the oldest message is that we should look at + oldest_struct = time.gmtime(time.time() - (60*60*24*maxage)) - #format this manually - otherwise locales could cause problems - monthnames_standard = ["Jan", "Feb", "Mar", "Apr", "May", \ - "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] + #format months manually - otherwise locales cause problems + monthnames = ["Jan", "Feb", "Mar", "Apr", "May", \ + "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - our_monthname = monthnames_standard[oldest_time_struct[1]-1] - daystr = "%(day)02d" % {'day' : oldest_time_struct[2]} - date_search_str = "SINCE " + daystr + "-" + our_monthname \ - + "-" + str(oldest_time_struct[0]) + month = monthnames[oldest_struct[1]-1] + daystr = "%(day)02d" % {'day' : oldest_struct[2]} - search_condition += date_search_str + search_cond += "SINCE %s-%s-%s" % (daystr, month, + oldest_struct[0]) - if(maxsize != -1): - if(maxage != -1): #There are two conditions - add a space - search_condition += " " + if(maxsize != -1): + if(maxage != -1): # There are two conditions, add space + search_cond += " " + search_cond += "SMALLER %d" % maxsize - search_condition += "SMALLER " + self.config.getdefault("Account " + self.accountname, "maxsize", -1) + search_cond += ")" - search_condition += ")" - - res_type, res_data = imapobj.search(None, search_condition) - #result UIDs come back seperated by space - messagesToFetch = imaputil.uid_sequence(res_data.split()) - except KeyError: - return + res_type, res_data = imapobj.search(None, search_cond) + # Result UIDs seperated by space, coalesce into ranges + messagesToFetch = imaputil.uid_sequence(res_data.split()) if not messagesToFetch: - # No messages; return - return + return # No messages to sync + else: # 1. Some mail servers do not return an EXISTS response # if the folder is empty. 2. ZIMBRA servers can return