tron.config.config_utils module

Utilities used for configuration parsing and validation.

class tron.config.config_utils.ConfigContext(path, nodes, command_context, namespace)

Bases: object

An object to encapsulate the context in a configuration file. Supplied to Validators to perform validation which requires knowledge of configuration outside of the immediate configuration dictionary.

build_child_context(path)

Construct a new ConfigContext based on this one.

partial = False
class tron.config.config_utils.NullConfigContext

Bases: object

static build_child_context(_)
command_context = {}
namespace = 'MASTER'
nodes = {}
partial = False
path = ''
class tron.config.config_utils.PartialConfigContext(path, namespace)

Bases: object

A context object which has only a partial context. It is missing command_context and nodes. This is likely because it is being used in a named configuration fragment that does not have access to those pieces of the configuration.

build_child_context(path)
partial = True
class tron.config.config_utils.StringFormatter(context=None)

Bases: Formatter

get_value(key, args, kwds)
class tron.config.config_utils.UniqueNameDict(fmt_string)

Bases: dict

A dict like object that throws a ConfigError if a key exists and __setitem__ is called to change the value of that key.

fmt_string - format string used to create an error message, expects a

single format argument of ‘key’

class tron.config.config_utils.Validator

Bases: object

Base class for validating a collection and creating a mutable collection from the source.

property all_keys
build_config(in_dict, config_context)

Construct the configuration by validating the contents, setting defaults, and returning an instance of the config_class.

build_context(in_dict, config_context)
cast(in_dict, _)

If your validator accepts input in different formations, override this method to cast your input into a common format.

config_class: type | None = None
defaults = {}
do_shortcut(in_dict)

Override if your validator can skip most of the validation by checking this condition. If this returns a truthy value, the validation will end immediately and return that value.

optional = False
path_name(name=None)
post_validation(valid_input, config_context)

Hook to perform additional validation steps after key validation completes.

set_defaults(output_dict, _config_context)

Set any default values for any optional values that were not specified.

property type_name

Return a string that represents the config_class being validated. This name is used for error messages, so we strip off the word Config so the name better matches what the user sees in the config.

validate(in_dict, config_context)
validate_contents(input, config_context)

Override this to validate each value in the input.

validate_extra_keys(in_dict)

Check that no unexpected keys are present.

validate_required_keys(in_dict)

Check that all required keys are present.

validators = {}
tron.config.config_utils.build_dict_name_validator(item_validator, allow_empty=False)

Build a validator which validates a list or dict, and returns a dict. Item validator must expect a “name” key, mapped to the key of the dict item

tron.config.config_utils.build_dict_value_validator(item_validator, allow_empty=False)

Build a validator which validates values of a dict, and returns a dict

tron.config.config_utils.build_enum_validator(enum)
tron.config.config_utils.build_list_of_type_validator(item_validator, allow_empty=False)

Build a validator which validates a list contains items which pass item_validator.

tron.config.config_utils.build_real_enum_validator(enum)
tron.config.config_utils.build_type_validator(validator, error_fmt)

Create a validator function using validator to validate the value. validator - a function which takes a single argument value error_fmt - a string which accepts two format variables (path, value)

Returns a function func(value, config_context) where

value - the value to validate config_context - a ConfigContext object Returns True if the value is valid

tron.config.config_utils.unique_names(fmt_string, *seqs)

Validate that each object in all sequences has a unique name.

tron.config.config_utils.valid_bool(value, config_context)
tron.config.config_utils.valid_dict(value, config_context)
tron.config.config_utils.valid_exit_code(value, config_context, *, allow_negative=True)
tron.config.config_utils.valid_float(value, config_context, allow_negative=False)
tron.config.config_utils.valid_identifier(value, config_context)
tron.config.config_utils.valid_int(value, config_context, allow_negative=False)
tron.config.config_utils.valid_list(value, config_context)
tron.config.config_utils.valid_name_identifier(value, config_context)
tron.config.config_utils.valid_number(type_func, value, config_context, allow_negative=False)
tron.config.config_utils.valid_string(value, config_context)
tron.config.config_utils.valid_time(value, config_context)
tron.config.config_utils.valid_time_delta(value, config_context)