rougail/docs/fill.rst

588 lines
12 KiB
ReStructuredText
Raw Normal View History

2023-12-17 20:25:53 +01:00
Calculated default values
==============================
Synopsis
-----------
A value can be calculated. In this case we have four possibilities:
- calculation via Jinja
- calculation via a variable
- calculation via information
- calculation via a suffix: in the case of a variable in a dynamic family
- calculation via an index: in the case of a follower variable
If the user modifies the value of the variable, the default value is no longer used, so the calculation is no longer carried out. This is also the case if the variable has the `auto_save` attribute.
On the other hand, if the variable is hidden (with the `hidden` parameter), it is the default value that is used and not the value customized by the user.
.. note:: A follower variable cannot be calculated automatically.
Parameters
--------------
Depending on the types of calculation, the parameters will be different:
2024-10-14 19:17:44 +02:00
.. list-table::
2023-12-17 20:25:53 +01:00
:widths: 15 25 20 15
:header-rows: 1
2024-10-14 19:17:44 +02:00
* - Calculation type
2023-12-17 20:25:53 +01:00
- Parameter
- Comments
- Sample
2024-10-14 19:17:44 +02:00
* -
- **type**
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
- Type of calculation, possible values are: jinja, variable, information, suffix or index
- jinja
2024-10-14 19:17:44 +02:00
* - Jinja
2023-12-17 20:25:53 +01:00
- **jinja**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
- Template Jinja. For a multiple variable, each line represents a value.
2024-10-14 19:17:44 +02:00
- `{% if rougail.variable %}`
`{{ rougail.variable }}`
2023-12-17 20:25:53 +01:00
2024-10-14 19:17:44 +02:00
`{% endif %}`
* - Jinja
- **params**
2023-12-17 20:25:53 +01:00
2024-10-14 19:17:44 +02:00
`list`
2023-12-17 20:25:53 +01:00
- Additional parameters passed to the Jinja template
2024-10-14 19:17:44 +02:00
-
2023-12-17 20:25:53 +01:00
* - Variable (`mandatory`)
Information
2024-10-14 19:17:44 +02:00
- **variable**
2023-12-17 20:25:53 +01:00
`string`
- Name of associated variable
2024-10-14 19:17:44 +02:00
- rougail.variable
2023-12-17 20:25:53 +01:00
* - Variable
2024-10-14 19:17:44 +02:00
- **propertyerror**
2023-12-17 20:25:53 +01:00
`boolean`
- If access to the variable is not possible due to a property (for example `disabled`) by default an error is returned. If the attribute is `false`, the calculated value is empty.
**Default value:** `true`
2024-10-14 19:17:44 +02:00
- false
2023-12-17 20:25:53 +01:00
* - Information
- **information**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
- Name of the information whose value we want to retrieve.
- doc
In the case of a Jinja type calculation, it is possible to have parameters.
There are two types of parameter:
- the standard parameters (string, boolean, integer, null), in this case just do: "key: value"
- the advanced settings:
- parameter via a variable
- parameter via an information
- parameter via a suffix: in the case of a variable in a dynamic family
- parameter via an index: in the case of a follower variable
2024-10-14 19:17:44 +02:00
.. list-table::
2023-12-17 20:25:53 +01:00
:widths: 15 25 20 15
:header-rows: 1
2024-10-14 19:17:44 +02:00
* - Parameter type
2023-12-17 20:25:53 +01:00
- Parameter
- Comments
- Sample
2024-10-14 19:17:44 +02:00
* -
2023-12-17 20:25:53 +01:00
- **name**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
- parameter's name
2024-10-14 19:17:44 +02:00
- my_param
* -
2023-12-17 20:25:53 +01:00
- **type**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
- parameter's type, possible values are: variable, information, suffix or index
- suffix
* - Variable
- **variable**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
2024-10-14 19:17:44 +02:00
- Variable's name
2023-12-17 20:25:53 +01:00
- rougail.variable
* - Variable (`mandatory`) information
2024-10-14 19:17:44 +02:00
- **propertyerror**
2023-12-17 20:25:53 +01:00
`boolean`
- If access to the variable is not possible due to a property (for example `disabled`) by default an error is returned. If the attribute is `False`, the parameter is not passed to the Jinja template.
- **Default value**: `True`
2024-10-14 19:17:44 +02:00
* - Variable
2023-12-17 20:25:53 +01:00
- **optional**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`boolean`
2024-10-14 19:17:44 +02:00
- The variable may not exist depending on YAML file imports.
2023-12-17 20:25:53 +01:00
If the optional parameter is `True`, the parameter will simply be deleted if the variable does not exist.
2024-10-14 19:17:44 +02:00
Default value : `False`
2023-12-17 20:25:53 +01:00
- True
* - Information
- **information**
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`string`
2024-10-14 19:17:44 +02:00
2023-12-17 20:25:53 +01:00
`mandatory`
- Name of the information whose value we want to retrieve.
- doc
2024-02-01 22:11:40 +01:00
The variable path
-----------------
Normal family
~~~~~~~~~~~~~
The default namespace is defined in RougailConfig["variable_namespace"] with the default value "rougail".
In addition, there are extras namespaces defined with in RougailConfig["extra_dictionaries"].
Inside those namespaces we can add families and variables.
Here is an hierarchic examples:
.. code-block::
2024-10-14 19:17:44 +02:00
2024-02-01 22:11:40 +01:00
rougail
├── variable1
├── family1
│ ├── variable2
│ └── variable3
└── family2
└── subfamily1
└── variable4
extra1
└── family3
├── variable5
└── variable6
In `calculation` we can use other variables.
Here is all paths:
- rougail.variable1
- rougail.family1.variable2
- rougail.family1.variable3
- rougail.family2.subfamily1.variable4
- extra1.family3.variable5
- extra1.family3.variable6
Inside a variable's `calculation` we can use relative path. "_" means that other variable is in same family. "__" means that other variables are in parent family, and so on...
For example, in variable2's `calculation`, we can use relative path:
- __.variable1
- _.variable3
- __.family2.subfamily1.variable4
But we cannot access to extra1 variables with relative path.
Dynamic family
~~~~~~~~~~~~~~~~~~
Hire is a dynamic family "{{ suffix }}":
.. code-block::
2024-10-14 19:17:44 +02:00
2024-02-01 22:11:40 +01:00
rougail
├── variable1: ["val1", "val2"]
├── {{ suffix }}
│ ├── variable2
│ └── variable3
└── family
└── variable4
For variable2's calculation, we can use:
- rougail.{{ suffix }}.variable3
- _.variable3
In this case, we get value for "variable3" with the same suffix as "variable2".
For variable4's calculation, we have two possibility:
- retrieves all values with all suffixes:
- rougail.{{ suffix }}.variable3
- __.{{ suffix }}.variable3
- retrieves a value for a specified suffix:
- rougail.val1.variable3
- __.val1.variable3
2023-12-17 20:25:53 +01:00
Examples
-----------
Calculation via a Jinja template
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Let's start with an example from a simple Jinja template:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
type: jinja
jinja: 'no'
2024-10-14 19:17:44 +02:00
Here is a second example with a boolean variable:
2023-12-17 20:25:53 +01:00
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
type: boolean
default:
type: jinja
jinja: 'false'
And a multiple value of the number type:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
type: number
multi: true
default:
type: jinja
jinja: |
1
2
2024-10-14 19:17:44 +02:00
3
2023-12-17 20:25:53 +01:00
Let's create a variable whose value is returned by a python function:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
type: jinja
jinja: '{{ return_no() }}'
Then let's create the `return_no` function:
2024-10-14 19:17:44 +02:00
.. code-block:: python
2023-12-17 20:25:53 +01:00
def return_no():
return 'no'
An example with parameters:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
description: my description
default:
type: jinja
jinja: |
{{ param1 }}{% if param2 is defined %}_{{ param2 }}{% endif %}_{{ param3 }}
params:
param1: value
param2:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.unknown_variable
2023-12-17 20:25:53 +01:00
optional: true
param3:
type: information
information: doc
2024-02-01 22:11:40 +01:00
variable: _.my_calculated_variable
2023-12-17 20:25:53 +01:00
An example with a `suffix` type parameter:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
varname:
multi: true
default:
- val1
- val2
my_dyn_family_:
type: dynamic
dynamic:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.varname
2023-12-17 20:25:53 +01:00
description: 'Describe '
my_dyn_var:
type: string
default:
type: jinja
jinja: 'the suffix is: {{ param1 }}'
params:
param1:
type: suffix
In this example, we see a dynamic family. Two families will be created: `rougail.my_dyn_family_val1.my_dyn_var` and `rougail.my_dyn_family_val2.my_dyn_var`.
The value of the variable inside this family 'this suffix is: ' + the value of the suffix (`val1` and `val2` respectively).
An example with an index type parameter:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
family:
type: leadership
leader:
multi: true
default:
- val1
- val2
follower1:
default:
type: jinja
jinja: 'the index is: {{ param1 }}'
params:
param1:
type: index
Calculation via a variable
-----------------------------
2024-10-14 19:17:44 +02:00
Copy a variable in another:
2023-12-17 20:25:53 +01:00
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_variable:
multi: true
default:
- val1
- val2
my_calculated_variable:
multi: true
default:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_variable
2023-12-17 20:25:53 +01:00
Copy the default value from a variable, means copy type, params and multi attribute too if not define in second variable.
.. code-block:: yaml
---
version: 1.1
my_variable:
multi: true
type: domainname
params:
allow_ip: true
my_calculated_variable:
default:
type: variable
variable: _.var1
Here my_calculated_variable is a domainname variable with parameter allow_ip=True and multi to true.
2023-12-17 20:25:53 +01:00
Copy one variable to another if the source has no `property` problem:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_variable:
default: val1
disabled: true
my_calculated_variable:
multi: true
default:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_variable
2023-12-17 20:25:53 +01:00
propertyerror: false
Copy two non-multiple variables into a multiple variable:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_variable_1:
default: val1
my_variable_2:
default: val2
my_calculated_variable:
multi: true
default:
- type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_variable_1
2023-12-17 20:25:53 +01:00
- type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_variable_2
2023-12-17 20:25:53 +01:00
A variable in a dynamic family can also be used in a calculation.
For example using the variable for a particular suffix:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
varname:
multi: true
default:
- val1
- val2
my_dyn_family_:
type: dynamic
dynamic:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.varname
2023-12-17 20:25:53 +01:00
description: 'Describe '
my_dyn_var_:
type: string
default:
type: suffix
all_dyn_var:
default:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_dyn_family_val1.my_dyn_var_val1
2023-12-17 20:25:53 +01:00
In this case, we recover the value `val1`.
Second example using the variable for all suffixes:
.. code-block:: yaml
---
2024-10-14 19:17:44 +02:00
version: '1.1'
2023-12-17 20:25:53 +01:00
varname:
multi: true
default:
- val1
- val2
my_dyn_family_:
type: dynamic
dynamic:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.varname
2023-12-17 20:25:53 +01:00
description: 'Describe '
my_dyn_var_:
type: string
default:
type: suffix
all_dyn_var:
multi: true
default:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.my_dyn_family_.my_dyn_var_
2023-12-17 20:25:53 +01:00
In this case, we recover the `val1` and `val2` list.
Calculation via a suffix
---------------------------
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
varname:
multi: true
default:
- val1
- val2
my_dyn_family_:
type: dynamic
dynamic:
type: variable
2024-02-01 22:11:40 +01:00
variable: _.varname
2023-12-17 20:25:53 +01:00
description: 'Describe '
my_dyn_var_:
type: string
default:
type: suffix
Calculation via an index
--------------------------
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
family:
type: leadership
leader:
multi: true
default:
- val1
- val2
follower1:
type: number
default:
type: index
Redefinition
----------------
In a first dictionary, let's declare our variable and our calculation:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
default:
type: jinja
jinja: 'the value is calculated'
In a second dictionary, it is possible to redefine the calculation:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
redefine: true
default:
type: jinja
jinja: 'the value is redefined'
In a third dictionary, we even can delete the calculation if needed:
.. code-block:: yaml
---
version: '1.1'
2023-12-17 20:25:53 +01:00
my_calculated_variable:
redefine: true
default: null