config_manager module

Load, validate and post-process the configuration.

exception ConfigFileNotFoundError[source]

Bases: FileNotFoundError

Custom exception raised when the configuration file is not found.

exception InvalidTomlSyntaxError[source]

Bases: ValueError

Custom exception raised for invalid TOML syntax.

process_config(toml_path, config_keys, warn_mismatch=False, override=None, conf_specs_t=<class 'lightwin.config.full_specs.ConfSpec'>)[source]

Load and test the configuration file.

Parameters:
  • toml_path (Path | str | Traversable) – Path to the configuration file. It can be path to a real file or a resource reference.

  • config_keys (dict[str, str]) – Associate the name of LightWin’s group of parameters to the entry in the configuration file.

  • warn_mismatch (bool, default: False) – Raise a warning if a key in a override sub-dict is not found.

  • override (dict[str, dict[str, Any]] | None, default: None) – To override entries in the TOML.

  • conf_specs_t (type[ConfSpec], default: <class 'lightwin.config.full_specs.ConfSpec'>) – The specifications that the TOML must match to be accepted.

Return type:

ConfigKw

Returns:

A dictionary holding all the keyword arguments that will be passed to LightWin objects, eg beam_calculator will be passed to BeamCalculator.

_load_toml(toml_path)[source]

Load the TOML and extract the dicts asked by user.

Parameters:

toml_path (Path | str | Traversable) – Path to the configuration file. It can be path to a real file or a resource reference.

Return type:

dict[str, dict[str, Any]]

Returns:

Dictionary holding the whole TOML file.

_process_toml(raw_toml, config_keys, *, warn_mismatch, override)[source]

Extract the dicts asked by user. Override some keys if requested.

Parameters:
  • raw_toml (dict[str, dict[str, Any]]) – Dictionary holding the whole TOML file.

  • config_keys (dict[str, str]) – Keys will be the keys of the output. Values are the name of the tables in the configuration file. If config_keys = {"beam": "proton_beam"} , the output will look like {"beam": {<content of [proton_beam]>}}.

  • warn_mismatch (bool) – Check if there are discrepancies between override and the keys or dicts to override in config_keys.

  • override (dict[str, dict[str, Any]] | None) – To override some entries of the output dictionary, before even testing it.

Return type:

ConfigKw

Returns:

A dictionary which keys are the keys of config_keys, and the values are dictionaries holding corresponding table entries from the configuration file.

_user_override_toml_entries(toml_fulldict, warn_mismatch, **override)[source]

Override some entries before testing.

Parameters:
Return type:

None

dict_to_toml(toml_fulldict, toml_path, conf_specs, allow_overwrite=False, original_toml_folder=None, **kwargs)[source]

Write the provided configuration dict to a TOML file.

Parameters:
  • toml_fulldict (dict[str, dict[str, Any]]) – The configuration as a nested dictionary. The keys will be used as table entries.

  • toml_path (Path) – Where to save the TOML.

  • conf_specs (ConfSpec) – Holds the template to be respected. In particular, the type of the values in the different tables.

  • allow_overwrite (bool, default: False) – If a pre-existing TOML can be overwritten. The default is False, in which case an error will be raised.

  • original_toml_folder (Path | None, default: None) – Where the original TOML was; this is used to resolve paths relative to this location.

Return type:

None

_indue_overwritting(toml_path, allow_overwrite=False)[source]

Ensure that TOML will not be overwritten if not wanted.

Parameters:
  • toml_path (Path)

  • allow_overwrite (bool, default: False)

Return type:

bool