From 8801765bfbfba0e72d02cf0263cee709f30c2490 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 2 Sep 2026 22:16:34 +0200 Subject: [PATCH] fix: variable and family object are same has type object --- src/rougail/convert/__init__.py | 5 +- src/rougail/convert/convert.py | 101 +++++++++++++++++++------------- src/rougail/types.py | 11 ++-- 3 files changed, 70 insertions(+), 47 deletions(-) diff --git a/src/rougail/convert/__init__.py b/src/rougail/convert/__init__.py index 23b988498..b0115e3b1 100644 --- a/src/rougail/convert/__init__.py +++ b/src/rougail/convert/__init__.py @@ -42,8 +42,11 @@ class Rougail(UserData): load_from_tiramisu_cache and Path(self.rougailconfig["tiramisu_cache"]).is_file() ) - types = rougail_type(self.rougailconfig) if not self.load_from_tiramisu_cache: + if rougailconfig["types"]: + types = rougail_type(self.rougailconfig) + else: + types = {} self.converted = RougailConvert(self.rougailconfig, **types) self.config = None diff --git a/src/rougail/convert/convert.py b/src/rougail/convert/convert.py index bbdd4ee81..266e2c6b3 100644 --- a/src/rougail/convert/convert.py +++ b/src/rougail/convert/convert.py @@ -206,48 +206,63 @@ class ParserVariable: return root = Path(__file__).parent.parent self.walker = None - self.variable = Variable - self.family = Family - for structural_name in self.structurals: - structural = f"structural_{structural_name}" - module_path = root / structural / "__init__.py" - if not module_path.is_file(): - continue - module = load_modules(f"rougail.{structural}", str(module_path)) - if "Variable" in module.__all__: - self.variable = type( - self.variable.__name__ + "_" + structural, - (self.variable, module.Variable), - {}, - ) - if "Family" in module.__all__: - self.family = type( - self.family.__name__ + "_" + structural, - (self.family, module.Family), - {}, - ) - if not self.walker and "Walker" in module.__all__: - self.walker = module.Walker - self.dynamic = type(Dynamic.__name__, (Dynamic, self.family), {}) - self.choices = type(Choices.__name__, (Choices, self.variable), {}) - self.regexp = type(Regexp.__name__, (Regexp, self.variable), {}) - variable_types = self.convert_options.copy() - variable_types.remove("choice") - variable_types.remove("regexp") - variable_types.remove("symlink") - self.variable_objects = [ - self.get_variable_object(obj, is_variable=True) - for obj in [ - (self.variable, variable_types), - SymLink, - self.choices, - self.regexp, + if self.variable_objects is None: + self.variable = Variable + self.family = Family + for structural_name in self.structurals: + structural = f"structural_{structural_name}" + module_path = root / structural / "__init__.py" + if not module_path.is_file(): + continue + module = load_modules(f"rougail.{structural}", str(module_path)) + if "Variable" in module.__all__: + self.variable = type( + self.variable.__name__ + "_" + structural, + (self.variable, module.Variable), + {}, + ) + if "Family" in module.__all__: + self.family = type( + self.family.__name__ + "_" + structural, + (self.family, module.Family), + {}, + ) + if not self.walker and "Walker" in module.__all__: + self.walker = module.Walker + self.dynamic = type(Dynamic.__name__, (Dynamic, self.family), {}) + self.choices = type(Choices.__name__, (Choices, self.variable), {}) + self.regexp = type(Regexp.__name__, (Regexp, self.variable), {}) + variable_types = self.convert_options.copy() + variable_types.remove("choice") + variable_types.remove("regexp") + variable_types.remove("symlink") + self.variable_objects = [ + self.get_variable_object(obj, is_variable=True) + for obj in [ + (self.variable, variable_types), + SymLink, + self.choices, + self.regexp, + ] ] - ] - self.family_objects = [ - self.get_variable_object(obj, is_variable=False) - for obj in [self.dynamic, self.family] - ] + self.family_objects = [ + self.get_variable_object(obj, is_variable=False) + for obj in [self.dynamic, self.family] + ] + else: + self.variable = self.variable_objects[0] + self.choices = self.variable_objects[2] + self.regexp = self.variable_objects[3] + self.family = self.family_objects[-1] + self.dynamic = self.family_objects[0] + for structural_name in self.structurals: + structural = f"structural_{structural_name}" + module_path = root / structural / "__init__.py" + if not module_path.is_file(): + continue + module = load_modules(f"rougail.{structural}", str(module_path)) + if not self.walker and "Walker" in module.__all__: + self.walker = module.Walker self.is_init = True def get_variable_object(self, obj, *, is_variable: bool) -> dict: @@ -599,12 +614,16 @@ class RougailConvert(ParserVariable): *, custom_variable_types: dict = {}, custom_family_types: dict = {}, + variable_objects = None, + family_objects = None, ) -> None: self.annotator = False self.has_namespace = False self.custom_variable_types = custom_variable_types self.custom_family_types = custom_family_types self.loaded_custom_types = None + self.variable_objects = variable_objects + self.family_objects = family_objects super().__init__(rougailconfig) def get_attributes_types( diff --git a/src/rougail/types.py b/src/rougail/types.py index d61cabfcc..be6ee00ff 100644 --- a/src/rougail/types.py +++ b/src/rougail/types.py @@ -27,6 +27,7 @@ class TypeRougailConvert(StaticRougailConvert): main_structural_directories: list[str], secret_pattern: str, default_structural_format_version: str, + structurals: list[str], ) -> None: super().__init__( False, @@ -38,23 +39,21 @@ class TypeRougailConvert(StaticRougailConvert): ) self.default_structural_format_version = default_structural_format_version self.secret_pattern = secret_pattern + self.structurals = structurals self.loaded_custom_types = {} def load_config(self) -> None: super().load_config() # self.add_extra_options = self.add_extra_options self.sort_structural_files_all = False - self.structurals = ["directory"] def rougail_type(rougailconfig): - types = rougailconfig["types"] - if not types: - return {"custom_variable_types": {}, "custom_family_types": {}} convert = TypeRougailConvert( - types, + rougailconfig["types"], rougailconfig["secret_manager.pattern"], rougailconfig["default_structural_format_version"], + rougailconfig["step.structural"], ) convert.init() convert.parse_directories() @@ -79,6 +78,8 @@ def rougail_type(rougailconfig): else: custom_family_types[typ] = data return { + "variable_objects": convert.variable_objects, + "family_objects": convert.family_objects, "custom_variable_types": custom_variable_types, "custom_family_types": custom_family_types, }