Added env var querying for default and bool

This commit is contained in:
Alexander Taylor 2013-11-23 00:33:58 +00:00
parent 707b4935cd
commit 3afa85a7fb

View file

@ -934,7 +934,11 @@ class Buildozer(object):
def _get_config_default(self, section, token, default=None):
# monkey-patch method for ConfigParser
# get an appropriate env var if it exists, else
# get a key in a section, or the default
env_var = query_env(section, token)
if env_var is not None:
return env_var
if not self.config.has_section(section):
return default
if not self.config.has_option(section, token):
@ -944,6 +948,9 @@ class Buildozer(object):
def _get_config_bool(self, section, token, default=False):
# monkey-patch method for ConfigParser
# get a key in a section, or the default
env_var = query_env_bool(section, token)
if env_var is not None:
return env_var
if not self.config.has_section(section):
return default
if not self.config.has_option(section, token):
@ -1206,3 +1213,32 @@ def run_remote():
pass
except BuildozerException as error:
Buildozer().error('%s' % error)
def query_env(section, token):
'''Given config section and token names, returns the environment
variable SECTION_TOKEN, or None if the variable does not exist.'''
env_var_name = ''.join([section.upper(), '_',
token.upper().replace('.', '_')])
env_var = os.environ.get(env_var_name)
return env_var
def query_env_bool(section, token):
'''Given config section and token names, uses query_env to query an
appropriate environment variable. If the variable exists, coerces to a
bool before returning.'''
env_var = query_env(section, token)
if env_var is None:
return env_var
elif env_var.tolower() in ['1', 'yes', 'true', 'on']:
return True
elif env_var.tolower() in ['0', 'no', 'false', 'off']:
return False
else:
env_var_name = ''.join([section.upper(), '_',
token.upper().replace('.', '_')])
raise ValueError('Failed to parse {} as a config boolean. Should be one'
'of {}'.format(env_var_name
['1', 'yes', 'true', 'on',
'0', 'no', 'false', 'off']))