def get_eval_value(self, data): """ evaluate data recursively, each variable in data will be evaluated. """ if isinstance(data, (list, tuple)): return [self.get_eval_value(item) for item in data] if isinstance(data, dict): evaluated_data = {} for key, value in data.items(): evaluated_data[key] = self.get_eval_value(value) return evaluated_data if isinstance(data, (int, float)): return data # data is in string format here data = "" if data is None else data.strip() if utils.is_variable(data): # variable marker: $var variable_name = utils.parse_variable(data) value = self.testcase_variables_mapping.get(variable_name) if value is None: raise exception.ParamsError( "%s is not defined in bind variables!" % variable_name) return value elif utils.is_functon(data): # function marker: ${func(1, 2, a=3, b=4)} fuction_meta = utils.parse_function(data) func_name = fuction_meta['func_name'] args = fuction_meta.get('args', []) kwargs = fuction_meta.get('kwargs', {}) args = self.get_eval_value(args) kwargs = self.get_eval_value(kwargs) return self.testcase_config["functions"][func_name](*args, **kwargs) else: return data
def test_parse_functon(self): content = "${func()}" self.assertEqual(utils.parse_function(content), { 'func_name': 'func', 'args': [], 'kwargs': {} }) content = "${func(5)}" self.assertEqual(utils.parse_function(content), { 'func_name': 'func', 'args': [5], 'kwargs': {} }) content = "${func(1, 2)}" self.assertEqual(utils.parse_function(content), { 'func_name': 'func', 'args': [1, 2], 'kwargs': {} }) content = "${func(a=1, b=2)}" self.assertEqual(utils.parse_function(content), { 'func_name': 'func', 'args': [], 'kwargs': { 'a': 1, 'b': 2 } }) content = "${func(a= 1, b =2)}" self.assertEqual(utils.parse_function(content), { 'func_name': 'func', 'args': [], 'kwargs': { 'a': 1, 'b': 2 } }) content = "${func(1, 2, a=3, b=4)}" self.assertEqual(utils.parse_function(content), { 'func_name': 'func', 'args': [1, 2], 'kwargs': { 'a': 3, 'b': 4 } })
def get_eval_value(self, data): """ evaluate data recursively, each variable in data will be evaluated. """ if isinstance(data, (list, tuple)): return [self.get_eval_value(item) for item in data] if isinstance(data, dict): evaluated_data = {} for key, value in data.items(): evaluated_data[key] = self.get_eval_value(value) return evaluated_data if isinstance(data, (int, float)): return data # data is in string format here data = "" if data is None else data.strip() if utils.is_functon(data): # function marker: ${func(1, 2, a=3, b=4)} fuction_meta = utils.parse_function(data) func_name = fuction_meta['func_name'] args = fuction_meta.get('args', []) kwargs = fuction_meta.get('kwargs', {}) args = self.get_eval_value(args) kwargs = self.get_eval_value(kwargs) return self.testcase_functions_config[func_name](*args, **kwargs) elif utils.get_contain_variables(data): parsed_data = utils.parse_variables( data, self.testcase_variables_mapping) return parsed_data else: return data