Fixed #10487 -- Refactored the database-backend-selection logic into a function, django.db.load_backend. Thanks to Alex Gaynor for the initial patch

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10045 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2009-03-13 04:41:45 +00:00
parent c3dc837950
commit d8744cca26
1 changed files with 26 additions and 24 deletions

View File

@ -9,32 +9,34 @@ __all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
if not settings.DATABASE_ENGINE: if not settings.DATABASE_ENGINE:
settings.DATABASE_ENGINE = 'dummy' settings.DATABASE_ENGINE = 'dummy'
try: def load_backend(backend_name):
# Most of the time, the database backend will be one of the official
# backends that ships with Django, so look there first.
_import_path = 'django.db.backends.'
backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
except ImportError, e:
# If the import failed, we might be looking for a database backend
# distributed external to Django. So we'll try that next.
try: try:
_import_path = '' # Most of the time, the database backend will be one of the official
backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, ['']) # backends that ships with Django, so look there first.
except ImportError, e_user: return __import__('django.db.backends.%s.base' % backend_name, {}, {}, [''])
# The database backend wasn't found. Display a helpful error message except ImportError, e:
# listing all possible (built-in) database backends. # If the import failed, we might be looking for a database backend
backend_dir = os.path.join(__path__[0], 'backends') # distributed external to Django. So we'll try that next.
try: try:
available_backends = [f for f in os.listdir(backend_dir) return __import__('%s.base' % backend_name, {}, {}, [''])
if os.path.isdir(os.path.join(backend_dir, f))] except ImportError, e_user:
except EnvironmentError: # The database backend wasn't found. Display a helpful error message
available_backends = [] # listing all possible (built-in) database backends.
available_backends.sort() backend_dir = os.path.join(__path__[0], 'backends')
if settings.DATABASE_ENGINE not in available_backends: try:
raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s\nError was: %s" % \ available_backends = [f for f in os.listdir(backend_dir)
(settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)), e_user) if os.path.isdir(os.path.join(backend_dir, f))]
else: except EnvironmentError:
raise # If there's some other error, this must be an error in Django itself. available_backends = []
available_backends.sort()
if backend_name not in available_backends:
error_msg = "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
(backend_name, ", ".join(map(repr, available_backends)), e_user)
raise ImproperlyConfigured(error_msg)
else:
raise # If there's some other error, this must be an error in Django itself.
backend = load_backend(settings.DATABASE_ENGINE)
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
# for backend bits. # for backend bits.