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