Last active
December 15, 2015 06:09
-
-
Save felipe-prenholato/5213778 to your computer and use it in GitHub Desktop.
A splitted settings version
This file contains 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
# -*- coding: utf-8 -*- | |
from __future__ import absolute_import, division, unicode_literals | |
import os | |
from django.utils.importlib import import_module | |
from unipath import Path | |
SETTINGS_ROOT = Path(__file__).parent | |
PROJECT_ROOT = SETTINGS_ROOT.parent | |
def load_settings(app_name=None, settings_name=None, debug=True): | |
""" | |
Load settings from following locations, in order. | |
settings/<settings_name>.py | |
<app_name>/settings/__init__.py or <app_name>/settings.py | |
settings/<app_name>_settings.py | |
""" | |
if not app_name and not settings_name: | |
raise ValueError("What you are thinking that I will load if you don't" | |
" send me app_name or settings_name?") | |
if settings_name: | |
settings_name = settings_name.replace('.py', '') | |
settings_name = "%s.py" % settings_name | |
settings_path = SETTINGS_ROOT.child(settings_name) | |
if settings_path.exists() and settings_path.isfile(): | |
execfile(settings_path, globals()) | |
if debug: | |
print "Loaded settings from %s" % settings_name | |
else: | |
try: | |
mod = import_module("%s/settings" % app_name) | |
execfile(mod.__file__, globals()) | |
if debug: | |
print "Loaded settings from %s" % mod.__file__ | |
except ImportError: | |
fname = "%s_settings.py" % app_name | |
settings_path = SETTINGS_ROOT.child(fname) | |
if settings_path.exists() and settings_path.isfile(fname): | |
execfile(settings_path, globals()) | |
if debug: | |
print "Loaded settings from %s" % fname | |
# load defaults | |
load_settings(settings_name='defaults.py') | |
# load settings for each app | |
for app_name in INSTALLED_APPS: | |
load_settings(app_name) | |
# load env settings | |
env_settings_name = os.environ.get('APP_ENV', 'dev') | |
load_settings(settings_name=env_settings_name) | |
# load local settings | |
load_settings(settings_name='settings_local') |
This file contains 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
demo$ tree settings/ | |
settings/ | |
├── db.py | |
├── defaults.py | |
├── dev.py | |
├── __init__.py | |
├── production.py | |
└── qas1.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment