Skip to content

Commit

Permalink
#3 honor environment variables when loading configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Dec 9, 2015
1 parent 921e09c commit 500951c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion stups_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ def get_path(section):


def load_config(section):
'''Get configuration for given section/project
Tries to load YAML configuration file and also considers environment variables'''
path = get_path(section)
try:
with open(path, 'rb') as fd:
config = yaml.safe_load(fd)
except:
config = None
return config or {}
config = config or {}
env_prefix = '{}_'.format(section.upper().replace('-', '_'))
for key, val in os.environ.items():
if key.startswith(env_prefix):
config_key = key[len(env_prefix):].lower()
config[config_key] = val
return config


def store_config(config, section):
Expand Down

0 comments on commit 500951c

Please sign in to comment.