Library Usage

The library can be used to validate Arkindex workers configurations in two ways:

  • validate a Python dictionary

  • validate a YAML file available at a local path

General installation

You need to install the python package teklia-worker-configuration

pip install teklia-worker-configuration

Python dictionary

To validate a dictionary holding a potential worker configuration, use the method worker_configuration.validator.validate:

from worker_configuration.validator import validate

config = {
    "name": "Test",
}
validate(config)

If the payload is a valid configuration, the method returns True. You can then use your dictionary.

Otherwise an exception (yamale.yamale_error.YamaleError) is raised. A detailed explanation of all errors is available on the exception’s message attribute.

YAML File

To validate a local YAML file holding a potential worker configuration, use the method worker_configuration.validator.validate_file:

It requires the usage of a pathlib.Path instance to specify the YAML file path.

from worker_configuration.validator import validate_file
from pathlib import Path

config = validate_file(Path("./test.yaml"))

If the payload is a valid configuration, the method returns the worker configuration as a Python dict.

Otherwise an exception (yamale.yamale_error.YamaleError) is raised. A detailed explanation of all errors is available on the exception’s message attribute.

Handling errors

The YamaleError implementation also holds a list of all results (both valid and invalid), which you can inspect when an error arise to debug the parser behaviour.

As we only support processing one configuration file at a time, you should always get a single result in that list.

Example:

from worker_configuration.validator import validate_file
from pathlib import Path
from yamale import YamaleError

try:
    validate_file(Path("./test.yaml"))
except YamaleError as e:
    print(f"Parser failed: {e}")
    result = e.results.pop()
    assert not result.isValid()

    for error in result.errors:
        print(f"[ERROR] Parser says: {error}")