config_manager module
Load, validate and post-process the configuration.
- 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 (
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 aoverridesub-dict is not found.override (
dict[str,dict[str,Any]] |None, default:None) – To override entries in theTOML.conf_specs_t (
type[ConfSpec], default:<class 'lightwin.config.full_specs.ConfSpec'>) – The specifications that theTOMLmust match to be accepted.
- Return type:
- Returns:
A dictionary holding all the keyword arguments that will be passed to LightWin objects, eg
beam_calculatorwill be passed toBeamCalculator.
- _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 wholeTOMLfile.config_keys (
dict[str,str]) – Keys will be the keys of the output. Values are the name of the tables in the configuration file. Ifconfig_keys = {"beam": "proton_beam"}, the output will look like{"beam": {<content of [proton_beam]>}}.warn_mismatch (
bool) – Check if there are discrepancies betweenoverrideand 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.
- Return type:
- 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.
- 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 (
Path) – Where to save theTOML.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-existingTOMLcan be overwritten. The default is False, in which case an error will be raised.original_toml_folder (
Path|None, default:None) – Where the originalTOMLwas; this is used to resolve paths relative to this location.
- Return type: