Ticket #5454: user_backend_r6284.patch

File user_backend_r6284.patch, 3.5 KB (added by George Vilches <gav@…>, 17 years ago)

Moved global variables to curried functions.

  • django/db/__init__.py

     
    11from django.conf import settings
    22from django.core import signals
    33from django.dispatch import dispatcher
     4from django.utils.functional import curry
    45
    56__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
    67
     
    89    settings.DATABASE_ENGINE = 'dummy'
    910
    1011try:
    11     backend = __import__('django.db.backends.%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
     12    import_path = 'django.db.backends.'
     13    backend = __import__('%s%s.base' % (import_path, settings.DATABASE_ENGINE), {}, {}, [''])
    1214except ImportError, e:
    13     # The database backend wasn't found. Display a helpful error message
    14     # listing all possible database backends.
    15     from django.core.exceptions import ImproperlyConfigured
    16     import os
    17     backend_dir = os.path.join(__path__[0], 'backends')
    18     available_backends = [f for f in os.listdir(backend_dir) if not f.startswith('_') and not f.startswith('.') and not f.endswith('.py') and not f.endswith('.pyc')]
    19     available_backends.sort()
    20     if settings.DATABASE_ENGINE not in available_backends:
    21         raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s" % \
    22             (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)))
    23     else:
    24         raise # If there's some other error, this must be an error in Django itself.
     15    try:
     16        import_path = ''
     17        backend = __import__('%s%s.base' % (import_path, settings.DATABASE_ENGINE), {}, {}, [''])
     18    except ImportError, e_user:
     19        # The database backend wasn't found. Display a helpful error message
     20        # listing all possible database backends.
     21        from django.core.exceptions import ImproperlyConfigured
     22        import os
     23        backend_dir = os.path.join(__path__[0], 'backends')
     24        available_backends = [f for f in os.listdir(backend_dir) if not f.startswith('_') and not f.startswith('.') and not f.endswith('.py') and not f.endswith('.pyc')]
     25        available_backends.sort()
     26        if settings.DATABASE_ENGINE not in available_backends:
     27            raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s" % \
     28                (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)))
     29        else:
     30            raise # If there's some other error, this must be an error in Django itself.
    2531
    26 get_introspection_module = lambda: __import__('django.db.backends.%s.introspection' % settings.DATABASE_ENGINE, {}, {}, [''])
    27 get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, {}, {}, [''])
    28 runshell = lambda: __import__('django.db.backends.%s.client' % settings.DATABASE_ENGINE, {}, {}, ['']).runshell()
     32def get_module_curried(import_path='', module_name=''):
     33    return __import__('%s%s.%s' % (import_path, settings.DATABASE_ENGINE, module_name), {}, {}, [''])
    2934
     35get_introspection_module = curry(get_module_curried, import_path=import_path, module_name='introspection')
     36get_creation_module = curry(get_module_curried, import_path=import_path, module_name='creation')
     37runshell = curry(get_module_curried, import_path=import_path)(module_name='client').runshell
     38
    3039connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
    3140DatabaseError = backend.DatabaseError
    3241IntegrityError = backend.IntegrityError
Back to Top