valid_entier (with mini or maxi parameter) configure min_number or max_number for IntOption

This commit is contained in:
Emmanuel Garette 2020-08-01 20:55:15 +02:00
parent 331e386470
commit 2742774aa9
8 changed files with 54 additions and 37 deletions

View File

@ -68,6 +68,7 @@ PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_
CONVERT_PROPERTIES = {'auto_save': ['force_store_value'], 'auto_freeze': ['force_store_value', 'auto_freeze']}
RENAME_ATTIBUTES = {'description': 'doc'}
INTERNAL_FUNCTIONS = ['valid_enum', 'valid_in_network', 'valid_differ', 'valid_entier']
class SpaceAnnotator:
"""Transformations applied on a CreoleObjSpace instance
@ -498,6 +499,10 @@ class VariableAnnotator:
families.doc = families.name
for family in families.family.values():
family.doc = family.name
for key, value in RENAME_ATTIBUTES.items():
if hasattr(family, key):
setattr(family, value, getattr(family, key))
setattr(family, key, None)
family.name = normalize_family(family.name)
if hasattr(family, 'variable'):
for variable in family.variable.values():
@ -621,7 +626,7 @@ class ConstraintAnnotator:
def check_check(self):
remove_indexes = []
functions = dir(self.eosfunc)
functions.extend(['valid_enum', 'valid_in_network', 'valid_differ'])
functions.extend(INTERNAL_FUNCTIONS)
for check_idx, check in enumerate(self.objectspace.space.constraints.check):
if not check.name in functions:
raise DictConsistencyError(_('cannot find check function {}').format(check.name))
@ -959,34 +964,48 @@ class ConstraintAnnotator:
def convert_check(self):
for check in self.objectspace.space.constraints.check:
variable = self.objectspace.paths.get_variable_obj(check.target)
check_ = self.objectspace.check()
name = check.name
if name == 'valid_differ':
name = 'valid_not_equal'
elif name == 'valid_network_netmask':
params_len = 1
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
elif name == 'valid_ipnetmask':
params_len = 1
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
name = 'valid_ip_netmask'
elif name == 'valid_broadcast':
params_len = 2
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
elif name == 'valid_in_network':
params_len = 2
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
check_.name = name
check_.warnings_only = check.warnings_only
if hasattr(check, 'param'):
check_.param = check.param
if not hasattr(variable, 'check'):
variable.check = []
variable.check.append(check_)
if name == 'valid_entier':
if not hasattr(check, 'param'):
raise DictConsistencyError(_('{} must have, at least, 1 param').format(name))
for param in check.param:
if param.type not in ['string', 'number']:
raise DictConsistencyError(_(f'param in "valid_entier" must not be a "{param.type}"'))
if param.name == 'mini':
variable.min_number = int(param.text)
elif param.name == 'maxi':
print('pppp', param.text)
variable.max_number = int(param.text)
else:
raise DictConsistencyError(_(f'unknown parameter {param.text} in check "valid_entier" for variable {check.target}'))
else:
check_ = self.objectspace.check()
if name == 'valid_differ':
name = 'valid_not_equal'
elif name == 'valid_network_netmask':
params_len = 1
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
elif name == 'valid_ipnetmask':
params_len = 1
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
name = 'valid_ip_netmask'
elif name == 'valid_broadcast':
params_len = 2
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
elif name == 'valid_in_network':
params_len = 2
if len(check.param) != params_len:
raise DictConsistencyError(_('{} must have {} param').format(name, params_len))
check_.name = name
check_.warnings_only = check.warnings_only
if hasattr(check, 'param'):
check_.param = check.param
if not hasattr(variable, 'check'):
variable.check = []
variable.check.append(check_)
del self.objectspace.space.constraints.check
def convert_fill(self): # pylint: disable=C0111,R0912

View File

@ -337,6 +337,8 @@ class Variable(Common):
for key in self.get_attributes(self.elt):
value = getattr(self.elt, key)
if key in FORCE_INFORMATIONS:
if key == 'test' and self.object_type == 'IntOption':
value = int(value)
self.informations[key] = value
else:
self.attrib[key] = value

View File

@ -24,10 +24,6 @@ def get_mount_point_device(*args, **kwargs):
pass
def valid_entier(*args, **kwargs):
pass
def valid_differ(*args, **kwargs):
pass

View File

@ -3,7 +3,7 @@ from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = StrOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='b')
option_4 = IntOption(properties=frozenset({'normal'}), validators=[Calculation(func.valid_entier, Params((ParamSelfOption()), kwargs={'mini': ParamValue("0"), 'maxi': ParamValue("100")}), warnings_only=False)], name='int', doc='No change', multi=False)
option_4 = IntOption(properties=frozenset({'normal'}), name='int', doc='No change', multi=False, max_number=100, min_number=0)
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'normal'}), children=[option_3, option_4])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -14,7 +14,7 @@
</variables>
<constraints>
<check name="valid_entier" target="int"/>
<check name="valid_lower" target="int"/>
</constraints>
<help/>

View File

@ -3,7 +3,7 @@ from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = StrOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='b')
option_4 = IntOption(properties=frozenset({'normal'}), validators=[Calculation(func.valid_entier, Params((ParamSelfOption()), kwargs={}), warnings_only=False)], name='int', doc='No change', multi=False)
option_4 = IntOption(properties=frozenset({'normal'}), validators=[Calculation(func.valid_lower, Params((ParamSelfOption()), kwargs={}), warnings_only=False)], name='int', doc='No change', multi=False)
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'normal'}), children=[option_3, option_4])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -17,7 +17,7 @@
</variables>
<constraints>
<check name="valid_entier" target="int">
<check name="valid_lower" target="int">
<param name="mini">0</param>
<param name="maxi" type="variable">int2</param>
</check>

View File

@ -4,7 +4,7 @@ import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = StrOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='b')
option_4 = IntOption(properties=frozenset({'mandatory', 'normal'}), name='int2', doc='No change', multi=False, default=100)
option_5 = IntOption(properties=frozenset({'normal'}), validators=[Calculation(func.valid_entier, Params((ParamSelfOption()), kwargs={'mini': ParamValue("0"), 'maxi': ParamOption(option_4, notraisepropertyerror=False, todict=False)}), warnings_only=False)], name='int', doc='No change', multi=False)
option_5 = IntOption(properties=frozenset({'normal'}), validators=[Calculation(func.valid_lower, Params((ParamSelfOption()), kwargs={'mini': ParamValue("0"), 'maxi': ParamOption(option_4, notraisepropertyerror=False, todict=False)}), warnings_only=False)], name='int', doc='No change', multi=False)
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'normal'}), children=[option_3, option_4, option_5])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])