feat: family or dynamic could needs type attribute

This commit is contained in:
egarette@silique.fr 2025-09-30 21:54:47 +02:00
parent 6f18fbb701
commit f1b8ea81cb
4 changed files with 65 additions and 5 deletions

View file

@ -77,6 +77,18 @@ RoundTripRepresenter.add_representer(str, represent_str)
# XXX
class RougailConvertFormatter(RougailConvert):
def parse_root_file(
self,
filename: str,
path: str,
version: str,
objects: dict,
) -> None:
self.objects = objects
super().parse_root_file(filename, path, version, objects)
class RougailOutputFormatter:
output_name = "formatter"
@ -161,7 +173,7 @@ class RougailOutputFormatter:
filename
)
self.version_name, datas = RougailUpgrade(self.rougailconfig).run(filename)
self.rougail = RougailConvert(self.rougailconfig)
self.rougail = RougailConvertFormatter(self.rougailconfig)
self.rougail.load_config()
self.rougail.init()
self.filename_str = str(filename)
@ -239,10 +251,36 @@ class RougailOutputFormatter:
attr = f"_{attr}"
family[attr] = self.object_to_yaml(attr, type_, value, False, path)
if type_ == "dynamic" or (children and type_ == "family"):
if "_type" in family:
del family["_type"]
tmp_family = family.copy()
if "_type" in tmp_family:
del tmp_family["_type"]
else:
del family["type"]
del tmp_family["type"]
for child_path in self.rougail.parents[path]:
child = self.rougail.objects
if self.rougail.has_namespace:
split_path = child_path.split('.')[1:]
else:
split_path = child_path.split('.')
for cpath in split_path:
if cpath not in child and '{{ identifier }}' in cpath:
# support format 1.0
if cpath.replace('{{ identifier }}', '') in child:
cpath = cpath.replace('{{ identifier }}', '')
elif cpath.replace('{{ identifier }}', '{{ suffix }}') in child:
cpath = cpath.replace('{{ identifier }}', '{{ suffix }}')
child = child[cpath]
tmp_family[cpath] = child
family_type = self.rougail.is_family_or_variable("", tmp_family, False, "")
if family_type == "family":
family_type = self.rougail.get_family_or_variable_type(tmp_family)
if family_type is None:
family_type = 'family'
if family_type == type_:
if "_type" in family:
del family["_type"]
else:
del family["type"]
if not set(family):
ret[name] = CommentedMap()
ret.yaml_value_comment_extend(

View file

@ -0,0 +1,13 @@
%YAML 1.2
---
version: 1.1
my_family:
type: family
dynamic:
- val1
- val2
var: true # a variable
...

View file

@ -0,0 +1,9 @@
%YAML 1.2
---
version: 1.1
my_family:
type: family
default: true
...

View file

@ -15,7 +15,7 @@ excludes = [
]
test_ok = get_structures_list(excludes)
# test_ok = [Path('../rougail-tests/structures/40_2leadership_calculation_index')]
# test_ok = [Path('../rougail-tests/structures/60_6family_dynamic_sub_dynamic_1_0_2')]
def idfn(fixture_value):