Skip to content

Commit

Permalink
Improve repository/Maildir.getfolder() to use cached objects
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
spaetz committed Sep 19, 2011
1 parent c7420e6 commit 792243c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions offlineimap/repository/Maildir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *

Expand Down Expand Up @@ -114,11 +115,20 @@ def deletefolder(self, foldername):
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)

"""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
Expand Down Expand Up @@ -157,11 +167,7 @@ def _getfolders_scandir(self, root, extension = None):
os.path.isdir(os.path.join(fullname, 'tmp'))):
# This directory has maildir stuff -- process
self.debug(" This is maildir folder '%s'." % foldername)

if self.config.has_option('Repository %s' % self,
'restoreatime') and \
self.config.getboolean('Repository %s' % self,
'restoreatime'):
if self.getconfboolean('restoreatime', False):
self._append_folder_atimes(foldername)
retval.append(folder.Maildir.MaildirFolder(self.root,
foldername,
Expand Down

0 comments on commit 792243c

Please sign in to comment.