config: start adding support of multiline values
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
2cdc7b870d
commit
3bf87222fd
1 changed files with 26 additions and 13 deletions
|
@ -189,7 +189,7 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
|
||||||
""" Export option to configuration file """
|
""" Export option to configuration file """
|
||||||
lines = []
|
lines = []
|
||||||
if self.comment:
|
if self.comment:
|
||||||
lines.append('# ' + self.comment)
|
lines.append(f'# {self.comment}')
|
||||||
value = self.to_config()
|
value = self.to_config()
|
||||||
default_value = (
|
default_value = (
|
||||||
'' if self.default is None else
|
'' if self.default is None else
|
||||||
|
@ -199,28 +199,41 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
|
||||||
'export_to_config(%s, %s): value=%s / default=%s',
|
'export_to_config(%s, %s): value=%s / default=%s',
|
||||||
self.section.name, self.name, value, default_value)
|
self.section.name, self.name, value, default_value)
|
||||||
if default_value:
|
if default_value:
|
||||||
|
if isinstance(default_value, str) and '\n' in default_value:
|
||||||
|
lines.append('# Default:')
|
||||||
|
lines.extend([f'# {line}' for line in default_value.split('\n')])
|
||||||
|
else:
|
||||||
lines.append(
|
lines.append(
|
||||||
'# Default: %s' % default_value
|
f'# Default: {default_value}'
|
||||||
)
|
)
|
||||||
if value and value != default_value:
|
if value and value != default_value:
|
||||||
|
if isinstance(value, str) and '\n' in value:
|
||||||
|
value_lines = value.split('\n')
|
||||||
|
lines.append(f'# Default: {value_lines[0]}')
|
||||||
|
lines.extend([f' {line.replace("#", "%(hash)s")}' for line in value_lines[1:]])
|
||||||
|
else:
|
||||||
lines.append(
|
lines.append(
|
||||||
'%s = %s' %
|
f'{self.name} = {value}'
|
||||||
(self.name, value)
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
lines.append('# %s =' % self.name)
|
lines.append(f'# {self.name} =')
|
||||||
lines.append('')
|
lines.append('')
|
||||||
return '\n'.join(lines)
|
return '\n'.join(lines)
|
||||||
|
|
||||||
def _ask_value(self, prompt=None, **kwargs):
|
def _ask_value(self, prompt=None, **kwargs):
|
||||||
""" Ask to user to enter value of this option and return it """
|
""" Ask to user to enter value of this option and return it """
|
||||||
if self.comment:
|
if self.comment:
|
||||||
print('# ' + self.comment)
|
print(f'# {self.comment}')
|
||||||
default_value = kwargs.get('default_value', self.get())
|
default_value = kwargs.get('default_value', self.get())
|
||||||
if not prompt:
|
if not prompt:
|
||||||
prompt = "%s: " % self.name
|
prompt = f'{self.name}: '
|
||||||
if default_value is not None:
|
if default_value is not None:
|
||||||
prompt += "[%s] " % self.to_config(default_value)
|
if isinstance(default_value, str) and '\n' in default_value:
|
||||||
|
prompt += "[\n %s\n] " % "\n ".join(
|
||||||
|
self.to_config(default_value).split('\n')
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
prompt += f'[{self.to_config(default_value)}] '
|
||||||
value = input(prompt)
|
value = input(prompt)
|
||||||
return default_value if value == '' else value
|
return default_value if value == '' else value
|
||||||
|
|
||||||
|
@ -646,7 +659,7 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
def load_file(self, filepath, execute_callback=True):
|
def load_file(self, filepath, execute_callback=True):
|
||||||
""" Read configuration file """
|
""" Read configuration file """
|
||||||
|
|
||||||
self.config_parser = ConfigParser()
|
self.config_parser = ConfigParser(defaults={'hash': '#'})
|
||||||
self._filepath = filepath
|
self._filepath = filepath
|
||||||
|
|
||||||
# Checking access of configuration file
|
# Checking access of configuration file
|
||||||
|
|
Loading…
Reference in a new issue