[PATCH 4/6] Clean up the maxsize maxage code

Sebastian Spaeth Sebastian at SSpaeth.de
Mon Aug 15 13:15:57 BST 2011


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 at SSpaeth.de>
---
 offlineimap/folder/IMAP.py |   57 ++++++++++++++++++++-----------------------
 1 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index d7d1832..f04b783 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
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list