table_spec module

Define the base objects constraining values/types of config parameters.

class TableConfSpec(configured_object, table_entry, specs, is_mandatory=True, can_have_untested_keys=False, selectkey_n_default=None, monkey_patches=None)[source]

Bases: object

Set specifications for a table, which holds several key-value pairs.

Note

This object can be subclassed for specific configuration needs, eg BeamTableConfSpec.

Parameters:
__init__(configured_object, table_entry, specs, is_mandatory=True, can_have_untested_keys=False, selectkey_n_default=None, monkey_patches=None)[source]

Set a table of properties. Correspond to a [table] in the .toml.

Parameters:
  • configured_object (str) – Name of the object that will receive associated parameters.

  • table_entry (str) – Name of the table in the .toml file, without brackets.

  • specs (Collection[KeyValConfSpec] | dict[str, Collection[KeyValConfSpec]] | dict[str, Collection[KeyValConfSpec]]) – The KeyValConfSpec objects in the current table. When the format of the table depends on the value of a key, provide a dictionary linking every possible table with the corresponding value.

  • is_mandatory (bool, optional) – If the current table must be provided. The default is True.

  • can_have_untested_keys (bool, optional) – If LightWin should remain calm when some keys are provided in the .toml but do not correspond to any KeyValConfSpec. The default is False.

  • selectkey_n_default (tuple[str, str | bool] | None, optional) – Must be given if specs is a dict. First value is name of the spec, second value is default value. We will look for this spec in the configuration file and select the proper Collection of KeyValConfSpec accordingly.

  • monkey_patches (dict[str, dict[str, Callable]] | dict[bool, dict[str, Callable]] | None, optional) – Same keys as specs, to override some default methods. The default is None.

_get_specs(toml_subdict=None)[source]

Get the proper list of KeyValConfSpec.

Used when we need to read the value of _selectkey_n_default in the .toml to choose precisely which configuration we should match.

Parameters:

toml_subdict (dict[str, Any] | None, optional) – The content of the toml file. We use it only if self._specs is not already a Collection. We look for the value of self._selectkey_n_default[0] and use it to select the proper table. If not provided, we fall back on a default value.

Return type:

list[KeyValConfSpec]

_set_specs_as_dict(toml_subdict=None)[source]

Set the dict of specifications.

Used when we need to read the value of _selectkey_n_default in the .toml to choose precisely which configuration we should match. If toml_subdict is not provided, we use a default value.

Parameters:

toml_subdict (dict[str, Any] | None, default: None)

Return type:

None

_get_proper_spec(spec_name)[source]

Get the specification for the property named spec_name.

Parameters:

spec_name (str)

Return type:

KeyValConfSpec | None

to_toml_strings(toml_subdict, original_toml_folder=None, **kwargs)[source]

Convert the given dict in string that can be put in a .toml.

Parameters:
  • toml_subdict (dict[str, Any]) – A dictionary corresponding to a .toml table.

  • original_toml_folder (pathlib.Path | None, optional) – Where the original .toml was; this is used to resolve paths relative to this location.

Returns:

All the .toml lines corresponding to the table under study.

Return type:

list[str]

_pre_treat(toml_subdict, **kwargs)[source]

Edit some values, create new ones. To call before validation.

Note

In general, the edited values will undergo the validation process.

Parameters:

toml_subdict (dict[str, Any])

Return type:

None

prepare(toml_subdict, **kwargs)[source]

Validate the config dict and edit some values.

Parameters:

toml_subdict (dict[str, Any])

Return type:

bool

_validate(toml_subdict, **kwargs)[source]

Check that key-values in toml_subdict are valid.

This method is defined to keep an implementation of the original method even when validate is overriden by a monkey patch.

Parameters:

toml_subdict (dict[str, Any])

Return type:

bool

_post_treat(toml_subdict, **kwargs)[source]

Edit some values, create new ones. To call after validation.

Note

In general, the edited values will not be validated. To handle with care.

Parameters:

toml_subdict (dict[str, Any])

Return type:

None

_make_paths_absolute(toml_subdict, toml_folder=None, **kwargs)[source]

Transform the paths to their absolute resolved version.

Parameters:
Return type:

None

_mandatory_keys_are_present(toml_keys)[source]

Ensure that all the mandatory parameters are defined.

Parameters:

toml_keys (Collection[str])

Return type:

bool

generate_dummy_dict(only_mandatory=True)[source]

Generate a default dummy dict that should let LightWin work.

Parameters:

only_mandatory (bool, default: True)

Return type:

dict[str, Any]

_apply_monkey_patches(monkey_patches)[source]

Override the base methods.

Parameters:

monkey_patches (dict[str, Callable])

Return type:

None

_remove_overriden_keys(specs)[source]

Remove the KeyValConfSpec objects to override.

Todo

Not Pythonic at all.

Parameters:

specs (Collection[KeyValConfSpec])

Return type:

list[KeyValConfSpec]