Permalink
Browse files

Correct how settings are loaded in order to guarantee things like LOG…

…GING get executed
  • Loading branch information...
1 parent 357b56d commit 135fcc1ef7d7ed5fffc344d6558312b2fe23a7a5 @dcramer committed Sep 9, 2012
Showing with 53 additions and 26 deletions.
  1. +6 −6 logan/runner.py
  2. +46 −19 logan/settings.py
  3. +1 −1 setup.py
View
@@ -15,8 +15,7 @@
import re
import sys
-from logan.settings import create_default_settings, load_settings, \
- add_settings
+from logan.settings import create_default_settings, install_settings
def sanitize_name(project):
@@ -121,12 +120,13 @@ def run_app(project=None, default_config_path=None, default_settings=None,
raise ValueError("Configuration file does not exist. Use '%s init' to initialize the file." % runner_name)
if default_settings:
- settings_mod = import_module(default_settings)
+ default_settings_mod = import_module(default_settings)
# TODO: logan should create a proxy module for its settings
- management.setup_environ(settings_mod, default_settings)
- add_settings(settings_mod, allow_extras=allow_extras)
+ # management.setup_environ(settings_mod, default_settings)
+ else:
+ default_settings_mod = None
- load_settings(config_path, allow_extras=allow_extras)
+ install_settings(config_path, default_settings_mod, allow_extras=allow_extras)
if initializer is not None:
from django.conf import settings
View
@@ -12,7 +12,8 @@
import errno
import imp
import os
-from django.conf import settings as _settings
+import sys
+from django.conf import Settings, global_settings, settings as _settings
__all__ = ('create_default_settings', 'load_settings')
@@ -33,24 +34,50 @@ def create_default_settings(filepath, settings_initializer):
fp.write(output)
-def load_settings(filename, silent=False, allow_extras=True, settings=_settings):
- """
- Configures django settings from an arbitrary (non sys.path) filename.
- """
- mod = imp.new_module('config')
- mod.__file__ = filename
- try:
- execfile(filename, mod.__dict__)
- except IOError, e:
- if silent and e.errno in (errno.ENOENT, errno.EISDIR):
- return False
- e.strerror = 'Unable to load configuration file (%s)' % e.strerror
- raise
-
- if not settings.configured:
- settings.configure()
-
- add_settings(mod, allow_extras=allow_extras, settings=settings)
+def create_module(name, install=True):
+ mod = imp.new_module(name)
+ if install:
+ sys.modules[name] = mod
+ return mod
+
+
+def install_settings(filename, default_settings=global_settings, silent=False, allow_extras=True, settings=_settings):
+ settings_mod = create_module('logan_config')
+
+ # Django doesn't play too nice without the config file living as a real file, so let's fake it.
+ settings_mod.__file__ = filename
+
+ # Gunicorn doesn't play nice without DJANGO_SETTINGS_MODULE
+ os.environ['DJANGO_SETTINGS_MODULE'] = filename
+
+ # install the default settings for this app
+ load_settings(default_settings, allow_extras=allow_extras, settings=settings_mod)
+
+ # install the custom settings for this app
+ load_settings(filename, allow_extras=allow_extras, settings=settings_mod)
+
+ # HACK: we need to ensure that the settings object doesnt configure itself until we tell it to
+ settings_inst = Settings('logan_config')
+ settings._wrapped = settings_inst
+
+ return settings
+
+
+def load_settings(mod_or_filename, silent=False, allow_extras=True, settings=_settings):
+ if isinstance(mod_or_filename, basestring):
+ conf = create_module('temp_config', install=False)
+ conf.__file__ = mod_or_filename
+ try:
+ execfile(mod_or_filename, conf.__dict__)
+ except IOError, e:
+ if silent and e.errno in (errno.ENOENT, errno.EISDIR):
+ return settings
+ e.strerror = 'Unable to load configuration file (%s)' % e.strerror
+ raise
+ else:
+ conf = mod_or_filename
+
+ add_settings(conf, allow_extras=allow_extras, settings=settings)
def add_settings(mod, allow_extras=True, settings=_settings):
View
@@ -14,7 +14,7 @@
setup(
name='logan',
- version='0.3.1',
+ version='0.4.0',
author='David Cramer',
author_email='dcramer@gmail.com',
url='http://github.com/dcramer/logan',

0 comments on commit 135fcc1

Please sign in to comment.