From 410e2d35e97ce6a0fcbc3519a7725d3bfb8702f4 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Fri, 16 Sep 2011 10:54:23 +0200 Subject: [PATCH] Set accountname in BaseFolder, and don't pass it in initialization We passed in the accountname to all derivatives of BaseFolder, such as IMAPFolder(...,repository,...,accountname), although it is perfectly possible to get the accountname from the Repository(). So remove this unneeded parameter. Each backend had to define getaccountname() (although the function is hardly used and most accessed .accountname directly). On the other hand BaseFolder was using getaccountname but it never defined the function. So make the sane thing, remove all definitions from backends and define accountname() once in Basefolder. It was made a property and not just a (public) attribute, so it will show up in our developer documentation as public API. Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/folder/Base.py | 11 ++++++++--- offlineimap/folder/Gmail.py | 4 ++-- offlineimap/folder/IMAP.py | 6 +----- offlineimap/folder/LocalStatus.py | 6 +----- offlineimap/folder/LocalStatusSQLite.py | 3 +-- offlineimap/folder/Maildir.py | 6 +----- offlineimap/repository/Base.py | 11 +++++++---- offlineimap/repository/IMAP.py | 6 +++--- offlineimap/repository/LocalStatus.py | 2 +- offlineimap/repository/Maildir.py | 3 +-- 10 files changed, 26 insertions(+), 32 deletions(-) diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py index 9c8feeb..3339424 100644 --- a/offlineimap/folder/Base.py +++ b/offlineimap/folder/Base.py @@ -44,6 +44,11 @@ class BaseFolder(object): def __str__(self): return self.name + @property + def accountname(self): + """Account name as string""" + return self.repository.accountname + def suggeststhreads(self): """Returns true if this folder suggests using threads for actions; false otherwise. Probably only IMAP will return true.""" @@ -239,7 +244,7 @@ class BaseFolder(object): # self.getmessage(). So, don't call self.getmessage unless # really needed. if register: # output that we start a new thread - self.ui.registerthread(self.getaccountname()) + self.ui.registerthread(self.accountname) try: message = None @@ -295,7 +300,7 @@ class BaseFolder(object): self.ui.error(e, exc_info()[2]) except Exception, e: self.ui.error(e, "Copying message %s [acc: %s]:\n %s" %\ - (uid, self.getaccountname(), + (uid, self.accountname, traceback.format_exc())) raise #raise on unknown errors, so we can fix those @@ -443,5 +448,5 @@ class BaseFolder(object): self.ui.error(e, exc_info()[2]) except Exception, e: self.ui.error(e, exc_info()[2], "Syncing folder %s [acc: %s]" %\ - (self, self.getaccountname())) + (self, self.accountname)) raise # raise unknown Exceptions so we can fix them diff --git a/offlineimap/folder/Gmail.py b/offlineimap/folder/Gmail.py index e65793d..dc301d0 100644 --- a/offlineimap/folder/Gmail.py +++ b/offlineimap/folder/Gmail.py @@ -33,9 +33,9 @@ class GmailFolder(IMAPFolder): http://mail.google.com/support/bin/answer.py?answer=77657&topic=12815 """ - def __init__(self, imapserver, name, visiblename, accountname, repository): + def __init__(self, imapserver, name, visiblename, repository): super(GmailFolder, self).__init__(imapserver, name, visiblename, - accountname, repository) + repository) self.realdelete = repository.getrealdelete(name) self.trash_folder = repository.gettrashfolder(name) #: Gmail will really delete messages upon EXPUNGE in these folders diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index f17e2bc..32625fa 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -32,7 +32,7 @@ except NameError: class IMAPFolder(BaseFolder): - def __init__(self, imapserver, name, visiblename, accountname, repository): + def __init__(self, imapserver, name, visiblename, repository): name = imaputil.dequote(name) super(IMAPFolder, self).__init__(name, repository) self.config = imapserver.config @@ -42,7 +42,6 @@ class IMAPFolder(BaseFolder): self.imapserver = imapserver self.messagelist = None self.visiblename = visiblename - self.accountname = accountname self.randomgenerator = random.Random() #self.ui is set in BaseFolder @@ -60,9 +59,6 @@ class IMAPFolder(BaseFolder): except imapobj.readonly: imapobj.select(self.getfullname(), readonly = 1) - def getaccountname(self): - return self.accountname - def suggeststhreads(self): return 1 diff --git a/offlineimap/folder/LocalStatus.py b/offlineimap/folder/LocalStatus.py index 732c5a6..ad7ccb6 100644 --- a/offlineimap/folder/LocalStatus.py +++ b/offlineimap/folder/LocalStatus.py @@ -26,7 +26,7 @@ except NameError: magicline = "OFFLINEIMAP LocalStatus CACHE DATA - DO NOT MODIFY - FORMAT 1" class LocalStatusFolder(BaseFolder): - def __init__(self, root, name, repository, accountname, config): + def __init__(self, root, name, repository, config): super(LocalStatusFolder, self).__init__(name, repository) self.root = root self.sep = '.' @@ -36,10 +36,6 @@ class LocalStatusFolder(BaseFolder): self.savelock = threading.Lock() self.doautosave = config.getdefaultboolean("general", "fsync", False) """Should we perform fsyncs as often as possible?""" - self.accountname = accountname - - def getaccountname(self): - return self.accountname def storesmessages(self): return 0 diff --git a/offlineimap/folder/LocalStatusSQLite.py b/offlineimap/folder/LocalStatusSQLite.py index bd38930..6eccbaa 100644 --- a/offlineimap/folder/LocalStatusSQLite.py +++ b/offlineimap/folder/LocalStatusSQLite.py @@ -46,10 +46,9 @@ class LocalStatusSQLiteFolder(LocalStatusFolder): #current version of our db format cur_version = 1 - def __init__(self, root, name, repository, accountname, config): + def __init__(self, root, name, repository, config): super(LocalStatusSQLiteFolder, self).__init__(root, name, repository, - accountname, config) # dblock protects against concurrent writes in same connection diff --git a/offlineimap/folder/Maildir.py b/offlineimap/folder/Maildir.py index 20b306b..5d75121 100644 --- a/offlineimap/folder/Maildir.py +++ b/offlineimap/folder/Maildir.py @@ -58,14 +58,13 @@ def gettimeseq(): timelock.release() class MaildirFolder(BaseFolder): - def __init__(self, root, name, sep, repository, accountname, config): + def __init__(self, root, name, sep, repository, config): super(MaildirFolder, self).__init__(name, repository) self.config = config self.dofsync = config.getdefaultboolean("general", "fsync", True) self.root = root self.sep = sep self.messagelist = None - self.accountname = accountname self.wincompatible = self.config.getdefaultboolean( "Account "+self.accountname, "maildir-windows-compatible", False) @@ -80,9 +79,6 @@ class MaildirFolder(BaseFolder): # Cache the full folder path, as we use getfullname() very often self._fullname = os.path.join(self.getroot(), self.getname()) - def getaccountname(self): - return self.accountname - def getfullname(self): """Return the absolute file path to the Maildir folder (sans cur|new)""" return self._fullname diff --git a/offlineimap/repository/Base.py b/offlineimap/repository/Base.py index 72a5f88..ae17ee8 100644 --- a/offlineimap/repository/Base.py +++ b/offlineimap/repository/Base.py @@ -22,13 +22,14 @@ from offlineimap import CustomConfig from offlineimap.ui import getglobalui class BaseRepository(object, CustomConfig.ConfigHelperMixin): + def __init__(self, reposname, account): self.ui = getglobalui() self.account = account self.config = account.getconfig() self.name = reposname self.localeval = account.getlocaleval() - self.accountname = self.account.getname() + self._accountname = self.account.getname() self.uiddir = os.path.join(self.config.getmetadatadir(), 'Repository-' + self.name) if not os.path.exists(self.uiddir): os.mkdir(self.uiddir, 0700) @@ -71,15 +72,17 @@ class BaseRepository(object, CustomConfig.ConfigHelperMixin): def __str__(self): return self.name + @property + def accountname(self): + """Account name as string""" + return self._accountname + def getuiddir(self): return self.uiddir def getmapdir(self): return self.mapdir - def getaccountname(self): - return self.accountname - def getsection(self): return 'Repository ' + self.name diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py index daa31c9..0de6044 100644 --- a/offlineimap/repository/IMAP.py +++ b/offlineimap/repository/IMAP.py @@ -261,7 +261,7 @@ class IMAPRepository(BaseRepository): def getfolder(self, foldername): return self.getfoldertype()(self.imapserver, foldername, self.nametrans(foldername), - self.accountname, self) + self) def getfoldertype(self): return folder.IMAP.IMAPFolder @@ -303,7 +303,7 @@ class IMAPRepository(BaseRepository): continue retval.append(self.getfoldertype()(self.imapserver, foldername, self.nametrans(foldername), - self.accountname, self)) + self)) if len(self.folderincludes): imapobj = self.imapserver.acquireconnection() try: @@ -320,7 +320,7 @@ class IMAPRepository(BaseRepository): retval.append(self.getfoldertype()(self.imapserver, foldername, self.nametrans(foldername), - self.accountname, self)) + self)) finally: self.imapserver.releaseconnection(imapobj) diff --git a/offlineimap/repository/LocalStatus.py b/offlineimap/repository/LocalStatus.py index a392dcf..20291a1 100644 --- a/offlineimap/repository/LocalStatus.py +++ b/offlineimap/repository/LocalStatus.py @@ -83,7 +83,7 @@ class LocalStatusRepository(BaseRepository): def getfolder(self, foldername): """Return the Folder() object for a foldername""" return self.LocalStatusFolderClass(self.directory, foldername, - self, self.accountname, + self, self.config) def getfolders(self): diff --git a/offlineimap/repository/Maildir.py b/offlineimap/repository/Maildir.py index aeb9776..a7f7c79 100644 --- a/offlineimap/repository/Maildir.py +++ b/offlineimap/repository/Maildir.py @@ -118,7 +118,7 @@ class MaildirRepository(BaseRepository): self._append_folder_atimes(foldername) return folder.Maildir.MaildirFolder(self.root, foldername, self.getsep(), self, - self.accountname, self.config) + self.config) def _getfolders_scandir(self, root, extension = None): """Recursively scan folder 'root'; return a list of MailDirFolder @@ -168,7 +168,6 @@ class MaildirRepository(BaseRepository): foldername, self.getsep(), self, - self.accountname, self.config)) if self.getsep() == '/' and dirname != '.': # Recursively check sub-directories for folders too.