118176322SMichael Walsh#!/usr/bin/env python 218176322SMichael Walsh 318176322SMichael Walshr""" 418176322SMichael WalshThis file contains functions useful for validating variables in robot. 518176322SMichael Walsh""" 618176322SMichael Walsh 718176322SMichael Walshimport gen_robot_print as grp 818176322SMichael Walshimport gen_valid as gv 918176322SMichael Walsh 1018176322SMichael Walshfrom robot.libraries.BuiltIn import BuiltIn 1118176322SMichael Walshfrom robot.api import logger 1218176322SMichael Walsh 1318176322SMichael Walsh 1418176322SMichael Walshdef rvalid_value(var_name, 1518176322SMichael Walsh invalid_values=[], 1618176322SMichael Walsh valid_values=[]): 1718176322SMichael Walsh r""" 183e26e109SMichael Walsh Validate a robot value. 193e26e109SMichael Walsh 203e26e109SMichael Walsh This function is the robot wrapper for gen_robot_print.svalid_value. 2118176322SMichael Walsh 2218176322SMichael Walsh Description of arguments: 2318176322SMichael Walsh var_name The name of the variable whose value is to 2418176322SMichael Walsh be validated. 2518176322SMichael Walsh invalid_values A list of invalid values. If var_value is 2618176322SMichael Walsh equal to any of these, it is invalid. 2718176322SMichael Walsh Note that if you specify anything for 2818176322SMichael Walsh invalid_values (below), the valid_values 2918176322SMichael Walsh list is not even processed. 3018176322SMichael Walsh valid_values A list of invalid values. var_value must 3118176322SMichael Walsh be equal to one of these values to be 3218176322SMichael Walsh considered valid. 3318176322SMichael Walsh 3418176322SMichael Walsh Examples of robot calls and corresponding output: 3518176322SMichael Walsh 3618176322SMichael Walsh Robot code... 3718176322SMichael Walsh rvalid_value MY_PARM 3818176322SMichael Walsh 3918176322SMichael Walsh Output... 4018176322SMichael Walsh #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e. 4118176322SMichael Walsh #it's undefined). 4218176322SMichael Walsh 4318176322SMichael Walsh or if it is defined but blank: 4418176322SMichael Walsh 4518176322SMichael Walsh Output... 4618176322SMichael Walsh #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an 4718176322SMichael Walsh #invalid value: 4818176322SMichael Walsh MY_PARM: 4918176322SMichael Walsh 5018176322SMichael Walsh It must NOT be one of the following values: 5118176322SMichael Walsh invalid_values: 5218176322SMichael Walsh invalid_values[0]: <blank> 5318176322SMichael Walsh 5418176322SMichael Walsh Robot code... 5518176322SMichael Walsh ${invalid_values}= Create List one two three 5618176322SMichael Walsh ${MY_PARM}= Set Variable one 5718176322SMichael Walsh rvalid_value MY_PARM invalid_values=${invalid_values} 5818176322SMichael Walsh 5918176322SMichael Walsh Output... 6018176322SMichael Walsh #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an 6118176322SMichael Walsh #invalid value: 6218176322SMichael Walsh MY_PARM: one 6318176322SMichael Walsh 6418176322SMichael Walsh It must NOT be one of the following values: 6518176322SMichael Walsh invalid_values: 6618176322SMichael Walsh invalid_values[0]: one 6718176322SMichael Walsh invalid_values[1]: two 6818176322SMichael Walsh invalid_values[2]: three 6918176322SMichael Walsh 7018176322SMichael Walsh """ 7118176322SMichael Walsh 7218176322SMichael Walsh # Note: get_variable_value() seems to have no trouble with local variables. 7318176322SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 7418176322SMichael Walsh 7518176322SMichael Walsh if var_value is None: 7618176322SMichael Walsh var_value = "" 7718176322SMichael Walsh error_message = "Variable \"" + var_name +\ 7818176322SMichael Walsh "\" not found (i.e. it's undefined).\n" 7918176322SMichael Walsh else: 8018176322SMichael Walsh error_message = gv.svalid_value(var_value, invalid_values, 8118176322SMichael Walsh valid_values, var_name) 8218176322SMichael Walsh if not error_message == "": 838fb1f53eSMichael Walsh error_message = grp.sprint_error_report(error_message) 8418176322SMichael Walsh BuiltIn().fail(error_message) 8518176322SMichael Walsh 8618176322SMichael Walsh 8718176322SMichael Walshdef rvalid_integer(var_name): 8818176322SMichael Walsh r""" 893e26e109SMichael Walsh Validate a robot integer. 903e26e109SMichael Walsh 913e26e109SMichael Walsh This function is the robot wrapper for gen_robot_print.svalid_integer. 9218176322SMichael Walsh 9318176322SMichael Walsh Description of arguments: 9418176322SMichael Walsh var_name The name of the variable whose value is to 9518176322SMichael Walsh be validated. 9618176322SMichael Walsh 9718176322SMichael Walsh Examples of robot calls and corresponding output: 9818176322SMichael Walsh 9918176322SMichael Walsh Robot code... 10018176322SMichael Walsh Rvalid Integer MY_PARM 10118176322SMichael Walsh 10218176322SMichael Walsh Output... 10318176322SMichael Walsh #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e. 10418176322SMichael Walsh #it's undefined). 10518176322SMichael Walsh 10618176322SMichael Walsh or if it is defined but blank: 10718176322SMichael Walsh 10818176322SMichael Walsh Output... 10918176322SMichael Walsh #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value: 11018176322SMichael Walsh MY_PARM: <blank> 11118176322SMichael Walsh 11218176322SMichael Walsh Robot code... 11318176322SMichael Walsh ${MY_PARM}= Set Variable HELLO 11418176322SMichael Walsh Rvalid Integer MY_PARM 11518176322SMichael Walsh 11618176322SMichael Walsh Output... 11718176322SMichael Walsh #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value: 11818176322SMichael Walsh MY_PARM: HELLO 11918176322SMichael Walsh 12018176322SMichael Walsh """ 12118176322SMichael Walsh 12218176322SMichael Walsh # Note: get_variable_value() seems to have no trouble with local variables. 12318176322SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 12418176322SMichael Walsh 12518176322SMichael Walsh if var_value is None: 12618176322SMichael Walsh var_value = "" 12718176322SMichael Walsh error_message = "Variable \"" + var_name +\ 12818176322SMichael Walsh "\" not found (i.e. it's undefined).\n" 12918176322SMichael Walsh else: 13018176322SMichael Walsh error_message = gv.svalid_integer(var_value, var_name) 13118176322SMichael Walsh if not error_message == "": 1328fb1f53eSMichael Walsh error_message = grp.sprint_error_report(error_message) 13318176322SMichael Walsh BuiltIn().fail(error_message) 134*2c687e98SMichael Walsh 135*2c687e98SMichael Walsh 136*2c687e98SMichael Walshdef rvalid_range(var_name, 137*2c687e98SMichael Walsh range): 138*2c687e98SMichael Walsh r""" 139*2c687e98SMichael Walsh Validate that a robot integer is within the given range. 140*2c687e98SMichael Walsh 141*2c687e98SMichael Walsh This function is the robot wrapper for gen_robot_print.svalid_range. 142*2c687e98SMichael Walsh 143*2c687e98SMichael Walsh Description of arguments: 144*2c687e98SMichael Walsh var_name The name of the variable whose value is to 145*2c687e98SMichael Walsh be validated. 146*2c687e98SMichael Walsh range A list comprised of one or two elements 147*2c687e98SMichael Walsh which are the lower and upper ends of a 148*2c687e98SMichael Walsh range. These values must be integers 149*2c687e98SMichael Walsh except where noted. Valid specifications 150*2c687e98SMichael Walsh may be of the following forms: [lower, 151*2c687e98SMichael Walsh upper], [lower] or [None, upper]. The 152*2c687e98SMichael Walsh caller may also specify this value as a 153*2c687e98SMichael Walsh string which will then be converted to a 154*2c687e98SMichael Walsh list in the aforementioned format: 155*2c687e98SMichael Walsh lower..upper, lower.. or ..upper. 156*2c687e98SMichael Walsh 157*2c687e98SMichael Walsh Examples of robot calls and corresponding output: 158*2c687e98SMichael Walsh 159*2c687e98SMichael Walsh Robot code... 160*2c687e98SMichael Walsh Rvalid Range MY_PARM 5..9 161*2c687e98SMichael Walsh 162*2c687e98SMichael Walsh Output... 163*2c687e98SMichael Walsh #(CDT) 2018/05/09 11:45:00.166344 - 0.004252 - **ERROR** The following 164*2c687e98SMichael Walsh # variable is not within the expected range: 165*2c687e98SMichael Walsh MY_PARM: 4 166*2c687e98SMichael Walsh valid_range: 5..9 167*2c687e98SMichael Walsh """ 168*2c687e98SMichael Walsh 169*2c687e98SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 170*2c687e98SMichael Walsh 171*2c687e98SMichael Walsh if var_value is None: 172*2c687e98SMichael Walsh var_value = "" 173*2c687e98SMichael Walsh error_message = "Variable \"" + var_name +\ 174*2c687e98SMichael Walsh "\" not found (i.e. it's undefined).\n" 175*2c687e98SMichael Walsh else: 176*2c687e98SMichael Walsh if type(range) is unicode: 177*2c687e98SMichael Walsh range = range.split("..") 178*2c687e98SMichael Walsh if range[0] == "": 179*2c687e98SMichael Walsh range[0] = None 180*2c687e98SMichael Walsh range = [x for x in range if x] 181*2c687e98SMichael Walsh error_message = gv.svalid_range(var_value, range, var_name) 182*2c687e98SMichael Walsh if not error_message == "": 183*2c687e98SMichael Walsh error_message = grp.sprint_error_report(error_message) 184*2c687e98SMichael Walsh BuiltIn().fail(error_message) 185