From 45c334dd8bf9945e745a68b1199c2e536a3274d3 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 20 Jul 2005 06:05:32 +0000 Subject: [PATCH] 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 --- django/conf/settings.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/django/conf/settings.py b/django/conf/settings.py index 6f03d1c202..29b8f043aa 100644 --- a/django/conf/settings.py +++ b/django/conf/settings.py @@ -10,6 +10,8 @@ import os import sys 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?) me = sys.modules[__name__] @@ -20,14 +22,16 @@ for setting in dir(global_settings): # try to load DJANGO_SETTINGS_MODULE 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: - raise EnvironmentError, "Environment variable DJANGO_SETTINGS_MODULE is undefined." + raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE try: mod = __import__(me.SETTINGS_MODULE, '', '', ['']) 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 # as strings. @@ -41,7 +45,7 @@ for setting in dir(mod): setattr(me, setting, setting_value) # 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 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(): delattr(me, k) del me, k -