config: Add ask_value() helper to section & config objects
Some checks failed
Run tests / tests (push) Failing after 57s

This commit is contained in:
Benjamin Renard 2024-04-13 17:52:05 +02:00
parent 3cf6a2682c
commit 28103836ac

View file

@ -249,12 +249,16 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
def ask_value(self, set_it=True): def ask_value(self, set_it=True):
""" """
Ask to user to enter value of this option and set or Ask to user to enter value of this option and set it if set_it parameter is True
return it regarding set parameter
:param set_it: If True (default), option value will be updated with user input
:return: The configuration option value.
:rtype: mixed
""" """
value = self._ask_value() value = self._ask_value()
if set_it: if set_it:
return self.set(value) self.set(value)
return value return value
@ -510,8 +514,12 @@ class PasswordOption(StringOption):
def ask_value(self, set_it=True): def ask_value(self, set_it=True):
""" """
Ask to user to enter value of this option and set or Ask to user to enter value of this option and set it if set_it parameter is True
return it regarding set parameter
:param set_it: If True (default), option value will be updated with user input
:return: The configuration option value.
:rtype: mixed
""" """
value = self._ask_value() value = self._ask_value()
if set_it: if set_it:
@ -530,7 +538,7 @@ class PasswordOption(StringOption):
use_keyring = False use_keyring = False
else: else:
print("Invalid answer. Possible values: Y or N (case insensitive)") print("Invalid answer. Possible values: Y or N (case insensitive)")
return self.set(value, use_keyring=use_keyring) self.set(value, use_keyring=use_keyring)
return value return value
@ -610,28 +618,32 @@ class ConfigSection:
:param set_it: If True (default), option value will be updated with user input :param set_it: If True (default), option value will be updated with user input
:return: If set_it is True, return True if valid value for each configuration :return: a dict of configuration options and their value.
option have been retrieved and set. If False, return a dict of configuration
options and their value.
:rtype: bool of dict :rtype: bool of dict
""" """
if self.comment: if self.comment:
print(f"# {self.comment}") print(f"# {self.comment}")
print(f"[{self.name}]\n") print(f"[{self.name}]\n")
result = {} result = {}
error = False
for name, option in self.options.items(): for name, option in self.options.items():
option_result = option.ask_value(set_it=set_it) result[name] = option.ask_value(set_it=set_it)
if set_it:
result[name] = option_result
elif not option_result:
error = True
print() print()
print() print()
if set_it:
return not error
return result return result
def ask_value(self, option, set_it=True):
"""
Ask user to enter value for the specified configuration option of the section
:param options: The configuration option name
:param set_it: If True (default), option value will be updated with user input
:return: The configuration option value.
:rtype: mixed
"""
assert self.defined(option), f"Option {option} unknown"
return self.options[option].ask_value(set_it=set_it)
class RawWrappedTextHelpFormatter(argparse.RawDescriptionHelpFormatter): class RawWrappedTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
""" """
@ -1073,8 +1085,7 @@ class Config: # pylint: disable=too-many-instance-attributes
self._loaded() self._loaded()
if self.get_option("mylib_config_reconfigure", default=False): if self.get_option("mylib_config_reconfigure", default=False):
if self.ask_values(set_it=True) and self.save(): self.ask_values(set_it=True)
sys.exit(0)
sys.exit(1) sys.exit(1)
return options return options
@ -1100,27 +1111,32 @@ class Config: # pylint: disable=too-many-instance-attributes
:param execute_callback: Sections's loaded callbacks will be finally executed :param execute_callback: Sections's loaded callbacks will be finally executed
(only if set_it is True, default: False) (only if set_it is True, default: False)
:return: If set_it is True, return True if valid value for each configuration :return: a dict of configuration section and their options value.
option have been retrieved and set. If False, return a dict of configuration :rtype: dict
section and their options value.
:rtype: bool of dict
""" """
result = {} result = {}
error = False
for name, section in self.sections.items(): for name, section in self.sections.items():
section_result = section.ask_values(set_it=set_it) result[name] = section.ask_values(set_it=set_it)
if not set_it:
result[name] = section_result if set_it and execute_callback:
elif not section_result: self._loaded()
error = True
if set_it:
if error:
return False
if execute_callback:
self._loaded()
return True
return result return result
def ask_value(self, section, option, set_it=True):
"""
Ask user to enter value for the specified configuration option
:param section: The configuration section name
:param option: The configuration option name
:param set_it: If True (default), option value will be updated with user input
:return: The configuration option value.
:rtype: mixed
"""
assert self.defined(section, option), f"Unknown option {section}.{option}"
return self.sections[section].ask_value(option, set_it=set_it)
def configure(self, argv=None, description=False): def configure(self, argv=None, description=False):
""" """
Entry point of a script you could use to created your configuration file Entry point of a script you could use to created your configuration file