Cleaned up code in django.conf.settings to move DJANGO_SETTINGS_MODULE into a variable, so other scripts can introspect it. Also made it display a better error message if DJANGO_SETTINGS_MODULE is set to the empty string

git-svn-id: http://code.djangoproject.com/svn/django/trunk@239 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-07-20 06:05:32 +00:00
parent 584cab7bc5
commit 45c334dd8b
1 changed files with 8 additions and 5 deletions

View File

@ -10,6 +10,8 @@ import os
import sys import sys
from django.conf import global_settings from django.conf import global_settings
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
# get a reference to this module (why isn't there a __module__ magic var?) # get a reference to this module (why isn't there a __module__ magic var?)
me = sys.modules[__name__] me = sys.modules[__name__]
@ -20,14 +22,16 @@ for setting in dir(global_settings):
# try to load DJANGO_SETTINGS_MODULE # try to load DJANGO_SETTINGS_MODULE
try: try:
me.SETTINGS_MODULE = os.environ["DJANGO_SETTINGS_MODULE"] me.SETTINGS_MODULE = os.environ[ENVIRONMENT_VARIABLE]
if not me.SETTINGS_MODULE: # If it's set but is an empty string.
raise KeyError
except KeyError: except KeyError:
raise EnvironmentError, "Environment variable DJANGO_SETTINGS_MODULE is undefined." raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
try: try:
mod = __import__(me.SETTINGS_MODULE, '', '', ['']) mod = __import__(me.SETTINGS_MODULE, '', '', [''])
except ImportError, e: except ImportError, e:
raise EnvironmentError, "Could not import DJANGO_SETTINGS_MODULE '%s' (is it on sys.path?): %s" % (me.SETTINGS_MODULE, e) raise EnvironmentError, "Could not import %s '%s' (is it on sys.path?): %s" % (ENVIRONMENT_VARIABLE, me.SETTINGS_MODULE, e)
# Settings that should be converted into tuples if they're mistakenly entered # Settings that should be converted into tuples if they're mistakenly entered
# as strings. # as strings.
@ -41,7 +45,7 @@ for setting in dir(mod):
setattr(me, setting, setting_value) setattr(me, setting, setting_value)
# save DJANGO_SETTINGS_MODULE in case anyone in the future cares # save DJANGO_SETTINGS_MODULE in case anyone in the future cares
me.SETTINGS_MODULE = os.environ.get('DJANGO_SETTINGS_MODULE', '') me.SETTINGS_MODULE = os.environ.get(ENVIRONMENT_VARIABLE, '')
# move the time zone info into os.environ # move the time zone info into os.environ
os.environ['TZ'] = me.TIME_ZONE os.environ['TZ'] = me.TIME_ZONE
@ -51,4 +55,3 @@ for k in dir(me):
if not k.startswith('_') and k != 'me' and k != k.upper(): if not k.startswith('_') and k != 'me' and k != k.upper():
delattr(me, k) delattr(me, k)
del me, k del me, k