Created
February 8, 2012 06:18
-
-
Save rca/1766010 to your computer and use it in GitHub Desktop.
Django split settings module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# settings convention from: | |
# https://code.djangoproject.com/wiki/SplitSettings#SettingInheritancewithHierarchy | |
import os, pwd | |
# certain keys we want to merge instead of copy | |
merge_keys = ('INSTALLED_APPS', 'MIDDLEWARE_CLASSES') | |
def deep_update(from_dict, to_dict): | |
for (key, value) in from_dict.iteritems(): | |
if key in to_dict.keys() and isinstance(to_dict[key], dict) and isinstance(value, dict): | |
deep_update(value, to_dict[key]) | |
elif key in merge_keys: | |
if not key in to_dict: | |
to_dict[key] = () | |
to_dict[key] = to_dict[key] + from_dict[key] | |
else: | |
to_dict[key] = value | |
# this should be one of prod, qa, staging, dev. Default to dev for safety. | |
env = os.environ.get('APP_ENV', 'dev') | |
imported_modules = set() | |
# try to load user specific settings | |
modules = ('common', env) | |
try: | |
uid = pwd.getpwuid(os.getuid())[0] | |
modules = modules + (uid,) | |
except KeyError: | |
uid = None | |
current = __name__ | |
for module_name in modules: | |
try: | |
module = getattr(__import__(current, globals(), locals(), [module_name]), module_name) | |
except ImportError, e: | |
print 'ERROR: Unable to import %s configuration: %s' % (module_name, e) | |
raise | |
except AttributeError, e: | |
if module_name == uid: | |
print 'WARNING: Unable to import settings.%s; does it exist?' % (module_name,) | |
else: | |
raise | |
# create a local copy of this module's settings | |
module_settings = {} | |
for setting in dir(module): | |
# all django settings are uppercase, so this ensures we | |
# are only processing settings from the dir() call | |
if setting == setting.upper(): | |
module_settings[setting] = getattr(module, setting) | |
deep_update(module_settings, locals()) | |
imported_modules.add(module_name) | |
#print locals() # for debugging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment