1#!/usr/bin/env python 2 3r""" 4This file contains functions useful for validating variables in robot. 5""" 6 7import gen_print as gp 8import gen_valid as gv 9 10from robot.libraries.BuiltIn import BuiltIn 11from robot.api import logger 12 13 14def rvalid_value(var_name, 15 invalid_values=[], 16 valid_values=[]): 17 r""" 18 Validate a robot value. 19 20 This function is the robot wrapper for gen_valid.svalid_value. 21 22 Description of arguments: 23 var_name The name of the variable whose value is to 24 be validated. 25 invalid_values A list of invalid values. If var_value is 26 equal to any of these, it is invalid. 27 Note that if you specify anything for 28 invalid_values (below), the valid_values 29 list is not even processed. 30 valid_values A list of invalid values. var_value must 31 be equal to one of these values to be 32 considered valid. 33 34 If either the invalid_values or the valid_values parms are not of type 35 "list", they will be processed as python code in order to generate a list. 36 This allows the robot programmer to essentially specify a list literal. 37 For example, the robot code could contain the following: 38 39 Rvalid Value var1 valid_values=['one', 'two'] 40 41 Examples of robot calls and corresponding output: 42 43 Robot code... 44 rvalid_value MY_PARM 45 46 Output... 47 #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e. 48 #it's undefined). 49 50 or if it is defined but blank: 51 52 Output... 53 #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an 54 #invalid value: 55 MY_PARM: 56 57 It must NOT be one of the following values: 58 invalid_values: 59 invalid_values[0]: <blank> 60 61 Robot code... 62 ${invalid_values}= Create List one two three 63 ${MY_PARM}= Set Variable one 64 rvalid_value MY_PARM invalid_values=${invalid_values} 65 66 Output... 67 #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an 68 #invalid value: 69 MY_PARM: one 70 71 It must NOT be one of the following values: 72 invalid_values: 73 invalid_values[0]: one 74 invalid_values[1]: two 75 invalid_values[2]: three 76 77 """ 78 79 # Note: get_variable_value() seems to have no trouble with local variables. 80 var_value = BuiltIn().get_variable_value("${" + var_name + "}") 81 82 if type(valid_values) is not list: 83 # Evaluate python syntax to convert to a list. 84 exec("valid_values = " + valid_values) 85 if type(invalid_values) is not list: 86 # Evaluate python syntax to convert to a list. 87 exec("invalid_values = " + invalid_values) 88 89 if var_value is None: 90 var_value = "" 91 error_message = "Variable \"" + var_name +\ 92 "\" not found (i.e. it's undefined).\n" 93 else: 94 error_message = gv.svalid_value(var_value, invalid_values, 95 valid_values, var_name) 96 if not error_message == "": 97 error_message = gp.sprint_error_report(error_message) 98 BuiltIn().fail(error_message) 99 100 101def rvalid_integer(var_name): 102 r""" 103 Validate a robot integer. 104 105 This function is the robot wrapper for gen_valid.svalid_integer. 106 107 Description of arguments: 108 var_name The name of the variable whose value is to 109 be validated. 110 111 Examples of robot calls and corresponding output: 112 113 Robot code... 114 Rvalid Integer MY_PARM 115 116 Output... 117 #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e. 118 #it's undefined). 119 120 or if it is defined but blank: 121 122 Output... 123 #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value: 124 MY_PARM: <blank> 125 126 Robot code... 127 ${MY_PARM}= Set Variable HELLO 128 Rvalid Integer MY_PARM 129 130 Output... 131 #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value: 132 MY_PARM: HELLO 133 134 """ 135 136 # Note: get_variable_value() seems to have no trouble with local variables. 137 var_value = BuiltIn().get_variable_value("${" + var_name + "}") 138 139 if var_value is None: 140 var_value = "" 141 error_message = "Variable \"" + var_name +\ 142 "\" not found (i.e. it's undefined).\n" 143 else: 144 error_message = gv.svalid_integer(var_value, var_name) 145 if not error_message == "": 146 error_message = gp.sprint_error_report(error_message) 147 BuiltIn().fail(error_message) 148 149 150def rvalid_range(var_name, 151 range): 152 r""" 153 Validate that a robot integer is within the given range. 154 155 This function is the robot wrapper for gen_valid.svalid_range. 156 157 Description of arguments: 158 var_name The name of the variable whose value is to 159 be validated. 160 range A list comprised of one or two elements 161 which are the lower and upper ends of a 162 range. These values must be integers 163 except where noted. Valid specifications 164 may be of the following forms: [lower, 165 upper], [lower] or [None, upper]. The 166 caller may also specify this value as a 167 string which will then be converted to a 168 list in the aforementioned format: 169 lower..upper, lower.. or ..upper. 170 171 Examples of robot calls and corresponding output: 172 173 Robot code... 174 Rvalid Range MY_PARM 5..9 175 176 Output... 177 #(CDT) 2018/05/09 11:45:00.166344 - 0.004252 - **ERROR** The following 178 # variable is not within the expected range: 179 MY_PARM: 4 180 valid_range: 5..9 181 """ 182 183 var_value = BuiltIn().get_variable_value("${" + var_name + "}") 184 185 if var_value is None: 186 var_value = "" 187 error_message = "Variable \"" + var_name +\ 188 "\" not found (i.e. it's undefined).\n" 189 else: 190 try: 191 range = range.split("..") 192 except AttributeError: 193 pass 194 if range[0] == "": 195 range[0] = None 196 range = [x for x in range if x] 197 error_message = gv.svalid_range(var_value, range, var_name) 198 if not error_message == "": 199 error_message = gp.sprint_error_report(error_message) 200 BuiltIn().fail(error_message) 201