2
Settings and configuration for Django.
4
Values will be read from the module specified by the DJANGO_SETTINGS_MODULE environment
5
variable, and then from django.conf.global_settings; see the global settings file for
6
a list of all possible variables.
10
import time # Needed for Windows
11
from django.conf import global_settings
13
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
15
class LazySettings(object):
17
A lazy proxy for either global Django settings or a custom settings object.
18
The user can manually configure settings prior to using them. Otherwise,
19
Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE.
22
# _target must be either None or something that supports attribute
23
# access (getattr, hasattr, etc).
26
def __getattr__(self, name):
27
if self._target is None:
28
self._import_settings()
29
if name == '__members__':
30
# Used to implement dir(obj), for example.
31
return self._target.get_all_members()
32
return getattr(self._target, name)
34
def __setattr__(self, name, value):
36
# Assign directly to self.__dict__, because otherwise we'd call
37
# __setattr__(), which would be an infinite loop.
38
self.__dict__['_target'] = value
40
setattr(self._target, name, value)
42
def _import_settings(self):
44
Load the settings module pointed to by the environment variable. This
45
is used the first time we need any settings at all, if the user has not
46
previously configured the settings manually.
49
settings_module = os.environ[ENVIRONMENT_VARIABLE]
50
if not settings_module: # If it's set but is an empty string.
53
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
55
self._target = Settings(settings_module)
57
def configure(self, default_settings=global_settings, **options):
59
Called to manually configure the settings. The 'default_settings'
60
parameter sets where to retrieve any unspecified values from (its
61
argument must support attribute access (__getattr__)).
63
if self._target != None:
64
raise EnvironmentError, 'Settings already configured.'
65
holder = UserSettingsHolder(default_settings)
66
for name, value in options.items():
67
setattr(holder, name, value)
70
class Settings(object):
71
def __init__(self, settings_module):
72
# update this dict from global settings (but only for ALL_CAPS settings)
73
for setting in dir(global_settings):
74
if setting == setting.upper():
75
setattr(self, setting, getattr(global_settings, setting))
77
# store the settings module in case someone later cares
78
self.SETTINGS_MODULE = settings_module
81
mod = __import__(self.SETTINGS_MODULE, {}, {}, [''])
82
except ImportError, e:
83
raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
85
# Settings that should be converted into tuples if they're mistakenly entered
87
tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
89
for setting in dir(mod):
90
if setting == setting.upper():
91
setting_value = getattr(mod, setting)
92
if setting in tuple_settings and type(setting_value) == str:
93
setting_value = (setting_value,) # In case the user forgot the comma.
94
setattr(self, setting, setting_value)
96
# Expand entries in INSTALLED_APPS like "django.contrib.*" to a list
98
new_installed_apps = []
99
for app in self.INSTALLED_APPS:
100
if app.endswith('.*'):
101
appdir = os.path.dirname(__import__(app[:-2], {}, {}, ['']).__file__)
102
for d in os.listdir(appdir):
103
if d.isalpha() and os.path.isdir(os.path.join(appdir, d)):
104
new_installed_apps.append('%s.%s' % (app[:-2], d))
106
new_installed_apps.append(app)
107
self.INSTALLED_APPS = new_installed_apps
109
if hasattr(time, 'tzset'):
110
# Move the time zone info into os.environ. See ticket #2315 for why
111
# we don't do this unconditionally (breaks Windows).
112
os.environ['TZ'] = self.TIME_ZONE
114
def get_all_members(self):
117
class UserSettingsHolder(object):
119
Holder for user configured settings.
121
# SETTINGS_MODULE doesn't make much sense in the manually configured
123
SETTINGS_MODULE = None
125
def __init__(self, default_settings):
127
Requests for configuration variables not in this class are satisfied
128
from the module specified in default_settings (if possible).
130
self.default_settings = default_settings
132
def __getattr__(self, name):
133
return getattr(self.default_settings, name)
135
def get_all_members(self):
136
return dir(self) + dir(self.default_settings)
138
settings = LazySettings()
140
# This function replaces itself with django.utils.translation.gettext() the
141
# first time it's run. This is necessary because the import of
142
# django.utils.translation requires a working settings module, and loading it
143
# from within this file would cause a circular import.
144
def first_time_gettext(*args):
145
from django.utils.translation import gettext
146
__builtins__['_'] = gettext
147
return gettext(*args)
149
__builtins__['_'] = first_time_gettext