12
12
13
13
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
14
14
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
16
69
70
+ class Settings :
17
71
def __init__ (self , settings_module ):
18
-
19
72
# update this dict from global settings (but only for ALL_CAPS settings)
20
73
for setting in dir (global_settings ):
21
74
if setting == setting .upper ():
@@ -27,7 +80,7 @@ def __init__(self, settings_module):
27
80
try :
28
81
mod = __import__ (self .SETTINGS_MODULE , '' , '' , ['' ])
29
82
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 )
31
84
32
85
# Settings that should be converted into tuples if they're mistakenly entered
33
86
# as strings.
@@ -56,18 +109,32 @@ def __init__(self, settings_module):
56
109
# move the time zone info into os.environ
57
110
os .environ ['TZ' ] = self .TIME_ZONE
58
111
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
66
122
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 ()
69
137
70
138
# install the translation machinery so that it is available
71
139
from django .utils import translation
72
140
translation .install ()
73
-
0 commit comments