Improve repository/Maildir.getfolder() to use cached objects

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@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2011-08-29 15:33:58 +02:00
parent c7420e6ad4
commit 792243c78f

View File

@ -19,6 +19,7 @@
from Base import BaseRepository from Base import BaseRepository
from offlineimap import folder from offlineimap import folder
from offlineimap.ui import getglobalui from offlineimap.ui import getglobalui
from offlineimap.error import OfflineImapError
import os import os
from stat import * from stat import *
@ -114,11 +115,20 @@ class MaildirRepository(BaseRepository):
self.ui.warn("NOT YET IMPLEMENTED: DELETE FOLDER %s" % foldername) self.ui.warn("NOT YET IMPLEMENTED: DELETE FOLDER %s" % foldername)
def getfolder(self, foldername): def getfolder(self, foldername):
if self.config.has_option('Repository ' + self.name, 'restoreatime') and self.config.getboolean('Repository ' + self.name, 'restoreatime'): """Return a Folder instance of this Maildir
self._append_folder_atimes(foldername)
return folder.Maildir.MaildirFolder(self.root, foldername, If necessary, scan and cache all foldernames to make sure that
self.getsep(), self) 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): def _getfolders_scandir(self, root, extension = None):
"""Recursively scan folder 'root'; return a list of MailDirFolder """Recursively scan folder 'root'; return a list of MailDirFolder
@ -157,11 +167,7 @@ class MaildirRepository(BaseRepository):
os.path.isdir(os.path.join(fullname, 'tmp'))): os.path.isdir(os.path.join(fullname, 'tmp'))):
# This directory has maildir stuff -- process # This directory has maildir stuff -- process
self.debug(" This is maildir folder '%s'." % foldername) self.debug(" This is maildir folder '%s'." % foldername)
if self.getconfboolean('restoreatime', False):
if self.config.has_option('Repository %s' % self,
'restoreatime') and \
self.config.getboolean('Repository %s' % self,
'restoreatime'):
self._append_folder_atimes(foldername) self._append_folder_atimes(foldername)
retval.append(folder.Maildir.MaildirFolder(self.root, retval.append(folder.Maildir.MaildirFolder(self.root,
foldername, foldername,