From 500951c154a6b6ef11b055a1973deaad87d3aff3 Mon Sep 17 00:00:00 2001 From: Henning Jacobs Date: Wed, 9 Dec 2015 12:25:03 +0100 Subject: [PATCH] #3 honor environment variables when loading configuration --- stups_cli/config.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/stups_cli/config.py b/stups_cli/config.py index b12d99a..fc14655 100644 --- a/stups_cli/config.py +++ b/stups_cli/config.py @@ -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):