From 7f62590085c5f6bbac035ad9e3fb31d2cba11f50 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 28 Sep 2025 15:47:42 +0200 Subject: [PATCH] feat: default value for a calculated variable with an unknown optional variable --- src/rougail_tests/utils.py | 63 +++++++++++++------ ...sion => default_structural_format_version} | 0 ...sion => default_structural_format_version} | 0 .../rougail/00-base.yml | 17 +++++ .../rougail/00-base.yml | 2 +- structures/00_6integer/rougail/00-base.yml | 18 ++++++ structures/00_6ip/rougail/00-base.yml | 23 +++++++ structures/00_6network/rougail/00-base.yml | 21 +++++++ .../rougail/00-base.yml | 12 ++++ .../00_9default_integer/rougail/00-base.yml | 2 +- .../00_9default_number/rougail/00-base.yml | 12 ++++ .../rougail/00-base.yml | 6 +- .../rougail/00-base.yml | 13 ++++ .../rougail/00-base.yml | 8 +++ .../rougail/00-base.yml | 37 +++++++++++ structures/04_5validators/rougail/00-base.yml | 4 +- .../rougail/00-base.yml | 6 +- .../rougail/00-base.yml | 2 +- .../rougail/00-base.yml | 13 ++++ .../rougail/00-base.yml | 2 +- .../rougail/00-base.yml | 4 +- .../rougail/00-base.yml | 24 +++++++ .../force_namespace | 0 .../rougail/00-base.yml | 2 +- .../force_namespace | 0 .../rougail/00-base.yml | 15 +++++ .../rougail/00-base.yml | 2 + 27 files changed, 273 insertions(+), 35 deletions(-) rename structures/00_0no_variable_default_version/{default_dictionary_format_version => default_structural_format_version} (100%) rename structures/00_0no_variable_remove_version/{default_dictionary_format_version => default_structural_format_version} (100%) create mode 100644 structures/00_2default_calculated_variable/rougail/00-base.yml create mode 100644 structures/00_6integer/rougail/00-base.yml create mode 100644 structures/00_6ip/rougail/00-base.yml create mode 100644 structures/00_6network/rougail/00-base.yml create mode 100644 structures/00_9default_calculation_multi_optional_default/rougail/00-base.yml create mode 100644 structures/00_9default_number/rougail/00-base.yml rename structures/{01_6number_multi => 01_6integer_multi}/rougail/00-base.yml (90%) create mode 100644 structures/01_9choice_variable_optional/rougail/00-base.yml create mode 100644 structures/04_0type_param_integer/rougail/00-base.yml create mode 100644 structures/04_5disabled_calculation_optional_default/rougail/00-base.yml create mode 100644 structures/40_2leadership_calculation_index_2/rougail/00-base.yml create mode 100644 structures/44_4disabled_calcultion_follower_index/rougail/00-base.yml rename structures/{40_2leadership_calculation_index => 60_0family_dynamic_jinja_integer_empty}/force_namespace (100%) rename structures/{60_0family_dynamic_jinja_number_empty => 60_0family_dynamic_jinja_integer_empty}/rougail/00-base.yml (96%) delete mode 100644 structures/60_0family_dynamic_jinja_number_empty/force_namespace create mode 100644 structures/60_0family_dynamic_variable_optional/rougail/00-base.yml diff --git a/src/rougail_tests/utils.py b/src/rougail_tests/utils.py index 5ab0371..dbddb46 100644 --- a/src/rougail_tests/utils.py +++ b/src/rougail_tests/utils.py @@ -1,3 +1,4 @@ +from tiramisu import owners from pathlib import Path from rougail import RougailConfig @@ -12,36 +13,49 @@ def get_funcs(): return [str(test) for test in sorted((Path(__file__).parent.parent.parent / 'funcs').iterdir()) if test.name.endswith('.py')] -def get_rougail_config(test_dir, namespace=False): +def get_rougail_config(test_dir, namespace=False, relative_to=None): rougailconfig = RougailConfig rougailconfig['functions_files'] = get_funcs() - dirs = [str(test_dir / 'rougail')] + if relative_to: + dirs = [str((test_dir / 'rougail').relative_to(relative_to, walk_up=True))] + else: + dirs = [str((test_dir / 'rougail'))] subfolder = test_dir / 'rougail2' if subfolder.is_dir(): - dirs.append(str(subfolder)) - rougailconfig['main_dictionaries'] = dirs + if relative_to: + dirs.append(str(subfolder.relative_to(relative_to, walk_up=True))) + else: + dirs.append(str(subfolder)) + rougailconfig['main_structural_directories'] = dirs if namespace: - rougailconfig['main_namespace'] = 'Rougail' if (test_dir / 'force_no_namespace').is_file(): return None + rougailconfig['main_namespace'] = 'Rougail' else: - rougailconfig['main_namespace'] = None if (test_dir / 'force_namespace').is_file(): return None - extra_dictionaries = {} + rougailconfig['main_namespace'] = None + if (test_dir / 'default_structural_format_version').is_file(): + rougailconfig["default_structural_format_version"] = "1.1" + else: + rougailconfig["default_structural_format_version"] = None + extra_namespaces = {} extras = list(test_dir.iterdir()) extras.sort() for extra in extras: if extra.name in ['rougail', 'rougail2', 'file']: continue if extra.is_dir(): - extra_dictionaries[extra.name] = [str(extra)] - if extra_dictionaries: + if relative_to: + extra_namespaces[extra.name] = [str(extra.relative_to(relative_to, walk_up=True))] + else: + extra_namespaces[extra.name] = [str(extra)] + if extra_namespaces: if not namespace: return None - rougailconfig['extra_dictionaries'] = extra_dictionaries + rougailconfig['extra_namespaces'] = extra_namespaces else: - rougailconfig['extra_dictionaries'] = {} + rougailconfig['extra_namespaces'] = {} rougailconfig['custom_types']['custom'] = CustomOption # rougailconfig['tiramisu_cache'] = "cache.py" return rougailconfig @@ -56,7 +70,7 @@ def get_values_for_config(config, specify_dynamic_id=True, level="all", use_unre uconfig = config excludes = [] get_excludes(uconfig, excludes) - config.property.read_only() + config.property.read_write() root_config = uconfig if level == 'all': only = False @@ -111,15 +125,15 @@ def get_value(variable, index, excludes, config, use_unrestraint): elif variable.type() == 'choice': tests = variable.value.list() elif variable.type() == 'network address': - if variable.extra('_cidr'): - tests = ['192.168.1.0/24', '10.0.0.0/24'] + if variable.extra('cidr'): + tests = ['192.168.1.6/32', '10.0.0.0/24'] else: tests = ['192.168.1.0', '10.0.0.0'] elif variable.type() == 'netmask address': tests = ['255.255.255.0', '255.255.0.0'] elif variable.type() == 'IP': - if variable.extra('_cidr'): - tests = ['192.168.1.6/32', '10.0.10.0/24'] + if variable.extra('cidr'): + tests = ['192.168.1.6/24', '10.0.10.0/24'] else: tests = ['192.168.1.6', '10.0.10.10'] elif variable.type() == 'email address': @@ -137,16 +151,25 @@ def get_value(variable, index, excludes, config, use_unrestraint): tests = ['string1', 'string2', 'string3'] if not variable.ismulti(): tests = tests[0] + elif variable.isleader() and variable.owner.get() == owners.default: + len_leader = len(variable.value.get()) + if len_leader: + for idx in range(len_leader - 1, -1, -1): + variable.value.pop(idx) elif index is not None and variable.isfollower() and variable.issubmulti() is False: if len(tests) > index: tests = tests[index] else: tests = tests[0] - if not use_unrestraint: - config.property.read_write() +# if not use_unrestraint: +# config.property.read_write() variable.value.set(tests) - if not use_unrestraint: - config.property.read_only() + if variable.index() is None: + variable.information.set('loaded_from', 'loaded from rougail-test') + else: + no_index_variable = config.option(variable.path()).information.set(f'loaded_from_{index}', 'loaded from rougail-test') +# if not use_unrestraint: +# config.property.read_only() # if tests == None: # tests = "" if index is not None and variable.isleader(): diff --git a/structures/00_0no_variable_default_version/default_dictionary_format_version b/structures/00_0no_variable_default_version/default_structural_format_version similarity index 100% rename from structures/00_0no_variable_default_version/default_dictionary_format_version rename to structures/00_0no_variable_default_version/default_structural_format_version diff --git a/structures/00_0no_variable_remove_version/default_dictionary_format_version b/structures/00_0no_variable_remove_version/default_structural_format_version similarity index 100% rename from structures/00_0no_variable_remove_version/default_dictionary_format_version rename to structures/00_0no_variable_remove_version/default_structural_format_version diff --git a/structures/00_2default_calculated_variable/rougail/00-base.yml b/structures/00_2default_calculated_variable/rougail/00-base.yml new file mode 100644 index 0000000..303f84a --- /dev/null +++ b/structures/00_2default_calculated_variable/rougail/00-base.yml @@ -0,0 +1,17 @@ +--- +version: 1.1 + +var1: + description: a first variable + multi: true + type: domainname + params: + allow_ip: true + +var2: + description: a second variable + multi: true + type: domainname + default: + type: variable + variable: _.var1 diff --git a/structures/00_6choice_calculation/rougail/00-base.yml b/structures/00_6choice_calculation/rougail/00-base.yml index 1d03c46..4e5d7cd 100644 --- a/structures/00_6choice_calculation/rougail/00-base.yml +++ b/structures/00_6choice_calculation/rougail/00-base.yml @@ -8,5 +8,5 @@ var: {% for n in trange(0, 10) %} {{ n }} {% endfor %} - return_type: 'number' + return_type: 'integer' description: choices is 0 to 9 diff --git a/structures/00_6integer/rougail/00-base.yml b/structures/00_6integer/rougail/00-base.yml new file mode 100644 index 0000000..a1e5da9 --- /dev/null +++ b/structures/00_6integer/rougail/00-base.yml @@ -0,0 +1,18 @@ +--- +version: '1.1' +var1: 0 # the first variable +var2: + description: the second variable + default: 0 +var3: + description: the third variable + type: integer + default: 0 +var4: 10 # this forth variable +var5: + description: the fifth variable + default: 10 +var6: + description: the sixth variable + type: integer + default: 10 diff --git a/structures/00_6ip/rougail/00-base.yml b/structures/00_6ip/rougail/00-base.yml new file mode 100644 index 0000000..6333e6d --- /dev/null +++ b/structures/00_6ip/rougail/00-base.yml @@ -0,0 +1,23 @@ +%YAML 1.2 +--- +version: 1.1 + +var1: + description: an IP + type: ip + default: 1.1.1.1 + +var2: + description: an IP in CIDR format + type: ip + params: + cidr: true + default: 1.1.1.1/24 + examples: + - 192.168.0.128/25 + +var3: + description: an IP in CIDR format with obsolete CIDR type + type: cidr + default: 1.1.1.1/24 +... diff --git a/structures/00_6network/rougail/00-base.yml b/structures/00_6network/rougail/00-base.yml new file mode 100644 index 0000000..3a03bf4 --- /dev/null +++ b/structures/00_6network/rougail/00-base.yml @@ -0,0 +1,21 @@ +%YAML 1.2 +--- +version: 1.1 + +var1: + description: an network + type: network + default: 1.1.1.0 + +var2: + description: an network in CIDR format + type: network + params: + cidr: true + default: 1.1.1.0/24 + +var3: + description: an network in CIDR format with obsolete CIDR type + type: network_cidr + default: 1.1.1.0/24 +... diff --git a/structures/00_9default_calculation_multi_optional_default/rougail/00-base.yml b/structures/00_9default_calculation_multi_optional_default/rougail/00-base.yml new file mode 100644 index 0000000..ccabe29 --- /dev/null +++ b/structures/00_9default_calculation_multi_optional_default/rougail/00-base.yml @@ -0,0 +1,12 @@ +--- +version: 1.1 +my_variable: + default: val1 +my_calculated_variable: + multi: true + default: + - variable: _.my_variable + optional: true + - variable: _.my_variable_unexists + optional: true + default: value diff --git a/structures/00_9default_integer/rougail/00-base.yml b/structures/00_9default_integer/rougail/00-base.yml index 9513e4b..493007f 100644 --- a/structures/00_9default_integer/rougail/00-base.yml +++ b/structures/00_9default_integer/rougail/00-base.yml @@ -8,5 +8,5 @@ var: {% for item in trange(0, 10) %} {{ item }} {%- endfor %} - return_type: number + return_type: integer description: choice for 0 to 9 diff --git a/structures/00_9default_number/rougail/00-base.yml b/structures/00_9default_number/rougail/00-base.yml new file mode 100644 index 0000000..9513e4b --- /dev/null +++ b/structures/00_9default_number/rougail/00-base.yml @@ -0,0 +1,12 @@ +--- +version: '1.1' +var: + description: a variable + default: 9 + choices: + jinja: |+ + {% for item in trange(0, 10) %} + {{ item }} + {%- endfor %} + return_type: number + description: choice for 0 to 9 diff --git a/structures/01_6number_multi/rougail/00-base.yml b/structures/01_6integer_multi/rougail/00-base.yml similarity index 90% rename from structures/01_6number_multi/rougail/00-base.yml rename to structures/01_6integer_multi/rougail/00-base.yml index daa918b..688a74d 100644 --- a/structures/01_6number_multi/rougail/00-base.yml +++ b/structures/01_6integer_multi/rougail/00-base.yml @@ -8,7 +8,7 @@ var2: - 0 var3: description: the third variable - type: number + type: integer default: - 0 var4: # the forth variable @@ -19,7 +19,7 @@ var5: - 10 var6: description: the sixth variable - type: number + type: integer default: - 10 @@ -30,7 +30,7 @@ var7: - 0 var8: description: the eighth variable - type: number + type: integer multi: true default: - 0 diff --git a/structures/01_9choice_variable_optional/rougail/00-base.yml b/structures/01_9choice_variable_optional/rougail/00-base.yml new file mode 100644 index 0000000..3344ce1 --- /dev/null +++ b/structures/01_9choice_variable_optional/rougail/00-base.yml @@ -0,0 +1,13 @@ +--- +version: 1.1 + +variable: + description: a variable + choices: + variable: _.unknown_variable + optional: true + default: + - a + - b + - c + default: c diff --git a/structures/04_0type_param_integer/rougail/00-base.yml b/structures/04_0type_param_integer/rougail/00-base.yml new file mode 100644 index 0000000..016f86f --- /dev/null +++ b/structures/04_0type_param_integer/rougail/00-base.yml @@ -0,0 +1,8 @@ +--- +version: '1.1' +int: + description: A limited integer + default: 10 + params: + min_integer: 0 + max_integer: 100 diff --git a/structures/04_5disabled_calculation_optional_default/rougail/00-base.yml b/structures/04_5disabled_calculation_optional_default/rougail/00-base.yml new file mode 100644 index 0000000..1652f15 --- /dev/null +++ b/structures/04_5disabled_calculation_optional_default/rougail/00-base.yml @@ -0,0 +1,37 @@ +%YAML 1.2 +--- +version: 1.1 + +condition: false # a condition + +var1: + description: a first variable + mandatory: false + hidden: + variable: _.unknown + optional: true + +var2: + description: a first variable + mandatory: false + hidden: + variable: _.unknown + optional: true + default: true + +var3: + description: a second variable + mandatory: false + hidden: + variable: _.condition + optional: true + default: true + +var4: + description: a forth variable + mandatory: false + hidden: + variable: _.condition + optional: true + default: false +... diff --git a/structures/04_5validators/rougail/00-base.yml b/structures/04_5validators/rougail/00-base.yml index ca145a5..2aeef9d 100644 --- a/structures/04_5validators/rougail/00-base.yml +++ b/structures/04_5validators/rougail/00-base.yml @@ -1,8 +1,8 @@ --- version: '1.1' int: - description: A number - type: number + description: An integer + type: integer validators: - jinja: | {% if _.int > 100 %} diff --git a/structures/24_7validators_variable_optional/rougail/00-base.yml b/structures/24_7validators_variable_optional/rougail/00-base.yml index 7a000ba..1c89731 100644 --- a/structures/24_7validators_variable_optional/rougail/00-base.yml +++ b/structures/24_7validators_variable_optional/rougail/00-base.yml @@ -2,8 +2,8 @@ version: '1.1' general: # a family int: - description: a first number - type: number + description: a first integer + type: integer test: - 5 validators: @@ -25,4 +25,4 @@ general: # a family variable: _.int3 optional: true description: int and int3 must be different - int2: 1 # a second number + int2: 1 # a second integer diff --git a/structures/40_2leadership_calculation_index/rougail/00-base.yml b/structures/40_2leadership_calculation_index/rougail/00-base.yml index 17989d1..796563e 100644 --- a/structures/40_2leadership_calculation_index/rougail/00-base.yml +++ b/structures/40_2leadership_calculation_index/rougail/00-base.yml @@ -9,6 +9,6 @@ leader: - c follower1: description: a follower - type: number + type: integer default: type: index diff --git a/structures/40_2leadership_calculation_index_2/rougail/00-base.yml b/structures/40_2leadership_calculation_index_2/rougail/00-base.yml new file mode 100644 index 0000000..30e3519 --- /dev/null +++ b/structures/40_2leadership_calculation_index_2/rougail/00-base.yml @@ -0,0 +1,13 @@ +--- +version: '1.1' +leader: + description: a leadership + type: leadership + leader: # a leader + - a + - b + - c + follower1: + description: a follower + default: + type: index diff --git a/structures/40_2leadership_calculation_param_index/rougail/00-base.yml b/structures/40_2leadership_calculation_param_index/rougail/00-base.yml index 2a63e40..f390e47 100644 --- a/structures/40_2leadership_calculation_param_index/rougail/00-base.yml +++ b/structures/40_2leadership_calculation_param_index/rougail/00-base.yml @@ -9,7 +9,7 @@ leader: - c follower1: description: a follower - type: number + type: integer default: jinja: '{{ index }}' params: diff --git a/structures/40_8calculation_integer/rougail/00-base.yml b/structures/40_8calculation_integer/rougail/00-base.yml index f51c098..13a2dda 100644 --- a/structures/40_8calculation_integer/rougail/00-base.yml +++ b/structures/40_8calculation_integer/rougail/00-base.yml @@ -3,14 +3,14 @@ version: '1.1' bool: false # a boolean variable int1: description: first integer variable - type: number + type: integer default: jinja: | {% if rougail.bool %}1{% else %}2{% endif %} description: if bool returns 1 otherwise return 2 int2: description: second integer variable - type: number + type: integer default: jinja: | {% if not rougail.bool %}3{% else %}4{% endif %} diff --git a/structures/44_4disabled_calcultion_follower_index/rougail/00-base.yml b/structures/44_4disabled_calcultion_follower_index/rougail/00-base.yml new file mode 100644 index 0000000..66ec4f0 --- /dev/null +++ b/structures/44_4disabled_calcultion_follower_index/rougail/00-base.yml @@ -0,0 +1,24 @@ +--- +version: '1.1' + +leadership: + description: a leadership + type: leadership + + leader: + description: aleader + default: + - a + - b + + follower: + description: a follower + default: value + disabled: + jinja: | + {% if not index %} + the first follower + {% endif %} + params: + index: + type: index diff --git a/structures/40_2leadership_calculation_index/force_namespace b/structures/60_0family_dynamic_jinja_integer_empty/force_namespace similarity index 100% rename from structures/40_2leadership_calculation_index/force_namespace rename to structures/60_0family_dynamic_jinja_integer_empty/force_namespace diff --git a/structures/60_0family_dynamic_jinja_number_empty/rougail/00-base.yml b/structures/60_0family_dynamic_jinja_integer_empty/rougail/00-base.yml similarity index 96% rename from structures/60_0family_dynamic_jinja_number_empty/rougail/00-base.yml rename to structures/60_0family_dynamic_jinja_integer_empty/rougail/00-base.yml index 85367d2..4b23203 100644 --- a/structures/60_0family_dynamic_jinja_number_empty/rougail/00-base.yml +++ b/structures/60_0family_dynamic_jinja_integer_empty/rougail/00-base.yml @@ -4,7 +4,7 @@ var: description: a suffix variable multi: true mandatory: false - type: number + type: integer test: - 1 - 2 diff --git a/structures/60_0family_dynamic_jinja_number_empty/force_namespace b/structures/60_0family_dynamic_jinja_number_empty/force_namespace deleted file mode 100644 index e69de29..0000000 diff --git a/structures/60_0family_dynamic_variable_optional/rougail/00-base.yml b/structures/60_0family_dynamic_variable_optional/rougail/00-base.yml new file mode 100644 index 0000000..0106fb7 --- /dev/null +++ b/structures/60_0family_dynamic_variable_optional/rougail/00-base.yml @@ -0,0 +1,15 @@ +%YAML 1.2 +--- +version: 1.1 + +"dyn{{ identifier }}": + description: a dynamic family + dynamic: + variable: _.unknown_var + optional: true + default: + - a + - b + + var: val # a variable inside dynamic family +... diff --git a/structures/60_6family_dynamic_sub_dynamic_empty/rougail/00-base.yml b/structures/60_6family_dynamic_sub_dynamic_empty/rougail/00-base.yml index 3e7fa31..2d75041 100644 --- a/structures/60_6family_dynamic_sub_dynamic_empty/rougail/00-base.yml +++ b/structures/60_6family_dynamic_sub_dynamic_empty/rougail/00-base.yml @@ -20,7 +20,9 @@ var: default: jinja: | {% for val in __.var %} + {% if val is not none %} t{{ val }} + {% endif %} {% endfor %} description: add 't' to each var value