Created
May 9, 2011 15:57
-
-
Save c4urself/962776 to your computer and use it in GitHub Desktop.
Django settings file as loader for server specific settings files
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
# | |
# 1. Imports base settings from `/serversettings/base.py` | |
# 2. Imports settings based on server name, unless: | |
# a. The environmental setting `SERVER_TYPE` has been set in the `django.wsgi` script | |
# b. The name of the server corresponds to a name in the `DEVELOPMENT_MACHINES` tuple | |
# | |
import os | |
import platform | |
import sys | |
import warnings | |
from django.utils.importlib import import_module | |
DEVELOPMENT_MACHINES = ('christian-mac',) | |
TESTING_MACHINES = ('alpha','beta',) | |
PRODUCTION_MACHINES = ('echo',) | |
def get_server_name(): | |
server_type = os.environ.get('SERVER_TYPE','') | |
server_name = platform.node().split('.')[0] | |
if server_type: | |
return server_type | |
if server_name in DEVELOPMENT_MACHINES: | |
return 'local' | |
if server_name in TESTING_MACHINES: | |
return 'testing' | |
if server_name in PRODUCTION_MACHINES: | |
return 'production' | |
else: | |
return server_name | |
def override_settings(dottedpath): | |
sys.path.append(os.sep.join(os.path.abspath(os.path.dirname(__file__)).split(os.sep)[:-1])) | |
try: | |
_module = import_module(dottedpath) | |
except ImportError, e: | |
warnings.warn("Failed to import %s" % dottedpath) | |
warnings.warn("Exact error was: %s" % e) | |
else: | |
_thismodule = sys.modules[__name__] | |
for _keyword in dir(_module): | |
if _keyword.isupper() and not _keyword.startswith('__'): | |
setattr(_thismodule, _keyword, getattr(_module, _keyword)) | |
override_settings('mysite.serversettings.base') | |
override_settings('mysite.serversettings.%s' % get_server_name()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment