feat: default value for a calculated variable with an unknown optional variable

This commit is contained in:
egarette@silique.fr 2025-09-28 15:47:42 +02:00
parent af473317c3
commit 7f62590085
27 changed files with 273 additions and 35 deletions

View file

@ -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():

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
...

View file

@ -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
...

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,13 @@
---
version: 1.1
variable:
description: a variable
choices:
variable: _.unknown_variable
optional: true
default:
- a
- b
- c
default: c

View file

@ -0,0 +1,8 @@
---
version: '1.1'
int:
description: A limited integer
default: 10
params:
min_integer: 0
max_integer: 100

View file

@ -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
...

View file

@ -1,8 +1,8 @@
---
version: '1.1'
int:
description: A number
type: number
description: An integer
type: integer
validators:
- jinja: |
{% if _.int > 100 %}

View file

@ -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

View file

@ -9,6 +9,6 @@ leader:
- c
follower1:
description: a follower
type: number
type: integer
default:
type: index

View file

@ -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

View file

@ -9,7 +9,7 @@ leader:
- c
follower1:
description: a follower
type: number
type: integer
default:
jinja: '{{ index }}'
params:

View file

@ -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 %}

View file

@ -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

View file

@ -4,7 +4,7 @@ var:
description: a suffix variable
multi: true
mandatory: false
type: number
type: integer
test:
- 1
- 2

View file

@ -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
...

View file

@ -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