bluemira.base.reactor_config
Class to hold parameters and config values.
Attributes
Classes
Container for the global and local parameters of a ReactorConfig. |
|
Class that provides a simple interface over config JSON files and |
Module Contents
- class bluemira.base.reactor_config.ConfigParams
Container for the global and local parameters of a ReactorConfig.
- global_params: bluemira.base.parameter_frame.typed.ParameterFrameT
- local_params: dict[str, bluemira.base.parameter_frame._parameter.ParamDictT]
- bluemira.base.reactor_config._PARAMETERS_KEY = 'params'
- bluemira.base.reactor_config._FILEPATH_PREFIX = '$path:'
- bluemira.base.reactor_config._FILEPATH_EXPANSION_PREFIX = '$path_expand:'
- class bluemira.base.reactor_config.ReactorConfig(config_path: str | pathlib.Path | dict, global_params_type: type[bluemira.base.parameter_frame.typed.ParameterFrameT], *, warn_on_duplicate_keys: bool = False, warn_on_empty_local_params: bool = False, warn_on_empty_config: bool = False)
Class that provides a simple interface over config JSON files and handles overwriting multiply defined attributes.
If an attribute is defined more than once in a component, the more globally scoped value is used (global overwrites local).
- Parameters:
config_path (str | pathlib.Path | dict) – The path to the config JSON file or a dict of the data.
global_params_type (type[bluemira.base.parameter_frame.typed.ParameterFrameT]) – The ParameterFrame type for the global params.
warn_on_duplicate_keys (bool) – Print a warning when duplicate keys are found, whose value will be overwritten.
warn_on_empty_local_params (bool) – Print a warning when the local params for some args are empty, when calling params_for(args)
warn_on_empty_config (bool) – Print a warning when the config for some args are empty, when calling config_for(args)
Example
from bluemira.base.parameter_frame import Parameter, ParameterFrame @dataclass class GlobalParams(ParameterFrame): a: Parameter[int] reactor_config = ReactorConfig( { "params": {"a": {"value": 10, "unit": 'm'}}, "comp A": { "params": { "a": {"value": 5, "unit": 'm'}, "b": {"value": 5, "unit": 'm'}, }, "designer": { "params": {"a": {"value": 1, "unit": 'm'}}, "some_config": "some_value", }, "builder": { "params": { "b": {"value": 1, "unit": 'm'}, "c": {"value": 1, "unit": 'm'}, }, "another_config": "another_value", }, }, "comp B": { "params": {"b": {"value": 1, "unit": 'm'}}, "builder": { "third_config": "third_value", }, }, }, GlobalParams )
- warn_on_duplicate_keys = False
- warn_on_empty_local_params = False
- warn_on_empty_config = False
- config_data
- global_params
- __str__() str
Returns config_data as a nicely pretty formatted string.
- Returns:
The pretty formatted string of the config_data.
- Return type:
str
- static _warn_or_debug_log(msg: str, *, warn: bool = False) None
- Parameters:
msg (str)
warn (bool)
- Return type:
None
- params_for(component_name: str, *args: str) ConfigParams
Gets the params for the component_name from the config file.
These are all the values defined by a “params” key in the config file.
This will merge all multiply defined params, with global overwriting local.
- Parameters:
component_name (str) – The component name, must match a key in the config
*args (str) –
Optionally, specify the keys of nested attributes.
This will hoist the values defined in the nested attributes to the top level of the local_params dict in the returned ConfigParams object.
The args must be in the order that they appear in the config.
- Returns:
Holds the global_params (from self.global_params)
and the extracted local_params.
Use the
helper function to convert it into a typed ParameterFrame.
- Return type:
- config_for(component_name: str, *args: str) dict
Gets the config for the component_name from the config file.
These are all the values other than those defined by a “params” key in the config file.
This will merge all multiply defined values, with global overwriting local.
- Parameters:
component_name (str) – The component name, must match a key in the config
*args (str) –
Optionally, specify the keys of nested attributes.
This will hoist the values defined in the nested attributes to the top level of the returned dict.
The args must be in the order that they appear in the config.
- Return type:
The extracted config.
- static _read_or_return(config_path: str | pathlib.Path | dict) dict
- Parameters:
config_path (str | pathlib.Path | dict)
- Return type:
dict
- static _read_json_file(path: pathlib.Path | str) dict
- Parameters:
path (pathlib.Path | str)
- Return type:
dict
- static _pprint_dict(d: dict) str
- Parameters:
d (dict)
- Return type:
str
- _warn_on_duplicate_keys(shared_key: str, arg: str, existing_value)
- Parameters:
shared_key (str)
arg (str)
- static _check_args_are_strings(args: collections.abc.Iterable[str])
- Parameters:
args (collections.abc.Iterable[str])
- _expand_paths_in_dict(d: dict[str, Any], rel_path: pathlib.Path)
Expand all file paths by replacing their values with the json file’s contents.
Notes
This mutates the passed in dict.
- Parameters:
d (dict[str, Any])
rel_path (pathlib.Path)
- _extract_and_expand_file_data_if_needed(value: Any, rel_path: pathlib.Path) tuple[Any | dict, pathlib.Path]
Returns the file data and the path to the file if value is a path.
Otherwise, returns value and rel_path that was passed in.
rel_path is the path to the file that the value is in.
- Returns:
Tuple of the file data and the path to the file if value is a path.
- Raises:
FileNotFoundError – Cannot find provided path
- Parameters:
value (Any)
rel_path (pathlib.Path)
- Return type:
tuple[Any | dict, pathlib.Path]
Notes
If the value is not a path, returns the value and the passed in rel_path.
- _extract(arg_keys: tuple[str], *, is_config: bool = True) dict
- Parameters:
arg_keys (tuple[str])
is_config (bool)
- Return type:
dict
- static _expand_filepath_if_needed(value: str, config_dir: pathlib.Path) str
Checks if the value has a path expansion directive and expands it to the absolute path with respect to the config directory.
- Return type:
Absolute path as str
- Parameters:
value (str)
config_dir (pathlib.Path)