reorganisation
This commit is contained in:
@ -188,8 +188,9 @@ class FamilyAnnotator:
|
||||
for family in families.family.values():
|
||||
if 'dynamic' not in vars(family):
|
||||
continue
|
||||
family.dynamic = self.objectspace.paths.get_variable(family.dynamic)
|
||||
if not family.dynamic.multi:
|
||||
family.suffixes = self.objectspace.paths.get_variable(family.dynamic)
|
||||
del family.dynamic
|
||||
if not family.suffixes.multi:
|
||||
xmlfiles = self.objectspace.display_xmlfiles(family.xmlfiles)
|
||||
msg = _(f'dynamic family "{family.name}" must be linked '
|
||||
f'to multi variable in {xmlfiles}')
|
||||
|
@ -7,9 +7,6 @@ from .objspace import RootRougailObject
|
||||
|
||||
|
||||
FUNC_TO_DICT = []
|
||||
ATTRIBUTES_ORDER = ('name', 'doc', 'default', 'values', 'multi', 'default_multi', 'properties',
|
||||
'validators', 'min_number', 'max_number',
|
||||
)
|
||||
|
||||
|
||||
class Root(): # pylint: disable=R0903
|
||||
@ -107,32 +104,15 @@ class TiramisuReflector:
|
||||
not isinstance(child, RootRougailObject):
|
||||
continue
|
||||
if isinstance(child, self.objectspace.variable):
|
||||
function = self.populate_variable
|
||||
self.set_name(child)
|
||||
family.add(Variable(child,
|
||||
self.text,
|
||||
self.objectspace,
|
||||
))
|
||||
else:
|
||||
function = self.populate_family
|
||||
function(family,
|
||||
child,
|
||||
)
|
||||
|
||||
def populate_variable(self,
|
||||
family,
|
||||
elt,
|
||||
):
|
||||
"""Populate variable
|
||||
"""
|
||||
if family.is_leadership:
|
||||
is_leader = elt.name == family.elt.variable[0].name
|
||||
is_follower = not is_leader
|
||||
else:
|
||||
is_leader = False
|
||||
is_follower = False
|
||||
self.set_name(elt)
|
||||
family.add(Variable(elt,
|
||||
self.text,
|
||||
self.objectspace,
|
||||
is_follower,
|
||||
is_leader,
|
||||
))
|
||||
self.populate_family(family,
|
||||
child,
|
||||
)
|
||||
|
||||
def set_name(self,
|
||||
elt,
|
||||
@ -157,9 +137,59 @@ class Common:
|
||||
):
|
||||
self.elt = elt
|
||||
self.option_name = None
|
||||
self.attrib = {}
|
||||
self.text = text
|
||||
self.elt.reflector_object = self
|
||||
self.object_type = None
|
||||
|
||||
def get(self):
|
||||
"""Get tiramisu's object
|
||||
"""
|
||||
if self.option_name is None:
|
||||
self.option_name = self.elt.reflector_name
|
||||
self.populate_attrib()
|
||||
self.populate_informations()
|
||||
return self.option_name
|
||||
|
||||
def populate_attrib(self):
|
||||
"""Populate attributes
|
||||
"""
|
||||
keys = {}
|
||||
keys['name'] = self.convert_str(self.elt.name)
|
||||
if hasattr(self.elt, 'doc'):
|
||||
keys['doc'] = self.convert_str(self.elt.doc)
|
||||
self._populate_attrib(keys)
|
||||
if hasattr(self.elt, 'properties'):
|
||||
keys['properties'] = self.properties_to_string(self.elt.properties)
|
||||
ret_list = []
|
||||
for key, value in keys.items():
|
||||
ret_list.append(f'{key}={value}')
|
||||
attrib = ', '.join(ret_list)
|
||||
self.text.append(f'{self.option_name} = {self.object_type}({attrib})')
|
||||
|
||||
def _populate_attrib(self,
|
||||
keys: dict,
|
||||
) -> None:
|
||||
raise NotImplementedError()
|
||||
|
||||
@staticmethod
|
||||
def convert_str(value):
|
||||
"""convert string
|
||||
"""
|
||||
return "'" + value.replace("'", "\\\'") + "'"
|
||||
|
||||
def properties_to_string(self,
|
||||
value: list,
|
||||
) -> None:
|
||||
"""Change properties to string
|
||||
"""
|
||||
properties = []
|
||||
calc_properties = []
|
||||
for property_ in value:
|
||||
if not isinstance(property_, str):
|
||||
calc_properties.append(self.populate_properties(property_))
|
||||
else:
|
||||
properties.append(f"'{property_}'")
|
||||
return 'frozenset({' + ', '.join(sorted(properties) + calc_properties) + '})'
|
||||
|
||||
@staticmethod
|
||||
def populate_properties(child) -> str:
|
||||
@ -174,46 +204,6 @@ class Common:
|
||||
kwargs += ", 'reverse_condition': ParamValue(True)"
|
||||
return 'Calculation(calc_value, Params(' + action + ', kwargs={' + kwargs + '}))'
|
||||
|
||||
def properties_to_string(self):
|
||||
"""Change properties to string
|
||||
"""
|
||||
properties = []
|
||||
calc_properties = []
|
||||
for property_ in self.attrib['properties']:
|
||||
if not isinstance(property_, str):
|
||||
calc_properties.append(self.populate_properties(property_))
|
||||
else:
|
||||
properties.append(f"'{property_}'")
|
||||
return 'frozenset({' + ', '.join(sorted(properties) + calc_properties) + '})'
|
||||
|
||||
def get_attrib(self):
|
||||
"""Get attributes
|
||||
"""
|
||||
ret_list = []
|
||||
for key, value in self.attrib.items():
|
||||
if value is None:
|
||||
continue
|
||||
if key == 'properties':
|
||||
value = self.properties_to_string()
|
||||
elif key == 'validators':
|
||||
value = '[' + ', '.join([self.calculation_value(val, ['ParamSelfOption()']) \
|
||||
for val in value]) + ']'
|
||||
elif key == 'values':
|
||||
if value[0].type == 'calculation':
|
||||
value = value[0].name.reflector_object.get()
|
||||
value = f"Calculation(func.calc_value, Params((ParamOption({value}))))"
|
||||
else:
|
||||
value = str(tuple([val.name for val in value]))
|
||||
elif key in ['multi', 'suffixes']:
|
||||
if not value:
|
||||
continue
|
||||
elif isinstance(value, str) and key != 'opt' and not value.startswith('['):
|
||||
value = "'" + value.replace("'", "\\\'") + "'"
|
||||
elif isinstance(value, self.objectspace.value):
|
||||
value = self.calculation_value(value, [])
|
||||
ret_list.append(f'{key}={value}')
|
||||
return ', '.join(ret_list)
|
||||
|
||||
def populate_informations(self):
|
||||
"""Populate Tiramisu's informations
|
||||
"""
|
||||
@ -226,13 +216,6 @@ class Common:
|
||||
value = '"' + value.replace('"', '\"') + '"'
|
||||
self.text.append(f'{self.option_name}.impl_set_information("{key}", {value})')
|
||||
|
||||
def populate_attrib(self):
|
||||
"""Populate attributes
|
||||
"""
|
||||
for attr in ATTRIBUTES_ORDER:
|
||||
if hasattr(self.elt, attr):
|
||||
self.attrib[attr] = getattr(self.elt, attr)
|
||||
|
||||
|
||||
class Variable(Common):
|
||||
"""Manage variable
|
||||
@ -241,29 +224,42 @@ class Variable(Common):
|
||||
elt,
|
||||
text,
|
||||
objectspace,
|
||||
is_follower,
|
||||
is_leader,
|
||||
):
|
||||
super().__init__(elt, text)
|
||||
self.objectspace = objectspace
|
||||
self.is_leader = is_leader
|
||||
self.is_follower = is_follower
|
||||
convert_option = CONVERT_OPTION[elt.type]
|
||||
self.object_type = convert_option['opttype']
|
||||
self.attrib.update(convert_option.get('initkwargs', {}))
|
||||
|
||||
def get(self):
|
||||
"""Get tiramisu's object
|
||||
"""
|
||||
if self.option_name is None:
|
||||
self.populate_attrib()
|
||||
if self.object_type == 'SymLinkOption':
|
||||
self.attrib['opt'] = self.elt.opt.reflector_object.get()
|
||||
attrib = self.get_attrib()
|
||||
self.option_name = self.elt.reflector_name
|
||||
self.text.append(f'{self.option_name} = {self.object_type}({attrib})')
|
||||
self.populate_informations()
|
||||
return self.option_name
|
||||
def _populate_attrib(self,
|
||||
keys: dict,
|
||||
):
|
||||
if hasattr(self.elt, 'opt'):
|
||||
keys['opt'] = self.elt.opt.reflector_object.get()
|
||||
if hasattr(self.elt, 'values'):
|
||||
values = self.elt.values
|
||||
if values[0].type == 'calculation':
|
||||
values = values[0].name.reflector_object.get()
|
||||
keys['values'] = f"Calculation(func.calc_value, Params((ParamOption({values}))))"
|
||||
else:
|
||||
keys['values'] = str(tuple([val.name for val in values]))
|
||||
if hasattr(self.elt, 'multi') and self.elt.multi:
|
||||
keys['multi'] = self.elt.multi
|
||||
for key in ['default', 'default_multi']:
|
||||
if hasattr(self.elt, key) and getattr(self.elt, key) is not None:
|
||||
value = getattr(self.elt, key)
|
||||
if isinstance(value, str):
|
||||
value = self.convert_str(value)
|
||||
elif isinstance(value, self.objectspace.value):
|
||||
value = self.calculation_value(value, [])
|
||||
keys[key] = value
|
||||
if hasattr(self.elt, 'validators'):
|
||||
keys['validators'] = '[' + ', '.join([self.calculation_value(val,
|
||||
['ParamSelfOption()']) for val in self.elt.validators]) + ']'
|
||||
for key in ['min_number', 'max_number']:
|
||||
if hasattr(self.elt, key):
|
||||
keys[key] = getattr(self.elt, key)
|
||||
for key, value in CONVERT_OPTION[self.elt.type].get('initkwargs', {}).items():
|
||||
keys[key] = value
|
||||
|
||||
def calculation_value(self,
|
||||
child,
|
||||
@ -334,6 +330,12 @@ class Family(Common):
|
||||
):
|
||||
super().__init__(elt, text)
|
||||
self.is_leadership = is_leadership
|
||||
if hasattr(self.elt, 'suffixes'):
|
||||
self.object_type = 'ConvertDynOptionDescription'
|
||||
elif self.is_leadership:
|
||||
self.object_type = 'Leadership'
|
||||
else:
|
||||
self.object_type = 'OptionDescription'
|
||||
self.children = []
|
||||
|
||||
def add(self, child):
|
||||
@ -341,32 +343,10 @@ class Family(Common):
|
||||
"""
|
||||
self.children.append(child)
|
||||
|
||||
def get(self):
|
||||
"""Get tiramisu's object
|
||||
"""
|
||||
if not self.option_name:
|
||||
self.populate_attrib()
|
||||
self.populate_dynamic()
|
||||
self.option_name = self.elt.reflector_name
|
||||
object_name = self.get_object_name()
|
||||
attrib = self.get_attrib() + \
|
||||
', children=[' + ', '.join([child.get() for child in self.children]) + ']'
|
||||
self.text.append(f'{self.option_name} = {object_name}({attrib})')
|
||||
self.populate_informations()
|
||||
return self.option_name
|
||||
|
||||
def populate_dynamic(self):
|
||||
"""populate dynamic family
|
||||
"""
|
||||
if hasattr(self.elt, 'dynamic'):
|
||||
dyn = self.elt.dynamic.reflector_object.get()
|
||||
self.attrib['suffixes'] = f"Calculation(func.calc_value, Params((ParamOption({dyn}))))"
|
||||
|
||||
def get_object_name(self):
|
||||
"""Get family object's name
|
||||
"""
|
||||
if 'suffixes' in self.attrib:
|
||||
return 'ConvertDynOptionDescription'
|
||||
if self.is_leadership:
|
||||
return 'Leadership'
|
||||
return 'OptionDescription'
|
||||
def _populate_attrib(self,
|
||||
keys: list,
|
||||
) -> None:
|
||||
if hasattr(self.elt, 'suffixes'):
|
||||
dyn = self.elt.suffixes.reflector_object.get()
|
||||
keys['suffixes'] = f"Calculation(func.calc_value, Params((ParamOption({dyn}))))"
|
||||
keys['children'] = '[' + ', '.join([child.get() for child in self.children]) + ']'
|
||||
|
Reference in New Issue
Block a user