[PATCH 1/2] folder: Implement helper function getmessagecount()

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Mar 9 07:53:20 GMT 2011

Rather than always having to call len(getmessagelist.keys()) as was done
before. No functional change, just nicer looking code. Also the SQLite
backend or other backends could implement more efficient implementations.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
 offlineimap/accounts.py    |    6 +++---
 offlineimap/folder/Base.py |    4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py
index 0f8c7be..e89c099 100644
--- a/offlineimap/accounts.py
+++ b/offlineimap/accounts.py
@@ -325,13 +325,13 @@ def syncfolder(accountname, remoterepos, remotefolder, localrepos,
         ui.syncingfolder(remoterepos, remotefolder, localrepos, localfolder)
         ui.loadmessagelist(localrepos, localfolder)
-        ui.messagelistloaded(localrepos, localfolder, len(localfolder.getmessagelist().keys()))
+        ui.messagelistloaded(localrepos, localfolder, localfolder.getmessagecount())
         # If either the local or the status folder has messages and there is a UID
         # validity problem, warn and abort.  If there are no messages, UW IMAPd
         # loses UIDVALIDITY.  But we don't really need it if both local folders are
         # empty.  So, in that case, just save it off.
-        if len(localfolder.getmessagelist()) or len(statusfolder.getmessagelist()):
+        if localfolder.getmessagecount() or statusfolder.getmessagecount():
             if not localfolder.isuidvalidityok():
@@ -348,7 +348,7 @@ def syncfolder(accountname, remoterepos, remotefolder, localrepos,
         ui.loadmessagelist(remoterepos, remotefolder)
         ui.messagelistloaded(remoterepos, remotefolder,
-                             len(remotefolder.getmessagelist().keys()))
+                             remotefolder.getmessagecount())
         # Synchronize remote changes.
         ui.syncingmessages(remoterepos, remotefolder, localrepos, localfolder)
diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index 5c06e6e..ffbbbbb 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -139,6 +139,10 @@ class BaseFolder:
         You must call cachemessagelist() before calling this function!"""
         raise NotImplementedException
+    def getmessagecount(self):
+        """Gets the number of messages."""
+        return len(self.getmessagelist())
     def getmessage(self, uid):
         """Returns the content of the specified message."""
         raise NotImplementedException

More information about the OfflineIMAP-project mailing list