Configuration Helpers

Module: ppc_robot_lib.utils.config

convert_to_bool(value)[source]

Convert value to a boolean.

Following strings are considered True:

  • 1

  • yes

  • true

  • y

All comparisons are case in-sensitive. Any other strings are considered False.

Other datatypes are converted using bool(value). None will be converted to False.

Parameters:

value (Any) – String value, boolean, or any value that can be converted to boolean.

Return type:

bool

Returns:

Boolean value.

convert_to_int(value)[source]

Convert a value to integer.

Parameters:

value (Any) – String value, integer, or any value that can be converted to integer.

Return type:

int | None

Returns:

Integer value, or None for None and empty strings.

env(name, default=None)[source]

Get a value of environment variable. Multiple lookups are performed, and the value can be loaded from files. If you pass <NAME>, the following lookups might be performed:

  • <NAME> - if this env is set, its value is used directly.

  • <NAME>_FILE - if this env is set, contents of the file given in the value will be loaded as value.

  • <NAME>_SECRET - if this env is set to <value>, contents of the file at /run/secrets/<value> will be loaded as value.

The lookups are performed in the order as listed, and the first value found will be used.

If no variable is set, default value will be used.

Parameters:
  • name (str) – Name of the environment variable to find.

  • default – Default value.

Return type:

str

Returns:

Variable value, or the default value.

has_env(name)[source]

Checks if the environment variable is set, or if any of the following variables is set:

  • <NAME>

  • <NAME>_FILE

  • <NAME>_SECRET

Parameters:

name (str) – Environment variable name.

Return type:

bool

Returns:

True if the environment variable is set.

secrets_dir = '/run/secrets'

Directory with secrets.

socket_address_to_tuple(host_str, default_host, default_port)[source]

Convert address with optional port to a (host, port) tuple.

Parameters:
  • host_str (str | None) – Address with : delimited port.

  • default_host (str) – Default host.

  • default_port (int) – Default port.

Return type:

tuple[str, int]

Returns:

(host, port) tuple.