This commit is contained in:
Emmanuel Garette 2021-01-06 21:50:53 +01:00
parent 1e0cb96b34
commit 8a07892276
2 changed files with 85 additions and 54 deletions

View File

@ -1,3 +1,5 @@
"""Annotate group
"""
from typing import List
from ..i18n import _
@ -5,15 +7,20 @@ from ..error import DictConsistencyError
class GroupAnnotator:
"""Annotate group
"""
def __init__(self,
objectspace,
):
self.objectspace = objectspace
if not hasattr(self.objectspace.space, 'constraints') or not hasattr(self.objectspace.space.constraints, 'group'):
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
"""convert groups
"""
for group in self.objectspace.space.constraints.group:
leader_fullname = group.leader
leader_family_name = self.objectspace.paths.get_variable_family_name(leader_fullname)
@ -23,18 +30,20 @@ class GroupAnnotator:
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])
leader_family = leader_fullname.rsplit('.', 1)[0]
ori_leader_family = self.objectspace.paths.get_family_obj(leader_family)
for variable in list(ori_leader_family.variable.values()):
if has_a_leader:
# it's a follower
self.manage_follower(namespace,
leader_family_name,
self.manage_follower(leader_family_name,
variable,
leadership_name,
follower_names,
leader_space,
leader_is_hidden,
)
if leader_is_hidden:
variable.frozen = True
variable.force_default_on_freeze = True
leader_space.variable.append(variable)
ori_leader_family.variable.pop(variable.name)
if follower_names == []:
# no more follower
@ -56,7 +65,6 @@ class GroupAnnotator:
leader_family_name,
leadership_name,
leader_name,
namespace,
variable,
group,
leader_fullname,
@ -65,7 +73,9 @@ class GroupAnnotator:
else:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
joined = '", "'.join(follower_names)
raise DictConsistencyError(_(f'when parsing leadership, we espect to find those followers "{joined}" in {xmlfiles}'), 31)
msg = _(f'when parsing leadership, we espect to find those followers "{joined}" '
f'in {xmlfiles}')
raise DictConsistencyError(msg, 31)
del self.objectspace.space.constraints.group
def manage_leader(self,
@ -73,15 +83,16 @@ class GroupAnnotator:
leader_family_name: str,
leadership_name: str,
leader_name: str,
namespace: str,
variable: 'Variable',
group: 'Group',
leader_fullname: str,
) -> None:
# manage leader's variable
"""manage leader's variable
"""
if variable.multi is not True:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
raise DictConsistencyError(_(f'the variable "{variable.name}" in a group must be multi in {xmlfiles}'), 32)
msg = _(f'the variable "{variable.name}" in a group must be multi in {xmlfiles}')
raise DictConsistencyError(msg, 32)
leader_space.variable = []
leader_space.name = leadership_name
leader_space.hidden = variable.hidden
@ -98,6 +109,7 @@ class GroupAnnotator:
leader_space.doc = variable.description
else:
leader_space.doc = variable.name
namespace = variable.namespace
leadership_path = namespace + '.' + leader_family_name + '.' + leadership_name
self.objectspace.paths.add_family(namespace,
leadership_path,
@ -115,23 +127,20 @@ class GroupAnnotator:
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]:
"""manage follower
"""
follower_name = follower_names.pop(0)
if variable.name != follower_name:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
raise DictConsistencyError(_(f'when parsing leadership, we espect to find the follower "{follower_names[0]}" but we found "{variable.name}" in {xmlfiles}'), 33)
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,
msg = _('when parsing leadership, we espect to find the follower '
f'"{follower_name}" but we found "{variable.name}" in {xmlfiles}')
raise DictConsistencyError(msg, 33)
self.objectspace.paths.set_leader(variable.namespace,
leader_family_name,
variable.name,
leader_name,

View File

@ -1,20 +1,31 @@
"""Annotate properties
"""
from ..i18n import _
from ..error import DictConsistencyError
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'],
}
class PropertyAnnotator:
"""Annotate properties
"""
def __init__(self, objectspace):
self.objectspace = objectspace
self.convert_annotator()
if hasattr(self.objectspace.space, 'services'):
self.convert_services()
if hasattr(self.objectspace.space, 'variables'):
self.convert_variables()
def convert_property(self,
variable,
):
) -> None:
"""convert properties
"""
properties = []
for prop in PROPERTIES:
if hasattr(variable, prop):
@ -27,35 +38,46 @@ class PropertyAnnotator:
variable.mode = None
if 'force_store_value' in properties and 'force_default_on_freeze' in properties:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles)
raise DictConsistencyError(_(f'cannot have auto_freeze or auto_store with the hidden variable "{variable.name}" in {xmlfiles}'), 50)
msg = _('cannot have auto_freeze or auto_store with the hidden '
f'variable "{variable.name}" in {xmlfiles}')
raise DictConsistencyError(msg, 50)
if properties:
variable.properties = frozenset(properties)
def convert_annotator(self): # pylint: disable=C0111
if hasattr(self.objectspace.space, 'services'):
self.convert_property(self.objectspace.space.services)
for services in self.objectspace.space.services.service.values():
self.convert_property(services)
for service in vars(services).values():
if isinstance(service, self.objectspace.family):
self.convert_property(service)
if hasattr(service, 'family'):
self.convert_property(service)
for family in service.family:
self.convert_property(family)
if hasattr(family, 'variable'):
for variable in family.variable:
self.convert_property(variable)
if hasattr(self.objectspace.space, 'variables'):
for variables in self.objectspace.space.variables.values():
if hasattr(variables, 'family'):
for family in variables.family.values():
self.convert_property(family)
if hasattr(family, 'variable'):
for variable in family.variable.values():
if isinstance(variable, self.objectspace.leadership):
self.convert_property(variable)
for follower in variable.variable:
self.convert_property(follower)
else:
self.convert_property(variable)
def convert_services(self) -> None:
"""convert services
"""
self.convert_property(self.objectspace.space.services)
for services in self.objectspace.space.services.service.values():
self.convert_property(services)
for service in vars(services).values():
if not isinstance(service, self.objectspace.family):
continue
self.convert_property(service)
if not hasattr(service, 'family'):
continue
self.convert_property(service)
for family in service.family:
self.convert_property(family)
if not hasattr(family, 'variable'):
continue
for variable in family.variable:
self.convert_property(variable)
def convert_variables(self) -> None:
"""convert variables
"""
for variables in self.objectspace.space.variables.values():
if not hasattr(variables, 'family'):
continue
for family in variables.family.values():
self.convert_property(family)
if not hasattr(family, 'variable'):
continue
for variable in family.variable.values():
if isinstance(variable, self.objectspace.leadership):
self.convert_property(variable)
for follower in variable.variable:
self.convert_property(follower)
else:
self.convert_property(variable)