Code

Sending of PGP-Encrypted mail to all users or selected users (via roles)
[roundup.git] / roundup / backends / __init__.py
index 3a1d7b36332d052040b89497d824e32c8dd3ede6..1fc794888400e87c30a1df4ad50dd61ac872ef0e 100644 (file)
 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
-# 
-# $Id: __init__.py,v 1.9 2001-12-12 02:30:51 richard Exp $
+#
+# $Id: __init__.py,v 1.40 2007-11-07 20:47:12 richard Exp $
 
-__all__ = []
+'''Container for the hyperdb storage backend implementations.
+'''
+__docformat__ = 'restructuredtext'
 
-try:
-    import anydbm, dumbdbm
-    # dumbdbm in python 2,2b2, 2.1.1 and earlier is seriously broken
-    assert anydbm._defaultmod != dumbdbm
-    del anydbm
-    del dumbdbm
-    import back_anydbm
-    anydbm = back_anydbm
-    __all__.append('anydbm')
-except AssertionError:
-    del back_anydbm
-except ImportError:
-    pass
+import sys
 
-try:
-    import back_bsddb
-    bsddb = back_bsddb
-    __all__.append('bsddb')
-except ImportError:
-    pass
+# These names are used to suppress import errors.
+# If get_backend raises an ImportError with appropriate
+# module name, have_backend quietly returns False.
+# Otherwise the error is reraised.
+_modules = {
+    'mysql': ('MySQLdb',),
+    'postgresql': ('psycopg',),
+    'tsearch2': ('psycopg',),
+    'sqlite': ('pysqlite', 'pysqlite2', 'sqlite3', '_sqlite3', 'sqlite'),
+}
 
-try:
-    import back_bsddb3
-    bsddb3 = back_bsddb3
-    __all__.append('bsddb3')
-except ImportError:
-    pass
+def get_backend(name):
+    '''Get a specific backend by name.'''
+    vars = globals()
+    # if requested backend has been imported yet, return current instance
+    if name in vars:
+        return vars[name]
+    # import the backend module
+    module_name = 'back_%s' % name
+    try:
+        module = __import__(module_name, vars)
+    except:
+        # import failed, but in versions prior to 2.4, a (broken)
+        # module is left in sys.modules and package globals;
+        # subsequent imports would succeed and get the broken module.
+        # This no longer happens in Python 2.4 and later.
+        if sys.version_info < (2, 4):
+            del sys.modules['.'.join((__name__, module_name))]
+            del vars[module_name]
+        raise
+    else:
+        vars[name] = module
+        return module
 
-#
-# $Log: not supported by cvs2svn $
-# Revision 1.8  2001/12/10 22:20:01  richard
-# Enabled transaction support in the bsddb backend. It uses the anydbm code
-# where possible, only replacing methods where the db is opened (it uses the
-# btree opener specifically.)
-# Also cleaned up some change note generation.
-# Made the backends package work with pydoc too.
-#
-# Revision 1.7  2001/12/10 00:57:38  richard
-# From CHANGES:
-#  . Added the "display" command to the admin tool - displays a node's values
-#  . #489760 ] [issue] only subject
-#  . fixed the doc/index.html to include the quoting in the mail alias.
-#
-# Also:
-#  . fixed roundup-admin so it works with transactions
-#  . disabled the back_anydbm module if anydbm tries to use dumbdbm
-#
-# Revision 1.6  2001/08/07 00:24:42  richard
-# stupid typo
-#
-# Revision 1.5  2001/08/07 00:15:51  richard
-# Added the copyright/license notice to (nearly) all files at request of
-# Bizar Software.
-#
-#
-#
-# vim: set filetype=python ts=4 sw=4 et si
+def have_backend(name):
+    '''Is backend "name" available?'''
+    if name == 'tsearch2':
+        # currently not working
+        return 0
+    try:
+        get_backend(name)
+        return 1
+    except ImportError, e:
+        for name in _modules.get(name, (name,)):
+            if str(e).startswith('No module named %s'%name):
+                return 0
+        raise
+    return 0
+
+def list_backends():
+    '''List all available backend names.
+
+    This function has side-effect of registering backward-compatible
+    globals for all available backends.
+
+    '''
+    l = []
+    for name in 'anydbm', 'mysql', 'sqlite', 'postgresql':
+        if have_backend(name):
+            l.append(name)
+    return l
+
+# vim: set filetype=python sts=4 sw=4 et si :