Skip to content

Commit 681763a

Browse files
committed
multi-auth: Merged to [2964]
git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@2965 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent d659956 commit 681763a

File tree

36 files changed

+2660
-1723
lines changed

36 files changed

+2660
-1723
lines changed

django/conf/__init__.py

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,63 @@
1212

1313
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
1414

15-
class Settings:
15+
class LazySettings:
16+
"""
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.
20+
"""
21+
def __init__(self):
22+
# _target must be either None or something that supports attribute
23+
# access (getattr, hasattr, etc).
24+
self._target = None
25+
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)
33+
34+
def __setattr__(self, name, value):
35+
if name == '_target':
36+
# Assign directly to self.__dict__, because otherwise we'd call
37+
# __setattr__(), which would be an infinite loop.
38+
self.__dict__['_target'] = value
39+
else:
40+
setattr(self._target, name, value)
41+
42+
def _import_settings(self):
43+
"""
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.
47+
"""
48+
try:
49+
settings_module = os.environ[ENVIRONMENT_VARIABLE]
50+
if not settings_module: # If it's set but is an empty string.
51+
raise KeyError
52+
except KeyError:
53+
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
54+
55+
self._target = Settings(settings_module)
56+
57+
def configure(self, default_settings=global_settings, **options):
58+
"""
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__)).
62+
"""
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)
68+
self._target = holder
1669

70+
class Settings:
1771
def __init__(self, settings_module):
18-
1972
# update this dict from global settings (but only for ALL_CAPS settings)
2073
for setting in dir(global_settings):
2174
if setting == setting.upper():
@@ -27,7 +80,7 @@ def __init__(self, settings_module):
2780
try:
2881
mod = __import__(self.SETTINGS_MODULE, '', '', [''])
2982
except ImportError, e:
30-
raise EnvironmentError, "Could not import settings '%s' (is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)
83+
raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
3184

3285
# Settings that should be converted into tuples if they're mistakenly entered
3386
# as strings.
@@ -56,18 +109,32 @@ def __init__(self, settings_module):
56109
# move the time zone info into os.environ
57110
os.environ['TZ'] = self.TIME_ZONE
58111

59-
# try to load DJANGO_SETTINGS_MODULE
60-
try:
61-
settings_module = os.environ[ENVIRONMENT_VARIABLE]
62-
if not settings_module: # If it's set but is an empty string.
63-
raise KeyError
64-
except KeyError:
65-
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
112+
def get_all_members(self):
113+
return dir(self)
114+
115+
class UserSettingsHolder:
116+
"""
117+
Holder for user configured settings.
118+
"""
119+
# SETTINGS_MODULE does not really make sense in the manually configured
120+
# (standalone) case.
121+
SETTINGS_MODULE = None
66122

67-
# instantiate the configuration object
68-
settings = Settings(settings_module)
123+
def __init__(self, default_settings):
124+
"""
125+
Requests for configuration variables not in this class are satisfied
126+
from the module specified in default_settings (if possible).
127+
"""
128+
self.default_settings = default_settings
129+
130+
def __getattr__(self, name):
131+
return getattr(self.default_settings, name)
132+
133+
def get_all_members(self):
134+
return dir(self) + dir(self.default_settings)
135+
136+
settings = LazySettings()
69137

70138
# install the translation machinery so that it is available
71139
from django.utils import translation
72140
translation.install()
73-
4.58 KB
Binary file not shown.

0 commit comments

Comments
 (0)