simplify tiramisureflector
This commit is contained in:
@ -56,7 +56,7 @@ class TiramisuReflector:
|
||||
self.set_name(baseelt)
|
||||
basefamily = Family(baseelt,
|
||||
self.text,
|
||||
False,
|
||||
self.objectspace,
|
||||
)
|
||||
for elt in self.reorder_family():
|
||||
self.populate_family(basefamily,
|
||||
@ -87,7 +87,7 @@ class TiramisuReflector:
|
||||
self.set_name(elt)
|
||||
family = Family(elt,
|
||||
self.text,
|
||||
isinstance(elt, self.objectspace.leadership),
|
||||
self.objectspace,
|
||||
)
|
||||
parent_family.add(family)
|
||||
for children in vars(elt).values():
|
||||
@ -134,10 +134,12 @@ class Common:
|
||||
def __init__(self,
|
||||
elt,
|
||||
text,
|
||||
objectspace,
|
||||
):
|
||||
self.elt = elt
|
||||
self.option_name = None
|
||||
self.text = text
|
||||
self.objectspace = objectspace
|
||||
self.elt.reflector_object = self
|
||||
self.object_type = None
|
||||
|
||||
@ -153,17 +155,13 @@ class Common:
|
||||
def populate_attrib(self):
|
||||
"""Populate attributes
|
||||
"""
|
||||
keys = {}
|
||||
keys['name'] = self.convert_str(self.elt.name)
|
||||
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)
|
||||
attrib = ', '.join([f'{key}={value}' for key, value in keys.items()])
|
||||
self.text.append(f'{self.option_name} = {self.object_type}({attrib})')
|
||||
|
||||
def _populate_attrib(self,
|
||||
@ -178,31 +176,25 @@ class Common:
|
||||
return "'" + value.replace("'", "\\\'") + "'"
|
||||
|
||||
def properties_to_string(self,
|
||||
value: list,
|
||||
values: 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_}'")
|
||||
properties = [f"'{property_}'" for property_ in values if isinstance(property_, str)]
|
||||
calc_properties = [self.calc_properties(property_) for property_ in values \
|
||||
if isinstance(property_, self.objectspace.property_)]
|
||||
return 'frozenset({' + ', '.join(sorted(properties) + calc_properties) + '})'
|
||||
|
||||
@staticmethod
|
||||
def populate_properties(child) -> str:
|
||||
def calc_properties(child) -> str:
|
||||
"""Populate properties
|
||||
"""
|
||||
assert child.type == 'calculation'
|
||||
action = f"ParamValue('{child.name}')"
|
||||
option_name = child.source.reflector_object.get()
|
||||
kwargs = (f"'condition': ParamOption({option_name}, todict=True), "
|
||||
f"'expected': ParamValue('{child.expected}')")
|
||||
if child.inverse:
|
||||
kwargs += ", 'reverse_condition': ParamValue(True)"
|
||||
return 'Calculation(calc_value, Params(' + action + ', kwargs={' + kwargs + '}))'
|
||||
return f"Calculation(calc_value, Params(ParamValue('{child.name}'), kwargs={{{kwargs}}}))"
|
||||
|
||||
def populate_informations(self):
|
||||
"""Populate Tiramisu's informations
|
||||
@ -213,8 +205,8 @@ class Common:
|
||||
if key == 'xmlfiles':
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
value = '"' + value.replace('"', '\"') + '"'
|
||||
self.text.append(f'{self.option_name}.impl_set_information("{key}", {value})')
|
||||
value = self.convert_str(value)
|
||||
self.text.append(f"{self.option_name}.impl_set_information('{key}', {value})")
|
||||
|
||||
|
||||
class Variable(Common):
|
||||
@ -225,10 +217,8 @@ class Variable(Common):
|
||||
text,
|
||||
objectspace,
|
||||
):
|
||||
super().__init__(elt, text)
|
||||
self.objectspace = objectspace
|
||||
convert_option = CONVERT_OPTION[elt.type]
|
||||
self.object_type = convert_option['opttype']
|
||||
super().__init__(elt, text, objectspace)
|
||||
self.object_type = CONVERT_OPTION[elt.type]['opttype']
|
||||
|
||||
def _populate_attrib(self,
|
||||
keys: dict,
|
||||
@ -238,8 +228,8 @@ class Variable(Common):
|
||||
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}))))"
|
||||
value = values[0].name.reflector_object.get()
|
||||
keys['values'] = f"Calculation(func.calc_value, Params((ParamOption({value}))))"
|
||||
else:
|
||||
keys['values'] = str(tuple([val.name for val in values]))
|
||||
if hasattr(self.elt, 'multi') and self.elt.multi:
|
||||
@ -292,18 +282,14 @@ class Variable(Common):
|
||||
"""Populate variable parameters
|
||||
"""
|
||||
if param.type == 'string':
|
||||
return f'ParamValue("{param.text}")'
|
||||
value = param.text
|
||||
if value is not None:
|
||||
value = self.convert_str(value)
|
||||
return f"ParamValue({value})"
|
||||
if param.type == 'number':
|
||||
return f'ParamValue({param.text})'
|
||||
if param.type == 'variable':
|
||||
value = {'option': param.text,
|
||||
'notraisepropertyerror': param.notraisepropertyerror,
|
||||
'todict': function in FUNC_TO_DICT,
|
||||
}
|
||||
if hasattr(param, 'suffix'):
|
||||
value['suffix'] = param.suffix
|
||||
value['family'] = param.family
|
||||
return self.build_param(value)
|
||||
return self.build_param(param, function)
|
||||
if param.type == 'information':
|
||||
return f'ParamInformation("{param.text}", None)'
|
||||
if param.type == 'suffix':
|
||||
@ -311,14 +297,17 @@ class Variable(Common):
|
||||
return '' # pragma: no cover
|
||||
|
||||
@staticmethod
|
||||
def build_param(param) -> str:
|
||||
def build_param(param,
|
||||
function: str,
|
||||
) -> str:
|
||||
"""build variable parameters
|
||||
"""
|
||||
option_name = param['option'].reflector_object.get()
|
||||
ends = f"notraisepropertyerror={param['notraisepropertyerror']}, todict={param['todict']})"
|
||||
if 'suffix' in param:
|
||||
family_name = param['family'].reflector_name
|
||||
return f"ParamDynOption({option_name}, '{param['suffix']}', {family_name}, {ends}"
|
||||
option_name = param.text.reflector_object.get()
|
||||
todict = function in FUNC_TO_DICT
|
||||
ends = f"notraisepropertyerror={param.notraisepropertyerror}, todict={todict})"
|
||||
if hasattr(param, 'suffix'):
|
||||
family_name = param.family.reflector_name
|
||||
return f"ParamDynOption({option_name}, '{param.suffix}', {family_name}, {ends}"
|
||||
return f"ParamOption({option_name}, {ends}"
|
||||
|
||||
|
||||
@ -328,13 +317,12 @@ class Family(Common):
|
||||
def __init__(self,
|
||||
elt,
|
||||
text,
|
||||
is_leadership
|
||||
objectspace,
|
||||
):
|
||||
super().__init__(elt, text)
|
||||
self.is_leadership = is_leadership
|
||||
super().__init__(elt, text, objectspace)
|
||||
if hasattr(self.elt, 'suffixes'):
|
||||
self.object_type = 'ConvertDynOptionDescription'
|
||||
elif self.is_leadership:
|
||||
elif isinstance(self.elt, self.objectspace.leadership):
|
||||
self.object_type = 'Leadership'
|
||||
else:
|
||||
self.object_type = 'OptionDescription'
|
||||
|
Reference in New Issue
Block a user