[PATCH] Create an abstract Repository class

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Mar 2 11:09:27 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>
---
Much of the added LOC are GPL boilerplate, so the changes are not as scary as they might look at first sight. Based on this, I plan to hand back a LocalStatusSQLiteRepository if the user has it configured.
 Changelog.draft.rst                |    2 +
 offlineimap/accounts.py            |   17 +++-----
 offlineimap/repository/Base.py     |   27 +------------
 offlineimap/repository/Gmail.py    |    2 +-
 offlineimap/repository/IMAP.py     |    2 +-
 offlineimap/repository/__init__.py |   72 +++++++++++++++++++++++++++++++++++-
 6 files changed, 84 insertions(+), 38 deletions(-)

diff --git a/Changelog.draft.rst b/Changelog.draft.rst
index 44b4a87..f49da37 100644
--- a/Changelog.draft.rst
+++ b/Changelog.draft.rst
@@ -20,6 +20,8 @@ Changes
 
 * Makefile use magic to find the version number.
 
+* Rework the repository module
+
 Bug Fixes
 ---------
 
diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py
index 4d1974c..ff53647 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
@@ -165,8 +165,8 @@ class Account(CustomConfig.ConfigHelperMixin):
         for item in kaobjs:
             item.stopkeepalive()
         return sleepresult
-            
-    
+
+
 class SyncableAccount(Account):
     """A syncable IMAP account.
 
@@ -179,13 +179,10 @@ class SyncableAccount(Account):
         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 98add53..46af762 100644
--- a/offlineimap/repository/Base.py
+++ b/offlineimap/repository/Base.py
@@ -16,33 +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 sys
 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..ea97b27 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>
+#
+#    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