Commit c0ec2f3c by Timm Behner

helper function for configuration update from environment variables

parent 4658f92c
......@@ -2,6 +2,7 @@ from .file_functions import read_in_chunks, get_directory_for_filename, create_d
from .git_functions import get_version_string_from_git
from .hash_functions import md5sum
from .fail_safe_file_operations import get_binary_from_file, write_binary_to_file, get_safe_name, delete_file, get_files_in_dir
from .config_functions import update_config_from_env
__all__ = [
'get_directory_for_filename',
......@@ -14,5 +15,6 @@ __all__ = [
'write_binary_to_file',
'get_safe_name',
'delete_file',
'get_files_in_dir'
'get_files_in_dir',
'update_config_from_env',
]
"""Helper functions for configuration files.
"""
import os
import logging
log = logging.getLogger(__name__)
def update_config_from_env(config):
""" Update configuration fields in config from environment variables.
The environment variable names are expected to be of the form, that the
section name and the variable name are seperated by a dubble underscore,
e.g.
SECTION_NAME__VARIABLE_NAME
"""
for key, val in os.environ.items():
if '__' in key:
section, section_key = key.lower().split('__')
try:
config[section.capitalize()][section_key] = val
except KeyError:
log.error('No section {} or value {} found.'.format(section.capitalize(), section_key))
return config
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment