src.fairreckitlib.core.parsing.parse_config_params
This module contains functionality to parse configuration parameter(s).
Functions:
parse_config_parameters: parse multiple parameters.
parse_config_param: parse a single parameter.
trim_config_params: trim unnecessary params that are not present in the config parameters.
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)
1"""This module contains functionality to parse configuration parameter(s). 2 3Functions: 4 5 parse_config_parameters: parse multiple parameters. 6 parse_config_param: parse a single parameter. 7 trim_config_params: trim unnecessary params that are not present in the config parameters. 8 9This program has been developed by students from the bachelor Computer Science at 10Utrecht University within the Software Project course. 11© Copyright Utrecht University (Department of Information and Computing Sciences) 12""" 13 14from typing import Any, Dict, Tuple 15 16from ..config.config_base_param import ConfigParam 17from ..config.config_parameters import ConfigParameters 18from ..events.event_dispatcher import EventDispatcher 19from .parse_assert import assert_is_container_not_empty, assert_is_type 20from .parse_assert import assert_is_key_in_dict, assert_is_one_of_list 21from .parse_event import ON_PARSE, ParseEventArgs 22 23 24def parse_config_parameters( 25 params_config: Any, 26 parent_name: str, 27 parameters: ConfigParameters, 28 event_dispatcher: EventDispatcher) -> Dict[str, Any]: 29 """Parse the object's parameters configuration. 30 31 Args: 32 params_config: dictionary with the configuration to parse. 33 parent_name: name of the parent related to the parameters' configuration. 34 parameters: the configuration parameters. 35 event_dispatcher: to dispatch the parse event on failure. 36 37 Returns: 38 the parsed params configuration as key-value pairs. 39 """ 40 # start with parameter defaults 41 parsed_params = parameters.get_defaults() 42 43 # assert params_config is a dict 44 if not assert_is_type( 45 params_config, 46 dict, 47 event_dispatcher, 48 'PARSE WARNING: ' + parent_name + ' invalid params value', 49 default_value=parsed_params 50 ): return parsed_params 51 52 # remove unnecessary parameters from configuration 53 params_config = trim_config_params( 54 params_config, 55 parent_name, 56 parameters, 57 event_dispatcher 58 ) 59 60 # assert params_config has entries left after trimming 61 if not assert_is_container_not_empty( 62 params_config, 63 event_dispatcher, 64 'PARSE WARNING: ' + parent_name + ' params is empty', 65 default_value=parsed_params 66 ): return parsed_params 67 68 # parse params_config entries 69 for param_name, _ in parsed_params.items(): 70 success, value = parse_config_param( 71 params_config, 72 parent_name, 73 parameters.get_param(param_name), 74 event_dispatcher 75 ) 76 77 # replace defaults on success 78 if success: 79 parsed_params[param_name] = value 80 81 return parsed_params 82 83 84def parse_config_param( 85 params_config: Dict[str, Any], 86 parent_name: str, 87 param: ConfigParam, 88 event_dispatcher: EventDispatcher) -> Tuple[bool, Any]: 89 """Parse a parameter from the specified configuration. 90 91 Args: 92 params_config: dictionary with the parameters' configuration. 93 parent_name: name of the parent related to the parameters' configuration. 94 param: the parameter that is being parsed. 95 event_dispatcher: to dispatch the parse event on failure. 96 97 Returns: 98 whether the parsing succeeded and the parsed (validated) value. 99 """ 100 param_default = param.default_value 101 102 # assert param_name is present in the configuration 103 if not assert_is_key_in_dict( 104 param.name, 105 params_config, 106 event_dispatcher, 107 'PARSE WARNING: ' + parent_name + ' missing param for \'' + param.name + '\'', 108 default_value=param_default 109 ): return False, param_default 110 111 config_value = params_config[param.name] 112 # validate the configuration value 113 success, value, error_msg = param.validate_value(config_value) 114 115 if not success: 116 event_dispatcher.dispatch(ParseEventArgs( 117 ON_PARSE, 118 'PARSE WARNING: ' + parent_name + ' invalid param \'' + param.name + '\'' + 119 '\n\t' + error_msg, 120 actual_type=config_value, 121 default_value=value 122 )) 123 # validation succeeded but extra info is available 124 elif len(error_msg) > 0: 125 event_dispatcher.dispatch(ParseEventArgs( 126 ON_PARSE, 127 'PARSE WARNING: ' + parent_name + ' modified param \'' + param.name + '\'' + 128 '\n\t' + error_msg, 129 actual_type=config_value 130 )) 131 132 return success, value 133 134 135def trim_config_params( 136 params_config: Dict[str, Any], 137 parent_name: str, 138 parameters: ConfigParameters, 139 event_dispatcher: EventDispatcher) -> Dict[str, Any]: 140 """Trim the parameters from the specified configuration. 141 142 Removes unnecessary parameters that are not present in the 143 original config parameter list. 144 145 Args: 146 params_config: dictionary with the parameters' configuration. 147 parent_name: name of the parent related to the parameters' configuration. 148 parameters: the configuration parameters. 149 event_dispatcher: to dispatch the parse event on failure. 150 151 Returns: 152 the dictionary with the trimmed parameters. 153 """ 154 trimmed_config = {} 155 156 for param_name, param_value in params_config.items(): 157 if assert_is_one_of_list( 158 param_name, 159 parameters.get_param_names(), 160 event_dispatcher, 161 'PARSE WARNING: ' + parent_name + ' unknown parameter \'' + param_name + '\'' 162 ): trimmed_config[param_name] = param_value 163 164 return trimmed_config
25def parse_config_parameters( 26 params_config: Any, 27 parent_name: str, 28 parameters: ConfigParameters, 29 event_dispatcher: EventDispatcher) -> Dict[str, Any]: 30 """Parse the object's parameters configuration. 31 32 Args: 33 params_config: dictionary with the configuration to parse. 34 parent_name: name of the parent related to the parameters' configuration. 35 parameters: the configuration parameters. 36 event_dispatcher: to dispatch the parse event on failure. 37 38 Returns: 39 the parsed params configuration as key-value pairs. 40 """ 41 # start with parameter defaults 42 parsed_params = parameters.get_defaults() 43 44 # assert params_config is a dict 45 if not assert_is_type( 46 params_config, 47 dict, 48 event_dispatcher, 49 'PARSE WARNING: ' + parent_name + ' invalid params value', 50 default_value=parsed_params 51 ): return parsed_params 52 53 # remove unnecessary parameters from configuration 54 params_config = trim_config_params( 55 params_config, 56 parent_name, 57 parameters, 58 event_dispatcher 59 ) 60 61 # assert params_config has entries left after trimming 62 if not assert_is_container_not_empty( 63 params_config, 64 event_dispatcher, 65 'PARSE WARNING: ' + parent_name + ' params is empty', 66 default_value=parsed_params 67 ): return parsed_params 68 69 # parse params_config entries 70 for param_name, _ in parsed_params.items(): 71 success, value = parse_config_param( 72 params_config, 73 parent_name, 74 parameters.get_param(param_name), 75 event_dispatcher 76 ) 77 78 # replace defaults on success 79 if success: 80 parsed_params[param_name] = value 81 82 return parsed_params
Parse the object's parameters configuration.
Args: params_config: dictionary with the configuration to parse. parent_name: name of the parent related to the parameters' configuration. parameters: the configuration parameters. event_dispatcher: to dispatch the parse event on failure.
Returns: the parsed params configuration as key-value pairs.
85def parse_config_param( 86 params_config: Dict[str, Any], 87 parent_name: str, 88 param: ConfigParam, 89 event_dispatcher: EventDispatcher) -> Tuple[bool, Any]: 90 """Parse a parameter from the specified configuration. 91 92 Args: 93 params_config: dictionary with the parameters' configuration. 94 parent_name: name of the parent related to the parameters' configuration. 95 param: the parameter that is being parsed. 96 event_dispatcher: to dispatch the parse event on failure. 97 98 Returns: 99 whether the parsing succeeded and the parsed (validated) value. 100 """ 101 param_default = param.default_value 102 103 # assert param_name is present in the configuration 104 if not assert_is_key_in_dict( 105 param.name, 106 params_config, 107 event_dispatcher, 108 'PARSE WARNING: ' + parent_name + ' missing param for \'' + param.name + '\'', 109 default_value=param_default 110 ): return False, param_default 111 112 config_value = params_config[param.name] 113 # validate the configuration value 114 success, value, error_msg = param.validate_value(config_value) 115 116 if not success: 117 event_dispatcher.dispatch(ParseEventArgs( 118 ON_PARSE, 119 'PARSE WARNING: ' + parent_name + ' invalid param \'' + param.name + '\'' + 120 '\n\t' + error_msg, 121 actual_type=config_value, 122 default_value=value 123 )) 124 # validation succeeded but extra info is available 125 elif len(error_msg) > 0: 126 event_dispatcher.dispatch(ParseEventArgs( 127 ON_PARSE, 128 'PARSE WARNING: ' + parent_name + ' modified param \'' + param.name + '\'' + 129 '\n\t' + error_msg, 130 actual_type=config_value 131 )) 132 133 return success, value
Parse a parameter from the specified configuration.
Args: params_config: dictionary with the parameters' configuration. parent_name: name of the parent related to the parameters' configuration. param: the parameter that is being parsed. event_dispatcher: to dispatch the parse event on failure.
Returns: whether the parsing succeeded and the parsed (validated) value.
136def trim_config_params( 137 params_config: Dict[str, Any], 138 parent_name: str, 139 parameters: ConfigParameters, 140 event_dispatcher: EventDispatcher) -> Dict[str, Any]: 141 """Trim the parameters from the specified configuration. 142 143 Removes unnecessary parameters that are not present in the 144 original config parameter list. 145 146 Args: 147 params_config: dictionary with the parameters' configuration. 148 parent_name: name of the parent related to the parameters' configuration. 149 parameters: the configuration parameters. 150 event_dispatcher: to dispatch the parse event on failure. 151 152 Returns: 153 the dictionary with the trimmed parameters. 154 """ 155 trimmed_config = {} 156 157 for param_name, param_value in params_config.items(): 158 if assert_is_one_of_list( 159 param_name, 160 parameters.get_param_names(), 161 event_dispatcher, 162 'PARSE WARNING: ' + parent_name + ' unknown parameter \'' + param_name + '\'' 163 ): trimmed_config[param_name] = param_value 164 165 return trimmed_config
Trim the parameters from the specified configuration.
Removes unnecessary parameters that are not present in the original config parameter list.
Args: params_config: dictionary with the parameters' configuration. parent_name: name of the parent related to the parameters' configuration. parameters: the configuration parameters. event_dispatcher: to dispatch the parse event on failure.
Returns: the dictionary with the trimmed parameters.