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

View File

@ -1,20 +1,31 @@
"""Annotate properties
"""
from ..i18n import _ from ..i18n import _
from ..error import DictConsistencyError from ..error import DictConsistencyError
PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_freeze', PROPERTIES = ('hidden', 'frozen', 'auto_freeze', 'auto_save', 'force_default_on_freeze',
'force_store_value', 'disabled', 'mandatory') '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: class PropertyAnnotator:
"""Annotate properties
"""
def __init__(self, objectspace): def __init__(self, objectspace):
self.objectspace = 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, def convert_property(self,
variable, variable,
): ) -> None:
"""convert properties
"""
properties = [] properties = []
for prop in PROPERTIES: for prop in PROPERTIES:
if hasattr(variable, prop): if hasattr(variable, prop):
@ -27,31 +38,42 @@ class PropertyAnnotator:
variable.mode = None variable.mode = None
if 'force_store_value' in properties and 'force_default_on_freeze' in properties: if 'force_store_value' in properties and 'force_default_on_freeze' in properties:
xmlfiles = self.objectspace.display_xmlfiles(variable.xmlfiles) 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: if properties:
variable.properties = frozenset(properties) variable.properties = frozenset(properties)
def convert_annotator(self): # pylint: disable=C0111 def convert_services(self) -> None:
if hasattr(self.objectspace.space, 'services'): """convert services
"""
self.convert_property(self.objectspace.space.services) self.convert_property(self.objectspace.space.services)
for services in self.objectspace.space.services.service.values(): for services in self.objectspace.space.services.service.values():
self.convert_property(services) self.convert_property(services)
for service in vars(services).values(): for service in vars(services).values():
if isinstance(service, self.objectspace.family): if not isinstance(service, self.objectspace.family):
continue
self.convert_property(service) self.convert_property(service)
if hasattr(service, 'family'): if not hasattr(service, 'family'):
continue
self.convert_property(service) self.convert_property(service)
for family in service.family: for family in service.family:
self.convert_property(family) self.convert_property(family)
if hasattr(family, 'variable'): if not hasattr(family, 'variable'):
continue
for variable in family.variable: for variable in family.variable:
self.convert_property(variable) self.convert_property(variable)
if hasattr(self.objectspace.space, 'variables'):
def convert_variables(self) -> None:
"""convert variables
"""
for variables in self.objectspace.space.variables.values(): for variables in self.objectspace.space.variables.values():
if hasattr(variables, 'family'): if not hasattr(variables, 'family'):
continue
for family in variables.family.values(): for family in variables.family.values():
self.convert_property(family) self.convert_property(family)
if hasattr(family, 'variable'): if not hasattr(family, 'variable'):
continue
for variable in family.variable.values(): for variable in family.variable.values():
if isinstance(variable, self.objectspace.leadership): if isinstance(variable, self.objectspace.leadership):
self.convert_property(variable) self.convert_property(variable)