warning and errors management

This commit is contained in:
gwen 2024-09-10 09:13:17 +02:00
parent 312b31d795
commit 56cf1fc97e
2 changed files with 19 additions and 83 deletions

View file

@ -28,6 +28,7 @@ def run(rougailconfig,
user_datas, user_datas,
): ):
RougailUserDataEnvironment(config, RougailUserDataEnvironment(config,
user_datas=user_datas,
rougailconfig=rougailconfig, rougailconfig=rougailconfig,
).run() ).run()

View file

@ -30,6 +30,7 @@ class RougailUserDataEnvironment:
config: 'Config', config: 'Config',
*, *,
rougailconfig: RougailConfig=None, rougailconfig: RougailConfig=None,
user_datas=None,
): ):
self.config = config self.config = config
if rougailconfig is None: if rougailconfig is None:
@ -42,8 +43,15 @@ class RougailUserDataEnvironment:
if 'environment' not in user_data: if 'environment' not in user_data:
raise Exception('environment is not set in step.user_data') raise Exception('environment is not set in step.user_data')
self.rougailconfig = rougailconfig self.rougailconfig = rougailconfig
self.errors = [] if user_datas:
self.warnings = [] self.errors = user_datas['errors']
self.warnings = user_datas['warnings']
else:
self.errors = []
self.warnings = []
# this variable will be used for generating warnings
# about unused ROUGAIL_ environment variables
self.unused_environment_var = get_rougail_environment_var()
def run(self): def run(self):
self.config.property.read_write() self.config.property.read_write()
@ -69,6 +77,10 @@ class RougailUserDataEnvironment:
else: else:
self.parse(self.config) self.parse(self.config)
self.config.property.read_only() self.config.property.read_only()
# if there are unused environment variables
if len(self.unused_environment_var):
unused_environment_var_str = " ".join(self.unused_environment_var)
self.warnings.append("the rougail environment variables were not used : " + unused_environment_var_str)
def parse(self, config, title_level=0): def parse(self, config, title_level=0):
for option in config: for option in config:
@ -94,6 +106,9 @@ class RougailUserDataEnvironment:
# #
rougail_environment_var = get_rougail_environment_var() rougail_environment_var = get_rougail_environment_var()
option_name = option.path() option_name = option.path()
# this is used only for warning purposes
self.unused_environment_var.remove(option_name)
# let's parse the environment variables values
if option_name in rougail_environment_var: if option_name in rougail_environment_var:
if ismulti: if ismulti:
option_bash_value = get_rougail_environment_dict()[option_name] option_bash_value = get_rougail_environment_dict()[option_name]
@ -109,84 +124,4 @@ class RougailUserDataEnvironment:
if type_obj is not None: if type_obj is not None:
option.value.set(type_obj(option_bash_value)) option.value.set(type_obj(option_bash_value))
else: else:
option.value.set(option_bash_value) option.value.set(option_bash_value)
#RougailValidator.option = option
#RougailValidator.option_type = {'type': option_type,
# 'func': type_obj,
# }
#RougailValidator.ismulti = ismulti
#if option_type == 'choice':
# question_funtion = select
# #RougailValidator.default = default
# kwargs['choices'] = option.value.list()
#elif option_type == 'boolean':
# question_funtion = confirm
#else:
# question_funtion = text
# kwargs['validate'] = RougailValidator
#args = ['📓 ' + option.description()]
#if ismulti:
# kwargs['multiline'] = True
# if default:
# kwargs['default'] = "\n".join(default)
#elif default is not None:
# kwargs['default'] = default
#value = RougailValidator().convert_value(question_funtion(*args, **kwargs).ask(), False)
#if isdefault and value == default:
# option.value.reset()
#else:
# option.value.set(value)
#class RougailValidator(Validator):
# def validate(self, document):
# return self.convert_value(document.text)
#
# def convert_value(self,
# document,
# validate=True,
# ):
# if not self.ismulti:
# value = self._convert_a_value(document, document, validate)
# else:
# value = []
# if document is not None:
# for val in document.strip().split('\n'):
# val = self._convert_a_value(val, document, validate)
# if val is not None:
# value.append(val)
# if validate:
# try:
# self.option.value.set(value)
# except ValueOptionError as err:
# err.prefix = ''
# raise ValidationError(
# message=str(err),
# cursor_position=len(document),
# )
# return value
#
# def _convert_a_value(self, value, document, validate):
# if value is None:
# if self.option_type['type'] == 'choice':
# return self.default
# return
# if isinstance(value, str):
# value = value.strip()
# if value == '':
# if validate and "mandatory" in self.option.property.get():
# raise ValidationError(
# message=f"Value must not be empty",
# cursor_position=len(document),
# )
# return
# if self.option_type['func']:
# try:
# return self.option_type['func'](value)
# except:
# raise ValidationError(
# message=f"Not a valid {self.option_type['type']}",
# cursor_position=len(document),
# )
# return value