1*18176322SMichael Walsh#!/usr/bin/env python 2*18176322SMichael Walsh 3*18176322SMichael Walshr""" 4*18176322SMichael WalshThis file contains functions useful for validating variables in robot. 5*18176322SMichael Walsh""" 6*18176322SMichael Walsh 7*18176322SMichael Walshimport gen_robot_print as grp 8*18176322SMichael Walshimport gen_valid as gv 9*18176322SMichael Walsh 10*18176322SMichael Walshfrom robot.libraries.BuiltIn import BuiltIn 11*18176322SMichael Walshfrom robot.api import logger 12*18176322SMichael Walsh 13*18176322SMichael Walsh 14*18176322SMichael Walsh############################################################################### 15*18176322SMichael Walshdef rvalid_value(var_name, 16*18176322SMichael Walsh invalid_values=[], 17*18176322SMichael Walsh valid_values=[]): 18*18176322SMichael Walsh 19*18176322SMichael Walsh r""" 20*18176322SMichael Walsh rprint stands for "Robot Valid Value". This function is the robot wrapper 21*18176322SMichael Walsh for gen_robot_print.svalid_value. 22*18176322SMichael Walsh 23*18176322SMichael Walsh Description of arguments: 24*18176322SMichael Walsh var_name The name of the variable whose value is to 25*18176322SMichael Walsh be validated. 26*18176322SMichael Walsh invalid_values A list of invalid values. If var_value is 27*18176322SMichael Walsh equal to any of these, it is invalid. 28*18176322SMichael Walsh Note that if you specify anything for 29*18176322SMichael Walsh invalid_values (below), the valid_values 30*18176322SMichael Walsh list is not even processed. 31*18176322SMichael Walsh valid_values A list of invalid values. var_value must 32*18176322SMichael Walsh be equal to one of these values to be 33*18176322SMichael Walsh considered valid. 34*18176322SMichael Walsh 35*18176322SMichael Walsh Examples of robot calls and corresponding output: 36*18176322SMichael Walsh 37*18176322SMichael Walsh Robot code... 38*18176322SMichael Walsh rvalid_value MY_PARM 39*18176322SMichael Walsh 40*18176322SMichael Walsh Output... 41*18176322SMichael Walsh #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e. 42*18176322SMichael Walsh #it's undefined). 43*18176322SMichael Walsh 44*18176322SMichael Walsh or if it is defined but blank: 45*18176322SMichael Walsh 46*18176322SMichael Walsh Output... 47*18176322SMichael Walsh #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an 48*18176322SMichael Walsh #invalid value: 49*18176322SMichael Walsh MY_PARM: 50*18176322SMichael Walsh 51*18176322SMichael Walsh It must NOT be one of the following values: 52*18176322SMichael Walsh invalid_values: 53*18176322SMichael Walsh invalid_values[0]: <blank> 54*18176322SMichael Walsh 55*18176322SMichael Walsh Robot code... 56*18176322SMichael Walsh ${invalid_values}= Create List one two three 57*18176322SMichael Walsh ${MY_PARM}= Set Variable one 58*18176322SMichael Walsh rvalid_value MY_PARM invalid_values=${invalid_values} 59*18176322SMichael Walsh 60*18176322SMichael Walsh Output... 61*18176322SMichael Walsh #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an 62*18176322SMichael Walsh #invalid value: 63*18176322SMichael Walsh MY_PARM: one 64*18176322SMichael Walsh 65*18176322SMichael Walsh It must NOT be one of the following values: 66*18176322SMichael Walsh invalid_values: 67*18176322SMichael Walsh invalid_values[0]: one 68*18176322SMichael Walsh invalid_values[1]: two 69*18176322SMichael Walsh invalid_values[2]: three 70*18176322SMichael Walsh 71*18176322SMichael Walsh """ 72*18176322SMichael Walsh 73*18176322SMichael Walsh # Note: get_variable_value() seems to have no trouble with local variables. 74*18176322SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 75*18176322SMichael Walsh 76*18176322SMichael Walsh if var_value is None: 77*18176322SMichael Walsh var_value = "" 78*18176322SMichael Walsh error_message = "Variable \"" + var_name +\ 79*18176322SMichael Walsh "\" not found (i.e. it's undefined).\n" 80*18176322SMichael Walsh else: 81*18176322SMichael Walsh error_message = gv.svalid_value(var_value, invalid_values, 82*18176322SMichael Walsh valid_values, var_name) 83*18176322SMichael Walsh if not error_message == "": 84*18176322SMichael Walsh error_message = grp.sprint_robot_error_report(error_message) 85*18176322SMichael Walsh BuiltIn().fail(error_message) 86*18176322SMichael Walsh 87*18176322SMichael Walsh############################################################################### 88*18176322SMichael Walsh 89*18176322SMichael Walsh 90*18176322SMichael Walsh############################################################################### 91*18176322SMichael Walshdef rvalid_integer(var_name): 92*18176322SMichael Walsh 93*18176322SMichael Walsh r""" 94*18176322SMichael Walsh rprint stands for "Robot Valid Integer". This function is the robot 95*18176322SMichael Walsh wrapper for gen_robot_print.svalid_integer. 96*18176322SMichael Walsh 97*18176322SMichael Walsh Description of arguments: 98*18176322SMichael Walsh var_name The name of the variable whose value is to 99*18176322SMichael Walsh be validated. 100*18176322SMichael Walsh 101*18176322SMichael Walsh Examples of robot calls and corresponding output: 102*18176322SMichael Walsh 103*18176322SMichael Walsh Robot code... 104*18176322SMichael Walsh Rvalid Integer MY_PARM 105*18176322SMichael Walsh 106*18176322SMichael Walsh Output... 107*18176322SMichael Walsh #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e. 108*18176322SMichael Walsh #it's undefined). 109*18176322SMichael Walsh 110*18176322SMichael Walsh or if it is defined but blank: 111*18176322SMichael Walsh 112*18176322SMichael Walsh Output... 113*18176322SMichael Walsh #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value: 114*18176322SMichael Walsh MY_PARM: <blank> 115*18176322SMichael Walsh 116*18176322SMichael Walsh Robot code... 117*18176322SMichael Walsh ${MY_PARM}= Set Variable HELLO 118*18176322SMichael Walsh Rvalid Integer MY_PARM 119*18176322SMichael Walsh 120*18176322SMichael Walsh Output... 121*18176322SMichael Walsh #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value: 122*18176322SMichael Walsh MY_PARM: HELLO 123*18176322SMichael Walsh 124*18176322SMichael Walsh """ 125*18176322SMichael Walsh 126*18176322SMichael Walsh # Note: get_variable_value() seems to have no trouble with local variables. 127*18176322SMichael Walsh var_value = BuiltIn().get_variable_value("${" + var_name + "}") 128*18176322SMichael Walsh 129*18176322SMichael Walsh if var_value is None: 130*18176322SMichael Walsh var_value = "" 131*18176322SMichael Walsh error_message = "Variable \"" + var_name +\ 132*18176322SMichael Walsh "\" not found (i.e. it's undefined).\n" 133*18176322SMichael Walsh else: 134*18176322SMichael Walsh error_message = gv.svalid_integer(var_value, var_name) 135*18176322SMichael Walsh if not error_message == "": 136*18176322SMichael Walsh error_message = grp.sprint_robot_error_report(error_message) 137*18176322SMichael Walsh BuiltIn().fail(error_message) 138*18176322SMichael Walsh 139*18176322SMichael Walsh############################################################################### 140