Define and loop on config file section

This review loop over the configuration file sections
instead of taking as paremeter the section and only
set the parameters by section one by one with the 'default'
for default section.
This avoid to set explicitely each sections as parameters.

Change-Id: I068e57e18b256907ce54bf48e753d496dec5327d
This commit is contained in:
matbu 2022-08-31 14:45:18 +02:00
parent 5076004733
commit c602a0843a
1 changed files with 12 additions and 10 deletions

View File

@ -30,8 +30,9 @@ from validations_libs.cli.common import ValidationHelpFormatter
class Base:
"""Base class for CLI arguments management"""
config = {}
config_section = ['default', 'ansible_runner', 'ansible_environment']
def set_argument_parser(self, vf_parser, args, section='default'):
def set_argument_parser(self, vf_parser, args):
""" Set Arguments parser depending of the precedence ordering:
* User CLI arguments
* Configuration file
@ -45,15 +46,16 @@ class Base:
for arg in cli_args if arg.startswith('--')]
self.config = utils.load_config(os.path.abspath(args.config))
config_args = self.config.get(section, {})
for key, value in args._get_kwargs():
if key in cli_key:
config_args.update({key: value})
elif parser.get_default(key) != value:
config_args.update({key: value})
elif key not in config_args.keys():
config_args.update({key: value})
return vars(args).update(**config_args)
for section in self.config_section:
config_args = self.config.get(section, {})
for key, value in args._get_kwargs():
if key in cli_key:
config_args.update({key: value})
elif parser.get_default(key) != value:
config_args.update({key: value})
elif key not in config_args.keys():
config_args.update({key: value})
vars(args).update(**config_args)
class BaseCommand(Command):