refactor condition

This commit is contained in:
Emmanuel Garette 2020-07-07 19:36:26 +02:00
parent b12bf443a6
commit 9fcd72d459
6 changed files with 68 additions and 122 deletions

View File

@ -539,7 +539,7 @@ class SpaceAnnotator(object):
for variable in family.variable.values():
if variable.auto_freeze:
new_condition = self.objectspace.condition()
new_condition.name = 'auto_hidden_if_in'
new_condition.name = 'auto_hidden_if_not_in'
new_condition.namespace = variables.name
new_condition.source = FREEZE_AUTOFREEZE_VARIABLE
new_param = self.objectspace.param()
@ -953,77 +953,7 @@ class SpaceAnnotator(object):
for idx in del_idx:
condition.target.pop(idx)
def filter_condition_servicelist(self):
# automatic generation of the service_access lists
# and the service_restriction lists from the servicelist
for condition in self.space.constraints.condition:
if hasattr(condition, 'target'):
new_targets = []
for target in condition.target:
if target.type == 'servicelist':
new_target = copy(target)
new_target.type = 'service_accesslist'
new_target.name = '___auto_{}'.format(new_target.name)
new_targets.append(new_target)
new_target = copy(target)
new_target.type = 'service_restrictionlist'
new_target.name = '___auto_{}'.format(new_target.name)
new_targets.append(new_target)
condition.target.extend(new_targets)
def check_condition_without_target(self):
for condition in self.space.constraints.condition:
if not hasattr(condition, 'target'):
raise CreoleDictConsistencyError(_('target is mandatory in condition'))
def check_condition_fallback_not_exists(self, fallback_variables, fallback_lists):
# a condition with a fallback **and** the source variable doesn't exist
remove_conditions = []
for idx, condition in enumerate(self.space.constraints.condition):
if (hasattr(condition, 'fallback') and condition.fallback is True and
not self.paths.path_is_defined(condition.source)):
for target in condition.target:
if target.type in ['variable', 'family']:
name = target.name.split('.')[-1]
if target.type == 'variable':
variable = self.paths.get_variable_obj(name)
else:
variable = self.paths.get_family_obj(name)
if condition.name in ['disabled_if_in']:
variable.disabled = True
if condition.name in ['mandatory_if_in']:
variable.mandatory = True
if condition.name in ['hidden_if_in', 'hidden_if_not_in']:
variable.hidden = False
if HIGH_COMPATIBILITY:
fallback_variables.append(name)
else:
listname = target.type
if not listname.endswith('list'):
raise Exception('not yet implemented')
listvars = self.objectspace.list_conditions.get(listname,
{}).get(target.name)
if listvars:
for listvar in listvars:
try:
variable = self.paths.get_variable_obj(listvar)
except CreoleDictConsistencyError:
variable = self.paths.get_family_obj(listvar)
if condition.name in ['disabled_if_in']:
variable.disabled = True
if condition.name in ['mandatory_if_in']:
variable.mandatory = True
if condition.name in ['hidden_if_in', 'hidden_if_not_in']:
variable.hidden = False
fallback_lists.append(listvar)
remove_conditions.append(idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.space.constraints.condition.pop(idx)
def convert_xxxlist_to_variable(self, fallback_lists): # pylint: disable=C0111
def convert_xxxlist_to_variable(self): # pylint: disable=C0111
# transform *list to variable or family
for condition_idx, condition in enumerate(self.space.constraints.condition):
new_targets = []
@ -1037,14 +967,8 @@ class SpaceAnnotator(object):
{}).get(target.name)
if listvars:
for listvar in listvars:
if listvar in fallback_lists:
continue
try:
variable = self.paths.get_variable_obj(listvar)
type_ = 'variable'
except CreoleDictConsistencyError:
variable = self.paths.get_family_obj(listvar)
type_ = 'family'
new_target = self.objectspace.target()
new_target.type = type_
new_target.name = listvar
@ -1058,21 +982,61 @@ class SpaceAnnotator(object):
condition.target.extend(new_targets)
def check_condition(self):
# if condition.name == 'hidden_if_in':
# condition.name = 'disabled_if_in'
# elif condition.name == 'hidden_if_not_in':
# condition.name = 'disabled_if_not_in'
for condition in self.space.constraints.condition:
if condition.name not in ['disabled_if_in', 'disabled_if_not_in', 'hidden_if_in', 'auto_hidden_if_in',
if condition.name not in ['disabled_if_in', 'disabled_if_not_in', 'hidden_if_in', 'auto_hidden_if_not_in',
'hidden_if_not_in', 'mandatory_if_in', 'mandatory_if_not_in']:
raise CreoleDictConsistencyError(_('unknown condition {}').format(condition.name))
raise CreoleDictConsistencyError(_(f'unknown condition {condition.name}'))
def check_params(self):
for condition in self.space.constraints.condition:
for param in condition.param:
if param.type not in TYPE_PARAM_CONDITION:
raise CreoleDictConsistencyError(_('cannot use {} type as a param '
'in a condition').format(param.type))
raise CreoleDictConsistencyError(_(f'cannot use {param.type} type as a param in a condition'))
def check_target(self):
for condition in self.space.constraints.condition:
if not hasattr(condition, 'target'):
raise CreoleDictConsistencyError(_('target is mandatory in condition'))
for target in condition.target:
if target.type.endswith('list') and condition.name not in ['disabled_if_in', 'disabled_if_not_in']:
raise CreoleDictConsistencyError(_(f'target in condition for {target.type} not allow in {condition.name}'))
def check_condition_fallback_not_exists(self):
# a condition with a fallback **and** the source variable doesn't exist
remove_conditions = []
for idx, condition in enumerate(self.space.constraints.condition):
if condition.fallback is True and not self.paths.path_is_defined(condition.source):
for target in condition.target:
if target.type in ['variable', 'family']:
if target.name.startswith(VARIABLE_NAMESPACE + '.'):
name = target.name.split('.')[-1]
else:
name = target.name
if target.type == 'variable':
variable = self.paths.get_variable_obj(name)
else:
variable = self.paths.get_family_obj(name)
if condition.name == 'disabled_if_in':
variable.disabled = True
if condition.name == 'mandatory_if_in':
variable.mandatory = True
if condition.name == 'hidden_if_in':
variable.hidden = True
else:
listname = target.type
listvars = self.objectspace.list_conditions.get(listname,
{}).get(target.name, None)
if listvars is not None:
for listvar in listvars:
variable = self.paths.get_variable_obj(listvar)
if condition.name in ['disabled_if_in']:
variable.value[0].name = False
del self.objectspace.list_conditions[listname][target.name]
remove_conditions.append(idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.space.constraints.condition.pop(idx)
def check_choice_option_condition(self, force_remove_targets):
# remove condition for ChoiceOption that don't have param
@ -1123,7 +1087,7 @@ class SpaceAnnotator(object):
for idx in remove_conditions:
self.space.constraints.condition.pop(idx)
def manage_variable_property(self, force_remove_targets, fallback_variables):
def manage_variable_property(self, force_remove_targets):
for condition in self.space.constraints.condition:
remove_targets = []
#parse each variable and family
@ -1138,9 +1102,6 @@ class SpaceAnnotator(object):
variable = self.paths.get_variable_obj(name)
else:
variable = self.paths.get_family_obj(name)
if name in fallback_variables:
remove_targets.append(target_idx)
continue
if condition.name in ['hidden_if_in', 'hidden_if_not_in']:
variable.hidden = False
if condition.name in ['mandatory_if_in', 'mandatory_if_not_in']:
@ -1169,41 +1130,26 @@ class SpaceAnnotator(object):
def filter_condition(self): # pylint: disable=C0111
if not hasattr(self.space, 'constraints') or not hasattr(self.space.constraints, 'condition'):
return
fallback_variables = []
fallback_lists = []
force_remove_targets = {}
self.check_condition()
self.check_params()
self.check_condition_without_target()
self.filter_condition_servicelist()
self.check_condition_fallback_not_exists(fallback_variables, fallback_lists)
self.check_target()
self.check_condition_fallback_not_exists()
self.filter_targets()
self.convert_xxxlist_to_variable(fallback_lists)
self.convert_xxxlist_to_variable()
self.check_choice_option_condition(force_remove_targets)
self.manage_variable_property(force_remove_targets, fallback_variables)
self.manage_variable_property(force_remove_targets)
self.remove_condition_with_empty_target()
for condition in self.space.constraints.condition:
if condition.name == 'disabled_if_in':
inverse = condition.name.endswith('_if_not_in')
if condition.name.startswith('disabled_if_'):
actions = ['disabled']
inverse = False
elif condition.name == 'disabled_if_not_in':
actions = ['disabled']
inverse = True
elif condition.name == 'hidden_if_in':
elif condition.name.startswith('hidden_if_'):
actions = ['frozen', 'hidden', 'force_default_on_freeze']
inverse = False
elif condition.name == 'hidden_if_not_in':
actions = ['frozen', 'hidden', 'force_default_on_freeze']
inverse = True
elif condition.name == 'mandatory_if_in':
elif condition.name.startswith('mandatory_if_'):
actions = ['mandatory']
inverse = False
elif condition.name == 'mandatory_if_not_in':
actions = ['mandatory']
inverse = True
elif condition.name == 'auto_hidden_if_in':
elif condition.name == 'auto_hidden_if_not_in':
actions = ['auto_frozen']
inverse = True
for param in condition.param:
if hasattr(param, 'text'):
param = param.text

View File

@ -145,7 +145,7 @@
<!ATTLIST param optional (True|False) "False">
<!ELEMENT target (#PCDATA)>
<!ATTLIST target type (family|variable|filelist|iplist|portlist|tcpwrapperlist|packagelist|actionlist) "variable">
<!ATTLIST target type (family|variable|filelist|iplist|portlist) "variable">
<!ATTLIST target optional (True|False) "False">
<!ELEMENT follower (#PCDATA)>

View File

@ -16,6 +16,7 @@
<property>disabled</property>
<property>mandatory</property>
<property>normal</property>
<property expected="oui" inverse="False" source="rougail.general.condition" type="calculation">disabled</property>
<value type="string">non</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif2" type="choice">

View File

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": "non", "rougail.general.condition": "non", "services.test.files.file1.group": "root", "services.test.files.file1.mode": "0644", "services.test.files.file1.name": "/tmp/file1", "services.test.files.file1.owner": "root", "services.test.files.file1.source": "file1", "services.test.files.file1.templating": true}
{"rougail.general.mode_conteneur_actif": "non", "rougail.general.condition": "non", "services.test.files.file1.group": "root", "services.test.files.file1.mode": "0644", "services.test.files.file1.name": "/tmp/file1", "services.test.files.file1.owner": "root", "services.test.files.file1.source": "file1", "services.test.files.file1.templating": true, "services.test.files.file1.activate": false}

View File

@ -24,8 +24,7 @@
<value>True</value>
</variable>
<variable doc="activate" multi="False" name="activate" type="boolean">
<property>disabled</property>
<value>True</value>
<value>False</value>
</variable>
</family>
</family>

View File

@ -29,8 +29,8 @@ for test in listdir(dico_dirs):
excludes = set([])
test_ok -= excludes
test_raise -= excludes
#test_ok = ['10valid_enum_none']
#"test_raise = []
#test_ok = ['11multi_disabled_if_in_filelist']
#test_raise = []
test_ok = list(test_ok)