-
-
Save pahko/9d1e2ac3d4d8dee39c96 to your computer and use it in GitHub Desktop.
Skeleton settings.py used by data-infra squad
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 json | |
import os | |
import socket | |
import tornado.options | |
tornado.options.define('environment', default='dev', help='environment') | |
app_name = os.path.basename(os.path.dirname(__file__)) | |
environment_settings = { | |
'dev': { | |
}, | |
'stage': { | |
}, | |
'live': { | |
} | |
} | |
# FIXME: this hacks in support for chef's environments; we should unify these | |
environment_settings['development'] = environment_settings['dev'] | |
environment_settings['production'] = environment_settings['live'] | |
host_settings = { | |
} | |
default_settings = { | |
} | |
def env(): | |
return tornado.options.options.environment | |
def memoized(f, cache={}): | |
def decorated(): | |
if f.__name__ not in cache: | |
cache[f.__name__] = f() | |
return cache[f.__name__] | |
return decorated | |
@memoized | |
def hostname(): | |
fqdn = socket.gethostname() | |
return 'dev.buzzfeed.org' if fqdn.endswith('.buzzfeed.org') else fqdn | |
@memoized | |
def local_settings(): | |
try: | |
with open('/buzzfeed/local/conf/%s/settings.json' % app_name) as f: | |
settings = json.load(f) | |
assert isinstance(settings, dict) | |
return settings | |
except IOError: | |
return {} | |
except Exception, e: | |
raise RuntimeError('Invalid local settings.json: %s' % e) | |
def get(key): | |
env = tornado.options.options.environment | |
if env not in environment_settings: | |
raise Exception('Invalid Environment (%s)' % env) | |
v = os.environ.get(key.upper()) | |
if v is None: | |
v = local_settings().get(key) | |
if v is None: | |
v = host_settings.get(hostname(), {}).get(key) | |
if v is None: | |
v = environment_settings.get(env).get(key) | |
if v is None: | |
v = default_settings.get(key) | |
assert v is not None, 'key %s not in any settings' % key | |
if callable(v): | |
return v() | |
return v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment