simplify tiramisureflector

This commit is contained in:
Emmanuel Garette 2021-01-23 13:44:21 +01:00
parent 4df2d0e2bf
commit 04e9706a1b
2 changed files with 50 additions and 69 deletions

View file

@ -65,7 +65,7 @@ class Rougail:
def save(self) -> str:
"""Return tiramisu object declaration as a string
"""
tiramisu_objects = TiramisuReflector(self.rougailobjspace.space,
tiramisu_objects = TiramisuReflector(self.rougailobjspace,
self.funcs_path,
)
return tiramisu_objects.get_text() + '\n'

View file

@ -3,6 +3,7 @@ flattened XML specific
"""
from .config import Config
from .annotator import ERASED_ATTRIBUTES, CONVERT_OPTION
from .objspace import RootRougailObject
FUNC_TO_DICT = []
@ -15,11 +16,19 @@ class Root(): # pylint: disable=R0903
path = '.'
class BaseElt: # pylint: disable=R0903
"""Base element
"""
name = 'baseoption'
doc = 'baseoption'
path = '.'
class TiramisuReflector:
"""Convert object to tiramisu representation
"""
def __init__(self,
space,
objectspace,
funcs_path,
):
self.index = 0
@ -38,63 +47,37 @@ class TiramisuReflector:
" from tiramisu import *",
"from rougail.tiramisu import ConvertDynOptionDescription",
]
self.make_tiramisu_objects(space)
self.objectspace = objectspace
self.make_tiramisu_objects()
def make_tiramisu_objects(self,
space,
):
def make_tiramisu_objects(self) -> None:
"""make tiramisu objects
"""
baseelt = BaseElt()
self.set_name(baseelt)
basefamily = Family(baseelt,
self.text,
False,
)
for elt in self.reorder_family(space):
self.iter_family(basefamily,
for elt in self.reorder_family():
self.populate_family(basefamily,
elt,
)
# parse object
baseelt.reflector_object.get() # pylint: disable=E1101
@staticmethod
def reorder_family(space):
def reorder_family(self):
"""variable_namespace family has to be loaded before any other family
because `extra` family could use `variable_namespace` variables.
"""
if hasattr(space, 'variables'):
if Config['variable_namespace'] in space.variables:
yield space.variables[Config['variable_namespace']]
for elt, value in space.variables.items():
if hasattr(self.objectspace.space, 'variables'):
if Config['variable_namespace'] in self.objectspace.space.variables:
yield self.objectspace.space.variables[Config['variable_namespace']]
for elt, value in self.objectspace.space.variables.items():
if elt != Config['variable_namespace']:
yield value
if hasattr(space, 'services'):
yield space.services
def get_attributes(self, space): # pylint: disable=R0201
"""Get attributes
"""
for attr in dir(space):
if not attr.startswith('_') and attr not in ERASED_ATTRIBUTES:
yield attr
def iter_family(self,
family,
child,
):
"""Iter each family
"""
tag = child.__class__.__name__
if tag == 'Variable':
function = self.populate_variable
elif tag == 'Property':
# property already imported with family
return
else:
function = self.populate_family
function(family,
child,
)
if hasattr(self.objectspace.space, 'services'):
yield self.objectspace.space.services
def populate_family(self,
parent_family,
@ -105,27 +88,29 @@ class TiramisuReflector:
self.set_name(elt)
family = Family(elt,
self.text,
isinstance(elt, self.objectspace.leadership),
)
parent_family.add(family)
for children in self.get_children(elt):
for child in children:
self.iter_family(family,
child,
for children in vars(elt).values():
if isinstance(children, self.objectspace.family):
self.populate_family(family,
children,
)
def get_children(self,
space,
):
"""Get children
"""
for tag in self.get_attributes(space):
children = getattr(space, tag)
if children.__class__.__name__ == 'Family':
children = [children]
continue
if isinstance(children, dict):
children = list(children.values())
if isinstance(children, list):
yield children
for child in children:
if isinstance(child, self.objectspace.property_) or \
not isinstance(child, RootRougailObject):
continue
if isinstance(child, self.objectspace.variable):
function = self.populate_variable
else:
function = self.populate_family
function(family,
child,
)
def populate_variable(self,
family,
@ -160,14 +145,6 @@ class TiramisuReflector:
return '\n'.join(self.text)
class BaseElt: # pylint: disable=R0903
"""Base element
"""
name = 'baseoption'
doc = 'baseoption'
path = '.'
class Common:
"""Common function for variable and family
"""
@ -234,7 +211,8 @@ class Common:
value = '"' + value.replace('"', '\"') + '"'
self.text.append(f'{self.option_name}.impl_set_information("{key}", {value})')
def get_attributes(self, space): # pylint: disable=R0201
@staticmethod
def get_attributes(space):
"""Get attributes
"""
for attr in ATTRIBUTES_ORDER:
@ -437,9 +415,10 @@ class Family(Common):
def __init__(self,
elt,
text,
is_leadership
):
super().__init__(elt, text)
self.is_leadership = self.elt.__class__.__name__ == 'Leadership'
self.is_leadership = is_leadership
self.children = []
def add(self, child):
@ -463,6 +442,8 @@ class Family(Common):
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}))))"