"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/roundup/backends/__init__.py" (26 Aug 2019, 2496 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "__init__.py": 1.6.1_vs_2.0.0.

    1 #
    2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
    3 # This module is free software, and you may redistribute it and/or modify
    4 # under the same terms as Python, so long as this copyright message and
    5 # disclaimer are retained in their original form.
    6 #
    7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
    8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
    9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
   10 # POSSIBILITY OF SUCH DAMAGE.
   11 #
   12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
   13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
   15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
   16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
   17 
   18 '''Container for the hyperdb storage backend implementations.
   19 '''
   20 __docformat__ = 'restructuredtext'
   21 
   22 import sys
   23 
   24 # These names are used to suppress import errors.
   25 # If get_backend raises an ImportError with appropriate
   26 # module name, have_backend quietly returns False.
   27 # Otherwise the error is reraised.
   28 _modules = {
   29     'mysql': ('MySQLdb',),
   30     'postgresql': ('psycopg2',),
   31     'sqlite': ('pysqlite', 'pysqlite2', 'sqlite3', '_sqlite3', 'sqlite'),
   32 }
   33 
   34 def get_backend(name):
   35     '''Get a specific backend by name.'''
   36     vars = globals()
   37     # if requested backend has been imported yet, return current instance
   38     if name in vars:
   39         return vars[name]
   40     # import the backend module
   41     module_name = 'back_%s' % name
   42     module = __import__(module_name, vars, level=1)
   43     vars[name] = module
   44     return module
   45 
   46 def have_backend(name):
   47     '''Is backend "name" available?'''
   48     try:
   49         get_backend(name)
   50         return 1
   51     except ImportError as e:
   52         if hasattr(e, 'name'):
   53             modname = e.name
   54         else:
   55             modname = e.args[0][16:] if e.args[0].startswith('No module named ') else None
   56 
   57         if modname and (modname in _modules.get(name, (name,))):
   58             return 0
   59         raise
   60     return 0
   61 
   62 def list_backends():
   63     '''List all available backend names.
   64 
   65     This function has side-effect of registering backward-compatible
   66     globals for all available backends.
   67 
   68     '''
   69     l = []
   70     for name in 'anydbm', 'mysql', 'sqlite', 'postgresql':
   71         if have_backend(name):
   72             l.append(name)
   73     return l
   74 
   75 # vim: set filetype=python sts=4 sw=4 et si :