1 """
2 Settings and configuration for Django.
3
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.
7 """
8
9 import os
10 import time
11 from django.conf import global_settings
12
13 ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
14
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 """
22
23
24 self._target = None
25
33
43
45 """
46 Load the settings module pointed to by the environment variable. This
47 is used the first time we need any settings at all, if the user has not
48 previously configured the settings manually.
49 """
50 try:
51 settings_module = os.environ[ENVIRONMENT_VARIABLE]
52 if not settings_module:
53 raise KeyError
54 except KeyError:
55 raise ImportError, "Environment variable %s is undefined so settings cannot be imported." % ENVIRONMENT_VARIABLE
56
57 self._target = Settings(settings_module)
58
71
74
75 for setting in dir(global_settings):
76 if setting == setting.upper():
77 setattr(self, setting, getattr(global_settings, setting))
78
79
80 self.SETTINGS_MODULE = settings_module
81
82 try:
83 mod = __import__(self.SETTINGS_MODULE, {}, {}, [''])
84 except ImportError, e:
85 raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
86
87
88
89 tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
90
91 for setting in dir(mod):
92 if setting == setting.upper():
93 setting_value = getattr(mod, setting)
94 if setting in tuple_settings and type(setting_value) == str:
95 setting_value = (setting_value,)
96 setattr(self, setting, setting_value)
97
98
99
100 new_installed_apps = []
101 for app in self.INSTALLED_APPS:
102 if app.endswith('.*'):
103 appdir = os.path.dirname(__import__(app[:-2], {}, {}, ['']).__file__)
104 for d in os.listdir(appdir):
105 if d.isalpha() and os.path.isdir(os.path.join(appdir, d)):
106 new_installed_apps.append('%s.%s' % (app[:-2], d))
107 else:
108 new_installed_apps.append(app)
109 self.INSTALLED_APPS = new_installed_apps
110
111 if hasattr(time, 'tzset'):
112
113
114 os.environ['TZ'] = self.TIME_ZONE
115 time.tzset()
116
119
121 """
122 Holder for user configured settings.
123 """
124
125
126 SETTINGS_MODULE = None
127
129 """
130 Requests for configuration variables not in this class are satisfied
131 from the module specified in default_settings (if possible).
132 """
133 self.default_settings = default_settings
134
136 return getattr(self.default_settings, name)
137
139 return dir(self) + dir(self.default_settings)
140
141 settings = LazySettings()
142