From 1d845dded27230b13cf480faaf0a66bd079f2081 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 6 Feb 2026 21:27:10 +0100 Subject: [PATCH] feat: structural data in a string --- src/rougail/structural_string/__init__.py | 115 ++++++++++++++++++++++ src/rougail/structural_string/config.py | 69 +++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 src/rougail/structural_string/__init__.py create mode 100644 src/rougail/structural_string/config.py diff --git a/src/rougail/structural_string/__init__.py b/src/rougail/structural_string/__init__.py new file mode 100644 index 000000000..1c07332cb --- /dev/null +++ b/src/rougail/structural_string/__init__.py @@ -0,0 +1,115 @@ +""" +Silique (https://www.silique.fr) +Copyright (C) 2025-2026 + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU Lesser General Public License as published by the +Free Software Foundation, either version 3 of the License, or (at your +option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see . +""" + + +from typing import List, Optional +from itertools import chain +from ruamel.yaml import YAML + +from ..tiramisu import normalize_family +from ..convert.path import Paths +#from ..error import DictConsistencyError +#from ..i18n import _ + + +class Walker: + def __init__( + self, + convert, + ) -> None: + """Parse directories content""" + self.convert = convert + self.yaml = YAML() + rougailconfig = self.convert.rougailconfig + if rougailconfig["main_namespace"]: + self.convert.has_namespace = True + self.convert.paths = Paths( + rougailconfig["main_namespace"], + rougailconfig["isolated_namespace"], + ) + self.load_with_extra( + rougailconfig["extra_namespaces"], + rougailconfig["main_namespace"], + rougailconfig["main_structural_strings"], + ) + else: + self.convert.namespace = None + namespace_path = "" + if namespace_path in self.convert.parents: + raise Exception("pfff") + for string in rougailconfig["main_structural_strings"]: + self.parse_variable( + string, + namespace_path, + ) + + def load_with_extra( + self, + extra_structures: dict, + main_namespace: str, + main_strings: Optional[List[str]] = None, + ) -> None: + directory_dict = chain( + ( + ( + main_namespace, + main_strings, + ), + ), + extra_structures.items(), + ) + for namespace, extra_objects in directory_dict: + namespace_path = normalize_family(namespace) + if namespace_path in self.convert.parents: + raise Exception("pfff") + self.convert.namespace = namespace_path + for idx, string in enumerate(extra_objects): + if not idx: + # create only for the first file + self.convert.create_namespace(namespace, namespace_path) + self.parse_variable( + string, + namespace_path, + ) + + def parse_variable( + self, + string: str, + path: str, + ) -> None: + objects = self.yaml.load(string) + if objects is None: + return + if path: + name = f'yaml file for {path}' + else: + name = 'yaml file' + version = self.convert.validate_file_version( + objects, + name, + ) + self.convert.parse_root_file( + [name], + path, + version, + objects, + ) + + +__all__ = ("Walker",) + diff --git a/src/rougail/structural_string/config.py b/src/rougail/structural_string/config.py new file mode 100644 index 000000000..a2e5f85d7 --- /dev/null +++ b/src/rougail/structural_string/config.py @@ -0,0 +1,69 @@ +""" +Silique (https://www.silique.fr) +Copyright (C) 2025-2026 + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU Lesser General Public License as published by the +Free Software Foundation, either version 3 of the License, or (at your +option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see . +""" + +from ..utils import _ + + +def get_rougail_config( + *, + backward_compatibility=True, +) -> dict: + if backward_compatibility: + main_namespace_default = "rougail" + else: + main_namespace_default = "null" + options = f""" +main_structural_strings: + description: {_("Structural files contents")} + help: {_("This variable is a list of string with YAML file format")} + multi: true + disabled: + jinja: >- + {{% if 'string' not in _.step.structural %}} + true + {{% elif cli is defined and cli.versions is defined and cli.versions %}} + true + {{% else %}} + false + {{% endif %}} + return_type: boolean + description: {_('string is not in "_.step.structural"')} + +extra_namespaces: + + strings: + description: {_("Extra structural contents")} + help: {_("This variable is a list of string with YAML file format")} + alternative_name: xc + multi: true + disabled: + jinja: >- + {{{{ 'string' not in __.step.structural }}}} + return_type: boolean + description: {_('string is not in "__.step.structural"')} +""" + return { + "name": "string", + "process": "structural", + "options": options, + "level": 15, + } + + +__all__ = "get_rougail_config" +