From 464271a273d3090138671484b6fdaf455ea4f45d Mon Sep 17 00:00:00 2001 From: Georg Bauer Date: Tue, 17 Jan 2006 14:19:10 +0000 Subject: [PATCH] Refs #1212 - moved settings from a dedicated module into a dedicated global instance git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2031 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/conf/__init__.py | 73 +++++++++++++++++++++++++++++++++++ django/conf/settings.py | 77 ------------------------------------- django/utils/translation.py | 6 +-- 3 files changed, 76 insertions(+), 80 deletions(-) delete mode 100644 django/conf/settings.py diff --git a/django/conf/__init__.py b/django/conf/__init__.py index e69de29bb2..291ba8ce3f 100644 --- a/django/conf/__init__.py +++ b/django/conf/__init__.py @@ -0,0 +1,73 @@ +""" +Settings and configuration for Django. + +Values will be read from the module specified by the DJANGO_SETTINGS_MODULE environment +variable, and then from django.conf.global_settings; see the global settings file for +a list of all possible variables. +""" + +import os +import sys +from django.conf import global_settings + +ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" + +class Settings: + + def __init__(self, settings_module): + + # update this dict from global settings (but only for ALL_CAPS settings) + for setting in dir(global_settings): + if setting == setting.upper(): + setattr(self, setting, getattr(global_settings, setting)) + + # store the settings module in case someone later cares + self.SETTINGS_MODULE = settings_module + + try: + mod = __import__(self.SETTINGS_MODULE, '', '', ['']) + except ImportError, e: + raise EnvironmentError, "Could not import settings '%s' (is it on sys.path?): %s" % (self.SETTINGS_MODULE, e) + + # Settings that should be converted into tuples if they're mistakenly entered + # as strings. + tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS") + + for setting in dir(mod): + if setting == setting.upper(): + setting_value = getattr(mod, setting) + if setting in tuple_settings and type(setting_value) == str: + setting_value = (setting_value,) # In case the user forgot the comma. + setattr(self, setting, setting_value) + + # Expand entries in INSTALLED_APPS like "django.contrib.*" to a list + # of all those apps. + new_installed_apps = [] + for app in self.INSTALLED_APPS: + if app.endswith('.*'): + appdir = os.path.dirname(__import__(app[:-2], '', '', ['']).__file__) + for d in os.listdir(appdir): + if d.isalpha() and os.path.isdir(os.path.join(appdir, d)): + new_installed_apps.append('%s.%s' % (app[:-2], d)) + else: + new_installed_apps.append(app) + self.INSTALLED_APPS = new_installed_apps + + # move the time zone info into os.environ + os.environ['TZ'] = self.TIME_ZONE + +# try to load DJANGO_SETTINGS_MODULE +try: + settings_module = os.environ[ENVIRONMENT_VARIABLE] + if not settings_module: # If it's set but is an empty string. + raise KeyError +except KeyError: + raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE + +# instantiate the configuration object +settings = Settings(settings_module) + +# install the translation machinery so that it is available +from django.utils import translation +translation.install() + diff --git a/django/conf/settings.py b/django/conf/settings.py deleted file mode 100644 index f455f65afe..0000000000 --- a/django/conf/settings.py +++ /dev/null @@ -1,77 +0,0 @@ -""" -Settings and configuration for Django. - -Values will be read from the module specified by the DJANGO_SETTINGS_MODULE environment -variable, and then from django.conf.global_settings; see the global settings file for -a list of all possible variables. -""" - -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__] - -# update this dict from global settings (but only for ALL_CAPS settings) -for setting in dir(global_settings): - if setting == setting.upper(): - setattr(me, setting, getattr(global_settings, setting)) - -# try to load DJANGO_SETTINGS_MODULE -try: - 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 %s is undefined." % ENVIRONMENT_VARIABLE - -try: - mod = __import__(me.SETTINGS_MODULE, '', '', ['']) -except ImportError, 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. -tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS") - -for setting in dir(mod): - if setting == setting.upper(): - setting_value = getattr(mod, setting) - if setting in tuple_settings and type(setting_value) == str: - setting_value = (setting_value,) # In case the user forgot the comma. - setattr(me, setting, setting_value) - -# Expand entries in INSTALLED_APPS like "django.contrib.*" to a list -# of all those apps. -new_installed_apps = [] -for app in me.INSTALLED_APPS: - if app.endswith('.*'): - appdir = os.path.dirname(__import__(app[:-2], '', '', ['']).__file__) - for d in os.listdir(appdir): - if d.isalpha() and os.path.isdir(os.path.join(appdir, d)): - new_installed_apps.append('%s.%s' % (app[:-2], d)) - else: - new_installed_apps.append(app) -me.INSTALLED_APPS = new_installed_apps - -# save DJANGO_SETTINGS_MODULE in case anyone in the future cares -me.SETTINGS_MODULE = os.environ.get(ENVIRONMENT_VARIABLE, '') - -# move the time zone info into os.environ -os.environ['TZ'] = me.TIME_ZONE - -# finally, clean up my namespace -for k in dir(me): - if not k.startswith('_') and k != 'me' and k != k.upper(): - delattr(me, k) -del me, k - -# as the last step, install the translation machinery and -# remove the module again to not clutter the namespace. -from django.utils import translation -translation.install() -del translation - diff --git a/django/utils/translation.py b/django/utils/translation.py index 01568d1969..7bd442a070 100644 --- a/django/utils/translation.py +++ b/django/utils/translation.py @@ -115,7 +115,7 @@ def translation(language): if sys.version_info < (2, 4): klass = DjangoTranslation23 - globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') + globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale') parts = settings.SETTINGS_MODULE.split('.') project = __import__(parts[0], {}, {}, []) @@ -275,7 +275,7 @@ def check_for_language(lang_code): only used for language codes from either the cookies or session. """ from django.conf import settings - globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') + globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale') if gettext_module.find('django', globalpath, [to_locale(lang_code)]) is not None: return True else: @@ -289,7 +289,7 @@ def get_language_from_request(request): """ global _accepted from django.conf import settings - globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') + globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale') supported = dict(settings.LANGUAGES) if hasattr(request, 'session'):