~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/db/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
from django.core import signals
4
4
from django.core.exceptions import ImproperlyConfigured
5
5
from django.utils.functional import curry
 
6
from django.utils.importlib import import_module
6
7
 
7
8
__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
8
9
 
9
10
if not settings.DATABASE_ENGINE:
10
11
    settings.DATABASE_ENGINE = 'dummy'
11
12
 
12
 
try:
13
 
    # Most of the time, the database backend will be one of the official
14
 
    # backends that ships with Django, so look there first.
15
 
    _import_path = 'django.db.backends.'
16
 
    backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
17
 
except ImportError, e:
18
 
    # If the import failed, we might be looking for a database backend
19
 
    # distributed external to Django. So we'll try that next.
 
13
def load_backend(backend_name):
20
14
    try:
21
 
        _import_path = ''
22
 
        backend = __import__('%s.base' % settings.DATABASE_ENGINE, {}, {}, [''])
23
 
    except ImportError, e_user:
24
 
        # The database backend wasn't found. Display a helpful error message
25
 
        # listing all possible (built-in) database backends.
26
 
        backend_dir = os.path.join(__path__[0], 'backends')
 
15
        # Most of the time, the database backend will be one of the official
 
16
        # backends that ships with Django, so look there first.
 
17
        return import_module('.base', 'django.db.backends.%s' % backend_name)
 
18
    except ImportError, e:
 
19
        # If the import failed, we might be looking for a database backend
 
20
        # distributed external to Django. So we'll try that next.
27
21
        try:
28
 
            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')]
29
 
        except EnvironmentError:
30
 
            available_backends = []
31
 
        available_backends.sort()
32
 
        if settings.DATABASE_ENGINE not in available_backends:
33
 
            raise ImproperlyConfigured, "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
34
 
                (settings.DATABASE_ENGINE, ", ".join(map(repr, available_backends)), e_user)
35
 
        else:
36
 
            raise # If there's some other error, this must be an error in Django itself.
37
 
 
38
 
# Convenient aliases for backend bits.
39
 
connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)
 
22
            return import_module('.base', backend_name)
 
23
        except ImportError, e_user:
 
24
            # The database backend wasn't found. Display a helpful error message
 
25
            # listing all possible (built-in) database backends.
 
26
            backend_dir = os.path.join(__path__[0], 'backends')
 
27
            try:
 
28
                available_backends = [f for f in os.listdir(backend_dir)
 
29
                        if os.path.isdir(os.path.join(backend_dir, f))
 
30
                        and not f.startswith('.')]
 
31
            except EnvironmentError:
 
32
                available_backends = []
 
33
            available_backends.sort()
 
34
            if backend_name not in available_backends:
 
35
                error_msg = "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
 
36
                    (backend_name, ", ".join(map(repr, available_backends)), e_user)
 
37
                raise ImproperlyConfigured(error_msg)
 
38
            else:
 
39
                raise # If there's some other error, this must be an error in Django itself.
 
40
 
 
41
backend = load_backend(settings.DATABASE_ENGINE)
 
42
 
 
43
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
 
44
# for backend bits.
 
45
 
 
46
# DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
 
47
# we manually create the dictionary from the settings, passing only the
 
48
# settings that the database backends care about. Note that TIME_ZONE is used
 
49
# by the PostgreSQL backends.
 
50
connection = backend.DatabaseWrapper({
 
51
    'DATABASE_HOST': settings.DATABASE_HOST,
 
52
    'DATABASE_NAME': settings.DATABASE_NAME,
 
53
    'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
 
54
    'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
 
55
    'DATABASE_PORT': settings.DATABASE_PORT,
 
56
    'DATABASE_USER': settings.DATABASE_USER,
 
57
    'TIME_ZONE': settings.TIME_ZONE,
 
58
})
40
59
DatabaseError = backend.DatabaseError
41
60
IntegrityError = backend.IntegrityError
42
61