From 8ed035c7ff14eca2a31838c174872594c868a923 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 8 Jul 2020 11:14:47 +0200 Subject: [PATCH] refactor check --- src/rougail/annotator.py | 105 +++++++----------- src/rougail/objspace.py | 51 +++++---- .../10check_optional/result/00-base.xml | 1 - .../result/00-base.xml | 6 - .../01-base.xml | 2 +- tests/test_flattener.py | 2 +- 6 files changed, 75 insertions(+), 92 deletions(-) diff --git a/src/rougail/annotator.py b/src/rougail/annotator.py index 3d4b8c74b..7de9f08dc 100644 --- a/src/rougail/annotator.py +++ b/src/rougail/annotator.py @@ -305,6 +305,7 @@ class SpaceAnnotator(object): self.convert_helps() if hasattr(self.space, 'constraints'): del self.space.constraints.index + del self.space.constraints.namespace if vars(self.space.constraints): raise Exception('constraints again?') del self.space.constraints @@ -766,75 +767,47 @@ class SpaceAnnotator(object): values = param.text return values - def filter_check(self): # pylint: disable=C0111 - # valid param in check - if not hasattr(self.space, 'constraints') or not hasattr(self.space.constraints, 'check'): - return - space = self.space.constraints.check + def check_check(self): remove_indexes = [] - for check_idx, check in enumerate(space): - namespace = check.namespace + functions = dir(self.eosfunc) + functions.extend(['valid_enum', 'valid_in_network', 'valid_differ']) + for check_idx, check in enumerate(self.space.constraints.check): + if not check.name in functions: + raise CreoleDictConsistencyError(_('cannot find check function {}').format(check.name)) if hasattr(check, 'param'): param_option_indexes = [] for idx, param in enumerate(check.param): if param.type not in TYPE_PARAM_CHECK: raise CreoleDictConsistencyError(_('cannot use {} type as a param in check for {}').format(param.type, check.target)) - if param.type == 'variable': - try: - param.text = self.paths.get_variable_path(param.text, namespace) - except CreoleDictConsistencyError as err: - if param.optional is True: - param_option_indexes.append(idx) - else: - raise err + if param.type == 'variable' and not self.paths.path_is_defined(param.text): + if param.optional is True: + param_option_indexes.append(idx) + else: + raise CreoleDictConsistencyError(_(f'unknown param {param.text} in check')) param_option_indexes = list(set(param_option_indexes)) param_option_indexes.sort(reverse=True) for idx in param_option_indexes: check.param.pop(idx) - if not HIGH_COMPATIBILITY and check.param == []: + if check.param == []: remove_indexes.append(check_idx) remove_indexes.sort(reverse=True) for idx in remove_indexes: - del space[idx] - variables = {} - for index, check in enumerate(space): - namespace = check.namespace - if HIGH_COMPATIBILITY: - if not self.paths.path_is_defined(check.target): - continue - check.is_in_leadership = self.paths.get_leader(check.target) != None + del self.space.constraints.check[idx] + + def check_replace_text(self): + for check_idx, check in enumerate(self.space.constraints.check): + if hasattr(check, 'param'): + namespace = check.namespace + for idx, param in enumerate(check.param): + if param.type == 'variable': + param.text = self.paths.get_variable_path(param.text, namespace) + check.is_in_leadership = self.paths.get_leader(check.target) != None # let's replace the target by the path check.target = self.paths.get_variable_path(check.target, namespace) - if check.target not in variables: - variables[check.target] = [] - variables[check.target].append((index, check)) - # remove check already set for a variable + + def check_valid_enum(self): remove_indexes = [] - for checks in variables.values(): - names = {} - for idx, check in checks: - if HIGH_COMPATIBILITY and check.name == 'valid_enum': - redefine = True - else: - redefine = False - #redefine = bool(check.redefine) - if redefine and check.name in names: - remove_indexes.append(names[check.name]) - del names[check.name] - names[check.name] = idx - del check.index - remove_indexes.sort(reverse=True) - for idx in remove_indexes: - del space[idx] - remove_indexes = [] - functions = dir(self.eosfunc) - functions.extend(['valid_enum', 'valid_in_network', 'valid_differ']) - for idx, check in enumerate(space): - if not check.name in functions: - raise CreoleDictConsistencyError(_('cannot find check function {}').format(check.name)) - #is_probe = not check.name in self.eosfunc.func_on_zephir_context - #if is_probe: - # raise CreoleDictConsistencyError(_('cannot have a check with probe function ({})').format(check.name)) + for idx, check in enumerate(self.space.constraints.check): if check.name == 'valid_enum': proposed_value_type = False remove_params = [] @@ -856,30 +829,30 @@ class SpaceAnnotator(object): 'for valid_enum for variable {}' '').format(check.target)) param = check.param[0] + if check.target in self.valid_enums: + raise CreoleDictConsistencyError(_('valid_enum already set for {}' + '').format(check.target)) if proposed_value_type: if param.type == 'variable': try: values = self.load_params_in_validenum(param) except NameError as err: raise CreoleDictConsistencyError(_('cannot load value for variable {}: {}').format(check.target, err)) - add_value = True - if HIGH_COMPATIBILITY and check.is_in_leadership: - add_value = False - if add_value and values: + add_default_value = not check.is_in_leadership + if add_default_value and values: self.force_value[check.target] = values[0] else: - if check.target in self.valid_enums: - raise CreoleDictConsistencyError(_('valid_enum already set for {}' - '').format(check.target)) values = self.load_params_in_validenum(param) self.valid_enums[check.target] = {'type': param.type, 'values': values} remove_indexes.append(idx) remove_indexes.sort(reverse=True) for idx in remove_indexes: - del space[idx] + del self.space.constraints.check[idx] + + def check_change_warning(self): #convert level to "warnings_only" and hidden to "transitive" - for check in space: + for check in self.space.constraints.check: if check.level == 'warning': check.warnings_only = True else: @@ -891,6 +864,14 @@ class SpaceAnnotator(object): check.transitive = False param.hidden = None + def filter_check(self): # pylint: disable=C0111 + # valid param in check + if not hasattr(self.space, 'constraints') or not hasattr(self.space.constraints, 'check'): + return + self.check_check() + self.check_replace_text() + self.check_valid_enum() + self.check_change_warning() if not self.space.constraints.check: del self.space.constraints.check diff --git a/src/rougail/objspace.py b/src/rougail/objspace.py index cabefb756..553e664fd 100644 --- a/src/rougail/objspace.py +++ b/src/rougail/objspace.py @@ -96,6 +96,8 @@ class CreoleObjSpace: self.xmlreflector = XMLReflector() self.xmlreflector.parse_dtd(dtdfilename) self.redefine_variables = None + self.check_removed = None + self.condition_removed = None # ['variable', 'separator', 'family'] self.forced_text_elts = set() @@ -155,6 +157,8 @@ class CreoleObjSpace: """ for xmlfile, document in self.xmlreflector.load_xml_from_folders(xmlfolders): self.redefine_variables = [] + self.check_removed = [] + self.condition_removed = [] self.xml_parse_document(document, self.space, namespace, @@ -199,15 +203,15 @@ class CreoleObjSpace: variableobj, ) self.variableobj_tree_visitor(child, - variableobj, - namespace, - ) + variableobj, + namespace, + ) self.fill_variableobj_path_attribute(space, - child, - namespace, - document, - variableobj, - ) + child, + namespace, + document, + variableobj, + ) self.add_to_tree_structure(variableobj, space, child, @@ -466,10 +470,10 @@ class CreoleObjSpace: found = True def variableobj_tree_visitor(self, - child, - variableobj, - namespace, - ): + child, + variableobj, + namespace, + ): """Creole object tree manipulations """ if child.tag == 'variable': @@ -477,22 +481,27 @@ class CreoleObjSpace: self.remove_check(variableobj.name) if child.attrib.get('remove_condition', False): self.remove_condition(variableobj.name) - if child.tag in ['fill', 'check']: + if child.tag == 'fill': # if variable is a redefine in current dictionary # XXX not working with variable not in variable and in leader/followers variableobj.redefine = child.attrib['target'] in self.redefine_variables if not hasattr(variableobj, 'index'): variableobj.index = self.index - if child.tag in ['fill', 'condition', 'check']: - variableobj.namespace = namespace + if child.tag == 'check' and child.attrib['target'] in self.redefine_variables and child.attrib['target'] not in self.check_removed: + self.remove_check(child.attrib['target']) + self.check_removed.append(child.attrib['target']) + if child.tag == 'condition' and child.attrib['source'] in self.redefine_variables and child.attrib['source'] not in self.check_removed: + self.remove_condition(child.attrib['source']) + self.condition_removed.append(child.attrib['source']) + variableobj.namespace = namespace def fill_variableobj_path_attribute(self, - space, - child, - namespace, - document, - variableobj, - ): # pylint: disable=R0913 + space, + child, + namespace, + document, + variableobj, + ): # pylint: disable=R0913 """Fill self.paths attributes """ if isinstance(space, self.help): # pylint: disable=E1101 diff --git a/tests/flattener_dicos/10check_optional/result/00-base.xml b/tests/flattener_dicos/10check_optional/result/00-base.xml index e7857bcd5..6ada42197 100644 --- a/tests/flattener_dicos/10check_optional/result/00-base.xml +++ b/tests/flattener_dicos/10check_optional/result/00-base.xml @@ -12,7 +12,6 @@ rougail.general.int2 - normal diff --git a/tests/flattener_dicos/10check_valid_differ_add/result/00-base.xml b/tests/flattener_dicos/10check_valid_differ_add/result/00-base.xml index 42454274c..0b4374d45 100644 --- a/tests/flattener_dicos/10check_valid_differ_add/result/00-base.xml +++ b/tests/flattener_dicos/10check_valid_differ_add/result/00-base.xml @@ -25,12 +25,6 @@ non - - rougail.general.mode_conteneur_actif1 - - - rougail.general.mode_conteneur_actif2 - rougail.general.mode_conteneur_actif1 diff --git a/tests/flattener_dicos/10check_valid_differ_removecheck/01-base.xml b/tests/flattener_dicos/10check_valid_differ_removecheck/01-base.xml index 2f4e1a741..e8a453de8 100644 --- a/tests/flattener_dicos/10check_valid_differ_removecheck/01-base.xml +++ b/tests/flattener_dicos/10check_valid_differ_removecheck/01-base.xml @@ -5,7 +5,7 @@ - + oui diff --git a/tests/test_flattener.py b/tests/test_flattener.py index d2962255a..8056111e0 100644 --- a/tests/test_flattener.py +++ b/tests/test_flattener.py @@ -29,7 +29,7 @@ for test in listdir(dico_dirs): excludes = set([]) test_ok -= excludes test_raise -= excludes -#test_ok = ['11multi_disabled_if_in_filelist'] +#test_ok = ['10check_valid_differ_removecheck'] #test_raise = []