[PATCH 7/8] Improve repository/Maildir.getfolder() to use cached objects
Sebastian Spaeth
Sebastian at SSpaeth.de
Mon Aug 29 15:00:16 BST 2011
Previously, getfolder() would always construct new MaildirFolder()
objects, independent of whether the folder exists or not. Improve the
function to:
1) Scan and cache the folders if not already done
2) Return the same cached object if we ask for the same foldername twice
3) Reduce a tiny bit of code duplication
This is important because we handle stuff like folderfilter in the
scandir function and if we discard the scanned dir and create a new
object on folderget(), we will lose the folderfilter information.
Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
offlineimap/repository/Maildir.py | 21 +++++++++++++++------
1 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/offlineimap/repository/Maildir.py b/offlineimap/repository/Maildir.py
index ef3a723..6b0736b 100644
--- a/offlineimap/repository/Maildir.py
+++ b/offlineimap/repository/Maildir.py
@@ -19,6 +19,7 @@
from Base import BaseRepository
from offlineimap import folder
from offlineimap.ui import getglobalui
+from offlineimap.error import OfflineImapError
import os
from stat import *
@@ -110,12 +111,20 @@ class MaildirRepository(BaseRepository):
self.ui.warn("NOT YET IMPLEMENTED: DELETE FOLDER %s" % foldername)
def getfolder(self, foldername):
- if self.config.has_option('Repository ' + self.name, 'restoreatime') and self.config.getboolean('Repository ' + self.name, 'restoreatime'):
- self._append_folder_atimes(foldername)
- return folder.Maildir.MaildirFolder(self.root, foldername,
- self.getsep(), self,
- self.accountname, self.config)
-
+ """Return a Folder instance of this Maildir
+
+ If necessary, scan and cache all foldernames to make sure that
+ we only return existing folders and that 2 calls with the same
+ name will return the same object."""
+ # getfolders() will scan and cache the values *if* necessary
+ folders = self.getfolders()
+ for folder in folders:
+ if foldername == folder.name:
+ return folder
+ raise OfflineImapError("getfolder() asked for a nonexisting "
+ "folder '%s'." % foldername,
+ OfflineImapError.ERROR.FOLDER)
+
def _getfolders_scandir(self, root, extension = None):
"""Recursively scan folder 'root'; return a list of MailDirFolder
--
1.7.4.1
More information about the OfflineIMAP-project
mailing list