Reduce cfg classes, add env resolver that uses env and move special config stuff to config resolver
This commit is contained in:
parent
4e0c03841b
commit
8f87890dab
@ -202,59 +202,19 @@ class ConfigResolver(object):
|
||||
self.backing = backing
|
||||
|
||||
def get(self, section, option):
|
||||
return self.backing.get(section, option)
|
||||
return self._resolve_value(section, option, self._get_bashed(section, option))
|
||||
|
||||
def set(self, section, option, value):
|
||||
self.backing.set(section, option, value)
|
||||
|
||||
|
||||
class DynamicResolver(ConfigResolver):
|
||||
|
||||
def get(self, section, option):
|
||||
return self._resolve_value(section, option, ConfigResolver.get(self, section, option))
|
||||
|
||||
def _resolve_value(self, section, option, value_gotten):
|
||||
if section == 'host' and option == 'ip':
|
||||
LOG.debug("Host ip from configuration/environment was empty, programatically attempting to determine it.")
|
||||
value_gotten = utils.get_host_ip()
|
||||
LOG.debug("Determined your host ip to be: %r" % (value_gotten))
|
||||
if not value_gotten:
|
||||
if section == 'host' and option == 'ip':
|
||||
LOG.debug("Host ip from configuration/environment was empty, programatically attempting to determine it.")
|
||||
value_gotten = utils.get_host_ip()
|
||||
LOG.debug("Determined your host ip to be: %r" % (value_gotten))
|
||||
return value_gotten
|
||||
|
||||
|
||||
class CliResolver(object):
|
||||
|
||||
def __init__(self, cli_args):
|
||||
self.cli_args = cli_args
|
||||
|
||||
def get(self, section, option):
|
||||
return self.cli_args.get(cfg_helpers.make_id(section, option))
|
||||
|
||||
@classmethod
|
||||
def create(cls, cli_args):
|
||||
parsed_args = dict()
|
||||
for c in cli_args:
|
||||
if not c:
|
||||
continue
|
||||
split_up = c.split("/")
|
||||
if len(split_up) != 3:
|
||||
LOG.warn("Incorrectly formatted cli option: %r", c)
|
||||
else:
|
||||
section = (split_up[0]).strip()
|
||||
if not section or section.lower() == 'default':
|
||||
section = 'DEFAULT'
|
||||
option = split_up[1].strip()
|
||||
if not option:
|
||||
LOG.warn("Badly formatted cli option - no option name: %r", c)
|
||||
else:
|
||||
parsed_args[cfg_helpers.make_id(section, option)] = split_up[2]
|
||||
return cls(parsed_args)
|
||||
|
||||
|
||||
class EnvResolver(DynamicResolver):
|
||||
|
||||
def get(self, section, option):
|
||||
return self._get_bashed(section, option)
|
||||
|
||||
def _getdefaulted(self, section, option, default_value):
|
||||
val = self.get(section, option)
|
||||
if not val or not val.strip():
|
||||
@ -262,7 +222,7 @@ class EnvResolver(DynamicResolver):
|
||||
return val
|
||||
|
||||
def _get_bashed(self, section, option):
|
||||
value = DynamicResolver.get(self, section, option)
|
||||
value = self.backing.get(section, option)
|
||||
if value is None:
|
||||
return value
|
||||
extracted_val = ''
|
||||
@ -298,3 +258,44 @@ class EnvResolver(DynamicResolver):
|
||||
return self._getdefaulted(section, option, '')
|
||||
|
||||
return SUB_MATCH.sub(replacer, value)
|
||||
|
||||
|
||||
class CliResolver(object):
|
||||
|
||||
def __init__(self, cli_args):
|
||||
self.cli_args = cli_args
|
||||
|
||||
def get(self, section, option):
|
||||
return self.cli_args.get(cfg_helpers.make_id(section, option))
|
||||
|
||||
@classmethod
|
||||
def create(cls, cli_args):
|
||||
parsed_args = dict()
|
||||
for c in cli_args:
|
||||
if not c:
|
||||
continue
|
||||
split_up = c.split("/")
|
||||
if len(split_up) != 3:
|
||||
LOG.warn("Incorrectly formatted cli option: %r", c)
|
||||
else:
|
||||
section = (split_up[0]).strip()
|
||||
if not section or section.lower() == 'default':
|
||||
section = 'DEFAULT'
|
||||
option = split_up[1].strip()
|
||||
if not option:
|
||||
LOG.warn("Badly formatted cli option - no option name: %r", c)
|
||||
else:
|
||||
parsed_args[cfg_helpers.make_id(section, option)] = split_up[2]
|
||||
return cls(parsed_args)
|
||||
|
||||
|
||||
class EnvResolver(object):
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def _form_key(self, section, option):
|
||||
return cfg_helpers.make_id(section, option)
|
||||
|
||||
def get(self, section, option):
|
||||
return env.get_key(self._form_key(section, option))
|
||||
|
7
stack
7
stack
@ -145,10 +145,11 @@ def establish_config(args):
|
||||
base_config.readfp(fh)
|
||||
proxy_config = cfg.ProxyConfig()
|
||||
proxy_config.add_read_resolver(cfg.CliResolver.create(args['cli_overrides']))
|
||||
proxy_config.add_read_resolver(cfg.EnvResolver())
|
||||
if base_config:
|
||||
env_resolver = cfg.EnvResolver(base_config)
|
||||
proxy_config.add_read_resolver(env_resolver)
|
||||
proxy_config.add_set_resolver(env_resolver)
|
||||
cfg_resolver = cfg.ConfigResolver(base_config)
|
||||
proxy_config.add_read_resolver(cfg_resolver)
|
||||
proxy_config.add_set_resolver(cfg_resolver)
|
||||
utils.log_iterable(utils.get_class_names(proxy_config.read_resolvers),
|
||||
header="Config lookup will use the following resolvers:",
|
||||
logger=LOG)
|
||||
|
@ -60,7 +60,8 @@ if __name__ == "__main__":
|
||||
base_config.read([stack_config])
|
||||
base_config.set('img', 'image_urls', uri_sep)
|
||||
config = cfg.ProxyConfig()
|
||||
config.add_read_resolver(cfg.EnvResolver(base_config))
|
||||
config.add_read_resolver(cfg.EnvResolver())
|
||||
config.add_read_resolver(cfg.ConfigResolver(base_config))
|
||||
pw_gen = passwords.PasswordGenerator(config)
|
||||
uploader = uploader.Service(config, pw_gen)
|
||||
uploader.install()
|
||||
|
Loading…
x
Reference in New Issue
Block a user