Compare commits

...

15 Commits

878 changed files with 4723 additions and 7695 deletions

23
doc/auto.rst Normal file
View File

@ -0,0 +1,23 @@
Valeur automatiquement modifiée
===============================
Une variable avec valeur automatiquement modifiée est une variable dont la valeur sera considéré comme modifié quand le serveur sera déployé.
Voici un variable a valeur automatiquement modifiée :
<variable name="my_variable" type="oui/non" description="My variable" auto_save="True">
Dans ce cas la valeur est fixée à la valeur actuelle.
Par exemple, si la valeur de cette variable est issue d'un calcul, la valeur ne sera plus recalculée.
Valeur en lecture seule automatique
===================================
Une variable avec valeur en lecture seule automatique est une variable dont la valeur ne sera plus modifiable par l'utilisateur quand le serveur sera déployé.
Voici un variable à valeur en lecture seule automatique :
<variable name="my_variable" type="oui/non" description="My variable" auto_freeze="True">
Dans ce cas la valeur est fixée à la valeur actuelle et elle ne sera plus modifiable par l'utilisateur.
Par exemple, si la valeur de cette variable est issue d'un calcul, la valeur ne sera plus recalculée.

277
doc/fill.rst Normal file
View File

@ -0,0 +1,277 @@
Les variables calculées
=======================
Une variable calculée est une variable donc sa valeur est le résultat d'une fonction python.
Variable avec une valeur par défaut calculée
--------------------------------------------
Créons une variable de type "oui/non" donc la valeur est retournée par la fonction "return_no" :
<variables>
<family name="family">
<variable name="my_calculated_variable" type="oui/non" description="My calculated variable"/>
</family>
</variables>
<constraints>
<fill name="return_no" target="my_calculated_variable"/>
</constraints>
Puis créons la fonction "return_no" :
def return_no():
return 'non'
Dans ce cas, la valeur par défaut est la valeur retournée par la fonction (ici "non"), elle sera calculée tant que l'utilisateur n'a pas de spécifié une valeur à cette variable.
Si l'utilisateur à définit une valeur par défaut à "my_calculated_variable" :
<variable name="my_calculated_variable" type="oui/non" description="My calculated variable">
<value>oui</value>
</variable>
Cette valeur par défaut sera complètement ignorée.
Variable avec une valeur calculée
---------------------------------
En ajoutant le paramètre "hidden" à "True" dans la variable précédente, l'utilisateur n'aura plus la possibilité de modifié la valeur. La valeur de la variable sera donc systématiquement calculée :
<variable name="my_calculated_variable" type="oui/non" description="My calculated variable" hidden="True"/>
Si une condition "hidden_if_in" est spécifié à la variable, la valeur sera modifiable par l'utilisateur si elle n'est pas cachée mais elle sera systèmatiquement calculée (même si elle a déjà était modifiée) si la variable est cachée.
Variable avec valeur calculée obligatoire
-----------------------------------------
Par défaut les variables calculées ne sont pas des varibles obligatoires.
Dans ce cas un calcul peut retourner None, mais surtout un utilisateur peut spécifier une valeur nulle à cette variable. Dans ce cas le calcul ne sera pas réalisé.
Fonction avec une valeur fixe comme paramètre positionnel
---------------------------------------------------------
Déclarons un calcul avec paramètre :
<constraints>
<fill name="return_value" target="my_calculated_variable">
<param>non</param>
</fill>
</constraints>
Créons la fonction correspondante :
def return_value(value):
return value
La variable aura donc "non" comme valeur puisque le paramètre aura la valeur fixe "non".
Paramètre nommée
----------------
Déclarons une contrainte avec un paramètre nommée :
<constraints>
<fill name="return_value" target="my_calculated_variable">
<param name="valeur">non</param>
</fill>
</constraints>
Dans ce cas la fonction return_value sera exécuté avec le paramètre nommé "valeur" dont sa valeur sera "non".
Paramètre avec un nombre
------------------------
Déclarons un calcul avec paramètre avec un nombre :
<constraints>
<fill name="return_value_with_number" target="my_calculated_variable">
<param type="number">1</param>
</fill>
</constraints>
Créons la fonction correspondante :
def return_value_with_number(value):
if value == 1:
return 'non'
return 'oui'
La variable aura donc "non" comme valeur puisque le paramètre aura la valeur fixe "1".
Paramètre dont la valeur est issue d'une autre variable
-------------------------------------------------------
Créons deux variables avec une contrainte de type variable qui contient le nom de la variable dont sa valeur sera utilisé comme paramètre :
<variables>
<family name="family">
<variable name="my_calculated_variable" type="oui/non" description="My calculated variable"/>
<variable name="my_variable" type="number" description="My variable">
<value>1</value>
</variable>
</family>
</variables>
<constraints>
<fill name="return_value_with_number" target="my_calculated_variable">
<param type="variable">my_variable</param>
</fill>
</constraints>
Si l'utilisateur laisse la valeur 1 à "my_variable", la valeur par défault de la variable "my_calculated_variable" sera "non".
Si la valeur de "my_variable" est différent de 1, la valeur par défaut de la variable "my_calculated_variable" sera "oui".
Paramètre dont la valeur est issue d'une information de la configuration
------------------------------------------------------------------------
Créons une variable et la contrainte :
<variables>
<family name="family">
<variable name="my_calculated_variable" type="string" description="My calculated variable"/>
</family>
</variables>
<constraints>
<fill name="return_value" target="my_calculated_variable">
<param type="information">server_name</param>
</fill>
</constraints>
Dans ce cas, l'information de la configuration "server_name" sera utilisé comme valeur de la variable "my_calculated_variable".
Si l'information n'existe pas, la paramètre aura la valeur "None".
Paramètre avec variable potentiellement non existante
-----------------------------------------------------
Suivant le contexte une variable peut exister ou ne pas exister.
Un paramètre de type "variable" peut être "optional" :
<variables>
<family name="family">
<variable name="my_calculated_variable" type="oui/non" description="My calculated variable"/>
</family>
</variables>
<constraints>
<fill name="return_value" target="my_calculated_variable">
<param type="variable" optional="True">unknow_variable</param>
</fill>
</constraints>
Dans ce cas la fonction "return_value" est exécuté sans paramètre.
Les variables suiveuses
-----------------------
FIXME :
- tests/flattener_dicos/10leadership_append/00-base.xml
- tests/flattener_dicos/10leadership_auto/00-base.xml
- tests/flattener_dicos/10leadership_autoleader/00-base.xml
- tests/flattener_dicos/10leadership_autoleader_expert/00-base.xml
Les variables dynamiques
------------------------
Paramètre avec variable dynamique
'''''''''''''''''''''''''''''''''
Il est possible de faire un calcul avec comme paramètre une variable dynamique mais pour une suffix particulier :
<variables>
<family name='family'>
<variable name='suffixes' type='string' description="Suffixes of dynamic family" multi="True">
<value>val1</value>
<value>val2</value>
</variable>
<variable name="my_calculated_variable" type="string" description="My calculated variable"/>
</family>
<family name='dyn' dynamic="suffixes">
<variable name='vardyn' type='string' description="Dynamic variable">
<value>val</value>
</variable>
</family>
</variables>
<constraints>
<fill name="return_value" target="my_calculated_variable">
<param type="variable">vardynval1</param>
</fill>
</constraints>
Dans ce cas, valeur du paramètre de la fonction "return_value" sera la valeur de la variable "vardyn" avec le suffix "val1".
Calcule d'une variable dynamique
''''''''''''''''''''''''''''''''
Il est également possible de calculer une variable dynamique à partir d'une variable standard :
<variables>
<family name='family'>
<variable name='suffixes' type='string' description="Suffixes of dynamic family" multi="True">
<value>val1</value>
<value>val2</value>
</variable>
<variable name="my_variable" type="string" description="My variable">
<value>val</value>
</variable>
</family>
<family name='dyn' dynamic="suffixes">
<variable name="my_calculated_variable_dyn_" type="string" description="My calculated variable"/>
<value>val</value>
</variable>
</family>
</variables>
<constraints>
<fill name="return_value" target="my_calculated_variable_dyn_">
<param type="variable">my_variable</param>
</fill>
</constraints>
Dans ce cas, les variables dynamiques "my_calculated_variable_dyn_" seront calculés à partir de la valeur de la variable "my_variable".
Que cela soit pour la variable "my_calculated_variable_dyn_val1" et "my_calculated_variable_dyn_val2".
Par contre, il n'est pas possible de faire un calcul pour une seule des deux variables issues de la variable dynamique.
Si c'est ce que vous cherchez à faire, il faudra prévoir un traitement particulier dans votre fonction.
Dans ce cas, il faut explicitement demander la valeur du suffix dans la fonction :
<constraints>
<fill name="return_value_suffix" target="my_calculated_variable_dyn_">
<param type="variable">my_variable</param>
<param type="suffix"/>
</fill>
</constraints>
Et ainsi faire un traitement spécifique pour ce suffix :
def return_value_suffix(value, suffix):
if suffix == 'val1':
return value
Redéfinition des calcules
-------------------------
Dans un premier dictionnaire déclarons notre variable et notre calcule :
<variables>
<family name="family">
<variable name="my_calculated_variable" type="oui/non" description="My calculated variable"/>
</family>
</variables>
<constraints>
<fill name="return_no" target="my_calculated_variable"/>
</constraints>
Dans un second dictionnaire il est possible de redéfinir le calcul :
<variables>
<family name="family">
<variable name="my_calculated_variable" redefine="True"/>
</family>
</variables>
<constraints>
<fill name="return_yes" target="my_calculated_variable"/>
</constraints>
Dans ce cas, à aucun moment la fonction "return_no" ne sera exécuté. Seul la fonction "return_yes" le sera.

13
doc/variable.rst Normal file
View File

@ -0,0 +1,13 @@
Variable
========
Variable obligatoire
--------------------
Variable dont une valeur est requise :
<variables>
<family name="family">
<variable name="my_variable" type="oui/non" description="My variable" mandatory="True"/>
</family>
</variables>

View File

@ -1,5 +1,5 @@
from .loader import load #from .loader import load
from .objspace import CreoleObjSpace from .objspace import CreoleObjSpace
from .annotator import modes from .annotator import modes
__ALL__ = ('load', 'CreoleObjSpace', 'modes') __ALL__ = ('CreoleObjSpace', 'modes')

View File

@ -10,8 +10,7 @@ import imp
from .i18n import _ from .i18n import _
from .utils import normalize_family from .utils import normalize_family
from .error import CreoleDictConsistencyError from .error import DictConsistencyError
from .xmlreflector import HIGH_COMPATIBILITY
#mode order is important #mode order is important
modes_level = ('basic', 'normal', 'expert') modes_level = ('basic', 'normal', 'expert')
@ -39,7 +38,7 @@ modes = mode_factory()
# that shall not be present in the exported (flatened) XML # that shall not be present in the exported (flatened) XML
ERASED_ATTRIBUTES = ('redefine', 'exists', 'fallback', 'optional', 'remove_check', 'namespace', ERASED_ATTRIBUTES = ('redefine', 'exists', 'fallback', 'optional', 'remove_check', 'namespace',
'remove_condition', 'path', 'instance_mode', 'index', 'is_in_leadership', 'remove_condition', 'path', 'instance_mode', 'index', 'is_in_leadership',
'level', 'submulti') # , '_real_container') 'level') # , '_real_container')
ERASED_CONTAINER_ATTRIBUTES = ('id', 'container', 'group_id', 'group', 'container_group') ERASED_CONTAINER_ATTRIBUTES = ('id', 'container', 'group_id', 'group', 'container_group')
FORCE_CHOICE = {'oui/non': ['oui', 'non'], FORCE_CHOICE = {'oui/non': ['oui', 'non'],
@ -57,14 +56,154 @@ KEY_TYPE = {'variable': 'symlink',
'URLOption': 'web_address', 'URLOption': 'web_address',
'FilenameOption': 'filename'} 'FilenameOption': 'filename'}
TYPE_PARAM_CHECK = ('string', 'python', 'variable')
TYPE_PARAM_CONDITION = ('string', 'python', 'number', 'variable')
TYPE_PARAM_FILL = ('string', 'number', 'variable')
CONVERSION = {'number': int} CONVERSION = {'number': int}
FREEZE_AUTOFREEZE_VARIABLE = 'module_instancie' FREEZE_AUTOFREEZE_VARIABLE = 'module_instancie'
VARIABLE_NAMESPACE = 'rougail' PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_freeze',
'force_store_value', 'disabled', 'mandatory')
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
"""
def __init__(self, objectspace, eosfunc_file):
self.objectspace = objectspace
GroupAnnotator(objectspace)
ServiceAnnotator(objectspace)
VariableAnnotator(objectspace)
ConstraintAnnotator(objectspace,
eosfunc_file,
)
FamilyAnnotator(objectspace)
PropertyAnnotator(objectspace)
class GroupAnnotator:
def __init__(self,
objectspace,
):
self.objectspace = objectspace
if not hasattr(self.objectspace.space, 'constraints') or not hasattr(self.objectspace.space.constraints, 'group'):
return
self.convert_groups()
def convert_groups(self): # pylint: disable=C0111
for group in self.objectspace.space.constraints.group:
leader_fullname = group.leader
leader_family_name = self.objectspace.paths.get_variable_family_name(leader_fullname)
leader_name = self.objectspace.paths.get_variable_name(leader_fullname)
namespace = self.objectspace.paths.get_variable_namespace(leader_fullname)
if '.' not in leader_fullname:
leader_fullname = '.'.join([namespace, leader_family_name, leader_fullname])
follower_names = list(group.follower.keys())
has_a_leader = False
ori_leader_family = self.objectspace.paths.get_family_obj(leader_fullname.rsplit('.', 1)[0])
for variable in list(ori_leader_family.variable.values()):
if has_a_leader:
# it's a follower
self.manage_follower(namespace,
leader_family_name,
variable,
leader_name,
follower_names,
leader_space,
leader_is_hidden,
)
ori_leader_family.variable.pop(variable.name)
if follower_names == []:
# no more follower
break
elif variable.name == leader_name:
# it's a leader
if isinstance(variable, self.objectspace.Leadership):
# append follower to an existed leadership
leader_space = variable
# if variable.hidden:
# leader_is_hidden = True
else:
leader_space = self.objectspace.Leadership()
leader_is_hidden = self.manage_leader(leader_space,
leader_family_name,
leader_name,
namespace,
variable,
group,
leader_fullname,
)
has_a_leader = True
else:
raise DictConsistencyError(_('cannot found followers {}').format(follower_names))
del self.objectspace.space.constraints.group
def manage_leader(self,
leader_space: 'Leadership',
leader_family_name: str,
leader_name: str,
namespace: str,
variable: 'Variable',
group: 'Group',
leader_fullname: str,
) -> None:
# manage leader's variable
if variable.multi is not True:
raise DictConsistencyError(_('the variable {} in a group must be multi').format(variable.name))
leader_space.variable = []
leader_space.name = leader_name
leader_space.hidden = variable.hidden
if variable.hidden:
leader_is_hidden = True
variable.frozen = True
variable.force_default_on_freeze = True
else:
leader_is_hidden = False
variable.hidden = None
if hasattr(group, 'description'):
leader_space.doc = group.description
elif hasattr(variable, 'description'):
leader_space.doc = variable.description
else:
leader_space.doc = variable.name
leader_path = namespace + '.' + leader_family_name + '.' + leader_name
self.objectspace.paths.add_family(namespace,
leader_path,
leader_space,
)
leader_family = self.objectspace.space.variables[namespace].family[leader_family_name]
leader_family.variable[leader_name] = leader_space
leader_space.variable.append(variable)
self.objectspace.paths.set_leader(namespace,
leader_family_name,
leader_name,
leader_name,
)
leader_space.path = leader_fullname
return leader_is_hidden
def manage_follower(self,
namespace: str,
leader_family_name: str,
variable: 'Variable',
leader_name: str,
follower_names: List[str],
leader_space: 'Leadership',
leader_is_hidden: bool,
) -> None:
if variable.name != follower_names[0]:
raise DictConsistencyError(_('cannot found this follower {}').format(follower_names[0]))
follower_names.remove(variable.name)
if leader_is_hidden:
variable.frozen = True
variable.force_default_on_freeze = True
leader_space.variable.append(variable) # pylint: disable=E1101
self.objectspace.paths.set_leader(namespace,
leader_family_name,
variable.name,
leader_name,
)
class ServiceAnnotator: class ServiceAnnotator:
@ -79,48 +218,56 @@ class ServiceAnnotator:
</services> </services>
""" """
def __init__(self, objectspace): def __init__(self, objectspace):
self.space = objectspace.space
self.paths = objectspace.paths
self.objectspace = objectspace self.objectspace = objectspace
self.grouplist_conditions = {}
self.convert_services() self.convert_services()
def gen_family(self,
name,
):
family = self.objectspace.family()
family.name = name
family.doc = name
family.mode = None
return family
def convert_services(self): def convert_services(self):
if not hasattr(self.space, 'services'): if not hasattr(self.objectspace.space, 'services'):
return return
if not hasattr(self.space.services, 'service'): if not hasattr(self.objectspace.space.services, 'service'):
del self.space.services del self.objectspace.space.services
return return
self.space.services.hidden = True self.objectspace.space.services.hidden = True
self.objectspace.space.services.name = 'services'
self.objectspace.space.services.doc = 'services'
families = {} families = {}
for idx, service_name in enumerate(self.space.services.service.keys()): for idx, service_name in enumerate(self.objectspace.space.services.service.keys()):
service = self.space.services.service[service_name] service = self.objectspace.space.services.service[service_name]
new_service = self.objectspace.service() new_service = self.objectspace.service()
for elttype, values in vars(service).items(): for elttype, values in vars(service).items():
if elttype == 'name' or elttype in ERASED_ATTRIBUTES: if not isinstance(values, (dict, list)) or elttype in ERASED_ATTRIBUTES:
setattr(new_service, elttype, values) setattr(new_service, elttype, values)
continue continue
eltname = elttype + 's' eltname = elttype + 's'
family = self.gen_family(eltname) path = '.'.join(['services', service_name, eltname])
family = self.gen_family(eltname,
path,
)
if isinstance(values, dict): if isinstance(values, dict):
values = list(values.values()) values = list(values.values())
family.family = self.make_group_from_elts(service_name, family.family = self.make_group_from_elts(service_name,
elttype, elttype,
values, values,
f'services.{service_name}.{eltname}', path,
) )
setattr(new_service, elttype, family) setattr(new_service, elttype, family)
new_service.doc = new_service.name
families[service_name] = new_service families[service_name] = new_service
self.space.services.service = families self.objectspace.space.services.service = families
def gen_family(self,
name,
path,
):
family = self.objectspace.family()
family.name = normalize_family(name)
family.doc = name
family.mode = None
self.objectspace.paths.add_family('services',
path,
family,
)
return family
def make_group_from_elts(self, def make_group_from_elts(self,
service_name, service_name,
@ -148,20 +295,22 @@ class ServiceAnnotator:
service_name, service_name,
) )
if hasattr(elt, 'source'): idx = 0
c_name = elt.source while True:
else: if hasattr(elt, 'source'):
c_name = elt.name c_name = elt.source
family = self.gen_family(c_name) else:
c_name = elt.name
if idx:
c_name += f'_{idx}'
subpath = '{}.{}'.format(path, c_name)
if not self.objectspace.paths.family_is_defined(subpath):
break
idx += 1
family = self.gen_family(c_name, subpath)
family.variable = [] family.variable = []
subpath = '{}.{}'.format(path, c_name)
listname = '{}list'.format(name) listname = '{}list'.format(name)
activate_path = '.'.join([subpath, 'activate']) activate_path = '.'.join([subpath, 'activate'])
if elt in self.grouplist_conditions:
# FIXME transformer le activate qui disparait en boolean
self.objectspace.list_conditions.setdefault(listname,
{}).setdefault(self.grouplist_conditions[elt],
[]).append(activate_path)
for key in dir(elt): for key in dir(elt):
if key.startswith('_') or key.endswith('_type') or key in ERASED_ATTRIBUTES: if key.startswith('_') or key.endswith('_type') or key in ERASED_ATTRIBUTES:
continue continue
@ -197,7 +346,7 @@ class ServiceAnnotator:
path, path,
): ):
variable = self.objectspace.variable() variable = self.objectspace.variable()
variable.name = key variable.name = normalize_family(key)
variable.mode = None variable.mode = None
if key == 'name': if key == 'name':
true_key = elt_name true_key = elt_name
@ -214,18 +363,23 @@ class ServiceAnnotator:
type_ = 'string' type_ = 'string'
variable.type = type_ variable.type = type_
if type_ == 'symlink': if type_ == 'symlink':
variable.opt = value variable.opt = self.objectspace.paths.get_variable_path(value,
'services',
)
# variable.opt = value
variable.multi = None
else: else:
variable.doc = key variable.doc = key
val = self.objectspace.value() val = self.objectspace.value()
val.type = type_
val.name = value val.name = value
variable.value = [val] variable.value = [val]
self.paths.add_variable('services', self.objectspace.paths.add_variable('services',
path, path,
'service', 'service',
False, False,
variable, variable,
) )
return variable return variable
def _reorder_elts(self, def _reorder_elts(self,
@ -274,214 +428,673 @@ class ServiceAnnotator:
if not hasattr(file_, 'source'): if not hasattr(file_, 'source'):
file_.source = basename(file_.name) file_.source = basename(file_.name)
elif not hasattr(file_, 'source'): elif not hasattr(file_, 'source'):
raise CreoleDictConsistencyError(_('attribute source mandatory for file with variable name ' raise DictConsistencyError(_('attribute source mandatory for file with variable name '
'for {}').format(file_.name)) 'for {}').format(file_.name))
class SpaceAnnotator(object): class VariableAnnotator:
"""Transformations applied on a CreoleObjSpace instance def __init__(self,
""" objectspace,
def __init__(self, objectspace, eosfunc_file): ):
self.paths = objectspace.paths
self.space = objectspace.space
self.objectspace = objectspace self.objectspace = objectspace
self.valid_enums = {}
self.force_value = {}
self.force_not_mandatory = []
if eosfunc_file:
self.eosfunc = imp.load_source('eosfunc', eosfunc_file)
else:
self.eosfunc = None
if HIGH_COMPATIBILITY:
self.has_hidden_if_in_condition = []
self.convert_variable() self.convert_variable()
self.convert_helps()
self.convert_auto_freeze() self.convert_auto_freeze()
self.convert_groups() self.convert_separators()
self.filter_check()
self.filter_condition() def convert_variable(self):
self.convert_valid_enums() def _convert_variable(variable,
self.convert_check() variable_type,
self.convert_fill() ):
if not hasattr(variable, 'type'):
variable.type = 'string'
if variable.type != 'symlink' and not hasattr(variable, 'description'):
variable.description = variable.name
if hasattr(variable, 'value'):
for value in variable.value:
if not hasattr(value, 'type'):
value.type = variable.type
value.name = CONVERSION.get(value.type, str)(value.name)
for key, value in RENAME_ATTIBUTES.items():
setattr(variable, value, getattr(variable, key))
setattr(variable, key, None)
if variable_type == 'follower':
if variable.multi is True:
variable.multi = 'submulti'
else:
variable.multi = True
def _convert_valid_enum(namespace,
variable,
path,
):
if variable.type in FORCE_CHOICE:
check = self.objectspace.check()
check.name = 'valid_enum'
check.target = path
check.namespace = namespace
check.param = []
for value in FORCE_CHOICE[variable.type]:
param = self.objectspace.param()
param.text = value
check.param.append(param)
if not hasattr(self.objectspace.space, 'constraints'):
self.objectspace.space.constraints = self.objectspace.constraints()
self.objectspace.space.constraints.namespace = namespace
if not hasattr(self.objectspace.space.constraints, 'check'):
self.objectspace.space.constraints.check = []
self.objectspace.space.constraints.check.append(check)
variable.type = 'string'
if not hasattr(self.objectspace.space, 'variables'):
return
for families in self.objectspace.space.variables.values():
namespace = families.name
if hasattr(families, 'family'):
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():
if isinstance(variable, self.objectspace.Leadership):
for idx, follower in enumerate(variable.variable):
if idx == 0:
variable_type = 'master'
else:
variable_type = 'follower'
path = '{}.{}.{}.{}'.format(namespace, normalize_family(family.name), variable.name, follower.name)
_convert_variable(follower,
variable_type,
)
_convert_valid_enum(namespace,
follower,
path,
)
else:
path = '{}.{}.{}'.format(namespace, normalize_family(family.name), variable.name)
_convert_variable(variable,
'variable',
)
_convert_valid_enum(namespace,
variable,
path,
)
def convert_helps(self):
if not hasattr(self.objectspace.space, 'help'):
return
helps = self.objectspace.space.help
if hasattr(helps, 'variable'):
for hlp in helps.variable.values():
variable = self.objectspace.paths.get_variable_obj(hlp.name)
variable.help = hlp.text
if hasattr(helps, 'family'):
for hlp in helps.family.values():
variable = self.objectspace.paths.get_family_obj(hlp.name)
variable.help = hlp.text
del self.objectspace.space.help
def convert_auto_freeze(self): # pylint: disable=C0111
def _convert_auto_freeze(variable, namespace):
if variable.auto_freeze:
new_condition = self.objectspace.condition()
new_condition.name = 'auto_hidden_if_not_in'
new_condition.namespace = namespace
new_condition.source = FREEZE_AUTOFREEZE_VARIABLE
new_param = self.objectspace.param()
new_param.text = 'oui'
new_condition.param = [new_param]
new_target = self.objectspace.target()
new_target.type = 'variable'
path = variable.namespace + '.' + normalize_family(family.name) + '.' + variable.name
new_target.name = path
new_condition.target = [new_target]
if not hasattr(self.objectspace.space.constraints, 'condition'):
self.objectspace.space.constraints.condition = []
self.objectspace.space.constraints.condition.append(new_condition)
if hasattr(self.objectspace.space, 'variables'):
for variables in self.objectspace.space.variables.values():
if hasattr(variables, 'family'):
namespace = variables.name
for family in variables.family.values():
if hasattr(family, 'variable'):
for variable in family.variable.values():
if isinstance(variable, self.objectspace.Leadership):
for follower in variable.variable:
_convert_auto_freeze(follower, namespace)
else:
_convert_auto_freeze(variable, namespace)
def convert_separators(self): # pylint: disable=C0111,R0201
if not hasattr(self.objectspace.space, 'variables'):
return
for family in self.objectspace.space.variables.values():
if not hasattr(family, 'separators'):
continue
if hasattr(family.separators, 'separator'):
for idx, separator in enumerate(family.separators.separator):
option = self.objectspace.paths.get_variable_obj(separator.name)
if hasattr(option, 'separator'):
subpath = self.objectspace.paths.get_variable_path(separator.name,
separator.namespace,
)
raise DictConsistencyError(_('{} already has a separator').format(subpath))
option.separator = separator.text
del family.separators
class ConstraintAnnotator:
def __init__(self,
objectspace,
eosfunc_file,
):
if not hasattr(objectspace.space, 'constraints'):
return
self.objectspace = objectspace
self.eosfunc = imp.load_source('eosfunc', eosfunc_file)
self.valid_enums = {}
if hasattr(self.objectspace.space.constraints, 'check'):
self.check_check()
self.check_replace_text()
self.check_valid_enum()
self.check_change_warning()
self.convert_check()
if hasattr(self.objectspace.space.constraints, 'condition'):
self.check_params_target()
self.filter_targets()
self.convert_xxxlist_to_variable()
self.check_condition_fallback_optional()
self.check_choice_option_condition()
self.remove_condition_with_empty_target()
self.convert_condition()
if hasattr(self.objectspace.space.constraints, 'fill'):
self.convert_fill()
self.remove_constraints()
def check_check(self):
remove_indexes = []
functions = dir(self.eosfunc)
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))
if hasattr(check, 'param'):
param_option_indexes = []
for idx, param in enumerate(check.param):
if param.type == 'variable' and not self.objectspace.paths.path_is_defined(param.text):
if param.optional is True:
param_option_indexes.append(idx)
else:
raise DictConsistencyError(_(f'unknown param {param.text} in check'))
if param.type != 'variable':
param.notraisepropertyerror = None
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 check.param == []:
remove_indexes.append(check_idx)
remove_indexes.sort(reverse=True)
for idx in remove_indexes:
del self.objectspace.space.constraints.check[idx]
def check_replace_text(self):
for check_idx, check in enumerate(self.objectspace.space.constraints.check):
namespace = check.namespace
if hasattr(check, 'param'):
for idx, param in enumerate(check.param):
if param.type == 'variable':
param.text = self.objectspace.paths.get_variable_path(param.text, namespace)
check.is_in_leadership = self.objectspace.paths.get_leader(check.target) != None
# let's replace the target by the path
check.target = self.objectspace.paths.get_variable_path(check.target, namespace)
def check_valid_enum(self):
remove_indexes = []
for idx, check in enumerate(self.objectspace.space.constraints.check):
if check.name == 'valid_enum':
if check.target in self.valid_enums:
raise DictConsistencyError(_(f'valid_enum already set for {check.target}'))
if not hasattr(check, 'param'):
raise DictConsistencyError(_(f'param is mandatory for a valid_enum of variable {check.target}'))
variable = self.objectspace.paths.get_variable_obj(check.target)
values = self.load_params_in_valid_enum(check.param,
variable.name,
variable.type,
)
self._set_valid_enum(variable,
values,
variable.type,
check.target
)
remove_indexes.append(idx)
remove_indexes.sort(reverse=True)
for idx in remove_indexes:
del self.objectspace.space.constraints.check[idx]
def load_params_in_valid_enum(self,
params,
variable_name,
variable_type,
):
has_variable = None
values = []
for param in params:
if param.type == 'variable':
if has_variable is not None:
raise DictConsistencyError(_(f'only one "variable" parameter is allowed for valid_enum of variable {variable_name}'))
has_variable = True
variable = self.objectspace.paths.get_variable_obj(param.text)
if not variable.multi:
raise DictConsistencyError(_(f'only multi "variable" parameter is allowed for valid_enum of variable {variable_name}'))
values = param.text
else:
if has_variable:
raise DictConsistencyError(_(f'only one "variable" parameter is allowed for valid_enum of variable {variable_name}'))
if not hasattr(param, 'text'):
if param.type == 'number':
raise DictConsistencyError(_(f'value is mandatory for valid_enum of variable {variable_name}'))
values.append(None)
else:
values.append(param.text)
return values
def check_change_warning(self):
#convert level to "warnings_only"
for check in self.objectspace.space.constraints.check:
if check.level == 'warning':
check.warnings_only = True
else:
check.warnings_only = False
check.level = None
def _get_family_variables_from_target(self,
target,
):
if target.type == 'variable':
variable = self.objectspace.paths.get_variable_obj(target.name)
family = self.objectspace.paths.get_family_obj(target.name.rsplit('.', 1)[0])
if isinstance(family, self.objectspace.Leadership) and family.name == variable.name:
return family, family.variable
return variable, [variable]
# it's a family
variable = self.objectspace.paths.get_family_obj(target.name)
return variable, list(variable.variable.values())
def check_params_target(self):
for condition in self.objectspace.space.constraints.condition:
if not hasattr(condition, 'target'):
raise DictConsistencyError(_('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 DictConsistencyError(_(f'target in condition for {target.type} not allow in {condition.name}'))
def filter_targets(self): # pylint: disable=C0111
for condition_idx, condition in enumerate(self.objectspace.space.constraints.condition):
namespace = condition.namespace
for idx, target in enumerate(condition.target):
if target.type == 'variable':
if condition.source == target.name:
raise DictConsistencyError(_('target name and source name must be different: {}').format(condition.source))
try:
target.name = self.objectspace.paths.get_variable_path(target.name, namespace)
except DictConsistencyError:
# for optional variable
pass
elif target.type == 'family':
try:
target.name = self.objectspace.paths.get_family_path(target.name, namespace)
except KeyError:
raise DictConsistencyError(_('cannot found family {}').format(target.name))
def convert_xxxlist_to_variable(self): # pylint: disable=C0111
# transform *list to variable or family
for condition_idx, condition in enumerate(self.objectspace.space.constraints.condition):
new_targets = []
remove_targets = []
for target_idx, target in enumerate(condition.target):
if target.type.endswith('list'):
listname = target.type
listvars = self.objectspace.list_conditions.get(listname,
{}).get(target.name)
if listvars:
for listvar in listvars:
variable = self.objectspace.paths.get_variable_obj(listvar)
type_ = 'variable'
new_target = self.objectspace.target()
new_target.type = type_
new_target.name = listvar
new_target.index = target.index
new_targets.append(new_target)
remove_targets.append(target_idx)
remove_targets.sort(reverse=True)
for target_idx in remove_targets:
condition.target.pop(target_idx)
condition.target.extend(new_targets)
def check_condition_fallback_optional(self):
# a condition with a fallback **and** the source variable doesn't exist
remove_conditions = []
for idx, condition in enumerate(self.objectspace.space.constraints.condition):
# fallback
if condition.fallback is True and not self.objectspace.paths.path_is_defined(condition.source):
apply_action = False
if condition.name in ['disabled_if_in', 'mandatory_if_in', 'hidden_if_in']:
apply_action = not condition.force_condition_on_fallback
else:
apply_action = condition.force_inverse_condition_on_fallback
if apply_action:
actions = self._get_condition_actions(condition.name)
for target in condition.target:
leader_or_variable, variables = self._get_family_variables_from_target(target)
for action_idx, action in enumerate(actions):
if action_idx == 0:
setattr(leader_or_variable, action, True)
else:
for variable in variables:
setattr(variable, action, True)
remove_conditions.append(idx)
continue
remove_targets = []
# optional
for idx, target in enumerate(condition.target):
if target.optional is True and not self.objectspace.paths.path_is_defined(target.name):
remove_targets.append(idx)
remove_targets = list(set(remove_targets))
remove_targets.sort(reverse=True)
for idx in remove_targets:
condition.target.pop(idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.objectspace.space.constraints.condition.pop(idx)
def _get_condition_actions(self, condition_name):
if condition_name.startswith('disabled_if_'):
return ['disabled']
elif condition_name.startswith('hidden_if_'):
return ['hidden', 'frozen', 'force_default_on_freeze']
elif condition_name.startswith('mandatory_if_'):
return ['mandatory']
elif condition_name == 'auto_hidden_if_not_in':
return ['auto_frozen']
def check_choice_option_condition(self):
# remove condition for ChoiceOption that don't have param
remove_conditions = []
for condition_idx, condition in enumerate(self.objectspace.space.constraints.condition):
namespace = condition.namespace
condition.source = self.objectspace.paths.get_variable_path(condition.source, namespace, allow_source=True)
src_variable = self.objectspace.paths.get_variable_obj(condition.source)
valid_enum = None
if condition.source in self.valid_enums and self.valid_enums[condition.source]['type'] == 'string':
valid_enum = self.valid_enums[condition.source]['values']
if valid_enum is not None:
remove_param = []
for param_idx, param in enumerate(condition.param):
if param.text not in valid_enum:
remove_param.append(param_idx)
remove_param.sort(reverse=True)
for idx in remove_param:
del condition.param[idx]
if condition.param == []:
remove_targets = []
for target in condition.target:
leader_or_variable, variables = self._get_family_variables_from_target(target)
if condition.name == 'disabled_if_not_in':
leader_or_variable.disabled = True
elif condition.name == 'hidden_if_not_in':
leader_or_variable.hidden = True
for variable in variables:
variable.frozen = True
variable.force_default_on_freeze = True
elif condition.name == 'mandatory_if_not_in':
variable.mandatory = True
remove_targets = list(set(remove_targets))
remove_targets.sort(reverse=True)
for target_idx in remove_targets:
condition.target.pop(target_idx)
remove_conditions.append(condition_idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.objectspace.space.constraints.condition.pop(idx)
def remove_condition_with_empty_target(self):
remove_conditions = []
for condition_idx, condition in enumerate(self.objectspace.space.constraints.condition):
if not condition.target:
remove_conditions.append(condition_idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.objectspace.space.constraints.condition.pop(idx)
def convert_condition(self):
for condition in self.objectspace.space.constraints.condition:
inverse = condition.name.endswith('_if_not_in')
actions = self._get_condition_actions(condition.name)
for param in condition.param:
if hasattr(param, 'text'):
param = param.text
else:
param = None
for target in condition.target:
leader_or_variable, variables = self._get_family_variables_from_target(target)
# if option is already disable, do not apply disable_if_in
if hasattr(leader_or_variable, actions[0]) and getattr(leader_or_variable, actions[0]) is True:
continue
for idx, action in enumerate(actions):
prop = self.objectspace.property_()
prop.type = 'calculation'
prop.inverse = inverse
prop.source = condition.source
prop.expected = param
prop.name = action
if idx == 0:
if not hasattr(leader_or_variable, 'property'):
leader_or_variable.property = []
leader_or_variable.property.append(prop)
else:
for variable in variables:
if not hasattr(variable, 'property'):
variable.property = []
variable.property.append(prop)
del self.objectspace.space.constraints.condition
def _set_valid_enum(self, variable, values, type_, target):
# value for choice's variable is mandatory
variable.mandatory = True
# build choice
variable.choice = []
if isinstance(values, str):
choice = self.objectspace.choice()
choice.type = 'calculation'
choice.name = values
variable.choice.append(choice)
else:
self.valid_enums[target] = {'type': type_,
'values': values,
}
choices = []
for value in values:
choice = self.objectspace.choice()
try:
if value is not None:
choice.name = CONVERSION.get(type_, str)(value)
else:
choice.name = value
except:
raise DictConsistencyError(_(f'unable to change type of a valid_enum entry "{value}" is not a valid "{type_}" for "{variable.name}"'))
if choice.name == '':
choice.name = None
choices.append(choice.name)
choice.type = type_
variable.choice.append(choice)
# check value or set first choice value has default value
if hasattr(variable, 'value'):
for value in variable.value:
value.type = type_
try:
cvalue = CONVERSION.get(type_, str)(value.name)
except:
raise DictConsistencyError(_(f'unable to change type of value "{value}" is not a valid "{type_}" for "{variable.name}"'))
if cvalue not in choices:
raise DictConsistencyError(_('value "{}" of variable "{}" is not in list of all expected values ({})').format(value.name, variable.name, choices))
else:
new_value = self.objectspace.value()
new_value.name = choices[0]
new_value.type = type_
variable.value = [new_value]
if not variable.choice:
raise DictConsistencyError(_('empty valid enum is not allowed for variable {}').format(variable.name))
variable.type = 'choice'
def convert_check(self):
for check in self.objectspace.space.constraints.check:
variable = self.objectspace.paths.get_variable_obj(check.target)
name = check.name
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':
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
# sort fill/auto by index
fills = {fill.index: fill for idx, fill in enumerate(self.objectspace.space.constraints.fill)}
indexes = list(fills.keys())
indexes.sort()
targets = []
eosfunc = dir(self.eosfunc)
for idx in indexes:
fill = fills[idx]
# test if it's redefined calculation
if fill.target in targets and not fill.redefine:
raise DictConsistencyError(_(f"A fill already exists for the target: {fill.target}"))
targets.append(fill.target)
#
if fill.name not in eosfunc:
raise DictConsistencyError(_('cannot find fill function {}').format(fill.name))
namespace = fill.namespace
# let's replace the target by the path
fill.target, suffix = self.objectspace.paths.get_variable_path(fill.target,
namespace,
with_suffix=True,
)
if suffix is not None:
raise DictConsistencyError(_(f'Cannot add fill function to "{fill.target}" only with the suffix "{suffix}"'))
value = self.objectspace.value()
value.type = 'calculation'
value.name = fill.name
if hasattr(fill, 'param'):
param_to_delete = []
for fill_idx, param in enumerate(fill.param):
if param.type not in ['suffix', 'string'] and not hasattr(param, 'text'):
raise DictConsistencyError(_(f"All '{param.type}' variables must have a value in order to calculate {fill.target}"))
if param.type == 'suffix' and hasattr(param, 'text'):
raise DictConsistencyError(_(f"All '{param.type}' variables must not have a value in order to calculate {fill.target}"))
if param.type == 'variable':
try:
param.text, suffix = self.objectspace.paths.get_variable_path(param.text,
namespace,
with_suffix=True,
)
if suffix:
param.suffix = suffix
except DictConsistencyError as err:
if param.optional is False:
raise err
param_to_delete.append(fill_idx)
continue
else:
param.notraisepropertyerror = None
param_to_delete.sort(reverse=True)
for param_idx in param_to_delete:
fill.param.pop(param_idx)
value.param = fill.param
variable = self.objectspace.paths.get_variable_obj(fill.target)
variable.value = [value]
del self.objectspace.space.constraints.fill
def remove_constraints(self):
if hasattr(self.objectspace.space.constraints, 'index'):
del self.objectspace.space.constraints.index
del self.objectspace.space.constraints.namespace
if vars(self.objectspace.space.constraints):
raise Exception('constraints again?')
del self.objectspace.space.constraints
class FamilyAnnotator:
def __init__(self,
objectspace,
):
self.objectspace = objectspace
self.remove_empty_families() self.remove_empty_families()
self.change_variable_mode() self.change_variable_mode()
self.change_family_mode() self.change_family_mode()
self.dynamic_families() self.dynamic_families()
self.filter_separators()
self.absolute_path_for_symlink_in_services()
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
def absolute_path_for_symlink_in_services(self):
if not hasattr(self.space, 'services'):
return
for family_name, family in vars(self.space.services).items():
if not isinstance(family, dict):
continue
for fam in family.values():
for fam1_name, fam1 in vars(fam).items():
if fam1_name == 'name' or fam1_name in ERASED_ATTRIBUTES:
continue
for fam2 in fam1.family:
for variable in fam2.variable:
if variable.type == 'symlink' and '.' not in variable.name:
variable.opt = self.paths.get_variable_path(variable.opt,
VARIABLE_NAMESPACE,
)
def convert_helps(self):
# FIXME l'aide doit etre dans la variable!
if not hasattr(self.space, 'help'):
return
helps = self.space.help
if hasattr(helps, 'variable'):
for hlp in helps.variable.values():
variable = self.paths.get_variable_obj(hlp.name)
variable.help = hlp.text
if hasattr(helps, 'family'):
for hlp in helps.family.values():
variable = self.paths.get_family_obj(hlp.name)
variable.help = hlp.text
del self.space.help
def manage_leader(self,
leader_space: 'Leadership',
leader_family_name: str,
leader_name: str,
namespace: str,
variable: 'Variable',
group: 'Group',
leader_fullname: str,
) -> None:
# manage leader's variable
if variable.multi is not True:
raise CreoleDictConsistencyError(_('the variable {} in a group must be multi').format(variable.name))
leader_space.variable = []
leader_space.name = leader_name
leader_space.hidden = variable.hidden
if variable.hidden:
leader_is_hidden = True
variable.frozen = True
variable.force_default_on_freeze = True
else:
leader_is_hidden = False
variable.hidden = None
if hasattr(group, 'description'):
leader_space.doc = group.description
else:
leader_space.doc = variable.description
leader_path = namespace + '.' + leader_family_name + '.' + leader_name
self.paths.add_family(namespace,
leader_path,
leader_space,
)
leader_family = self.space.variables[namespace].family[leader_family_name]
leader_family.variable[leader_name] = leader_space
leader_space.variable.append(variable)
self.paths.set_leader(namespace,
leader_family_name,
leader_name,
leader_name,
)
leader_space.path = leader_fullname
return leader_is_hidden
def manage_follower(self,
namespace: str,
leader_family_name: str,
variable: 'Variable',
leader_name: str,
follower_names: List[str],
leader_space: 'Leadership',
leader_is_hidden: bool,
) -> None:
if variable.name != follower_names[0]:
raise CreoleDictConsistencyError(_('cannot found this follower {}').format(follower_names[0]))
follower_names.remove(variable.name)
if leader_is_hidden:
variable.frozen = True
variable.force_default_on_freeze = True
# followers are multi
if not variable.multi:
raise CreoleDictConsistencyError(_('the variable {} in a group must be multi or submulti').format(variable.name))
leader_space.variable.append(variable) # pylint: disable=E1101
self.paths.set_leader(namespace,
leader_family_name,
variable.name,
leader_name,
)
def convert_groups(self): # pylint: disable=C0111
if hasattr(self.space, 'constraints') and hasattr(self.space.constraints, 'group'):
for group in self.space.constraints.group:
leader_fullname = group.leader
follower_names = list(group.follower.keys())
leader_family_name = self.paths.get_variable_family_name(leader_fullname)
namespace = self.paths.get_variable_namespace(leader_fullname)
leader_name = self.paths.get_variable_name(leader_fullname)
ori_leader_family = self.space.variables[namespace].family[leader_family_name]
has_a_leader = False
for variable in list(ori_leader_family.variable.values()):
if isinstance(variable, self.objectspace.Leadership):
# append follower to an existed leadership
if variable.name == leader_name:
leader_space = variable
has_a_leader = True
else:
if has_a_leader:
# it's a follower
self.manage_follower(namespace,
leader_family_name,
variable,
leader_name,
follower_names,
leader_space,
leader_is_hidden,
)
ori_leader_family.variable.pop(variable.name)
if follower_names == []:
# no more follower
break
elif variable.name == leader_name:
# it's a leader
leader_space = self.objectspace.Leadership()
leader_is_hidden = self.manage_leader(leader_space,
leader_family_name,
leader_name,
namespace,
variable,
group,
leader_fullname,
)
has_a_leader = True
else:
raise CreoleDictConsistencyError(_('cannot found followers {}').format(follower_names))
del self.space.constraints.group
def remove_empty_families(self): # pylint: disable=C0111,R0201 def remove_empty_families(self): # pylint: disable=C0111,R0201
if hasattr(self.space, 'variables'): if hasattr(self.objectspace.space, 'variables'):
for family in self.space.variables.values(): for family in self.objectspace.space.variables.values():
if hasattr(family, 'family'): if hasattr(family, 'family'):
space = family.family space = family.family
removed_families = [] removed_families = []
for family_name, family in space.items(): for family_name, family in space.items():
if not hasattr(family, 'variable') or len(family.variable) == 0: if not hasattr(family, 'variable') or len(family.variable) == 0:
removed_families.append(family_name) removed_families.append(family_name)
del space[family_name] for family_name in removed_families:
# remove help too del space[family_name]
if hasattr(self.space, 'help') and hasattr(self.space.help, 'family'):
for family in self.space.help.family.keys():
if family in removed_families:
del self.space.help.family[family]
def change_family_mode(self): # pylint: disable=C0111 def change_family_mode(self): # pylint: disable=C0111
if not hasattr(self.space, 'variables'): if not hasattr(self.objectspace.space, 'variables'):
return return
for family in self.space.variables.values(): for family in self.objectspace.space.variables.values():
if hasattr(family, 'family'): if hasattr(family, 'family'):
for family in family.family.values(): for family in family.family.values():
mode = modes_level[-1] mode = modes_level[-1]
@ -497,146 +1110,55 @@ class SpaceAnnotator(object):
family.mode = mode family.mode = mode
def dynamic_families(self): # pylint: disable=C0111 def dynamic_families(self): # pylint: disable=C0111
if not hasattr(self.space, 'variables'): if not hasattr(self.objectspace.space, 'variables'):
return return
for family in self.space.variables.values(): for family in self.objectspace.space.variables.values():
if hasattr(family, 'family'): if hasattr(family, 'family'):
for family in family.family.values(): for family in family.family.values():
if 'dynamic' in vars(family): if 'dynamic' in vars(family):
namespace = self.paths.get_variable_namespace(family.dynamic) namespace = self.objectspace.paths.get_variable_namespace(family.dynamic)
varpath = self.paths.get_variable_path(family.dynamic, namespace) varpath = self.objectspace.paths.get_variable_path(family.dynamic, namespace)
family.dynamic = varpath family.dynamic = varpath
def annotate_variable(self, variable, family_mode, path, is_follower=False): def annotate_variable(self, variable, family_mode, path, is_follower=False):
# if the variable is mandatory and doesn't have any value # if the variable is mandatory and doesn't have any value
# then the variable's mode is set to 'basic' # then the variable's mode is set to 'basic'
has_value = hasattr(variable, 'value') if not hasattr(variable, 'value') and variable.type == 'boolean':
if variable.mandatory is True and (not has_value or is_follower): new_value = self.objectspace.value()
new_value.name = True
new_value.type = 'boolean'
variable.value = [new_value]
if hasattr(variable, 'value') and variable.value:
has_value = True
for value in variable.value:
if value.type == 'calculation':
has_value = False
has_variable = False
if hasattr(value, 'param'):
for param in value.param:
if param.type == 'variable':
has_variable = True
break
#if not has_variable:
# # if one parameter is a variable, let variable choice if it's mandatory
# variable.mandatory = True
if has_value:
# if has value but without any calculation
variable.mandatory = True
if variable.mandatory is True and (not hasattr(variable, 'value') or is_follower):
variable.mode = modes_level[0] variable.mode = modes_level[0]
if variable.mode != None and modes[variable.mode] < modes[family_mode] and (not is_follower or variable.mode != modes_level[0]): if variable.mode != None and modes[variable.mode] < modes[family_mode] and (not is_follower or variable.mode != modes_level[0]):
variable.mode = family_mode variable.mode = family_mode
if has_value and path not in self.force_not_mandatory:
variable.mandatory = True
if variable.hidden is True: if variable.hidden is True:
variable.frozen = True variable.frozen = True
if not variable.auto_save is True and 'force_default_on_freeze' not in vars(variable): if not variable.auto_save is True and 'force_default_on_freeze' not in vars(variable):
variable.force_default_on_freeze = True variable.force_default_on_freeze = True
def convert_variable(self):
if not hasattr(self.space, 'variables'):
return
for families in self.space.variables.values():
if hasattr(families, 'family'):
for family in families.family.values():
if hasattr(family, 'variable'):
for variable in family.variable.values():
if not hasattr(variable, 'type'):
variable.type = 'string'
if variable.type != 'symlink' and not hasattr(variable, 'description'):
variable.description = variable.name
if variable.submulti:
variable.multi = 'submulti'
def convert_auto_freeze(self): # pylint: disable=C0111
if hasattr(self.space, 'variables'):
for variables in self.space.variables.values():
if hasattr(variables, 'family'):
for family in variables.family.values():
if hasattr(family, 'variable'):
for variable in family.variable.values():
if variable.auto_freeze:
new_condition = self.objectspace.condition()
new_condition.name = 'auto_hidden_if_not_in'
new_condition.namespace = variables.name
new_condition.source = FREEZE_AUTOFREEZE_VARIABLE
new_param = self.objectspace.param()
new_param.text = 'oui'
new_condition.param = [new_param]
new_target = self.objectspace.target()
new_target.type = 'variable'
if variables.name == VARIABLE_NAMESPACE:
path = variable.name
else:
path = variable.namespace + '.' + family.name + '.' + variable.name
new_target.name = path
new_condition.target = [new_target]
if not hasattr(self.space.constraints, 'condition'):
self.space.constraints.condition = []
self.space.constraints.condition.append(new_condition)
def _set_valid_enum(self, variable, values, type_):
variable.mandatory = True
variable.choice = []
choices = []
for value in values:
choice = self.objectspace.choice()
try:
if type_ in CONVERSION:
choice.name = CONVERSION[type_](value)
else:
choice.name = str(value)
except:
raise CreoleDictConsistencyError(_(f'unable to change type of a valid_enum entry "{value}" is not a valid "{type_}" for "{variable.name}"'))
choices.append(choice.name)
choice.type = type_
variable.choice.append(choice)
if not variable.choice:
raise CreoleDictConsistencyError(_('empty valid enum is not allowed for variable {}').format(variable.name))
if hasattr(variable, 'value'):
for value in variable.value:
value.type = type_
if type_ in CONVERSION:
cvalue = CONVERSION[type_](value.name)
else:
cvalue = value.name
if cvalue not in choices:
raise CreoleDictConsistencyError(_('value "{}" of variable "{}" is not in list of all expected values ({})').format(value.name, variable.name, choices))
else:
new_value = self.objectspace.value()
new_value.name = values[0]
new_value.type = type_
variable.value = [new_value]
variable.type = 'choice'
def _convert_valid_enum(self, variable, path):
if variable.type in FORCE_CHOICE:
if path in self.valid_enums:
raise CreoleDictConsistencyError(_('cannot set valid enum for variable with type {}').format(variable.type))
self._set_valid_enum(variable, FORCE_CHOICE[variable.type], 'string')
if path in self.valid_enums:
values = self.valid_enums[path]['values']
self._set_valid_enum(variable, values, variable.type)
del self.valid_enums[path]
if path in self.force_value:
new_value = self.objectspace.value()
new_value.name = self.force_value[path]
variable.value = [new_value]
del self.force_value[path]
def convert_valid_enums(self): # pylint: disable=C0111
if not hasattr(self.space, 'variables'):
return
for variables in self.space.variables.values():
namespace = variables.name
if hasattr(variables, 'family'):
for family in variables.family.values():
if hasattr(family, 'variable'):
for variable in family.variable.values():
if isinstance(variable, self.objectspace.Leadership):
for follower in variable.variable:
path = '{}.{}.{}.{}'.format(namespace, family.name, variable.name, follower.name)
self._convert_valid_enum(follower, path)
else:
path = '{}.{}.{}'.format(namespace, family.name, variable.name)
self._convert_valid_enum(variable, path)
# valid_enums must be empty now (all information are store in objects)
if self.valid_enums:
raise CreoleDictConsistencyError(_('valid_enum sets for unknown variables {}').format(self.valid_enums.keys()))
def change_variable_mode(self): # pylint: disable=C0111 def change_variable_mode(self): # pylint: disable=C0111
if not hasattr(self.space, 'variables'): if not hasattr(self.objectspace.space, 'variables'):
return return
for variables in self.space.variables.values(): for variables in self.objectspace.space.variables.values():
namespace = variables.name
if hasattr(variables, 'family'): if hasattr(variables, 'family'):
for family in variables.family.values(): for family in variables.family.values():
family_mode = family.mode family_mode = family.mode
@ -645,19 +1167,12 @@ class SpaceAnnotator(object):
if isinstance(variable, self.objectspace.Leadership): if isinstance(variable, self.objectspace.Leadership):
mode = modes_level[-1] mode = modes_level[-1]
for follower in variable.variable: for idx, follower in enumerate(variable.variable):
if follower.auto_save is True: if follower.auto_save is True:
raise CreoleDictConsistencyError(_('leader/followers {} ' raise DictConsistencyError(_(f'leader/followers {follower.name} could not be auto_save'))
'could not be '
'auto_save').format(follower.name))
if follower.auto_freeze is True: if follower.auto_freeze is True:
raise CreoleDictConsistencyError(_('leader/followers {} ' raise DictConsistencyError(_('leader/followers {follower.name} could not be auto_freeze'))
'could not be ' is_follower = idx != 0
'auto_freeze').format(follower.name))
if HIGH_COMPATIBILITY and variable.name != follower.name: # and variable.variable[0].mode != modes_level[0]:
is_follower = True
else:
is_follower = False
path = '{}.{}.{}'.format(family.path, variable.name, follower.name) path = '{}.{}.{}'.format(family.path, variable.name, follower.name)
self.annotate_variable(follower, family_mode, path, is_follower) self.annotate_variable(follower, family_mode, path, is_follower)
# leader's mode is minimum level # leader's mode is minimum level
@ -674,480 +1189,52 @@ class SpaceAnnotator(object):
path = '{}.{}'.format(family.path, variable.name) path = '{}.{}'.format(family.path, variable.name)
self.annotate_variable(variable, family_mode, path) self.annotate_variable(variable, family_mode, path)
def convert_fill(self): # pylint: disable=C0111,R0912 class PropertyAnnotator:
if not hasattr(self.space, 'constraints') or not hasattr(self.space.constraints, 'fill'): def __init__(self, objectspace):
return self.objectspace = objectspace
# sort fill/auto by index self.convert_annotator()
fills = {fill.index: fill for idx, fill in enumerate(self.space.constraints.fill)}
indexes = list(fills.keys())
indexes.sort()
targets = []
eosfunc = dir(self.eosfunc)
for idx in indexes:
fill = fills[idx]
# test if it's redefined calculation
if fill.target in targets and not fill.redefine:
raise CreoleDictConsistencyError(_(f"A fill already exists for the target: {fill.target}"))
targets.append(fill.target)
#
if not fill.name in eosfunc:
raise CreoleDictConsistencyError(_('cannot find fill function {}').format(fill.name))
namespace = fill.namespace def convert_property(self,
# let's replace the target by the path variable,
fill.target = self.paths.get_variable_path(fill.target, ):
namespace) properties = []
for prop in PROPERTIES:
if hasattr(variable, prop):
if getattr(variable, prop) == True:
for subprop in CONVERT_PROPERTIES.get(prop, [prop]):
properties.append(subprop)
setattr(variable, prop, None)
if hasattr(variable, 'mode') and variable.mode:
properties.append(variable.mode)
variable.mode = None
if properties:
variable.properties = frozenset(properties)
value = self.objectspace.value() def convert_annotator(self): # pylint: disable=C0111
value.type = 'calculation' if hasattr(self.objectspace.space, 'services'):
value.name = fill.name self.convert_property(self.objectspace.space.services)
if hasattr(fill, 'param'): for services in self.objectspace.space.services.service.values():
param_to_delete = [] self.convert_property(services)
for fill_idx, param in enumerate(fill.param): for service in vars(services).values():
if param.type not in TYPE_PARAM_FILL: if isinstance(service, self.objectspace.family):
raise CreoleDictConsistencyError(_(f'cannot use {param.type} type as a param in a fill/auto')) self.convert_property(service)
if param.type != 'string' and not hasattr(param, 'text'): if hasattr(service, 'family'):
raise CreoleDictConsistencyError(_(f"All '{param.type}' variables shall have a value in order to calculate {fill.target}")) self.convert_property(service)
if param.type == 'variable': for family in service.family:
try: self.convert_property(family)
param.text, suffix = self.paths.get_variable_path(param.text, if hasattr(family, 'variable'):
namespace, for variable in family.variable:
with_suffix=True) self.convert_property(variable)
if suffix: if hasattr(self.objectspace.space, 'variables'):
param.suffix = suffix for variables in self.objectspace.space.variables.values():
except CreoleDictConsistencyError as err: if hasattr(variables, 'family'):
if param.optional is False: for family in variables.family.values():
raise err self.convert_property(family)
param_to_delete.append(fill_idx) if hasattr(family, 'variable'):
continue for variable in family.variable.values():
if param.hidden is True: if isinstance(variable, self.objectspace.Leadership):
param.transitive = False self.convert_property(variable)
param.hidden = None for follower in variable.variable:
param_to_delete.sort(reverse=True) self.convert_property(follower)
for param_idx in param_to_delete: else:
fill.param.pop(param_idx) self.convert_property(variable)
value.param = fill.param
variable = self.paths.get_variable_obj(fill.target)
variable.value = [value]
del self.space.constraints.fill
def filter_separators(self): # pylint: disable=C0111,R0201
# FIXME devrait etre dans la variable
if not hasattr(self.space, 'variables'):
return
for family in self.space.variables.values():
if (hasattr(family, 'separators') and hasattr(family.separators, 'separator')):
space = family.separators.separator
names = []
for idx, separator in enumerate(space):
namespace = self.paths.get_variable_namespace(separator.name)
subpath = self.paths.get_variable_path(separator.name, namespace)
separator.name = subpath
if separator.name in names:
raise CreoleDictConsistencyError(_('{} already has a separator').format(separator.name))
names.append(separator.name)
def load_params_in_validenum(self, param):
if param.type in ['string', 'python', 'number']:
if not hasattr(param, 'text') and (param.type == 'python' or param.type == 'number'):
raise CreoleDictConsistencyError(_("All '{}' variables shall be set in order to calculate {}").format(param.type, 'valid_enum'))
if param.type in ['string', 'number']:
try:
values = literal_eval(param.text)
except ValueError:
raise CreoleDictConsistencyError(_('Cannot load {}').format(param.text))
elif param.type == 'python':
try:
#values = eval(param.text, {'eosfunc': self.eosfunc, '__builtins__': {'range': range, 'str': str}})
values = eval(param.text, {'eosfunc': self.eosfunc, '__builtins__': {'range': range, 'str': str}})
except NameError:
raise CreoleDictConsistencyError(_('The function {} is unknown').format(param.text))
if not isinstance(values, list):
raise CreoleDictConsistencyError(_('Function {} shall return a list').format(param.text))
new_values = []
for val in values:
new_values.append(val)
values = new_values
else:
values = param.text
return values
def check_check(self):
remove_indexes = []
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' 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 check.param == []:
remove_indexes.append(check_idx)
remove_indexes.sort(reverse=True)
for idx in remove_indexes:
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)
def check_valid_enum(self):
remove_indexes = []
for idx, check in enumerate(self.space.constraints.check):
if check.name == 'valid_enum':
proposed_value_type = False
remove_params = []
for param_idx, param in enumerate(check.param):
if hasattr(param, 'name') and param.name == 'checkval':
try:
proposed_value_type = self.objectspace.convert_boolean(param.text) == False
remove_params.append(param_idx)
except TypeError as err:
raise CreoleDictConsistencyError(_('cannot load checkval value for variable {}: {}').format(check.target, err))
if proposed_value_type:
# no more supported
raise CreoleDictConsistencyError(_('cannot load checkval value for variable {}, no more supported').format(check.target))
remove_params.sort(reverse=True)
for param_idx in remove_params:
del check.param[param_idx]
if len(check.param) != 1:
raise CreoleDictConsistencyError(_('cannot set more than one param '
'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_default_value = not check.is_in_leadership
if add_default_value and values:
self.force_value[check.target] = values[0]
else:
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 self.space.constraints.check[idx]
def check_change_warning(self):
#convert level to "warnings_only" and hidden to "transitive"
for check in self.space.constraints.check:
if check.level == 'warning':
check.warnings_only = True
else:
check.warnings_only = False
check.level = None
if hasattr(check, 'param'):
for param in check.param:
if not param.hidden is True:
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
def convert_check(self):
if not hasattr(self.space, 'constraints') or not hasattr(self.space.constraints, 'check'):
return
for check in self.space.constraints.check:
variable = self.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 CreoleDictConsistencyError(_('{} must have {} param').format(name, params_len))
elif name == 'valid_ipnetmask':
params_len = 1
if len(check.param) != params_len:
raise CreoleDictConsistencyError(_('{} 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 CreoleDictConsistencyError(_('{} must have {} param').format(name, params_len))
elif name == 'valid_in_network':
params_len = 2
if len(check.param) != params_len:
raise CreoleDictConsistencyError(_('{} 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.space.constraints.check
def filter_targets(self): # pylint: disable=C0111
for condition_idx, condition in enumerate(self.space.constraints.condition):
namespace = condition.namespace
for idx, target in enumerate(condition.target):
if target.type == 'variable':
if condition.source == target.name:
raise CreoleDictConsistencyError(_('target name and source name must be different: {}').format(condition.source))
target.name = self.paths.get_variable_path(target.name, namespace)
elif target.type == 'family':
try:
target.name = self.paths.get_family_path(target.name, namespace)
except KeyError:
raise CreoleDictConsistencyError(_('cannot found family {}').format(target.name))
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 = []
remove_targets = []
for target_idx, target in enumerate(condition.target):
if target.type not in ['variable', 'family']:
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:
variable = self.paths.get_variable_obj(listvar)
type_ = 'variable'
new_target = self.objectspace.target()
new_target.type = type_
new_target.name = listvar
new_target.index = target.index
new_targets.append(new_target)
remove_targets.append(target_idx)
remove_targets = list(set(remove_targets))
remove_targets.sort(reverse=True)
for target_idx in remove_targets:
condition.target.pop(target_idx)
condition.target.extend(new_targets)
def check_condition(self):
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_not_in',
'hidden_if_not_in', 'mandatory_if_in', 'mandatory_if_not_in']:
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(_(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_optional(self):
# a condition with a fallback **and** the source variable doesn't exist
remove_conditions = []
for idx, condition in enumerate(self.space.constraints.condition):
remove_targets = []
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)
for idx, target in enumerate(condition.target):
if target.optional is True and not self.paths.path_is_defined(target.name):
remove_targets.append(idx)
remove_targets = list(set(remove_targets))
remove_targets.sort(reverse=True)
for idx in remove_targets:
condition.target.pop(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):
# remove condition for ChoiceOption that don't have param
remove_conditions = []
for condition_idx, condition in enumerate(self.space.constraints.condition):
namespace = condition.namespace
src_variable = self.paths.get_variable_obj(condition.source)
condition.source = self.paths.get_variable_path(condition.source, namespace, allow_source=True)
valid_enum = None
if condition.source in self.valid_enums and \
self.valid_enums[condition.source]['type'] == 'string':
valid_enum = self.valid_enums[condition.source]['values']
if src_variable.type in FORCE_CHOICE:
valid_enum = FORCE_CHOICE[src_variable.type]
if valid_enum is not None:
remove_param = []
for param_idx, param in enumerate(condition.param):
if param.text not in valid_enum:
remove_param.append(param_idx)
remove_param.sort(reverse=True)
for idx in remove_param:
del condition.param[idx]
if condition.param == []:
remove_targets = []
for target in condition.target:
if target.name.startswith(f'{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_not_in':
variable.disabled = True
elif condition.name == 'hidden_if_not_in':
variable.hidden = True
elif condition.name == 'mandatory_if_not_in':
variable.mandatory = True
remove_targets = list(set(remove_targets))
remove_targets.sort(reverse=True)
for target_idx in remove_targets:
condition.target.pop(target_idx)
remove_conditions.append(condition_idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.space.constraints.condition.pop(idx)
def manage_variable_property(self):
for condition in self.space.constraints.condition:
#parse each variable and family
for target_idx, target in enumerate(condition.target):
if target.name.startswith(f'{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 in ['hidden_if_in', 'hidden_if_not_in']:
variable.hidden = False
if condition.name in ['mandatory_if_in', 'mandatory_if_not_in']:
variable.mandatory = False
if HIGH_COMPATIBILITY and condition.name in ['hidden_if_in',
'hidden_if_not_in']:
self.has_hidden_if_in_condition.append(name)
if condition.name in ['mandatory_if_in', 'mandatory_if_not_in']:
self.force_not_mandatory.append(target.name)
def remove_condition_with_empty_target(self):
remove_conditions = []
for condition_idx, condition in enumerate(self.space.constraints.condition):
if not condition.target:
remove_conditions.append(condition_idx)
remove_conditions = list(set(remove_conditions))
remove_conditions.sort(reverse=True)
for idx in remove_conditions:
self.space.constraints.condition.pop(idx)
def filter_condition(self): # pylint: disable=C0111
if not hasattr(self.space, 'constraints') or not hasattr(self.space.constraints, 'condition'):
return
self.check_condition()
self.check_params()
self.check_target()
self.check_condition_fallback_optional()
self.filter_targets()
self.convert_xxxlist_to_variable()
self.check_choice_option_condition()
self.manage_variable_property()
self.remove_condition_with_empty_target()
for condition in self.space.constraints.condition:
inverse = condition.name.endswith('_if_not_in')
if condition.name.startswith('disabled_if_'):
actions = ['disabled']
elif condition.name.startswith('hidden_if_'):
actions = ['frozen', 'hidden', 'force_default_on_freeze']
elif condition.name.startswith('mandatory_if_'):
actions = ['mandatory']
elif condition.name == 'auto_hidden_if_not_in':
actions = ['auto_frozen']
for param in condition.param:
if hasattr(param, 'text'):
param = param.text
else:
param = None
for target in condition.target:
if target.name.startswith(f'{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 not hasattr(variable, 'property'):
variable.property = []
for action in actions:
prop = self.objectspace.property_()
prop.type = 'calculation'
prop.inverse = inverse
prop.source = condition.source
prop.expected = param
prop.name = action
variable.property.append(prop)
del self.space.constraints.condition

View File

@ -15,3 +15,5 @@ dtdfilename = join(dtddir, 'rougail.dtd')
# chemin du répertoire source des fichiers templates # chemin du répertoire source des fichiers templates
patch_dir = '/srv/rougail/patch' patch_dir = '/srv/rougail/patch'
variable_namespace = 'rougail'

View File

@ -47,6 +47,7 @@
<!ELEMENT service ((port* | tcpwrapper* | ip* | interface* | package* | file* | digitalcertificate* | override*)*) > <!ELEMENT service ((port* | tcpwrapper* | ip* | interface* | package* | file* | digitalcertificate* | override*)*) >
<!ATTLIST service name CDATA #REQUIRED> <!ATTLIST service name CDATA #REQUIRED>
<!ATTLIST service manage (True|False) "True">
<!ELEMENT port (#PCDATA)> <!ELEMENT port (#PCDATA)>
<!ATTLIST port port_type (PortOption|SymLinkOption|variable) "PortOption"> <!ATTLIST port port_type (PortOption|SymLinkOption|variable) "PortOption">
@ -101,7 +102,6 @@
<!ATTLIST variable hidden (True|False) "False"> <!ATTLIST variable hidden (True|False) "False">
<!ATTLIST variable disabled (True|False) "False"> <!ATTLIST variable disabled (True|False) "False">
<!ATTLIST variable multi (True|False) "False"> <!ATTLIST variable multi (True|False) "False">
<!ATTLIST variable submulti (True|False) "False">
<!ATTLIST variable redefine (True|False) "False"> <!ATTLIST variable redefine (True|False) "False">
<!ATTLIST variable exists (True|False) "True"> <!ATTLIST variable exists (True|False) "True">
<!ATTLIST variable mandatory (True|False) "False"> <!ATTLIST variable mandatory (True|False) "False">
@ -130,18 +130,20 @@
<!ATTLIST check level (error|warning) "error"> <!ATTLIST check level (error|warning) "error">
<!ELEMENT condition ((target | param)+ )> <!ELEMENT condition ((target | param)+ )>
<!ATTLIST condition name CDATA #REQUIRED> <!ATTLIST condition name (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) #REQUIRED>
<!ATTLIST condition source CDATA #REQUIRED> <!ATTLIST condition source CDATA #REQUIRED>
<!ATTLIST condition fallback (True|False) "False"> <!ATTLIST condition fallback (True|False) "False">
<!ATTLIST condition force_condition_on_fallback (True|False) "False">
<!ATTLIST condition force_inverse_condition_on_fallback (True|False) "False">
<!ELEMENT group (follower+)> <!ELEMENT group (follower+)>
<!ATTLIST group leader CDATA #REQUIRED> <!ATTLIST group leader CDATA #REQUIRED>
<!ATTLIST group description CDATA #IMPLIED> <!ATTLIST group description CDATA #IMPLIED>
<!ELEMENT param (#PCDATA)> <!ELEMENT param (#PCDATA)>
<!ATTLIST param type (string|variable|number|python) "string"> <!ATTLIST param type (string|number|variable|information|suffix) "string">
<!ATTLIST param name CDATA #IMPLIED> <!ATTLIST param name CDATA #IMPLIED>
<!ATTLIST param hidden (True|False) "True"> <!ATTLIST param notraisepropertyerror (True|False) "False">
<!ATTLIST param optional (True|False) "False"> <!ATTLIST param optional (True|False) "False">
<!ELEMENT target (#PCDATA)> <!ELEMENT target (#PCDATA)>

View File

@ -1,9 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
Erreurs Creole
"""
class ConfigError(Exception): class ConfigError(Exception):
pass pass
@ -19,7 +14,7 @@ class TemplateDisabled(TemplateError):
pass pass
class CreoleOperationError(Exception): class OperationError(Exception):
"""Type error or value Error for Creole variable's type or values """Type error or value Error for Creole variable's type or values
""" """
@ -30,11 +25,11 @@ class SpaceObjShallNotBeUpdated(Exception):
""" """
class CreoleDictConsistencyError(Exception): class DictConsistencyError(Exception):
"""It's not only that the Creole XML is valid against the Creole DTD """It's not only that the Creole XML is valid against the Creole DTD
it's that it is not consistent. it's that it is not consistent.
""" """
class CreoleLoaderError(Exception): class LoaderError(Exception):
pass pass

View File

@ -1,552 +0,0 @@
"""loader
flattened XML specific
"""
from os.path import join, isfile
from lxml.etree import DTD
import imp
from tiramisu import (StrOption, OptionDescription, DynOptionDescription, PortOption,
IntOption, ChoiceOption, BoolOption, SymLinkOption, IPOption,
NetworkOption, NetmaskOption, DomainnameOption, BroadcastOption,
URLOption, EmailOption, FilenameOption, UsernameOption, DateOption,
PasswordOption, BoolOption, MACOption, Leadership, submulti,
Params, ParamSelfOption, ParamOption, ParamDynOption, ParamValue, Calculation, calc_value)
from .config import dtdfilename
from .i18n import _
from .utils import normalize_family
from .annotator import VARIABLE_NAMESPACE
from .error import CreoleLoaderError
FUNC_TO_DICT = ['valid_not_equal']
KNOWN_TAGS = ['family', 'variable', 'separators', 'leader', 'property']
class ConvertDynOptionDescription(DynOptionDescription):
def convert_suffix_to_path(self, suffix):
if not isinstance(suffix, str):
suffix = str(suffix)
return normalize_family(suffix,
check_name=False)
def convert_tiramisu_value(value, obj):
"""
convertit les variables dans le bon type si nécessaire
"""
def _convert_boolean(value):
prop = {'True': True,
'False': False,
'None': None}
if value not in prop:
raise Exception('unknown value {} while trying to cast {} to boolean'.format(value, obj))
return prop[value]
if value is None:
return value
func = {IntOption: int,
BoolOption: _convert_boolean}.get(obj, None)
if func is None:
return value
if isinstance(value, list):
return [func(val) for val in value]
else:
return func(value)
CONVERT_OPTION = {'number': dict(opttype=IntOption),
'choice': dict(opttype=ChoiceOption),
'string': dict(opttype=StrOption),
'password': dict(opttype=PasswordOption),
'mail': dict(opttype=EmailOption),
'boolean': dict(opttype=BoolOption, initkwargs={'default': True}),
'symlink': dict(opttype=SymLinkOption),
'filename': dict(opttype=FilenameOption),
'date': dict(opttype=DateOption),
'unix_user': dict(opttype=UsernameOption),
'ip': dict(opttype=IPOption, initkwargs={'allow_reserved': True}),
'local_ip': dict(opttype=IPOption, initkwargs={'private_only': True, 'warnings_only': True}),
'netmask': dict(opttype=NetmaskOption),
'network': dict(opttype=NetworkOption),
'broadcast': dict(opttype=BroadcastOption),
'netbios': dict(opttype=DomainnameOption, initkwargs={'type': 'netbios', 'warnings_only': True}),
'domain': dict(opttype=DomainnameOption, initkwargs={'type': 'domainname', 'allow_ip': True, 'allow_without_dot': True}),
'domain_strict': dict(opttype=DomainnameOption, initkwargs={'type': 'domainname', 'allow_ip': False}),
'hostname': dict(opttype=DomainnameOption, initkwargs={'type': 'hostname', 'allow_ip': True}),
'hostname_strict': dict(opttype=DomainnameOption, initkwargs={'type': 'hostname', 'allow_ip': False}),
'web_address': dict(opttype=URLOption, initkwargs={'allow_ip': True, 'allow_without_dot': True}),
'port': dict(opttype=PortOption, initkwargs={'allow_private': True}),
'mac': dict(opttype=MACOption),
'cidr': dict(opttype=IPOption, initkwargs={'cidr': True}),
'network_cidr': dict(opttype=NetworkOption, initkwargs={'cidr': True}),
}
class Elt:
def __init__(self, attrib):
self.attrib = attrib
class PopulateTiramisuObjects:
def __init__(self):
self.storage = ElementStorage()
self.booleans = []
def parse_dtd(self, dtdfilename):
"""Loads the Creole DTD
:raises IOError: if the DTD is not found
:param dtdfilename: the full filename of the Creole DTD
"""
if not isfile(dtdfilename):
raise IOError(_("no such DTD file: {}").format(dtdfilename))
with open(dtdfilename, 'r') as dtdfd:
dtd = DTD(dtdfd)
for elt in dtd.iterelements():
if elt.name == 'variable':
for attr in elt.iterattributes():
if set(attr.itervalues()) == set(['True', 'False']):
self.booleans.append(attr.name)
def get_root_family(self):
family = Family(Elt({'name': 'baseoption',
'doc': 'baseoption'}),
self.booleans,
self.storage,
self.eosfunc,
)
self.storage.add('.', family)
return family
def reorder_family(self, xmlroot):
xmlelts = []
for xmlelt in xmlroot:
# VARIABLE_NAMESPACE family has to be loaded before any other family
# because `extra` family could use `VARIABLE_NAMESPACE` variables.
if xmlelt.attrib['name'] == VARIABLE_NAMESPACE:
xmlelts.insert(0, xmlelt)
else:
xmlelts.append(xmlelt)
return xmlelts
def make_tiramisu_objects(self, xmlroot, eosfunc):
self.eosfunc = imp.load_source('eosfunc', eosfunc)
family = self.get_root_family()
for xmlelt in self.reorder_family(xmlroot):
self.iter_family(xmlelt,
family,
None,
)
def iter_family(self,
child,
family,
subpath,
):
if child.tag not in KNOWN_TAGS:
raise CreoleLoaderError(_('unknown tag {}').format(child.tag))
if child.tag in ['family', 'leader']:
self.populate_family(family,
child,
subpath,
)
elif child.tag == 'separators':
self.parse_separators(child)
elif child.tag == 'variable':
self.populate_variable(child, subpath, family)
elif child.tag == 'property':
self.parse_properties(family, child)
else:
raise Exception('unknown tag {}'.format(child.tag))
def populate_family(self,
parent_family,
elt,
subpath,
):
path = self.build_path(subpath,
elt,
)
family = Family(elt,
self.booleans,
self.storage,
self.eosfunc,
)
self.storage.add(path, family)
if elt.tag == 'leader':
family.set_leader()
parent_family.add(family)
if len(elt) != 0:
for child in elt:
self.iter_family(child,
family,
path,
)
def parse_separators(self,
separators,
):
for separator in separators:
elt = self.storage.get(separator.attrib['name'])
elt.add_information('separator', separator.text)
def populate_variable(self, elt, subpath, family):
is_follower = False
is_leader = False
if family.is_leader:
if elt.attrib['name'] != family.attrib['name']:
is_follower = True
else:
is_leader = True
path = self.build_path(subpath,
elt,
)
variable = Variable(elt,
self.booleans,
self.storage,
is_follower,
is_leader,
self.eosfunc,
)
self.storage.add(path, variable)
family.add(variable)
def parse_properties(self, family, child):
if child.get('type') == 'calculation':
kwargs = {'condition': child.attrib['source'],
'expected': ParamValue(child.attrib.get('expected'))}
if child.attrib['inverse'] == 'True':
kwargs['reverse_condition'] = ParamValue(True)
family.attrib['properties'].append((ParamValue(child.text), kwargs))
else:
family.attrib['properties'].append(child.text)
def build_path(self,
subpath,
elt,
):
if subpath is None:
return elt.attrib['name']
return subpath + '.' + elt.attrib['name']
class ElementStorage:
def __init__(self):
self.paths = {}
def add(self, path, elt):
if path in self.paths:
raise CreoleLoaderError(_('path already loaded {}').format(path))
self.paths[path] = elt
def get(self, path):
if path not in self.paths:
raise CreoleLoaderError(_('there is no element for path {}').format(path))
return self.paths[path]
class Common:
def build_properties(self):
for index, prop in enumerate(self.attrib['properties']):
if isinstance(prop, tuple):
action, kwargs = prop
kwargs['condition'] = ParamOption(self.storage.get(kwargs['condition']).get(), todict=True)
prop = Calculation(calc_value,
Params(action,
kwargs=kwargs))
self.attrib['properties'][index] = prop
if self.attrib['properties']:
self.attrib['properties'] = tuple(self.attrib['properties'])
else:
del self.attrib['properties']
class Variable(Common):
def __init__(self, elt, booleans, storage, is_follower, is_leader, eosfunc):
self.option = None
self.informations = {}
self.attrib = {}
convert_option = CONVERT_OPTION[elt.attrib['type']]
if 'initkwargs' in convert_option:
self.attrib.update(convert_option['initkwargs'])
if elt.attrib['type'] != 'symlink':
self.attrib['properties'] = []
self.attrib['validators'] = []
self.eosfunc = eosfunc
self.storage = storage
self.booleans = booleans
self.populate_attrib(elt)
self.is_follower = is_follower
self.is_leader = is_leader
self.object_type = convert_option['opttype']
self.populate_choice(elt)
for child in elt:
self.populate_property(child)
self.populate_value(child)
self.populate_check(child)
if elt.attrib['type'] == 'symlink':
self.attrib['opt'] = storage.get(self.attrib['opt'])
def populate_attrib(self,
elt,
):
for key, value in elt.attrib.items():
if key == 'multi' and elt.attrib['type'] == 'symlink':
continue
if key == 'multi' and value == 'submulti':
value = submulti
elif key in self.booleans:
if value == 'True':
value = True
elif value == 'False':
value = False
else:
raise CreoleLoaderError(_('unknown value {} for {}').format(value, key))
if key in ['help', 'test']:
self.add_information(key, value)
elif key != 'type':
self.attrib[key] = value
def populate_choice(self,
elt,
):
if elt.attrib['type'] == 'choice':
values = []
for child in elt:
if child.tag == 'choice':
value = child.text
if child.attrib['type'] == 'number':
value = int(value)
values.append(value)
self.attrib['values'] = tuple(values)
def populate_property(self, child):
if child.tag == 'property':
if child.get('type') == 'calculation':
kwargs = {'condition': child.attrib['source'],
'expected': ParamValue(child.attrib.get('expected'))}
if child.attrib['inverse'] == 'True':
kwargs['reverse_condition'] = ParamValue(True)
self.attrib['properties'].append((ParamValue(child.text), kwargs))
else:
self.attrib['properties'].append(child.text)
def populate_value(self, child):
if child.tag == 'value':
if child.attrib.get('type') == 'calculation':
if child.text is not None and child.text.strip():
self.attrib['default'] = (child.text.strip(),)
else:
params = []
for param in child:
params.append(self.parse_param(param))
self.attrib['default'] = (child.attrib['name'], params, False)
else:
if "type" in child.attrib:
type_ = CONVERT_OPTION[child.attrib['type']]['opttype']
else:
type_ = self.object_type
if self.attrib['multi'] is True and not self.is_follower:
if 'default' not in self.attrib:
self.attrib['default'] = []
value = convert_tiramisu_value(child.text, type_)
self.attrib['default'].append(value)
if 'default_multi' not in self.attrib and not self.is_leader:
self.attrib['default_multi'] = value
elif self.attrib['multi'] == submulti:
if 'default' not in self.attrib:
self.attrib['default'] = []
value = convert_tiramisu_value(child.text, type_)
if not self.is_follower:
if not isinstance(value, list):
dvalue = [value]
else:
dvalue = value
self.attrib['default'].append(dvalue)
if value and 'default_multi' not in self.attrib and not self.is_leader:
self.attrib['default_multi'] = []
if not self.is_leader:
self.attrib['default_multi'].append(value)
else:
value = convert_tiramisu_value(child.text, type_)
if self.is_follower:
self.attrib['default_multi'] = value
else:
self.attrib['default'] = value
def populate_check(self, child):
if child.tag == 'check':
params = []
for param in child:
params.append(self.parse_param(param))
#check.params = params
self.attrib['validators'].append((child.attrib['name'], params, child.attrib['warnings_only']))
def parse_param(self, param):
name = param.attrib.get('name', '')
if param.attrib['type'] == 'string':
value = param.text
elif param.attrib['type'] == 'variable':
transitive = param.attrib.get('transitive', 'False')
if transitive == 'True':
transitive = True
elif transitive == 'False':
transitive = False
else:
raise CreoleLoaderError(_('unknown transitive boolean {}').format(transitive))
value = [param.text, transitive, param.attrib.get('suffix')]
elif param.attrib['type'] == 'number':
value = int(param.text)
else:
raise CreoleLoaderError(_('unknown param type {}').format(param.attrib['type']))
return(name, value)
def add_information(self, key, value):
if key in self.informations:
raise CreoleLoaderError(_('key already exists in information {}').format(key))
self.informations[key] = value
def build_calculator(self, key):
if key in self.attrib:
values = self.attrib[key]
if isinstance(values, list):
is_list = True
else:
is_list = False
values = [values]
ret = []
for value in values:
if isinstance(value, tuple):
if key == 'validators':
args = [ParamSelfOption()]
else:
args = []
kwargs = {}
if len(value) == 3:
for param in value[1]:
if isinstance(param[1], list):
option = self.storage.get(param[1][0]).get()
param_kwargs = {'notraisepropertyerror': param[1][1]}
if value[0] in FUNC_TO_DICT:
param_kwargs['todict'] = True
if not param[1][2]:
param_value = ParamOption(option,
**param_kwargs,
)
else:
family = '.'.join(param[1][0].split('.', 3)[:2])
param_value = ParamDynOption(option,
param[1][2],
self.storage.get(family).get(),
**param_kwargs,
)
else:
param_value = ParamValue(param[1])
if not param[0]:
args.append(param_value)
else:
kwargs[param[0]] = param_value
ret.append(Calculation(getattr(self.eosfunc, value[0]),
Params(tuple(args),
kwargs=kwargs)))
else:
ret.append(value)
if not is_list:
self.attrib[key] = ret[0]
else:
self.attrib[key] = ret
def get(self):
if self.option is None:
if self.object_type is SymLinkOption:
self.attrib['opt'] = self.attrib['opt'].get()
else:
self.build_properties()
self.build_calculator('default')
self.build_calculator('validators')
if not self.attrib['validators']:
del self.attrib['validators']
try:
option = self.object_type(**self.attrib)
except Exception as err:
import traceback
traceback.print_exc()
name = self.attrib['name']
raise CreoleLoaderError(_('cannot create option "{}": {}').format(name, err))
for key, value in self.informations.items():
option.impl_set_information(key, value)
self.option = option
return self.option
class Family(Common):
def __init__(self,
elt,
booleans,
storage,
eosfunc,
):
self.option = None
self.attrib = {}
self.is_leader = False
self.informations = {}
self.children = []
self.storage = storage
self.eosfunc = eosfunc
self.attrib['properties'] = []
for key, value in elt.attrib.items():
if key == 'help':
self.add_information(key, value)
else:
self.attrib[key] = value
def add(self, child):
self.children.append(child)
def add_information(self, key, value):
if key in self.informations and not (key == 'icon' and self.informations[key] is None):
raise CreoleLoaderError(_('key already exists in information {}').format(key))
self.informations[key] = value
def set_leader(self):
self.is_leader = True
def get(self):
if self.option is None:
self.attrib['children'] = []
for child in self.children:
self.attrib['children'].append(child.get())
self.build_properties()
try:
if 'dynamic' in self.attrib:
dynamic = self.storage.get(self.attrib['dynamic']).get()
del self.attrib['dynamic']
self.attrib['suffixes'] = Calculation(self.eosfunc.calc_value,
Params((ParamOption(dynamic),)))
option = ConvertDynOptionDescription(**self.attrib)
elif not self.is_leader:
option = OptionDescription(**self.attrib)
else:
option = Leadership(**self.attrib)
except Exception as err:
print(self.attrib)
raise CreoleLoaderError(_('cannot create optiondescription "{}": {}').format(self.attrib['name'], err))
for key, value in self.informations.items():
option.impl_set_information(key, value)
self.option = option
return self.option
def load(xmlroot: str,
dtd_path: str,
funcs_path: str):
tiramisu_objects = PopulateTiramisuObjects()
tiramisu_objects.parse_dtd(dtd_path)
tiramisu_objects.make_tiramisu_objects(xmlroot,
funcs_path)
return tiramisu_objects.storage.paths['.'].get()

View File

@ -23,40 +23,36 @@ For example: a variable is redefined and shall be moved to another family
means that a variable1 = Variable() object in the object space who lives in the family1 parent means that a variable1 = Variable() object in the object space who lives in the family1 parent
has to be moved in family2. The visit procedure changes the varable1's object space's parent. has to be moved in family2. The visit procedure changes the varable1's object space's parent.
""" """
from collections import OrderedDict
from lxml.etree import Element, SubElement # pylint: disable=E0611 from lxml.etree import Element, SubElement # pylint: disable=E0611
from .i18n import _ from .i18n import _
from .xmlreflector import XMLReflector, HIGH_COMPATIBILITY from .xmlreflector import XMLReflector
from .annotator import ERASED_ATTRIBUTES, VARIABLE_NAMESPACE, ServiceAnnotator, SpaceAnnotator from .annotator import ERASED_ATTRIBUTES, SpaceAnnotator
from .tiramisureflector import TiramisuReflector
from .utils import normalize_family from .utils import normalize_family
from .error import CreoleOperationError, SpaceObjShallNotBeUpdated, CreoleDictConsistencyError from .error import OperationError, SpaceObjShallNotBeUpdated, DictConsistencyError
from .path import Path from .path import Path
from .config import variable_namespace
# CreoleObjSpace's elements like 'family' or 'follower', that shall be forced to the Redefinable type # CreoleObjSpace's elements like 'family' or 'follower', that shall be forced to the Redefinable type
FORCE_REDEFINABLES = ('family', 'follower', 'service', 'disknod', 'variables') FORCE_REDEFINABLES = ('family', 'follower', 'service', 'disknod', 'variables')
# CreoleObjSpace's elements that shall be forced to the UnRedefinable type # CreoleObjSpace's elements that shall be forced to the UnRedefinable type
FORCE_UNREDEFINABLES = ('value',) FORCE_UNREDEFINABLES = ('value',)
# CreoleObjSpace's elements that shall be set to the UnRedefinable type # CreoleObjSpace's elements that shall be set to the UnRedefinable type
UNREDEFINABLE = ('submulti', 'multi', 'type') UNREDEFINABLE = ('multi', 'type')
PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_freeze',
'force_store_value', 'disabled', 'mandatory')
CONVERT_PROPERTIES = {'auto_save': ['force_store_value'], 'auto_freeze': ['force_store_value', 'auto_freeze']} CONVERT_PROPERTIES = {'auto_save': ['force_store_value'], 'auto_freeze': ['force_store_value', 'auto_freeze']}
RENAME_ATTIBUTES = {'description': 'doc'} RENAME_ATTIBUTES = {'description': 'doc'}
INCOMPATIBLE_ATTRIBUTES = [['multi', 'submulti']]
FORCED_TEXT_ELTS_AS_NAME = ('choice', 'property', 'value', 'target') FORCED_TEXT_ELTS_AS_NAME = ('choice', 'property', 'value', 'target')
CONVERT_EXPORT = {'Leadership': 'leader', CONVERT_EXPORT = {'Leadership': 'leader',
'Separators': 'separators',
'Variable': 'variable', 'Variable': 'variable',
'Value': 'value', 'Value': 'value',
'Property': 'property', 'Property': 'property',
'Choice': 'choice', 'Choice': 'choice',
'Param': 'param', 'Param': 'param',
'Separator': 'separator',
'Check': 'check', 'Check': 'check',
} }
@ -69,20 +65,20 @@ class RootCreoleObject:
class CreoleObjSpace: class CreoleObjSpace:
"""DOM XML reflexion free internal representation of a Creole Dictionary """DOM XML reflexion free internal representation of a Creole Dictionary
""" """
choice = type('Choice', (RootCreoleObject,), OrderedDict()) choice = type('Choice', (RootCreoleObject,), dict())
property_ = type('Property', (RootCreoleObject,), OrderedDict()) property_ = type('Property', (RootCreoleObject,), dict())
# Creole ObjectSpace's Leadership variable class type # Creole ObjectSpace's Leadership variable class type
Leadership = type('Leadership', (RootCreoleObject,), OrderedDict()) Leadership = type('Leadership', (RootCreoleObject,), dict())
""" """
This Atom type stands for singleton, that is This Atom type stands for singleton, that is
an Object Space's atom object is present only once in the an Object Space's atom object is present only once in the
object space's tree object space's tree
""" """
Atom = type('Atom', (RootCreoleObject,), OrderedDict()) Atom = type('Atom', (RootCreoleObject,), dict())
"A variable that can't be redefined" "A variable that can't be redefined"
Redefinable = type('Redefinable', (RootCreoleObject,), OrderedDict()) Redefinable = type('Redefinable', (RootCreoleObject,), dict())
"A variable can be redefined" "A variable can be redefined"
UnRedefinable = type('UnRedefinable', (RootCreoleObject,), OrderedDict()) UnRedefinable = type('UnRedefinable', (RootCreoleObject,), dict())
def __init__(self, dtdfilename): # pylint: disable=R0912 def __init__(self, dtdfilename): # pylint: disable=R0912
@ -181,11 +177,11 @@ class CreoleObjSpace:
continue continue
if child.tag == 'family': if child.tag == 'family':
if child.attrib['name'] in family_names: if child.attrib['name'] in family_names:
raise CreoleDictConsistencyError(_('Family {} is set several times').format(child.attrib['name'])) raise DictConsistencyError(_('Family {} is set several times').format(child.attrib['name']))
family_names.append(child.attrib['name']) family_names.append(child.attrib['name'])
if child.tag == 'variables': if child.tag == 'variables':
child.attrib['name'] = namespace child.attrib['name'] = namespace
if HIGH_COMPATIBILITY and child.tag == 'value' and child.text == None: if child.tag == 'value' and child.text == None:
# FIXME should not be here # FIXME should not be here
continue continue
# variable objects creation # variable objects creation
@ -299,12 +295,12 @@ class CreoleObjSpace:
) )
elif exists is False: elif exists is False:
raise SpaceObjShallNotBeUpdated() raise SpaceObjShallNotBeUpdated()
raise CreoleDictConsistencyError(_(f'Already present in another XML file, {name} cannot be re-created')) raise DictConsistencyError(_(f'Already present in another XML file, {name} cannot be re-created'))
redefine = self.convert_boolean(subspace.get('redefine', False)) redefine = self.convert_boolean(subspace.get('redefine', False))
exists = self.convert_boolean(subspace.get('exists', False)) exists = self.convert_boolean(subspace.get('exists', False))
if redefine is False or exists is True: if redefine is False or exists is True:
return getattr(self, child.tag)() return getattr(self, child.tag)()
raise CreoleDictConsistencyError(_(f'Redefined object: {name} does not exist yet')) raise DictConsistencyError(_(f'Redefined object: {name} does not exist yet'))
def create_tree_structure(self, def create_tree_structure(self,
space, space,
@ -317,23 +313,23 @@ class CreoleObjSpace:
for example:: for example::
space = Family() space = Family()
space.variable = OrderedDict() space.variable = dict()
another example: another example:
space = Variable() space = Variable()
space.value = list() space.value = list()
""" """
if child.tag not in vars(space): if child.tag not in vars(space):
if isinstance(variableobj, self.Redefinable): if isinstance(variableobj, self.Redefinable):
setattr(space, child.tag, OrderedDict()) setattr(space, child.tag, dict())
elif isinstance(variableobj, self.UnRedefinable): elif isinstance(variableobj, self.UnRedefinable):
setattr(space, child.tag, []) setattr(space, child.tag, [])
elif not isinstance(variableobj, self.Atom): # pragma: no cover elif not isinstance(variableobj, self.Atom): # pragma: no cover
raise CreoleOperationError(_("Creole object {} " raise OperationError(_("Creole object {} "
"has a wrong type").format(type(variableobj))) "has a wrong type").format(type(variableobj)))
def is_already_exists(self, name, space, child, namespace): def is_already_exists(self, name, space, child, namespace):
if isinstance(space, self.family): # pylint: disable=E1101 if isinstance(space, self.family): # pylint: disable=E1101
if namespace != VARIABLE_NAMESPACE: if namespace != variable_namespace:
name = space.path + '.' + name name = space.path + '.' + name
return self.paths.path_is_defined(name) return self.paths.path_is_defined(name)
if child.tag == 'family': if child.tag == 'family':
@ -366,18 +362,18 @@ class CreoleObjSpace:
else: else:
norm_name = name norm_name = name
return getattr(family, variable.tag)[norm_name] return getattr(family, variable.tag)[norm_name]
if namespace == VARIABLE_NAMESPACE: if namespace == variable_namespace:
path = name path = name
else: else:
path = family.path + '.' + name path = family.path + '.' + name
old_family_name = self.paths.get_variable_family_name(path) old_family_name = self.paths.get_variable_family_name(path)
if normalize_family(family.name) == old_family_name: if normalize_family(family.name) == old_family_name:
return getattr(family, variable.tag)[name] return getattr(family, variable.tag)[name]
old_family = self.space.variables[VARIABLE_NAMESPACE].family[old_family_name] # pylint: disable=E1101 old_family = self.space.variables[variable_namespace].family[old_family_name] # pylint: disable=E1101
variable_obj = old_family.variable[name] variable_obj = old_family.variable[name]
del old_family.variable[name] del old_family.variable[name]
if 'variable' not in vars(family): if 'variable' not in vars(family):
family.variable = OrderedDict() family.variable = dict()
family.variable[name] = variable_obj family.variable[name] = variable_obj
self.paths.add_variable(namespace, self.paths.add_variable(namespace,
name, name,
@ -446,8 +442,6 @@ class CreoleObjSpace:
): ):
redefine = self.convert_boolean(child.attrib.get('redefine', False)) redefine = self.convert_boolean(child.attrib.get('redefine', False))
has_value = hasattr(variableobj, 'value') has_value = hasattr(variableobj, 'value')
if HIGH_COMPATIBILITY and has_value:
has_value = len(child) != 1 or child[0].text != None
if redefine is True and child.tag == 'variable' and has_value and len(child) != 0: if redefine is True and child.tag == 'variable' and has_value and len(child) != 0:
del variableobj.value del variableobj.value
for attr, val in child.attrib.items(): for attr, val in child.attrib.items():
@ -455,19 +449,12 @@ class CreoleObjSpace:
# UNREDEFINABLE concerns only 'variable' node so we can fix name # UNREDEFINABLE concerns only 'variable' node so we can fix name
# to child.attrib['name'] # to child.attrib['name']
name = child.attrib['name'] name = child.attrib['name']
raise CreoleDictConsistencyError(_(f'cannot redefine attribute {attr} for variable {name}')) raise DictConsistencyError(_(f'cannot redefine attribute {attr} for variable {name}'))
if attr in self.booleans_attributs: if attr in self.booleans_attributs:
val = self.convert_boolean(val) val = self.convert_boolean(val)
if not (attr == 'name' and getattr(variableobj, 'name', None) != None): if not (attr == 'name' and getattr(variableobj, 'name', None) != None):
setattr(variableobj, attr, val) setattr(variableobj, attr, val)
keys = list(vars(variableobj).keys()) keys = list(vars(variableobj).keys())
for incompatible in INCOMPATIBLE_ATTRIBUTES:
found = False
for inc in incompatible:
if inc in keys:
if found:
raise CreoleDictConsistencyError(_('those attributes are incompatible {}').format(incompatible))
found = True
def variableobj_tree_visitor(self, def variableobj_tree_visitor(self,
child, child,
@ -514,7 +501,7 @@ class CreoleObjSpace:
document.attrib.get('dynamic') != None, document.attrib.get('dynamic') != None,
variableobj) variableobj)
if child.attrib.get('redefine', 'False') == 'True': if child.attrib.get('redefine', 'False') == 'True':
if namespace == VARIABLE_NAMESPACE: if namespace == variable_namespace:
self.redefine_variables.append(child.attrib['name']) self.redefine_variables.append(child.attrib['name'])
else: else:
self.redefine_variables.append(namespace + '.' + family_name + '.' + self.redefine_variables.append(namespace + '.' + family_name + '.' +
@ -522,7 +509,7 @@ class CreoleObjSpace:
elif child.tag == 'family': elif child.tag == 'family':
family_name = normalize_family(child.attrib['name']) family_name = normalize_family(child.attrib['name'])
if namespace != VARIABLE_NAMESPACE: if namespace != variable_namespace:
family_name = namespace + '.' + family_name family_name = namespace + '.' + family_name
self.paths.add_family(namespace, self.paths.add_family(namespace,
family_name, family_name,
@ -531,82 +518,12 @@ class CreoleObjSpace:
variableobj.path = self.paths.get_family_path(family_name, namespace) variableobj.path = self.paths.get_family_path(family_name, namespace)
def space_visitor(self, eosfunc_file): # pylint: disable=C0111 def space_visitor(self, eosfunc_file): # pylint: disable=C0111
ServiceAnnotator(self) self.funcs_path = eosfunc_file
SpaceAnnotator(self, eosfunc_file) SpaceAnnotator(self, eosfunc_file)
def save(self, filename, force_no_save=False): def save(self,
"""Save an XML output on disk ):
tiramisu_objects = TiramisuReflector(self.space,
:param filename: the full XML filename self.funcs_path,
""" )
xml = Element('rougail') return tiramisu_objects.get_text() + '\n'
self._xml_export(xml, self.space)
if not force_no_save:
self.xmlreflector.save_xmlfile(filename, xml)
return xml
def get_attributes(self, space): # pylint: disable=R0201
for attr in dir(space):
if not attr.startswith('_'):
yield attr
def _sub_xml_export(self, name, node, node_name, space, current_space):
if isinstance(space, dict):
space = list(space.values())
if isinstance(space, list):
for subspace in space:
if name == 'value' and (not hasattr(subspace, 'name') or subspace.name is None):
raise Exception('pfff')
continue
_name = CONVERT_EXPORT.get(subspace.__class__.__name__, 'family')
child_node = SubElement(node, _name)
self._xml_export(child_node, subspace, _name)
elif isinstance(space, (self.Atom, (self.Redefinable, self.UnRedefinable))):
_name = CONVERT_EXPORT.get(space.__class__.__name__, 'family')
child_node = SubElement(node, _name)
if _name != name:
child_node.attrib['name'] = name
if 'doc' not in child_node.attrib.keys():
child_node.attrib['doc'] = name
for subname in self.get_attributes(space):
subspace = getattr(space, subname)
self._sub_xml_export(subname, child_node, name, subspace, space)
elif name not in ERASED_ATTRIBUTES:
# # FIXME plutot dans annotator ...
if node.tag in ['variable', 'family', 'leader']:
if name in PROPERTIES:
if space is True:
for prop in CONVERT_PROPERTIES.get(name, [name]):
SubElement(node, 'property').text = prop
return
if name == 'mode' and space:
SubElement(node, 'property').text = space
return
# Not param for calculation ...
if name == 'name' and node_name in FORCED_TEXT_ELTS_AS_NAME and not hasattr(current_space, 'param'):
node.text = str(space)
elif name == 'text' and node_name in self.forced_text_elts:
node.text = space
elif node.tag == 'family' and name == 'name':
if 'doc' not in node.attrib.keys():
node.attrib['doc'] = space
node.attrib['name'] = normalize_family(space, check_name=False)
else:
if name in RENAME_ATTIBUTES:
name = RENAME_ATTIBUTES[name]
if space is not None:
node.attrib[name] = str(space)
def _xml_export(self,
node,
space,
node_name=VARIABLE_NAMESPACE,
):
for name in self.get_attributes(space):
subspace = getattr(space, name)
self._sub_xml_export(name,
node,
node_name,
subspace,
space,
)

View File

@ -1,7 +1,7 @@
from .i18n import _ from .i18n import _
from .utils import normalize_family from .utils import normalize_family
from .error import CreoleOperationError, CreoleDictConsistencyError from .error import OperationError, DictConsistencyError
from .annotator import VARIABLE_NAMESPACE from .config import variable_namespace
class Path: class Path:
@ -13,6 +13,7 @@ class Path:
def __init__(self): def __init__(self):
self.variables = {} self.variables = {}
self.families = {} self.families = {}
self.full_paths = {}
# Family # Family
def add_family(self, def add_family(self,
@ -20,38 +21,54 @@ class Path:
name: str, name: str,
variableobj: str, variableobj: str,
) -> str: # pylint: disable=C0111 ) -> str: # pylint: disable=C0111
self.families[name] = dict(name=name, if '.' not in name and namespace == variable_namespace:
namespace=namespace, full_name = '.'.join([namespace, name])
variableobj=variableobj, self.full_paths[name] = full_name
) else:
full_name = name
if full_name in self.families and self.families[full_name]['variableobj'] != variableobj:
raise DictConsistencyError(_(f'Duplicate family name {name}'))
self.families[full_name] = dict(name=name,
namespace=namespace,
variableobj=variableobj,
)
def get_family_path(self, def get_family_path(self,
name: str, name: str,
current_namespace: str, current_namespace: str,
) -> str: # pylint: disable=C0111 ) -> str: # pylint: disable=C0111
name = normalize_family(name,
check_name=False,
allow_dot=True,
)
if '.' not in name and current_namespace == variable_namespace and name in self.full_paths:
name = self.full_paths[name]
if current_namespace is None: # pragma: no cover if current_namespace is None: # pragma: no cover
raise CreoleOperationError('current_namespace must not be None') raise OperationError('current_namespace must not be None')
dico = self.families[normalize_family(name, dico = self.families[name]
check_name=False, if dico['namespace'] != variable_namespace and current_namespace != dico['namespace']:
allow_dot=True, raise DictConsistencyError(_('A family located in the {} namespace '
)]
if dico['namespace'] != VARIABLE_NAMESPACE and current_namespace != dico['namespace']:
raise CreoleDictConsistencyError(_('A family located in the {} namespace '
'shall not be used in the {} namespace').format( 'shall not be used in the {} namespace').format(
dico['namespace'], current_namespace)) dico['namespace'], current_namespace))
path = dico['name'] return dico['name']
if dico['namespace'] is not None and '.' not in dico['name']:
path = '.'.join([dico['namespace'], path])
return path
def get_family_obj(self, def get_family_obj(self,
name: str, name: str,
) -> 'Family': # pylint: disable=C0111 ) -> 'Family': # pylint: disable=C0111
if '.' not in name and name in self.full_paths:
name = self.full_paths[name]
if name not in self.families: if name not in self.families:
raise CreoleDictConsistencyError(_('unknown family {}').format(name)) raise DictConsistencyError(_('unknown family {}').format(name))
dico = self.families[name] dico = self.families[name]
return dico['variableobj'] return dico['variableobj']
def family_is_defined(self,
name: str,
) -> str: # pylint: disable=C0111
if '.' not in name and name not in self.families and name in self.full_paths:
return True
return name in self.families
# Leadership # Leadership
def set_leader(self, def set_leader(self,
namespace: str, namespace: str,
@ -59,23 +76,25 @@ class Path:
name: str, name: str,
leader_name: str, leader_name: str,
) -> None: # pylint: disable=C0111 ) -> None: # pylint: disable=C0111
if namespace != VARIABLE_NAMESPACE: # need rebuild path and move object in new path
# need rebuild path and move object in new path old_path = namespace + '.' + leader_family_name + '.' + name
old_path = namespace + '.' + leader_family_name + '.' + name dico = self._get_variable(old_path)
dico = self._get_variable(old_path) del self.variables[old_path]
del self.variables[old_path] new_path = namespace + '.' + leader_family_name + '.' + leader_name + '.' + name
new_path = namespace + '.' + leader_family_name + '.' + leader_name + '.' + name self.add_variable(namespace,
self.add_variable(namespace, new_path,
new_path, dico['family'],
dico['family'], False,
False, dico['variableobj'],
dico['variableobj'], )
) if namespace == variable_namespace:
self.full_paths[name] = new_path
else:
name = new_path name = new_path
dico = self._get_variable(name) dico = self._get_variable(name)
if dico['leader'] != None: if dico['leader'] != None:
raise CreoleDictConsistencyError(_('Already defined leader {} for variable' raise DictConsistencyError(_('Already defined leader {} for variable'
' {}'.format(dico['leader'], name))) ' {}'.format(dico['leader'], name)))
dico['leader'] = leader_name dico['leader'] = leader_name
def get_leader(self, name): # pylint: disable=C0111 def get_leader(self, name): # pylint: disable=C0111
@ -89,16 +108,19 @@ class Path:
is_dynamic: bool, is_dynamic: bool,
variableobj, variableobj,
) -> str: # pylint: disable=C0111 ) -> str: # pylint: disable=C0111
if namespace == VARIABLE_NAMESPACE or '.' in name: if '.' not in name:
varname = name full_name = '.'.join([namespace, family, name])
self.full_paths[name] = full_name
else: else:
varname = '.'.join([namespace, family, name]) full_name = name
self.variables[varname] = dict(name=name, if namespace == variable_namespace:
family=family, name = name.rsplit('.', 1)[1]
namespace=namespace, self.variables[full_name] = dict(name=name,
leader=None, family=family,
is_dynamic=is_dynamic, namespace=namespace,
variableobj=variableobj) leader=None,
is_dynamic=is_dynamic,
variableobj=variableobj)
def get_variable_name(self, def get_variable_name(self,
name, name,
@ -127,7 +149,7 @@ class Path:
with_suffix: bool=False, with_suffix: bool=False,
) -> str: # pylint: disable=C0111 ) -> str: # pylint: disable=C0111
if current_namespace is None: # pragma: no cover if current_namespace is None: # pragma: no cover
raise CreoleOperationError('current_namespace must not be None') raise OperationError('current_namespace must not be None')
if with_suffix: if with_suffix:
dico, suffix = self._get_variable(name, dico, suffix = self._get_variable(name,
with_suffix=True, with_suffix=True,
@ -135,8 +157,8 @@ class Path:
else: else:
dico = self._get_variable(name) dico = self._get_variable(name)
if not allow_source: if not allow_source:
if dico['namespace'] not in [VARIABLE_NAMESPACE, 'services'] and current_namespace != dico['namespace']: if dico['namespace'] not in [variable_namespace, 'services'] and current_namespace != dico['namespace']:
raise CreoleDictConsistencyError(_('A variable located in the {} namespace ' raise DictConsistencyError(_('A variable located in the {} namespace '
'shall not be used in the {} namespace').format( 'shall not be used in the {} namespace').format(
dico['namespace'], current_namespace)) dico['namespace'], current_namespace))
if '.' in dico['name']: if '.' in dico['name']:
@ -154,6 +176,8 @@ class Path:
def path_is_defined(self, def path_is_defined(self,
name: str, name: str,
) -> str: # pylint: disable=C0111 ) -> str: # pylint: disable=C0111
if '.' not in name and name not in self.variables and name in self.full_paths:
return True
return name in self.variables return name in self.variables
def _get_variable(self, def _get_variable(self,
@ -161,14 +185,24 @@ class Path:
with_suffix: bool=False, with_suffix: bool=False,
) -> str: ) -> str:
if name not in self.variables: if name not in self.variables:
if name.startswith(f'{VARIABLE_NAMESPACE}.'): if name not in self.variables:
name = name.split('.')[-1] if '.' not in name and name in self.full_paths:
name = self.full_paths[name]
if name not in self.variables: if name not in self.variables:
for var_name, variable in self.variables.items(): for var_name, variable in self.variables.items():
if variable['is_dynamic'] and name.startswith(var_name): if variable['is_dynamic'] and name.startswith(var_name):
if not with_suffix:
raise Exception('This option is dynamic, should use "with_suffix" attribute')
return variable, name[len(var_name):] return variable, name[len(var_name):]
raise CreoleDictConsistencyError(_('unknown option {}').format(name)) if '.' not in name:
for var_name, path in self.full_paths.items():
if name.startswith(var_name):
variable = self.variables[self.full_paths[var_name]]
if variable['is_dynamic']:
if not with_suffix:
raise Exception('This option is dynamic, should use "with_suffix" attribute')
return variable, name[len(var_name):]
raise DictConsistencyError(_('unknown option {}').format(name))
if with_suffix: if with_suffix:
return self.variables[name], None return self.variables[name], None
return self.variables[name] return self.variables[name]

View File

@ -18,8 +18,7 @@ from Cheetah.NameMapper import NotFound as CheetahNotFound
from tiramisu import Config from tiramisu import Config
from tiramisu.error import PropertiesOptionError from tiramisu.error import PropertiesOptionError
from .annotator import VARIABLE_NAMESPACE from .config import patch_dir, variable_namespace
from .config import patch_dir
from .error import FileNotFound, TemplateError from .error import FileNotFound, TemplateError
from .i18n import _ from .i18n import _
from .utils import normalize_family from .utils import normalize_family
@ -373,7 +372,7 @@ class CreoleTemplateEngine:
""" """
for option in await self.config.option.list(type='all'): for option in await self.config.option.list(type='all'):
namespace = await option.option.name() namespace = await option.option.name()
if namespace == VARIABLE_NAMESPACE: if namespace == variable_namespace:
await self.load_eole_variables_rougail(option) await self.load_eole_variables_rougail(option)
else: else:
families = await self.load_eole_variables(namespace, families = await self.load_eole_variables(namespace,

10
src/rougail/tiramisu.py Normal file
View File

@ -0,0 +1,10 @@
from tiramisu import DynOptionDescription
from .utils import normalize_family
class ConvertDynOptionDescription(DynOptionDescription):
def convert_suffix_to_path(self, suffix):
if not isinstance(suffix, str):
suffix = str(suffix)
return normalize_family(suffix,
check_name=False)

View File

@ -0,0 +1,514 @@
"""loader
flattened XML specific
"""
from os.path import isfile
from lxml.etree import DTD
from .config import dtdfilename, variable_namespace
from .i18n import _
from .error import LoaderError
from .annotator import ERASED_ATTRIBUTES
FUNC_TO_DICT = ['valid_not_equal']
FORCE_INFORMATIONS = ['help', 'test', 'separator', 'manage']
ATTRIBUTES_ORDER = ('name', 'doc', 'default', 'multi')
CONVERT_OPTION = {'number': dict(opttype="IntOption", func=int),
'choice': dict(opttype="ChoiceOption"),
'string': dict(opttype="StrOption"),
'password': dict(opttype="PasswordOption"),
'mail': dict(opttype="EmailOption"),
'boolean': dict(opttype="BoolOption"),
'symlink': dict(opttype="SymLinkOption"),
'filename': dict(opttype="FilenameOption"),
'date': dict(opttype="DateOption"),
'unix_user': dict(opttype="UsernameOption"),
'ip': dict(opttype="IPOption", initkwargs={'allow_reserved': True}),
'local_ip': dict(opttype="IPOption", initkwargs={'private_only': True, 'warnings_only': True}),
'netmask': dict(opttype="NetmaskOption"),
'network': dict(opttype="NetworkOption"),
'broadcast': dict(opttype="BroadcastOption"),
'netbios': dict(opttype="DomainnameOption", initkwargs={'type': 'netbios', 'warnings_only': True}),
'domain': dict(opttype="DomainnameOption", initkwargs={'type': 'domainname', 'allow_ip': False}),
'hostname': dict(opttype="DomainnameOption", initkwargs={'type': 'hostname', 'allow_ip': False}),
'web_address': dict(opttype="URLOption", initkwargs={'allow_ip': True, 'allow_without_dot': True}),
'port': dict(opttype="PortOption", initkwargs={'allow_private': True}),
'mac': dict(opttype="MACOption"),
'cidr': dict(opttype="IPOption", initkwargs={'cidr': True}),
'network_cidr': dict(opttype="NetworkOption", initkwargs={'cidr': True}),
}
class TiramisuReflector:
def __init__(self,
xmlroot,
funcs_path,
):
self.storage = ElementStorage()
self.storage.text = ["from tiramisu import *",
"from rougail.tiramisu import ConvertDynOptionDescription",
"import imp",
f"func = imp.load_source('func', '{funcs_path}')",
]
self.make_tiramisu_objects(xmlroot)
# parse object
self.storage.get('.').get()
def make_tiramisu_objects(self,
xmlroot,
):
family = self.get_root_family()
for xmlelt in self.reorder_family(xmlroot):
self.iter_family(xmlelt,
family,
None,
)
def get_root_family(self):
family = Family(BaseElt('baseoption',
'baseoption',
),
self.storage,
False,
'.',
)
return family
def reorder_family(self, xmlroot):
# variable_namespace family has to be loaded before any other family
# because `extra` family could use `variable_namespace` variables.
if hasattr(xmlroot, 'variables'):
if variable_namespace in xmlroot.variables:
yield xmlroot.variables[variable_namespace]
for xmlelt, value in xmlroot.variables.items():
if xmlelt != variable_namespace:
yield value
if hasattr(xmlroot, 'services'):
yield xmlroot.services
def get_attributes(self, space): # pylint: disable=R0201
for attr in dir(space):
if not attr.startswith('_') and attr not in ERASED_ATTRIBUTES:
yield attr
def get_children(self,
space,
):
for tag in self.get_attributes(space):
children = getattr(space, tag)
if children.__class__.__name__ == 'Family':
children = [children]
if isinstance(children, dict):
children = list(children.values())
if isinstance(children, list):
yield tag, children
def iter_family(self,
child,
family,
subpath,
):
tag = child.__class__.__name__
if tag == 'Variable':
function = self.populate_variable
elif tag == 'Property':
# property already imported with family
return
else:
function = self.populate_family
#else:
# raise Exception('unknown tag {}'.format(child.tag))
function(family,
child,
subpath,
)
def populate_family(self,
parent_family,
elt,
subpath,
):
path = self.build_path(subpath,
elt,
)
tag = elt.__class__.__name__
family = Family(elt,
self.storage,
tag == 'Leadership',
path,
)
parent_family.add(family)
for tag, children in self.get_children(elt):
for child in children:
self.iter_family(child,
family,
path,
)
def populate_variable(self,
family,
elt,
subpath,
):
is_follower = False
is_leader = False
if family.is_leader:
if elt.name != family.elt.name:
is_follower = True
else:
is_leader = True
family.add(Variable(elt,
self.storage,
is_follower,
is_leader,
self.build_path(subpath,
elt,
)
))
def build_path(self,
subpath,
elt,
):
if subpath is None:
return elt.name
return subpath + '.' + elt.name
def get_text(self):
return '\n'.join(self.storage.get('.').get_text())
class BaseElt:
def __init__(self,
name,
doc,
):
self.name = name
self.doc = doc
def __iter__(self):
return iter([])
class ElementStorage:
def __init__(self,
):
self.paths = {}
self.text = []
self.index = 0
def add(self, path, elt):
self.paths[path] = (elt, self.index)
self.index += 1
def get(self, path):
if path not in self.paths or self.paths[path][0] is None:
raise LoaderError(_('there is no element for path {}').format(path))
return self.paths[path][0]
def get_name(self, path):
if path not in self.paths:
raise LoaderError(_('there is no element for index {}').format(path))
return f'option_{self.paths[path][1]}'
class Common:
def __init__(self,
elt,
storage,
is_leader,
path,
):
self.option_name = None
self.path = path
self.attrib = {}
self.informations = {}
self.storage = storage
self.is_leader = is_leader
self.storage.add(self.path, self)
def populate_properties(self, child):
if child.type == 'calculation':
action = f"ParamValue('{child.name}')"
option_name = self.storage.get(child.source).get()
kwargs = f"'condition': ParamOption({option_name}, todict=True), 'expected': ParamValue('{child.expected}')"
if child.inverse:
kwargs += ", 'reverse_condition': ParamValue(True)"
prop = 'Calculation(calc_value, Params(' + action + ', kwargs={' + kwargs + '}))'
else:
prop = "'" + child.text + "'"
if self.attrib['properties']:
self.attrib['properties'] += ', '
self.attrib['properties'] += prop
def get_attrib(self, attrib):
ret_list = []
for key, value in self.attrib.items():
if value is None:
continue
if key == 'properties':
if not self.attrib[key]:
continue
value = "frozenset({" + self.attrib[key] + "})"
elif key in ['default', 'multi', 'suffixes', 'validators', 'values']:
value = self.attrib[key]
elif isinstance(value, str) and key != 'opt' and not value.startswith('['):
value = "'" + value.replace("'", "\\\'") + "'"
ret_list.append(f'{key}={value}')
return ', '.join(ret_list)
def populate_informations(self):
for key, value in self.informations.items():
if isinstance(value, str):
value = '"' + value.replace('"', '\"') + '"'
self.storage.text.append(f'{self.option_name}.impl_set_information("{key}", {value})')
def get_text(self,
):
return self.storage.text
def get_attributes(self, space): # pylint: disable=R0201
attributes = dir(space)
for attr in ATTRIBUTES_ORDER:
if attr in attributes:
yield attr
for attr in dir(space):
if attr not in ATTRIBUTES_ORDER:
if not attr.startswith('_') and attr not in ERASED_ATTRIBUTES:
value = getattr(space, attr)
if not isinstance(value, (list, dict)) and not value.__class__.__name__ == 'Family':
yield attr
def get_children(self,
space,
):
for attr in dir(space):
if not attr.startswith('_') and attr not in ERASED_ATTRIBUTES:
value = getattr(space, attr)
if isinstance(value, (list, dict)):
children = getattr(space, attr)
if children.__class__.__name__ == 'Family':
children = [children]
if isinstance(children, dict):
children = list(children.values())
if children and isinstance(children, list):
yield attr, children
class Variable(Common):
def __init__(self,
elt,
storage,
is_follower,
is_leader,
path,
):
super().__init__(elt,
storage,
is_leader,
path,
)
self.is_follower = is_follower
convert_option = CONVERT_OPTION[elt.type]
del elt.type
self.object_type = convert_option['opttype']
self.attrib.update(convert_option.get('initkwargs', {}))
if self.object_type != 'SymLinkOption':
self.attrib['properties'] = []
self.attrib['validators'] = []
self.elt = elt
def get(self):
if self.option_name is None:
self.populate_attrib()
if self.object_type == 'SymLinkOption':
self.attrib['opt'] = self.storage.get(self.attrib['opt']).get()
else:
self.parse_children()
attrib = self.get_attrib(self.attrib)
self.option_name = self.storage.get_name(self.path)
self.storage.text.append(f'{self.option_name} = {self.object_type}({attrib})')
self.populate_informations()
return self.option_name
def populate_attrib(self):
for key in self.get_attributes(self.elt):
value = getattr(self.elt, key)
if key in FORCE_INFORMATIONS:
if key == 'test':
value = value.split(',')
if self.object_type == 'IntOption':
value = [int(v) for v in value]
self.informations[key] = value
else:
self.attrib[key] = value
def parse_children(self):
if not 'default' in self.attrib or self.attrib['multi']:
self.attrib['default'] = []
if self.attrib['multi'] == 'submulti' and self.is_follower:
self.attrib['default_multi'] = []
choices = []
if 'properties' in self.attrib:
if self.attrib['properties']:
self.attrib['properties'] = "'" + "', '".join(sorted(list(self.attrib['properties']))) + "'"
else:
self.attrib['properties'] = ''
for tag, children in self.get_children(self.elt):
for child in children:
if tag == 'property':
self.populate_properties(child)
elif tag == 'value':
if child.type == 'calculation':
self.attrib['default'] = self.calculation_value(child, [])
else:
self.populate_value(child)
elif tag == 'check':
self.attrib['validators'].append(self.calculation_value(child, ['ParamSelfOption()']))
elif tag == 'choice':
if child.type == 'calculation':
value = self.storage.get(child.name).get()
choices = f"Calculation(func.calc_value, Params((ParamOption({value}))))"
else:
choices.append(child.name)
if choices:
if isinstance(choices, list):
self.attrib['values'] = str(tuple(choices))
else:
self.attrib['values'] = choices
if self.attrib['default'] == []:
del self.attrib['default']
elif not self.attrib['multi'] and isinstance(self.attrib['default'], list):
self.attrib['default'] = self.attrib['default'][-1]
if self.attrib['validators'] == []:
del self.attrib['validators']
else:
self.attrib['validators'] = '[' + ', '.join(self.attrib['validators']) + ']'
def calculation_value(self, child, args):
kwargs = []
if hasattr(child, 'name'):
# has parameters
function = child.name
if hasattr(child, 'param'):
for param in child.param:
value = self.populate_param(param)
if not hasattr(param, 'name'):
args.append(str(value))
else:
kwargs.append(f"'{param.name}': " + value)
else:
# function without any parameter
function = child.text.strip()
ret = f"Calculation(func.{function}, Params((" + ', '.join(args) + "), kwargs=" + "{" + ', '.join(kwargs) + "})"
if hasattr(child, 'warnings_only'):
ret += f', warnings_only={child.warnings_only}'
return ret + ')'
def populate_param(self,
param,
):
if param.type == 'string':
return f'ParamValue("{param.text}")'
elif param.type == 'number':
return f'ParamValue({param.text})'
elif param.type == 'variable':
value = {'option': param.text,
'notraisepropertyerror': param.notraisepropertyerror,
'todict': param.text in FUNC_TO_DICT,
}
if hasattr(param, 'suffix'):
value['suffix'] = param.suffix
return self.build_param(value)
elif param.type == 'information':
return f'ParamInformation("{param.text}", None)'
elif param.type == 'suffix':
return f'ParamSuffix()'
raise LoaderError(_('unknown param type {}').format(param.type))
def populate_value(self,
child,
):
value = child.name
if self.attrib['multi'] == 'submulti':
self.attrib['default_multi'].append(value)
elif self.is_follower:
self.attrib['default_multi'] = value
elif self.attrib['multi']:
self.attrib['default'].append(value)
if not self.is_leader:
self.attrib['default_multi'] = value
elif isinstance(value, int):
self.attrib['default'].append(value)
else:
self.attrib['default'].append("'" + value + "'")
def build_param(self,
param,
):
option_name = self.storage.get(param['option']).get()
if 'suffix' in param:
family = '.'.join(param['option'].split('.')[:-1])
family_option = self.storage.get_name(family)
return f"ParamDynOption({option_name}, '{param['suffix']}', {family_option}, notraisepropertyerror={param['notraisepropertyerror']}, todict={param['todict']})"
return f"ParamOption({option_name}, notraisepropertyerror={param['notraisepropertyerror']}, todict={param['todict']})"
class Family(Common):
def __init__(self,
elt,
storage,
is_leader,
path,
):
super().__init__(elt,
storage,
is_leader,
path,
)
self.children = []
self.elt = elt
def add(self, child):
self.children.append(child)
def get(self):
if not self.option_name:
self.populate_attrib()
self.parse_children()
self.option_name = self.storage.get_name(self.path)
object_name = self.get_object_name()
attrib = self.get_attrib(self.attrib) + ', children=[' + ', '.join([child.get() for child in self.children]) + ']'
self.storage.text.append(f'{self.option_name} = {object_name}({attrib})')
self.populate_informations()
return self.option_name
def populate_attrib(self):
for key in self.get_attributes(self.elt):
value = getattr(self.elt, key)
if key in FORCE_INFORMATIONS:
self.informations[key] = value
elif key == 'dynamic':
dynamic = self.storage.get(value).get()
self.attrib['suffixes'] = f"Calculation(func.calc_value, Params((ParamOption({dynamic}))))"
else:
self.attrib[key] = value
def parse_children(self):
if 'properties' in self.attrib:
self.attrib['properties'] = "'" + "', '".join(sorted(list(self.attrib['properties']))) + "'"
if hasattr(self.elt, 'property'):
#self.attrib['properties'] = ''
for child in self.elt.property:
self.populate_properties(child)
if not self.attrib['properties']:
del self.attrib['properties']
def get_object_name(self):
if 'suffixes' in self.attrib:
return 'ConvertDynOptionDescription'
elif not self.is_leader:
return 'OptionDescription'
return 'Leadership'

View File

@ -6,9 +6,8 @@ from os import listdir
from lxml.etree import DTD, parse, tostring # , XMLParser from lxml.etree import DTD, parse, tostring # , XMLParser
from .i18n import _ from .i18n import _
from .error import CreoleDictConsistencyError from .error import DictConsistencyError
HIGH_COMPATIBILITY = True
class XMLReflector(object): class XMLReflector(object):
"""Helper class for loading the Creole XML file, """Helper class for loading the Creole XML file,
@ -38,7 +37,7 @@ class XMLReflector(object):
# document = parse(BytesIO(xmlfile), XMLParser(remove_blank_text=True)) # document = parse(BytesIO(xmlfile), XMLParser(remove_blank_text=True))
document = parse(xmlfile) document = parse(xmlfile)
if not self.dtd.validate(document): if not self.dtd.validate(document):
raise CreoleDictConsistencyError(_("not a valid xml file: {}").format(xmlfile)) raise DictConsistencyError(_("not a valid xml file: {}").format(xmlfile))
return document.getroot() return document.getroot()
def load_xml_from_folders(self, xmlfolders): def load_xml_from_folders(self, xmlfolders):

0
tests/__init__.py Normal file
View File

View File

@ -1,7 +1,8 @@
from tiramisu import valid_not_equal, valid_ip_netmask, calc_value from tiramisu import valid_not_equal, valid_ip_netmask, calc_value
def calc_val(*args, **kwargs): def calc_val(*args, **kwargs):
pass if len(args) > 0:
return args[0]
def concat(*args, **kwargs): def concat(*args, **kwargs):
@ -24,10 +25,6 @@ def get_mount_point_device(*args, **kwargs):
pass pass
def valid_entier(*args, **kwargs):
pass
def valid_differ(*args, **kwargs): def valid_differ(*args, **kwargs):
pass pass
@ -62,4 +59,4 @@ def device_type(*args, **kwargs):
def calc_list(*args, **kwargs): def calc_list(*args, **kwargs):
return [] return list(args)

View File

@ -1,7 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family name="services" doc="services">
<property>hidden</property>
<family doc="tata" name="tata"/>
</family>
</rougail>

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_2 = OptionDescription(name='tata', doc='tata', children=[])
option_2.impl_set_information("manage", True)
option_1 = OptionDescription(name='services', doc='services', properties=frozenset({'hidden'}), children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -1,26 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>basic</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<property>force_store_value</property>
<property>auto_freeze</property>
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>basic</property>
<property expected="oui" inverse="True" source="rougail.general.module_instancie" type="calculation">auto_frozen</property>
<value type="string">non</value>
</variable>
<variable doc="No change" multi="False" name="module_instancie" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='module_instancie', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'auto_freeze', 'basic', 'force_store_value', 'mandatory', Calculation(calc_value, Params(ParamValue('auto_frozen'), kwargs={'condition': ParamOption(option_4, todict=True), 'expected': ParamValue('oui'), 'reverse_condition': ParamValue(True)}))}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', properties=frozenset({'basic'}), 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

@ -1,26 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<property>force_store_value</property>
<property>auto_freeze</property>
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>expert</property>
<property expected="oui" inverse="True" source="rougail.general.module_instancie" type="calculation">auto_frozen</property>
<value type="string">non</value>
</variable>
<variable doc="No change" multi="False" name="module_instancie" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='module_instancie', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'auto_freeze', 'expert', 'force_store_value', 'mandatory', Calculation(calc_value, Params(ParamValue('auto_frozen'), kwargs={'condition': ParamOption(option_4, todict=True), 'expected': ParamValue('oui'), 'reverse_condition': ParamValue(True)}))}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', 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

@ -1,17 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>basic</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<property>force_store_value</property>
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>basic</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'basic', 'force_store_value', 'mandatory'}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', properties=frozenset({'basic'}), children=[option_3])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -1,17 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>expert</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<property>force_store_value</property>
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>expert</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'expert', 'force_store_value', 'mandatory'}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', properties=frozenset({'expert'}), children=[option_3])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -1,19 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', properties=frozenset({'normal'}), children=[option_3])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -1,24 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
<variable doc="without_type" multi="False" name="without_type" type="string">
<property>mandatory</property>
<property>normal</property>
<value>non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_4 = StrOption(properties=frozenset({'mandatory', 'normal'}), name='without_type', doc='without_type', multi=False, default='non')
option_2 = OptionDescription(name='general', doc='général', 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

@ -1,19 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', properties=frozenset({'normal'}), children=[option_3])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -1,29 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="général" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_4 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='général', 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

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": null, "rougail.general.mode_conteneur_actif1": "non"} {"rougail.general.mode_conteneur_actif": "non", "rougail.general.mode_conteneur_actif1": "non"}

View File

@ -1,28 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value name="calc_val" type="calculation">
<param transitive="False" type="variable">rougail.general.mode_conteneur_actif1</param>
</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamOption(option_4, notraisepropertyerror=False, todict=False)), kwargs={})), values=('oui', 'non'))
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

@ -1,26 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="calculation">calc_val</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((), kwargs={})), values=('oui', 'non'))
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
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

@ -1,19 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>normal</property>
<variable doc="Redefine description" multi="True" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='Redefine description', multi=True, default=['non'], default_multi='non', values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'normal'}), children=[option_3])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

View File

@ -1,22 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<services/>
<variables>
<family name="general">
<variable name="mode_conteneur_actif" type="oui/non" description="Redefine description" hidden="True" submulti="True">
<value>non</value>
</variable>
</family>
<separators/>
</variables>
<constraints>
</constraints>
<help/>
</rougail>
<!-- vim: ts=4 sw=4 expandtab
-->

View File

@ -1 +0,0 @@
{"rougail.general.mode_conteneur_actif": [["non"]]}

View File

@ -1,19 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>normal</property>
<variable doc="Redefine description" multi="submulti" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": null, "rougail.general.mode_conteneur_actif1": "non", "rougail.general.module_instancie": "non"} {"rougail.general.mode_conteneur_actif": "non", "rougail.general.mode_conteneur_actif1": "non", "rougail.general.module_instancie": "non"}

View File

@ -1,35 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>basic</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<property>force_store_value</property>
<property>auto_freeze</property>
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>basic</property>
<property expected="oui" inverse="True" source="rougail.general.module_instancie" type="calculation">auto_frozen</property>
<value name="calc_val" type="calculation">
<param transitive="False" type="variable">rougail.general.mode_conteneur_actif1</param>
</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
<variable doc="No change" multi="False" name="module_instancie" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,10 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_5 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='module_instancie', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'auto_freeze', 'basic', 'force_store_value', 'mandatory', Calculation(calc_value, Params(ParamValue('auto_frozen'), kwargs={'condition': ParamOption(option_5, todict=True), 'expected': ParamValue('oui'), 'reverse_condition': ParamValue(True)}))}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamOption(option_4, notraisepropertyerror=False, todict=False)), kwargs={})), values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'basic'}), 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])

View File

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": null, "rougail.general.mode_conteneur_actif1": "non"} {"rougail.general.mode_conteneur_actif": "non", "rougail.general.mode_conteneur_actif1": "non"}

View File

@ -1,26 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>basic</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<property>force_store_value</property>
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>basic</property>
<value name="calc_val" type="calculation">
<param transitive="False" type="variable">rougail.general.mode_conteneur_actif1</param>
</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'basic', 'force_store_value', 'mandatory'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamOption(option_4, notraisepropertyerror=False, todict=False)), kwargs={})), values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'basic'}), 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

@ -5,9 +5,7 @@
<variables> <variables>
<family name="general"> <family name="general">
<variable name="mode_conteneur_actif" type="oui/non" description="No change" hidden="True"> <variable name="mode_conteneur_actif" type="oui/non" description="No change"/>
<value>non</value>
</variable>
<variable name="mode_conteneur_actif1" type="oui/non" description="No change"> <variable name="mode_conteneur_actif1" type="oui/non" description="No change">
<value>non</value> <value>non</value>
</variable> </variable>

View File

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": null, "rougail.general.mode_conteneur_actif1": "non"} {"rougail.general.mode_conteneur_actif": "non", "rougail.general.mode_conteneur_actif1": "non"}

View File

@ -1,28 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="general" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value name="calc_val" type="calculation">
<param transitive="False" type="variable">rougail.general.mode_conteneur_actif1</param>
</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamOption(option_4, notraisepropertyerror=False, todict=False)), kwargs={})), values=('oui', 'non'))
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

@ -1,24 +1,23 @@
<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?>
<rougail> <rougail>
<containers> <services/>
<container name="test" id="23">
</container>
</containers>
<variables> <variables>
<family name="general"> <family name="general">
<variable name="mode_conteneur_actif" type="oui/non" description="No change" hidden="True"> <variable name="mode_conteneur_actif" type="oui/non" description="No change" hidden="True">
<value>non</value> <value>non</value>
</variable> </variable>
<variable name="variable" type="string" description="No change"/> <variable name="mode_conteneur_actif1" type="oui/non" description="No change">
<value>non</value>
</variable>
</family> </family>
<separators/> <separators/>
</variables> </variables>
<constraints> <constraints>
<fill name="calc_val" target="variable"> <fill name="calc_val" target="mode_conteneur_actif">
<param type="container">test</param> <param type="variable">mode_conteneur_actif1</param>
</fill> </fill>
</constraints> </constraints>

View File

@ -0,0 +1 @@
{"rougail.general.mode_conteneur_actif": "non", "rougail.general.mode_conteneur_actif1": "non"}

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamOption(option_4, notraisepropertyerror=False, todict=False)), kwargs={})), values=('oui', 'non'))
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

@ -1 +1 @@
{"rougail.general.mode_conteneur_actif": null, "rougail.general.mode_conteneur_actif1": "non"} {"rougail.general.mode_conteneur_actif": "non", "rougail.general.mode_conteneur_actif1": "non"}

View File

@ -1,28 +0,0 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<family doc="rougail" name="rougail">
<family doc="Général" name="general">
<property>normal</property>
<variable doc="No change" multi="False" name="mode_conteneur_actif" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>force_default_on_freeze</property>
<property>frozen</property>
<property>hidden</property>
<property>mandatory</property>
<property>normal</property>
<value name="calc_val" type="calculation">
<param transitive="False" type="variable">rougail.general.mode_conteneur_actif1</param>
</value>
</variable>
<variable doc="No change" multi="False" name="mode_conteneur_actif1" type="choice">
<choice type="string">oui</choice>
<choice type="string">non</choice>
<property>mandatory</property>
<property>normal</property>
<value type="string">non</value>
</variable>
</family>
<separators/>
</family>
</rougail>

View File

@ -0,0 +1,9 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_4 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode_conteneur_actif1', doc='No change', multi=False, default='non', values=('oui', 'non'))
option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamOption(option_4, notraisepropertyerror=False, todict=False)), kwargs={})), values=('oui', 'non'))
option_2 = OptionDescription(name='general', doc='Général', 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

@ -5,16 +5,15 @@
<variables> <variables>
<family name="general"> <family name="general">
<variable name="mode_conteneur_actif" type="string" description="No change"/> <variable name="mode_conteneur_actif" type="string" description="No change" hidden="True"/>
</family> </family>
<separators/> <separators/>
</variables> </variables>
<constraints> <constraints>
<check name="valid_enum" target="mode_conteneur_actif"> <fill name="calc_val" target="mode_conteneur_actif">
<param>['a','b','c']</param> <param type="information">info</param>
<param name="checkval">True</param> </fill>
</check>
</constraints> </constraints>
<help/> <help/>

View File

@ -0,0 +1 @@
{"rougail.general.mode_conteneur_actif": "value"}

View File

@ -0,0 +1,8 @@
from tiramisu import *
from rougail.tiramisu import ConvertDynOptionDescription
import imp
func = imp.load_source('func', 'tests/flattener_dicos/../eosfunc/test.py')
option_3 = StrOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'normal'}), name='mode_conteneur_actif', doc='No change', multi=False, default=Calculation(func.calc_val, Params((ParamInformation("info", None)), kwargs={})))
option_2 = OptionDescription(name='general', doc='general', properties=frozenset({'normal'}), children=[option_3])
option_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1])

Some files were not shown because too many files have changed in this diff Show More