Added env check during config build

This commit is contained in:
Alexander Taylor 2013-11-23 01:51:51 +00:00
parent 11e994de14
commit fdb9c52371

View file

@ -100,6 +100,10 @@ class Buildozer(object):
self.config.read(filename) self.config.read(filename)
self.check_configuration_tokens() self.check_configuration_tokens()
# Check all section/tokens for env vars, and replace the
# config value if a suitable env var exists.
set_config_from_envs(self.config)
try: try:
self.log_level = int(self.config.getdefault( self.log_level = int(self.config.getdefault(
'buildozer', 'log_level', '1')) 'buildozer', 'log_level', '1'))
@ -915,7 +919,7 @@ class Buildozer(object):
# get a key as a list of string, seperated from the comma # get a key as a list of string, seperated from the comma
# if a section:token is defined, let's use the content as a list. # if a section:token is defined, let's use the content as a list.
set_config_from_env(section, token, self.config) set_config_token_from_env(section, token, self.config)
l_section = '{}:{}'.format(section, token) l_section = '{}:{}'.format(section, token)
if self.config.has_section(l_section): if self.config.has_section(l_section):
@ -938,7 +942,7 @@ class Buildozer(object):
# monkey-patch method for ConfigParser # monkey-patch method for ConfigParser
# get an appropriate env var if it exists, else # get an appropriate env var if it exists, else
# get a key in a section, or the default # get a key in a section, or the default
set_config_from_env(section, token, self.config) set_config_token_from_env(section, token, self.config)
if not self.config.has_section(section): if not self.config.has_section(section):
return default return default
if not self.config.has_option(section, token): if not self.config.has_option(section, token):
@ -948,7 +952,7 @@ class Buildozer(object):
def _get_config_bool(self, section, token, default=False): def _get_config_bool(self, section, token, default=False):
# monkey-patch method for ConfigParser # monkey-patch method for ConfigParser
# get a key in a section, or the default # get a key in a section, or the default
set_config_from_env(section, token, self.config) set_config_token_from_env(section, token, self.config)
if not self.config.has_section(section): if not self.config.has_section(section):
return default return default
if not self.config.has_option(section, token): if not self.config.has_option(section, token):
@ -1212,7 +1216,17 @@ def run_remote():
except BuildozerException as error: except BuildozerException as error:
Buildozer().error('%s' % error) Buildozer().error('%s' % error)
def set_config_from_env(section, token, config): def set_config_from_envs(config):
'''Takes a ConfigParser, and checks every section/token for an
environment variable of the form SECTION_TOKEN, with any dots
replaced by underscores. If the variable exists, sets the config
variable to the env value.
'''
for section in config.sections():
for token in config.options(section):
set_config_token_from_env(section, token, config)
def set_config_token_from_env(section, token, config):
'''Given a config section and token, checks for an appropriate '''Given a config section and token, checks for an appropriate
environment variable. If the variable exists, sets the config entry to environment variable. If the variable exists, sets the config entry to
its value. its value.