[PATCHv2] Create an abstract Repository class

Sebastian Spaeth Sebastian at SSpaeth.de
Thu Mar 3 10:05:15 GMT 2011


A Repository() returns the correctly instanciated dervivate of a
BaseRepository, depending on the parameters passed to it. The returned
instance is eg an ImapRepository(). This makes the code look nicer,
and we have less functions lying around outside of classes (no more
global LoadRepository() function).

This will also enable us to conveniently hand back a
LocalStatusRepository based on SQLITE rather than plain text, if the
user configures this to be the experimental and optional backend
(once it exists).

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
v2 has been based on current master. The remaining (c) 202-2007 by John
is because most of the code logic has been copied from
repository/Base.py which has that copyright, so I retained it. I added a
newer Copyright header as you proposed.

 Changelog.draft.rst                |    2 +
 offlineimap/accounts.py            |   13 ++----
 offlineimap/repository/Base.py     |   26 +------------
 offlineimap/repository/Gmail.py    |    2 +-
 offlineimap/repository/IMAP.py     |    2 +-
 offlineimap/repository/__init__.py |   72 +++++++++++++++++++++++++++++++++++-
 6 files changed, 82 insertions(+), 35 deletions(-)

diff --git a/Changelog.draft.rst b/Changelog.draft.rst
index d9977a2..c0e1d40 100644
--- a/Changelog.draft.rst
+++ b/Changelog.draft.rst
@@ -16,6 +16,8 @@ New Features
 Changes
 -------
 
+* Rework the repository module
+
 Bug Fixes
 ---------
 
diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py
index 8c03e69..7a0f5a9 100644
--- a/offlineimap/accounts.py
+++ b/offlineimap/accounts.py
@@ -16,7 +16,7 @@
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
 from offlineimap import threadutil, mbnames, CustomConfig
-import offlineimap.repository.Base, offlineimap.repository.LocalStatus
+from offlineimap.repository import Repository
 from offlineimap.ui import getglobalui
 from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
 from subprocess import Popen, PIPE
@@ -175,13 +175,10 @@ class AccountSynchronizationMixin:
         if not os.path.exists(accountmetadata):
             os.mkdir(accountmetadata, 0700)            
 
-        self.remoterepos = offlineimap.repository.Base.LoadRepository(self.getconf('remoterepository'), self, 'remote')
-
-        # Connect to the local repository.
-        self.localrepos = offlineimap.repository.Base.LoadRepository(self.getconf('localrepository'), self, 'local')
-
-        # Connect to the local cache.
-        self.statusrepos = offlineimap.repository.LocalStatus.LocalStatusRepository(self.getconf('localrepository'), self)
+        # get all three repositories
+        self.remoterepos = Repository(self, 'remote')
+        self.localrepos  = Repository(self, 'local')
+        self.statusrepos = Repository(self, 'status')
 
         #might need changes here to ensure that one account sync does not crash others...
         if not self.refreshperiod:
diff --git a/offlineimap/repository/Base.py b/offlineimap/repository/Base.py
index 7e1dd4d..eae41b2 100644
--- a/offlineimap/repository/Base.py
+++ b/offlineimap/repository/Base.py
@@ -16,32 +16,10 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
-from offlineimap import CustomConfig
-from offlineimap.ui import getglobalui
 import os.path
 import traceback
-
-def LoadRepository(name, account, reqtype):
-    from offlineimap.repository.Gmail import GmailRepository
-    from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository
-    from offlineimap.repository.Maildir import MaildirRepository
-    if reqtype == 'remote':
-        # For now, we don't support Maildirs on the remote side.
-        typemap = {'IMAP': IMAPRepository,
-                   'Gmail': GmailRepository}
-    elif reqtype == 'local':
-        typemap = {'IMAP': MappedIMAPRepository,
-                   'Maildir': MaildirRepository}
-    else:
-        raise ValueError, "Request type %s not supported" % reqtype
-    config = account.getconfig()
-    repostype = config.get('Repository ' + name, 'type').strip()
-    try:
-        repo = typemap[repostype]
-    except KeyError:
-        raise Exception, "'%s' repository not supported for %s repositories."%\
-            (repostype, reqtype)
-    return repo(name, account)
+from offlineimap import CustomConfig
+from offlineimap.ui import getglobalui
 
 class BaseRepository(CustomConfig.ConfigHelperMixin):
     def __init__(self, reposname, account):
