Skip to content

Commit

Permalink
Correct how settings are loaded in order to guarantee things like LOG…
Browse files Browse the repository at this point in the history
…GING get executed
  • Loading branch information
dcramer committed Sep 9, 2012
1 parent 357b56d commit 135fcc1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
12 changes: 6 additions & 6 deletions logan/runner.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import re import re
import sys import sys


from logan.settings import create_default_settings, load_settings, \ from logan.settings import create_default_settings, install_settings
add_settings




def sanitize_name(project): def sanitize_name(project):
Expand Down Expand Up @@ -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) raise ValueError("Configuration file does not exist. Use '%s init' to initialize the file." % runner_name)


if default_settings: 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 # TODO: logan should create a proxy module for its settings
management.setup_environ(settings_mod, default_settings) # management.setup_environ(settings_mod, default_settings)
add_settings(settings_mod, allow_extras=allow_extras) 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: if initializer is not None:
from django.conf import settings from django.conf import settings
Expand Down
65 changes: 46 additions & 19 deletions logan/settings.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import errno import errno
import imp import imp
import os 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') __all__ = ('create_default_settings', 'load_settings')


Expand All @@ -33,24 +34,50 @@ def create_default_settings(filepath, settings_initializer):
fp.write(output) fp.write(output)




def load_settings(filename, silent=False, allow_extras=True, settings=_settings): def create_module(name, install=True):
""" mod = imp.new_module(name)
Configures django settings from an arbitrary (non sys.path) filename. if install:
""" sys.modules[name] = mod
mod = imp.new_module('config') return mod
mod.__file__ = filename
try:
execfile(filename, mod.__dict__) def install_settings(filename, default_settings=global_settings, silent=False, allow_extras=True, settings=_settings):
except IOError, e: settings_mod = create_module('logan_config')
if silent and e.errno in (errno.ENOENT, errno.EISDIR):
return False # Django doesn't play too nice without the config file living as a real file, so let's fake it.
e.strerror = 'Unable to load configuration file (%s)' % e.strerror settings_mod.__file__ = filename
raise

# Gunicorn doesn't play nice without DJANGO_SETTINGS_MODULE
if not settings.configured: os.environ['DJANGO_SETTINGS_MODULE'] = filename
settings.configure()

# install the default settings for this app
add_settings(mod, allow_extras=allow_extras, settings=settings) 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): def add_settings(mod, allow_extras=True, settings=_settings):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


setup( setup(
name='logan', name='logan',
version='0.3.1', version='0.4.0',
author='David Cramer', author='David Cramer',
author_email='dcramer@gmail.com', author_email='dcramer@gmail.com',
url='http://github.com/dcramer/logan', url='http://github.com/dcramer/logan',
Expand Down

0 comments on commit 135fcc1

Please sign in to comment.