simplify get_children

This commit is contained in:
Emmanuel Garette 2020-12-24 18:31:13 +01:00
parent 6a771913e0
commit 0ef5429577

View file

@ -44,9 +44,7 @@ class TiramisuReflector:
) )
def get_root_family(self): def get_root_family(self):
family = Family(BaseElt('baseoption', family = Family(BaseElt(),
'baseoption',
),
self.storage, self.storage,
False, False,
'.', '.',
@ -158,15 +156,9 @@ class TiramisuReflector:
class BaseElt: class BaseElt:
def __init__(self, def __init__(self) -> None:
name, self.name = 'baseoption'
doc, self.doc = 'baseoption'
):
self.name = name
self.doc = doc
def __iter__(self):
return iter([])
class ElementStorage: class ElementStorage:
@ -181,13 +173,9 @@ class ElementStorage:
self.index += 1 self.index += 1
def get(self, path): def get(self, path):
if path not in self.paths or self.paths[path][0] is None:
raise LoaderError(_('there is no element for path {}').format(path))
return self.paths[path][0] return self.paths[path][0]
def get_name(self, path): def get_name(self, path):
if path not in self.paths:
raise LoaderError(_('there is no element for index {}').format(path))
return f'option_{self.paths[path][1]}' return f'option_{self.paths[path][1]}'
@ -207,15 +195,13 @@ class Common:
self.storage.add(self.path, self) self.storage.add(self.path, self)
def populate_properties(self, child): def populate_properties(self, child):
if child.type == 'calculation': assert child.type == 'calculation'
action = f"ParamValue('{child.name}')" action = f"ParamValue('{child.name}')"
option_name = self.storage.get(child.source).get() option_name = self.storage.get(child.source).get()
kwargs = f"'condition': ParamOption({option_name}, todict=True), 'expected': ParamValue('{child.expected}')" kwargs = f"'condition': ParamOption({option_name}, todict=True), 'expected': ParamValue('{child.expected}')"
if child.inverse: if child.inverse:
kwargs += ", 'reverse_condition': ParamValue(True)" kwargs += ", 'reverse_condition': ParamValue(True)"
prop = 'Calculation(calc_value, Params(' + action + ', kwargs={' + kwargs + '}))' prop = 'Calculation(calc_value, Params(' + action + ', kwargs={' + kwargs + '}))'
else:
prop = "'" + child.text + "'"
if self.attrib['properties']: if self.attrib['properties']:
self.attrib['properties'] += ', ' self.attrib['properties'] += ', '
self.attrib['properties'] += prop self.attrib['properties'] += prop
@ -263,15 +249,8 @@ class Common:
): ):
for attr in dir(space): for attr in dir(space):
if not attr.startswith('_') and attr not in ERASED_ATTRIBUTES: if not attr.startswith('_') and attr not in ERASED_ATTRIBUTES:
value = getattr(space, attr) if isinstance(getattr(space, attr), list):
if isinstance(value, (list, dict)): yield attr, getattr(space, attr)
children = getattr(space, attr)
if children.__class__.__name__ == 'Family':
children = [children]
if isinstance(children, dict):
children = list(children.values())
if children and isinstance(children, list):
yield attr, children
class Variable(Common): class Variable(Common):
@ -314,7 +293,7 @@ class Variable(Common):
for key in self.get_attributes(self.elt): for key in self.get_attributes(self.elt):
value = getattr(self.elt, key) value = getattr(self.elt, key)
if key in FORCE_INFORMATIONS: if key in FORCE_INFORMATIONS:
if key == 'test': if key == 'test': # pragma: no cover
value = value.split(',') value = value.split(',')
if self.object_type == 'IntOption': if self.object_type == 'IntOption':
value = [int(v) for v in value] value = [int(v) for v in value]
@ -368,19 +347,15 @@ class Variable(Common):
def calculation_value(self, child, args): def calculation_value(self, child, args):
kwargs = [] kwargs = []
if hasattr(child, 'name'): # has parameters
# has parameters function = child.name
function = child.name if hasattr(child, 'param'):
if hasattr(child, 'param'): for param in child.param:
for param in child.param: value = self.populate_param(function, param)
value = self.populate_param(function, param) if not hasattr(param, 'name'):
if not hasattr(param, 'name'): args.append(str(value))
args.append(str(value)) else:
else: kwargs.append(f"'{param.name}': " + value)
kwargs.append(f"'{param.name}': " + value)
else:
# function without any parameter
function = child.text.strip()
ret = f"Calculation(func.{function}, Params((" + ', '.join(args) + "), kwargs=" + "{" + ', '.join(kwargs) + "})" ret = f"Calculation(func.{function}, Params((" + ', '.join(args) + "), kwargs=" + "{" + ', '.join(kwargs) + "})"
if hasattr(child, 'warnings_only'): if hasattr(child, 'warnings_only'):
ret += f', warnings_only={child.warnings_only}' ret += f', warnings_only={child.warnings_only}'
@ -406,7 +381,7 @@ class Variable(Common):
return f'ParamInformation("{param.text}", None)' return f'ParamInformation("{param.text}", None)'
elif param.type == 'suffix': elif param.type == 'suffix':
return f'ParamSuffix()' return f'ParamSuffix()'
raise LoaderError(_('unknown param type {}').format(param.type)) raise LoaderError(_('unknown param type {}').format(param.type)) # pragma: no cover
def populate_value(self, def populate_value(self,
child, child,