[PATCH 6/8] Update to match semantics of new imaplib2

Ethan Glasser-Camp ethan at betacantrips.com
Tue Dec 28 11:46:23 GMT 2010


The biggest change here is that imapobj.untagged_responses is no
longer a dictionary, but a list. To access it, I use the semi-private
_get_untagged_response method.

* offlineimap/folder/IMAP.py (IMAPFolder.quickchanged,
  IMAPFolder.cachemessagelist): imaplib2 now explicitly removes its
  EXISTS response on select(), so instead we use the return values from
  select() to get the number of messages.

* offlineimap/imapserver.py (UsefulIMAPMixIn.select): imaplib2 now
  stores untagged_responses for different mailboxes, which confuses us
  because it seems like our mailboxes are "still" in read-only mode when
  we just re-opened them.  Additionally, we have to return the value
  from imaplib2's select() so that the above thing works.
---
 offlineimap/folder/IMAP.py |   14 +++++++-------
 offlineimap/imapserver.py  |    3 +++
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index e2c7485..1565654 100644
--- a/offlineimap/folder/IMAP.py
+++ b/offlineimap/folder/IMAP.py
@@ -71,7 +71,7 @@ class IMAPFolder(BaseFolder):
         try:
             # Primes untagged_responses
             self.selectro(imapobj)
-            return long(imapobj.untagged_responses['UIDVALIDITY'][0])
+            return long(imapobj._get_untagged_response('UIDVALIDITY', True)[0])
         finally:
             self.imapserver.releaseconnection(imapobj)
     
@@ -82,11 +82,11 @@ class IMAPFolder(BaseFolder):
         imapobj = self.imapserver.acquireconnection()
         try:
             # Primes untagged_responses
-            imapobj.select(self.getfullname(), readonly = 1, force = 1)
+            imaptype, imapdata = imapobj.select(self.getfullname(), readonly = 1, force = 1)
             try:
                 # Some mail servers do not return an EXISTS response if
                 # the folder is empty.
-                maxmsgid = long(imapobj.untagged_responses['EXISTS'][0])
+                maxmsgid = long(imapdata[0])
             except KeyError:
                 return True
 
@@ -123,7 +123,7 @@ class IMAPFolder(BaseFolder):
 
         try:
             # Primes untagged_responses
-            imapobj.select(self.getfullname(), readonly = 1, force = 1)
+            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)
@@ -169,7 +169,7 @@ class IMAPFolder(BaseFolder):
                     # Some mail servers do not return an EXISTS response if
                     # the folder is empty.
 
-                    maxmsgid = long(imapobj.untagged_responses['EXISTS'][0])
+                    maxmsgid = long(imapdata[0])
                     messagesToFetch = '1:%d' % maxmsgid;
                 except KeyError:
                     return
@@ -248,8 +248,8 @@ class IMAPFolder(BaseFolder):
         return leader + newline + trailer
 
     def savemessage_searchforheader(self, imapobj, headername, headervalue):
-        if imapobj.untagged_responses.has_key('APPENDUID'):
-            return long(imapobj.untagged_responses['APPENDUID'][-1].split(' ')[1])
+        if imapobj._get_untagged_response('APPENDUID', True):
+            return long(imapobj._get_untagged_response('APPENDUID', True)[-1].split(' ')[1])
 
         ui = UIBase.getglobalui()
         ui.debug('imap', 'savemessage_searchforheader called for %s: %s' % \
diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index 781ec12..f06c4ab 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -48,6 +48,8 @@ class UsefulIMAPMixIn:
            and self.is_readonly == readonly:
             # No change; return.
             return
+        # Wipe out all old responses, to maintain semantics with old imaplib2
+        del self.untagged_responses[:]
         result = self.__class__.__bases__[1].select(self, mailbox, readonly)
         if result[0] != 'OK':
             raise ValueError, "Error from select: %s" % str(result)
@@ -55,6 +57,7 @@ class UsefulIMAPMixIn:
             self.selectedfolder = mailbox
         else:
             self.selectedfolder = None
+        return result
 
     def _mesg(self, s, tn=None, secs=None):
         imaplibutil.new_mesg(self, s, tn, secs)
-- 
1.7.1





More information about the OfflineIMAP-project mailing list