Last active
December 18, 2015 22:58
-
-
Save laginha/5857762 to your computer and use it in GitHub Desktop.
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
import socket | |
def import_settings(configs={}, default=None, base=None): | |
''' | |
Args: | |
configs - dictionary of {hostname : settings-module} - settings mapped to current hostname is loaded. | |
default - settings-module - loaded if current hostname not in configs. | |
base - settings-module - loaded regardless of the other args. | |
import_settings( | |
default = 'settings.me', | |
base = 'settings.base', | |
configs = { | |
'production.hostname': 'settings.production', | |
'testing.hostname': 'settings.testing', | |
}, | |
) | |
''' | |
def do_import(module_name): | |
# Load settings from module | |
module = __import__(module_name, globals(), locals(), module_name) | |
for setting in dir(module): | |
if setting == setting.upper(): | |
globals()[setting] = getattr(module, setting) | |
if base: | |
do_import( base ) #load base settings | |
config = configs.get( socket.gethostname(), default ) | |
do_import( config ) if config else None #load machine dependent settings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a few extra notes:
settings.me
should me added to.gitignore
.setttings.me
,settings.testing
andsettings.production
have environment dependent settings only.settings.base
are common to all environments.settings.me
for local configurations (e.g.DEBUG=True
).settings.base
if the settings are to be later used in production.