From 0823353c0a661e23f343acbc6efbce6c9269a8c5 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 19 Jan 2022 17:11:55 +0100 Subject: [PATCH] config: Fix configuration interactive mode --- mylib/config.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/mylib/config.py b/mylib/config.py index 6d964cf..af99f1b 100644 --- a/mylib/config.py +++ b/mylib/config.py @@ -202,11 +202,11 @@ class BaseOption: # pylint: disable=too-many-instance-attributes lines.append('') return '\n'.join(lines) - def _ask_value(self, prompt=None): + def _ask_value(self, prompt=None, **kwargs): """ Ask to user to enter value of this option and return it """ if self.comment: print('# ' + self.comment) - default_value = self.get() + default_value = kwargs.get('default_value', self.get()) if not prompt: prompt = "%s: " % self.name if default_value is not None: @@ -261,7 +261,7 @@ class BooleanOption(BaseOption): ).lower().replace('_', '-') ) - def _ask_value(self, prompt=None): + def _ask_value(self, prompt=None, **kwargs): """ Ask to user to enter value of this option and return it """ default_value = self.get() prompt = "%s: " % self.name @@ -270,12 +270,12 @@ class BooleanOption(BaseOption): else: prompt += '[y/N] ' while True: - value = super()._ask_value(prompt).lower() - if value in ['', None]: + value = super()._ask_value(prompt, **kwargs) + if value in ['', None, default_value]: return default_value - if value == 'y': + if value.lower() == 'y': return True - if value == 'n': + if value.lower() == 'n': return False print('Invalid answer. Possible values: Y or N (case insensitive)') @@ -293,12 +293,12 @@ class FloatOption(BaseOption): def parser_type(self): return float - def _ask_value(self, prompt=None): + def _ask_value(self, prompt=None, **kwargs): """ Ask to user to enter value of this option and return it """ default_value = self.get() while True: - value = super()._ask_value() - if value in ['', None]: + value = super()._ask_value(prompt, **kwargs) + if value in ['', None, default_value]: return default_value try: return float(value) @@ -323,12 +323,12 @@ class IntegerOption(BaseOption): def parser_type(self): return int - def _ask_value(self, prompt=None): + def _ask_value(self, prompt=None, **kwargs): """ Ask to user to enter value of this option and return it """ - default_value = self.get() + default_value = kwargs.pop('default_value', self.get()) while True: - value = super()._ask_value() - if value in ['', None]: + value = super()._ask_value(prompt, default_value=default_value, **kwargs) + if value in ['', None, default_value]: return default_value try: return int(value) @@ -399,11 +399,11 @@ class PasswordOption(StringOption): value = self.keyring_value super().set(value) - def _ask_value(self, prompt=None): + def _ask_value(self, prompt=None, **kwargs): """ Ask to user to enter value of this option and return it """ if self.comment: print('# ' + self.comment) - default_value = self.get() + default_value = kwargs.pop('default_value', self.get()) if not prompt: prompt = '%s: ' % self.name if default_value is not None: