cliconfig.processing.create

Functions to create new processing quickly.

create_processing_value(func, processing_type='premerge', *, regex=None, tag_name=None, order=0.0, persistent=False)

Create a processing object that modifies a value in config using tag or regex.

The processing is applied on pre-merge. It triggers when the key matches the tag or the regex. The function apply flat_dict[key] = func(flat_dict[key]). You must only provide one of tag or regex. If tag is provided, the tag will be removed from the key during pre-merge.

It also possible to pass the flat config as a second argument of the function func. In this case, the function apply flat_dict[key] = func(flat_dict[key], flat_config).

Parameters:
  • func (Callable) – The function to apply to the value (and eventually the flat config) to make the new value so that: flat_dict[key] = func(flat_dict[key]) or func(flat_dict[key], flat_config)

  • processing_type (str, optional) – One of “premerge”, “postmerge”, “presave”, “postload” or “endbuild”. Timing to apply the value update. In all cases the tag is removed on pre-merge. By default “premerge”.

  • regex (Optional[str]) – The regex to match the key.

  • tag_name (Optional[str]) – The tag (without “@”) to match the key. The tag is removed on pre-merge.

  • order (int, optional) – The pre-merge order. By default 0.0.

  • persistent (bool, optional) – If True, the processing will be applied on all keys that have already matched the tag before. By nature, using regex make the processing always persistent. By default, False.

Raises:
  • ValueError – If both tag and regex are provided or if none of them are provided.

  • ValueError – If the processing type is not one of “premerge”, “postmerge”, “presave”, “postload” or “endbuild”.

Returns:

processing – The processing object with the pre-merge method.

Return type:

Processing

Examples

With the following config and 2 processings:

proc2 = create_processing_value(lambda val: -val, regex="neg_number.*",
    order=0.0)
proc1 = create_processing_value(lambda val: val + 1, tag_name="add1",
    order=1.0)

When config.yaml is merged with an other config, it will be considered before merging as:

{'number1': -1, 'number2': -1, 'number3': 0}

Using the config as a second argument of the function:

proc = create_processing_value(
    lambda val, config: eval(val, {'config': config}),
    processing_type='postmerge', tag_name='eval', persistent=False
)

After config.yaml is merged with another config, param2 will be evaluated as 2 (except if config.param1 has changed with a processing before).

create_processing_keep_property(func, regex=None, tag_name=None, premerge_order=0.0, postmerge_order=0.0, endbuild_order=0.0)

Create a processing object that keep a property from a value using tag or regex.

The pre-merge processing looks for keys that match the tag or the regex, apply the function func on the value and store the result (= the “property”): property = func(flat_dict[key]). The post-merge processing will check that the property is the same as the one stored during pre-merge. If not, it will raise a ValueError.

It also possible to pass the flat config as a second argument of the function func. In this case, the function apply property = func(flat_dict[key], flat_config).

Parameters:
  • func (Callable) – The function to apply to the value (and eventually the flat config) to define the property to keep. property = func(flat_dict[key]) or func(flat_dict[key], flat_config)

  • regex (Optional[str]) – The regex to match the key.

  • tag_name (Optional[str]) – The tag (without “@”) to match the key. The values are modified when triggering the pattern “.*@<tag_name>.*” and the tag is removed from the key.

  • premerge_order (float, optional) – The pre-merge order, by default 0.0

  • postmerge_order (float, optional) – The post-merge order, by default 0.0

  • endbuild_order (float, optional) – The end-build order, by default 0.0

Raises:

ValueError – If both tag and regex are provided or if none of them are provided.

Returns:

The processing object with the pre-merge and post-merge methods.

Return type:

Processing

Examples

A processing that enforce the types of all the parameters to be constant (equal to the type of the first value encountered):

create_processing_keep_property(type, regex=".*", premerge_order=15.0,
                                postmerge_order=15.0, endbuild_order=15.0)

A processing that protect parameters tagged with @protect from being changed:

create_processing_keep_property(lambda x: x, tag_name="protect",
                                premerge_order=15.0, postmerge_order=15.0)