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