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 <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Sebastian Spaeth 2011-08-18 09:08:54 +02:00 committed by Nicolas Sebrecht
parent 06bfff7c04
commit 8532a69458

View File

@ -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))
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", \
#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(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
res_type, res_data = imapobj.search(None, search_cond)
# Result UIDs seperated by space, coalesce into ranges
messagesToFetch = imaputil.uid_sequence(res_data.split())
except KeyError:
return
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