config_manager module
Load, validate and post-process the configuration.
Todo
Method to generate the .csv files of documentation from the tables, i.e. docs/manual/configuration_entries
- exception ConfigFileNotFoundError[source]
Bases:
FileNotFoundErrorCustom exception raised when the configuration file is not found.
- exception InvalidTomlSyntaxError[source]
Bases:
ValueErrorCustom 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 (pathlib.Path | str | importlib.resources.abc.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, optional) – Raise a warning if a key in a
overridesub-dict is not found.override (dict[str, dict[str, Any]] | None, optional) – To override entries in the
TOML. If not provided, we keep defaults.conf_specs_t (type[ConfSpec], optional) – The specifications that the
TOMLmust match to be accepted. If not provided, we take a default.
- Returns:
toml_fulldict – A dictionary holding all the keyword arguments that will be passed to LightWin objects, eg
beam_calculatorwill be passed toBeamCalculator.- Return type:
- _load_toml(toml_path)[source]
Load the
TOMLand extract the dicts asked by user.- Parameters:
toml_path (pathlib.Path | str | importlib.resources.abc.Traversable) – Path to the configuration file. It can be path to a real file or a resource reference.
- Returns:
raw_toml – Dictionary holding the whole
TOMLfile.- Return type:
- _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
TOMLfile.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
overrideand the keys or dicts to override inconfig_keys.override (dict[str, dict[str, Any]] | None) – To override some entries of the output dictionary, before even testing it.
- Returns:
toml_fulldict – A dictionary which keys are the keys of
config_keys, and the values are dictionaries holding corresponding table entries from the configuration file.- Return type:
- _override_some_toml_entries(toml_fulldict, warn_mismatch, **override)[source]
Override some entries before testing.
- 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
TOMLfile.- Parameters:
toml_fulldict (dict[str, dict[str, Any]]) – The configuration as a nested dictionary. The keys will be used as table entries.
toml_path (pathlib.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, optional) – If a pre-existing
TOMLcan be overwritten. The default is False, in which case an error will be raised.original_toml_folder (pathlib.Path | None, optional) – Where the original
TOMLwas; this is used to resolve paths relative to this location.
- Return type: