refactor check

This commit is contained in:
Emmanuel Garette 2020-07-08 11:14:47 +02:00
parent 7d032a72b2
commit 8ed035c7ff
6 changed files with 75 additions and 92 deletions

View file

@ -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.type == 'variable' and not self.paths.path_is_defined(param.text):
if param.optional is True:
param_option_indexes.append(idx)
else:
raise err
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):
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
if HIGH_COMPATIBILITY:
if not self.paths.path_is_defined(check.target):
continue
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

View file

@ -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,
@ -477,13 +481,18 @@ 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']:
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,

View file

@ -12,7 +12,6 @@
<check name="valid_not_equal" warnings_only="False">
<param type="variable">rougail.general.int2</param>
</check>
<check name="valid_not_equal" warnings_only="False"/>
<property>normal</property>
</variable>
<variable doc="No change" multi="False" name="int2" type="number">

View file

@ -25,12 +25,6 @@
<value type="string">non</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif3" type="string">
<check name="valid_not_equal" warnings_only="False">
<param type="variable">rougail.general.mode_conteneur_actif1</param>
</check>
<check name="valid_not_equal" warnings_only="False">
<param type="variable">rougail.general.mode_conteneur_actif2</param>
</check>
<check name="valid_not_equal" warnings_only="False">
<param type="variable">rougail.general.mode_conteneur_actif1</param>
</check>

View file

@ -5,7 +5,7 @@
<variables>
<family name="general">
<variable name="mode_conteneur_actif3" redefine="True" remove_check="True">
<variable name="mode_conteneur_actif3" redefine="True">
<value>oui</value>
</variable>
</family>

View file

@ -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 = []