Config: add set_default() / set_defaults() methods

This commit is contained in:
Benjamin Renard 2023-03-13 18:53:29 +01:00
parent 5aa6a0cea4
commit b5df95a2dd
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -116,6 +116,10 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
self._set = True
def set_default(self, default_value):
"""Set option default value"""
self.default = default_value
@property
def parser_action(self):
"""Get action as accept by argparse.ArgumentParser"""
@ -511,6 +515,16 @@ class ConfigSection:
assert self.defined(option), f"Option {option} unknown"
return self.options[option].set(value)
def set_default(self, option, default_value):
"""Set default option value"""
assert self.defined(option), f"Option {option} unknown"
return self.options[option].set_default(default_value)
def set_defaults(self, **default_values):
"""Set default options value"""
for option, default_value in default_values.items():
self.set_default(option, default_value)
def add_options_to_parser(self, parser):
"""Add section to argparse.ArgumentParser"""
assert isinstance(parser, argparse.ArgumentParser)
@ -660,6 +674,18 @@ class Config: # pylint: disable=too-many-instance-attributes
self._init_config_parser()
self.sections[section].set(option, value)
def set_default(self, section, option, default_value):
"""Set default option value"""
assert self.defined(section, option), f"Unknown option {section}.{option}"
self._init_config_parser()
self.sections[section].set_default(option, default_value)
def set_defaults(self, section, **default_values):
"""Set default options value"""
assert section in self.sections, f"Unknown section {section}"
self._init_config_parser()
self.sections[section].set_defaults(**default_values)
def _init_config_parser(self, force=False):
"""Initialize ConfigParser object"""
if not self.config_parser or force: