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: def save(self) -> str:
"""Return tiramisu object declaration as a string """Return tiramisu object declaration as a string
""" """
tiramisu_objects = TiramisuReflector(self.rougailobjspace.space, tiramisu_objects = TiramisuReflector(self.rougailobjspace,
self.funcs_path, self.funcs_path,
) )
return tiramisu_objects.get_text() + '\n' return tiramisu_objects.get_text() + '\n'

View File

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