simplify normalize_family

This commit is contained in:
Emmanuel Garette 2020-12-23 11:28:43 +01:00
parent 0e08757e22
commit afdc19887f
8 changed files with 33 additions and 60 deletions

View file

@ -166,7 +166,7 @@ class GroupAnnotator:
) )
has_a_leader = True has_a_leader = True
else: else:
raise DictConsistencyError(_('cannot found followers {}').format(follower_names)) raise DictConsistencyError(_('cannot found followers "{}"').format('", "'.join(follower_names)))
del self.objectspace.space.constraints.group del self.objectspace.space.constraints.group
def manage_leader(self, def manage_leader(self,
@ -464,8 +464,7 @@ 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 DictConsistencyError(_('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 VariableAnnotator: class VariableAnnotator:
@ -789,13 +788,15 @@ class ConstraintAnnotator:
if condition.source == target.name: if condition.source == target.name:
raise DictConsistencyError(_('target name and source name must be different: {}').format(condition.source)) raise DictConsistencyError(_('target name and source name must be different: {}').format(condition.source))
try: try:
target.name = self.objectspace.paths.get_variable_path(target.name, namespace) target_names = [normalize_family(name) for name in target.name.split('.')]
target.name = self.objectspace.paths.get_variable_path('.'.join(target_names), namespace)
except DictConsistencyError: except DictConsistencyError:
# for optional variable # for optional variable
pass pass
elif target.type == 'family': elif target.type == 'family':
try: try:
target.name = self.objectspace.paths.get_family_path(target.name, namespace) target_names = [normalize_family(name) for name in target.name.split('.')]
target.name = self.objectspace.paths.get_family_path('.'.join(target_names), namespace)
except KeyError: except KeyError:
raise DictConsistencyError(_('cannot found family {}').format(target.name)) raise DictConsistencyError(_('cannot found family {}').format(target.name))

View file

@ -292,6 +292,8 @@ class CreoleObjSpace:
name = child.text name = child.text
else: else:
name = subspace['name'] name = subspace['name']
if child.tag == 'family':
name = normalize_family(name)
existed_var = self.is_already_exists(name, existed_var = self.is_already_exists(name,
space, space,
child, child,
@ -362,13 +364,9 @@ class CreoleObjSpace:
if self.paths.path_is_defined(name): if self.paths.path_is_defined(name):
return self.paths.get_variable_obj(name) return self.paths.get_variable_obj(name)
return return
if child.tag == 'family':
norm_name = normalize_family(name)
else:
norm_name = name
children = getattr(space, child.tag, {}) children = getattr(space, child.tag, {})
if norm_name in children: if name in children:
return children[norm_name] return children[name]
def convert_boolean(self, value): # pylint: disable=R0201 def convert_boolean(self, value): # pylint: disable=R0201
"""Boolean coercion. The Creole XML may contain srings like `True` or `False` """Boolean coercion. The Creole XML may contain srings like `True` or `False`
@ -389,19 +387,15 @@ class CreoleObjSpace:
namespace, namespace,
): ):
if not isinstance(family, self.family): # pylint: disable=E1101 if not isinstance(family, self.family): # pylint: disable=E1101
if variable.tag == 'family': return getattr(family, variable.tag)[name]
norm_name = normalize_family(name)
else:
norm_name = name
return getattr(family, variable.tag)[norm_name]
if namespace == Config['variable_namespace']: if namespace == Config['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 family.path == old_family_name:
return getattr(family, variable.tag)[name] return getattr(family, variable.tag)[name]
old_family = self.space.variables[Config['variable_namespace']].family[old_family_name] # pylint: disable=E1101 old_family = self.space.variables[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):
@ -541,12 +535,14 @@ class CreoleObjSpace:
if isinstance(space, self.help): # pylint: disable=E1101 if isinstance(space, self.help): # pylint: disable=E1101
return return
if child.tag == 'variable': if child.tag == 'variable':
family_name = normalize_family(document.attrib['name']) family_name = document.attrib['name']
family_name = normalize_family(family_name)
self.paths.add_variable(namespace, self.paths.add_variable(namespace,
child.attrib['name'], child.attrib['name'],
family_name, family_name,
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 == Config['variable_namespace']: if namespace == Config['variable_namespace']:
self.redefine_variables.append(child.attrib['name']) self.redefine_variables.append(child.attrib['name'])

View file

@ -38,10 +38,7 @@ class Path:
name: str, name: str,
current_namespace: str, current_namespace: str,
) -> str: # pylint: disable=C0111 ) -> str: # pylint: disable=C0111
name = normalize_family(name, #name = normalize_family(name)
check_name=False,
allow_dot=True,
)
if '.' not in name and current_namespace == Config['variable_namespace'] and name in self.full_paths_families: if '.' not in name and current_namespace == Config['variable_namespace'] and name in self.full_paths_families:
name = self.full_paths_families[name] name = self.full_paths_families[name]
if current_namespace is None: # pragma: no cover if current_namespace is None: # pragma: no cover

View file

@ -9,5 +9,4 @@ class ConvertDynOptionDescription(DynOptionDescription):
def convert_suffix_to_path(self, suffix): def convert_suffix_to_path(self, suffix):
if not isinstance(suffix, str): if not isinstance(suffix, str):
suffix = str(suffix) suffix = str(suffix)
return normalize_family(suffix, return normalize_family(suffix)
check_name=False)

View file

@ -1,23 +1,19 @@
""" """
utilitaires créole utilitaires créole
""" """
import unicodedata from unicodedata import normalize, combining
from .i18n import _ from .i18n import _
def normalize_family(family_name: str, def valid_name(name: str) -> None:
check_name: bool=True, if '.' in name:
allow_dot: bool=False) -> str: raise ValueError(_(f'"{name}" is a forbidden variable or family name, dot is not allowed'))
def normalize_family(family_name: str) -> str:
"""replace space, accent, uppercase, ... by valid character """replace space, accent, uppercase, ... by valid character
""" """
family_name = family_name.replace('-', '_') family_name = family_name.replace('-', '_').replace(' ', '_').replace('.', '_')
if not allow_dot: nfkd_form = normalize('NFKD', family_name)
family_name = family_name.replace('.', '_') family_name = ''.join([c for c in nfkd_form if not combining(c)])
family_name = family_name.replace(' ', '_') return family_name.lower()
nfkd_form = unicodedata.normalize('NFKD', family_name)
family_name = ''.join([c for c in nfkd_form if not unicodedata.combining(c)])
family_name = family_name.lower()
if check_name and family_name == 'containers':
raise ValueError(_(f'"{family_name}" is a forbidden family name'))
return family_name

View file

@ -1,8 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?>
<rougail> <rougail>
<services/>
<variables> <variables>
<family name="general"> <family name="general">
<variable name="mode_conteneur_actif" type="oui/non" description="No change"> <variable name="mode_conteneur_actif" type="oui/non" description="No change">
@ -15,14 +12,10 @@
<variable name="follower2" type="string" description="follower2"/> <variable name="follower2" type="string" description="follower2"/>
</family> </family>
</variables> </variables>
<constraints> <constraints>
<group leader="leader"> <group leader="leader">
<follower>follower1</follower> <follower>follower1</follower>
<follower>follower2</follower> <follower>follower2</follower>
</group> </group>
</constraints> </constraints>
<help/>
</rougail> </rougail>

View file

@ -1,17 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?> <?xml version='1.0' encoding='UTF-8'?>
<rougail> <rougail>
<services/>
<variables> <variables>
<family name="general-1"> <family name="general-1">
<variable name="leader" redefine="True" mandatory="True"/> <variable name="leader" redefine="True" mandatory="True"/>
</family> </family>
</variables> </variables>
<constraints>
</constraints>
<help/>
</rougail> </rougail>

View file

@ -12,9 +12,9 @@ option_3 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen
option_4 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='activer_ejabberd', doc='No change', multi=False, default='non', values=('oui', 'non')) option_4 = ChoiceOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='activer_ejabberd', 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_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_1 = OptionDescription(name='rougail', doc='rougail', children=[option_2])
option_7 = StrOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='description', doc='description', multi=False, default='Exportation de la base de ejabberd') option_7 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='day', doc='day', multi=False, default=Calculation(func.calc_multi_condition, Params((ParamValue("non")), kwargs={'condition_1': ParamOption(option_4, notraisepropertyerror=True, todict=False), 'match': ParamValue("none"), 'mismatch': ParamValue("daily")})), values=('none', 'daily', 'weekly', 'monthly'))
option_8 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='day', doc='day', multi=False, default=Calculation(func.calc_multi_condition, Params((ParamValue("non")), kwargs={'condition_1': ParamOption(option_4, notraisepropertyerror=True, todict=False), 'match': ParamValue("none"), 'mismatch': ParamValue("daily")})), values=('none', 'daily', 'weekly', 'monthly')) option_8 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode', doc='mode', multi=False, default='pre', values=('pre', 'post'))
option_9 = ChoiceOption(properties=frozenset({'mandatory', 'normal'}), name='mode', doc='mode', multi=False, default='pre', values=('pre', 'post')) option_9 = StrOption(properties=frozenset({'force_default_on_freeze', 'frozen', 'hidden', 'mandatory', 'normal'}), name='description', doc='description', multi=False, default='Exportation de la base de ejabberd')
option_6 = OptionDescription(name='ejabberd', doc='ejabberd', properties=frozenset({'normal'}), children=[option_7, option_8, option_9]) option_6 = OptionDescription(name='ejabberd', doc='ejabberd', properties=frozenset({'normal'}), children=[option_7, option_8, option_9])
option_5 = OptionDescription(name='extra', doc='extra', children=[option_6]) option_5 = OptionDescription(name='extra', doc='extra', children=[option_6])
option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1, option_5]) option_0 = OptionDescription(name='baseoption', doc='baseoption', children=[option_1, option_5])