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 344eeaea0bSMichael Walsh If either the invalid_values or the valid_values parms are not of type 354eeaea0bSMichael Walsh "list", they will be processed as python code in order to generate a list. 364eeaea0bSMichael Walsh This allows the robot programmer to essentially specify a list literal. 374eeaea0bSMichael Walsh For example, the robot code could contain the following: 384eeaea0bSMichael Walsh 394eeaea0bSMichael Walsh Rvalid Value var1 valid_values=['one', 'two'] 404eeaea0bSMichael Walsh 4118176322SMichael Walsh Examples of robot calls and corresponding output: 4218176322SMichael Walsh 4318176322SMichael Walsh Robot code... 4418176322SMichael Walsh rvalid_value MY_PARM 4518176322SMichael Walsh 4618176322SMichael Walsh Output... 4718176322SMichael Walsh #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e. 4818176322SMichael Walsh #it's undefined). 4918176322SMichael Walsh 5018176322SMichael Walsh or if it is defined but blank: 5118176322SMichael Walsh 5218176322SMichael Walsh Output... 5318176322SMichael Walsh #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an 5418176322SMichael Walsh #invalid value: 5518176322SMichael Walsh MY_PARM: 5618176322SMichael Walsh 5718176322SMichael Walsh It must NOT be one of the following values: 5818176322SMichael Walsh invalid_values: 5918176322SMichael Walsh invalid_values[0]: <blank> 6018176322SMichael Walsh 6118176322SMichael Walsh Robot code... 6218176322SMichael Walsh ${invalid_values}= Create List one two three 6318176322SMichael Walsh ${MY_PARM}= Set Variable one 6418176322SMichael Walsh rvalid_value MY_PARM invalid_values=${invalid_values} 6518176322SMichael Walsh 6618176322SMichael Walsh Output... 6718176322SMichael Walsh #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an 6818176322SMichael Walsh #invalid value: 6918176322SMichael Walsh MY_PARM: one 7018176322SMichael Walsh 7118176322SMichael Walsh It must NOT be one of the following values: 7218176322SMichael Walsh invalid_values: 7318176322SMichael Walsh invalid_values[0]: one 7418176322SMichael Walsh invalid_values[1]: two 7518176322SMichael Walsh invalid_values[2]: three 7618176322SMichael Walsh 7718176322SMichael Walsh """ 7818176322SMichael Walsh 7918176322SMichael Walsh # Note: get_variable_value() seems to have no trouble with local variables. 8018176322SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 8118176322SMichael Walsh 824eeaea0bSMichael Walsh if type(valid_values) is not list: 834eeaea0bSMichael Walsh # Evaluate python syntax to convert to a list. 844eeaea0bSMichael Walsh exec("valid_values = " + valid_values) 854eeaea0bSMichael Walsh if type(invalid_values) is not list: 864eeaea0bSMichael Walsh # Evaluate python syntax to convert to a list. 874eeaea0bSMichael Walsh exec("invalid_values = " + invalid_values) 884eeaea0bSMichael Walsh 8918176322SMichael Walsh if var_value is None: 9018176322SMichael Walsh var_value = "" 9118176322SMichael Walsh error_message = "Variable \"" + var_name +\ 9218176322SMichael Walsh "\" not found (i.e. it's undefined).\n" 9318176322SMichael Walsh else: 9418176322SMichael Walsh error_message = gv.svalid_value(var_value, invalid_values, 9518176322SMichael Walsh valid_values, var_name) 9618176322SMichael Walsh if not error_message == "": 978fb1f53eSMichael Walsh error_message = grp.sprint_error_report(error_message) 9818176322SMichael Walsh BuiltIn().fail(error_message) 9918176322SMichael Walsh 10018176322SMichael Walsh 10118176322SMichael Walshdef rvalid_integer(var_name): 10218176322SMichael Walsh r""" 1033e26e109SMichael Walsh Validate a robot integer. 1043e26e109SMichael Walsh 1053e26e109SMichael Walsh This function is the robot wrapper for gen_robot_print.svalid_integer. 10618176322SMichael Walsh 10718176322SMichael Walsh Description of arguments: 10818176322SMichael Walsh var_name The name of the variable whose value is to 10918176322SMichael Walsh be validated. 11018176322SMichael Walsh 11118176322SMichael Walsh Examples of robot calls and corresponding output: 11218176322SMichael Walsh 11318176322SMichael Walsh Robot code... 11418176322SMichael Walsh Rvalid Integer MY_PARM 11518176322SMichael Walsh 11618176322SMichael Walsh Output... 11718176322SMichael Walsh #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e. 11818176322SMichael Walsh #it's undefined). 11918176322SMichael Walsh 12018176322SMichael Walsh or if it is defined but blank: 12118176322SMichael Walsh 12218176322SMichael Walsh Output... 12318176322SMichael Walsh #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value: 12418176322SMichael Walsh MY_PARM: <blank> 12518176322SMichael Walsh 12618176322SMichael Walsh Robot code... 12718176322SMichael Walsh ${MY_PARM}= Set Variable HELLO 12818176322SMichael Walsh Rvalid Integer MY_PARM 12918176322SMichael Walsh 13018176322SMichael Walsh Output... 13118176322SMichael Walsh #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value: 13218176322SMichael Walsh MY_PARM: HELLO 13318176322SMichael Walsh 13418176322SMichael Walsh """ 13518176322SMichael Walsh 13618176322SMichael Walsh # Note: get_variable_value() seems to have no trouble with local variables. 13718176322SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 13818176322SMichael Walsh 13918176322SMichael Walsh if var_value is None: 14018176322SMichael Walsh var_value = "" 14118176322SMichael Walsh error_message = "Variable \"" + var_name +\ 14218176322SMichael Walsh "\" not found (i.e. it's undefined).\n" 14318176322SMichael Walsh else: 14418176322SMichael Walsh error_message = gv.svalid_integer(var_value, var_name) 14518176322SMichael Walsh if not error_message == "": 1468fb1f53eSMichael Walsh error_message = grp.sprint_error_report(error_message) 14718176322SMichael Walsh BuiltIn().fail(error_message) 1482c687e98SMichael Walsh 1492c687e98SMichael Walsh 1502c687e98SMichael Walshdef rvalid_range(var_name, 1512c687e98SMichael Walsh range): 1522c687e98SMichael Walsh r""" 1532c687e98SMichael Walsh Validate that a robot integer is within the given range. 1542c687e98SMichael Walsh 1552c687e98SMichael Walsh This function is the robot wrapper for gen_robot_print.svalid_range. 1562c687e98SMichael Walsh 1572c687e98SMichael Walsh Description of arguments: 1582c687e98SMichael Walsh var_name The name of the variable whose value is to 1592c687e98SMichael Walsh be validated. 1602c687e98SMichael Walsh range A list comprised of one or two elements 1612c687e98SMichael Walsh which are the lower and upper ends of a 1622c687e98SMichael Walsh range. These values must be integers 1632c687e98SMichael Walsh except where noted. Valid specifications 1642c687e98SMichael Walsh may be of the following forms: [lower, 1652c687e98SMichael Walsh upper], [lower] or [None, upper]. The 1662c687e98SMichael Walsh caller may also specify this value as a 1672c687e98SMichael Walsh string which will then be converted to a 1682c687e98SMichael Walsh list in the aforementioned format: 1692c687e98SMichael Walsh lower..upper, lower.. or ..upper. 1702c687e98SMichael Walsh 1712c687e98SMichael Walsh Examples of robot calls and corresponding output: 1722c687e98SMichael Walsh 1732c687e98SMichael Walsh Robot code... 1742c687e98SMichael Walsh Rvalid Range MY_PARM 5..9 1752c687e98SMichael Walsh 1762c687e98SMichael Walsh Output... 1772c687e98SMichael Walsh #(CDT) 2018/05/09 11:45:00.166344 - 0.004252 - **ERROR** The following 1782c687e98SMichael Walsh # variable is not within the expected range: 1792c687e98SMichael Walsh MY_PARM: 4 1802c687e98SMichael Walsh valid_range: 5..9 1812c687e98SMichael Walsh """ 1822c687e98SMichael Walsh 1832c687e98SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 1842c687e98SMichael Walsh 1852c687e98SMichael Walsh if var_value is None: 1862c687e98SMichael Walsh var_value = "" 1872c687e98SMichael Walsh error_message = "Variable \"" + var_name +\ 1882c687e98SMichael Walsh "\" not found (i.e. it's undefined).\n" 1892c687e98SMichael Walsh else: 190*36efbc04SGeorge Keishing try: 1912c687e98SMichael Walsh range = range.split("..") 192*36efbc04SGeorge Keishing except AttributeError: 193*36efbc04SGeorge Keishing pass 1942c687e98SMichael Walsh if range[0] == "": 1952c687e98SMichael Walsh range[0] = None 1962c687e98SMichael Walsh range = [x for x in range if x] 1972c687e98SMichael Walsh error_message = gv.svalid_range(var_value, range, var_name) 1982c687e98SMichael Walsh if not error_message == "": 1992c687e98SMichael Walsh error_message = grp.sprint_error_report(error_message) 2002c687e98SMichael Walsh BuiltIn().fail(error_message) 201