bluemira.base.reactor_config ============================ .. py:module:: bluemira.base.reactor_config .. autoapi-nested-parse:: Class to hold parameters and config values. Attributes ---------- .. autoapisummary:: bluemira.base.reactor_config._PARAMETERS_KEY bluemira.base.reactor_config._FILEPATH_PREFIX bluemira.base.reactor_config._FILEPATH_EXPANSION_PREFIX Classes ------- .. autoapisummary:: bluemira.base.reactor_config.ConfigParams bluemira.base.reactor_config.ReactorConfig Module Contents --------------- .. py:class:: ConfigParams Container for the global and local parameters of a `ReactorConfig`. .. py:attribute:: global_params :type: bluemira.base.parameter_frame.typed.ParameterFrameT .. py:attribute:: local_params :type: dict[str, bluemira.base.parameter_frame._parameter.ParamDictT] .. py:data:: _PARAMETERS_KEY :value: 'params' .. py:data:: _FILEPATH_PREFIX :value: '$path:' .. py:data:: _FILEPATH_EXPANSION_PREFIX :value: '$path_expand:' .. py:class:: 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). :param config_path: The path to the config JSON file or a dict of the data. :param global_params_type: The ParameterFrame type for the global params. :param warn_on_duplicate_keys: Print a warning when duplicate keys are found, whose value will be overwritten. :param warn_on_empty_local_params: Print a warning when the local params for some args are empty, when calling params_for(args) :param warn_on_empty_config: Print a warning when the config for some args are empty, when calling config_for(args) .. rubric:: Example .. code-block:: python 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 ) .. py:attribute:: warn_on_duplicate_keys :value: False .. py:attribute:: warn_on_empty_local_params :value: False .. py:attribute:: warn_on_empty_config :value: False .. py:attribute:: config_data .. py:attribute:: global_params .. py:method:: __str__() -> str Returns config_data as a nicely pretty formatted string. :returns: The pretty formatted string of the config_data. .. py:method:: _warn_or_debug_log(msg: str, *, warn: bool = False) -> None :staticmethod: .. py:method:: 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. :param component_name: The component name, must match a key in the config :param \*args: 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* * :func:`bluemira.base.parameter_frame._frame.make_parameter_frame` * *helper function to convert it into a typed ParameterFrame.* .. py:method:: 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. :param component_name: The component name, must match a key in the config :param \*args: 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. :rtype: The extracted config. .. py:method:: _read_or_return(config_path: str | pathlib.Path | dict) -> dict :staticmethod: .. py:method:: _read_json_file(path: pathlib.Path | str) -> dict :staticmethod: .. py:method:: _pprint_dict(d: dict) -> str :staticmethod: .. py:method:: _warn_on_duplicate_keys(shared_key: str, arg: str, existing_value) .. py:method:: _check_args_are_strings(args: collections.abc.Iterable[str]) :staticmethod: .. py:method:: _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. .. rubric:: Notes This mutates the passed in dict. .. py:method:: _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 .. rubric:: Notes If the value is not a path, returns the value and the passed in rel_path. .. py:method:: _extract(arg_keys: tuple[str], *, is_config: bool = True) -> dict .. py:method:: _expand_filepath_if_needed(value: str, config_dir: pathlib.Path) -> str :staticmethod: Checks if the value has a path expansion directive and expands it to the absolute path with respect to the config directory. :rtype: Absolute path as str