diff --git a/offlineimap/repository/Gmail.py b/offlineimap/repository/Gmail.py
index 4793db7..a586460 100644
--- a/offlineimap/repository/Gmail.py
+++ b/offlineimap/repository/Gmail.py
@@ -15,7 +15,7 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
-from IMAP import IMAPRepository
+from offlineimap.repository.IMAP import IMAPRepository
 from offlineimap import folder, imaputil
 from offlineimap.imapserver import IMAPServer
 
diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py
index 3bfa5db..2bc9718 100644
--- a/offlineimap/repository/IMAP.py
+++ b/offlineimap/repository/IMAP.py
@@ -16,7 +16,7 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
-from Base import BaseRepository
+from offlineimap.repository.Base import BaseRepository
 from offlineimap import folder, imaputil, imapserver
 from offlineimap.folder.UIDMaps import MappedIMAPFolder
 from offlineimap.threadutil import ExitNotifyThread
diff --git a/offlineimap/repository/__init__.py b/offlineimap/repository/__init__.py
index be5c29e..c63f6db 100644
--- a/offlineimap/repository/__init__.py
+++ b/offlineimap/repository/__init__.py
@@ -1 +1,71 @@
-__all__ = ['Gmail', 'IMAP', 'Base', 'Maildir', 'LocalStatus']
+# Copyright (C) 2002-2007 John Goerzen <jgoerzen at complete.org>
+#               2010 Sebastian Spaeth <Sebastian at SSpaeth.de> and contributors
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the Free Software
+#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+
+from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository
+from offlineimap.repository.Gmail import GmailRepository
+from offlineimap.repository.Maildir import MaildirRepository
+from offlineimap.repository.LocalStatus import LocalStatusRepository
+
+class Repository(object):
+    """Abstract class that returns the correct Repository type
+    instance based on 'account' and 'reqtype', e.g.  a
+    class:`ImapRepository` instance."""
+
+    def __new__(cls, account, reqtype):
+        """
+        :param account: :class:`Account`
+        :param regtype: 'remote', 'local', or 'status'"""
+
+        if reqtype == 'remote':
+            name = account.getconf('remoterepository')
+            # We don't support Maildirs on the remote side.
+            typemap = {'IMAP': IMAPRepository,
+                       'Gmail': GmailRepository}
+
+        elif reqtype == 'local':
+            name = account.getconf('localrepository')
+            typemap = {'IMAP': MappedIMAPRepository,
+                       'Maildir': MaildirRepository}
+
+        elif reqtype == 'status':
+            # create and return a LocalStatusRepository
+            name = account.getconf('localrepository')
+            return LocalStatusRepository(name, account)
+
+        else:
+            raise ValueError, "Request type %s not supported" % reqtype
+
+        config = account.getconfig()
+        repostype = config.get('Repository ' + name, 'type').strip()
+        try:
+            repo = typemap[repostype]
+        except KeyError:
+            raise Exception, "'%s' repository not supported for %s repositories."%\
+                (repostype, reqtype)
+        return repo(name, account)
+
+
+    def __init__(self, account, reqtype):
+        """Load the correct Repository type and return that. The
+        __init__ of the corresponding Repository class will be
+        executed instead of this stub
+
+        :param account: :class:`Account`
+        :param regtype: 'remote', 'local', or 'status'
+        """
+        pass
+
-- 
1.7.1





More information about the OfflineIMAP-project mailing list