From 04e9706a1b3e0faedbebe510af4e06d7c865beb8 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 23 Jan 2021 13:44:21 +0100 Subject: [PATCH] simplify tiramisureflector --- src/rougail/rougail.py | 2 +- src/rougail/tiramisureflector.py | 117 +++++++++++++------------------ 2 files changed, 50 insertions(+), 69 deletions(-) diff --git a/src/rougail/rougail.py b/src/rougail/rougail.py index 04189a924..df316fa0e 100644 --- a/src/rougail/rougail.py +++ b/src/rougail/rougail.py @@ -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' diff --git a/src/rougail/tiramisureflector.py b/src/rougail/tiramisureflector.py index 74c2ca9bc..403e0f144 100644 --- a/src/rougail/tiramisureflector.py +++ b/src/rougail/tiramisureflector.py @@ -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, - elt, - ) + 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, - ) - - 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] + for children in vars(elt).values(): + if isinstance(children, self.objectspace.family): + self.populate_family(family, + 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}))))"