Skip to content

Instantly share code, notes, and snippets.

@laginha
Last active December 18, 2015 22:58
Show Gist options
  • Save laginha/5857762 to your computer and use it in GitHub Desktop.
Save laginha/5857762 to your computer and use it in GitHub Desktop.
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
@laginha
Copy link
Author

laginha commented Jun 25, 2013

Just a few extra notes:

  • This is the settings structure i use:

settings structure

  • settings.me should me added to .gitignore.
  • setttings.me, settings.testing and settings.production have environment dependent settings only.
  • settings in settings.base are common to all environments.
  • each developer should only change
    • settings.me for local configurations (e.g. DEBUG=True).
    • settings.base if the settings are to be later used in production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment