From 56cf1fc97eb7941ec0a13045926d83bd0347e85e Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 10 Sep 2024 09:13:17 +0200 Subject: [PATCH] warning and errors management --- src/rougail/user-data-environment/cli.py | 1 + src/rougail/user-data-environment/data.py | 101 ++++------------------ 2 files changed, 19 insertions(+), 83 deletions(-) diff --git a/src/rougail/user-data-environment/cli.py b/src/rougail/user-data-environment/cli.py index 40d48db..ebd8507 100644 --- a/src/rougail/user-data-environment/cli.py +++ b/src/rougail/user-data-environment/cli.py @@ -28,6 +28,7 @@ def run(rougailconfig, user_datas, ): RougailUserDataEnvironment(config, + user_datas=user_datas, rougailconfig=rougailconfig, ).run() diff --git a/src/rougail/user-data-environment/data.py b/src/rougail/user-data-environment/data.py index 4e24af5..e1c2f31 100644 --- a/src/rougail/user-data-environment/data.py +++ b/src/rougail/user-data-environment/data.py @@ -30,6 +30,7 @@ class RougailUserDataEnvironment: config: 'Config', *, rougailconfig: RougailConfig=None, + user_datas=None, ): self.config = config if rougailconfig is None: @@ -42,8 +43,15 @@ class RougailUserDataEnvironment: if 'environment' not in user_data: raise Exception('environment is not set in step.user_data') self.rougailconfig = rougailconfig - self.errors = [] - self.warnings = [] + if user_datas: + 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): self.config.property.read_write() @@ -69,6 +77,10 @@ class RougailUserDataEnvironment: else: self.parse(self.config) 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): for option in config: @@ -94,6 +106,9 @@ class RougailUserDataEnvironment: # rougail_environment_var = get_rougail_environment_var() 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 ismulti: option_bash_value = get_rougail_environment_dict()[option_name] @@ -109,84 +124,4 @@ class RougailUserDataEnvironment: if type_obj is not None: option.value.set(type_obj(option_bash_value)) else: - 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 + option.value.set(option_bash_value)