1#!/usr/bin/env python 2 3r""" 4This module provides valuable argument processing functions like 5gen_get_options and sprint_args. 6""" 7 8import sys 9 10import gen_print as gp 11 12 13############################################################################### 14def svalid_value(var_value, 15 invalid_values=[], 16 valid_values=[], 17 var_name=""): 18 19 r""" 20 Return an empty string if var_value is a valid value. Otherwise, return 21 an error string. 22 23 Description of arguments: 24 var_value The value being 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 var_name The name of the variable whose value is 34 passed in var_value. This parameter is 35 normally unnecessary as this function can 36 figure out the var_name. This is provided 37 for Robot callers. In this scenario, we 38 are unable to get the variable name 39 ourselves. 40 """ 41 42 success_message = "" 43 error_message = "" 44 stack_frame_ix = 3 45 46 len_valid_values = len(valid_values) 47 len_invalid_values = len(invalid_values) 48 if len_valid_values > 0 and len_invalid_values > 0: 49 error_message += "Programmer error - You must provide either an" +\ 50 " invalid_values list or a valid_values" +\ 51 " list but NOT both.\n" +\ 52 gp.sprint_var(invalid_values) +\ 53 gp.sprint_var(valid_values) 54 return error_message 55 56 show_blanks = 1 57 if len_valid_values > 0: 58 # Processing the valid_values list. 59 if var_value in valid_values: 60 return success_message 61 if var_name == "": 62 var_name = gp.get_arg_name(0, 1, stack_frame_ix) 63 error_message += "The following variable has an invalid" +\ 64 " value:\n" +\ 65 gp.sprint_varx(var_name, var_value, show_blanks) +\ 66 "\nIt must be one of the following values:\n" +\ 67 gp.sprint_varx("valid_values", valid_values, 68 show_blanks) 69 return error_message 70 71 if len_invalid_values == 0: 72 # Assign default value. 73 invalid_values = [""] 74 75 # Assertion: We have an invalid_values list. Processing it now. 76 if var_value not in invalid_values: 77 return success_message 78 79 if var_name == "": 80 var_name = gp.get_arg_name(0, 1, stack_frame_ix) 81 error_message += "The following variable has an invalid value:\n" +\ 82 gp.sprint_varx(var_name, var_value, show_blanks) +\ 83 "\nIt must NOT be one of the following values:\n" +\ 84 gp.sprint_varx("invalid_values", invalid_values, 85 show_blanks) 86 return error_message 87 88############################################################################### 89 90 91############################################################################### 92def valid_value(var_value, 93 invalid_values=[], 94 valid_values=[], 95 var_name=""): 96 97 r""" 98 Return True if var_value is a valid value. Otherwise, return False and 99 print an error message to stderr. 100 101 Description of arguments: 102 (See description of arguments for svalid_value (above). 103 """ 104 105 error_message = svalid_value(var_value, invalid_values, valid_values, 106 var_name) 107 108 if not error_message == "": 109 gp.print_error_report(error_message) 110 return False 111 return True 112 113############################################################################### 114 115 116############################################################################### 117def svalid_integer(var_value, 118 var_name=""): 119 120 r""" 121 Return an empty string if var_value is a valid integer. Otherwise, return 122 an error string. 123 124 Description of arguments: 125 var_value The value being validated. 126 var_name The name of the variable whose value is 127 passed in var_value. This parameter is 128 normally unnecessary as this function can 129 figure out the var_name. This is provided 130 for Robot callers. In this scenario, we 131 are unable to get the variable name 132 ourselves. 133 """ 134 135 # This currently allows floats which is not good. 136 137 success_message = "" 138 error_message = "" 139 try: 140 if type(int(var_value)) is int: 141 return success_message 142 except ValueError: 143 pass 144 145 # If we get to this point, the validation has failed. 146 if var_name is "": 147 stack_index = 3 148 var_name = gp.get_arg_name(0, 1, stack_index) 149 150 show_blanks = 1 151 error_message += "Invalid integer value:\n" +\ 152 gp.sprint_varx(var_name, var_value, show_blanks) 153 154 return error_message 155 156############################################################################### 157 158 159############################################################################### 160def valid_integer(var_value, 161 var_name=""): 162 163 r""" 164 Return True if var_value is a valid integer. Otherwise, return False and 165 print an error message to stderr. 166 167 Description of arguments: 168 var_value The value being validated. 169 """ 170 171 # This currently allows floats which is not good. 172 173 error_message = svalid_integer(var_value, var_name) 174 175 if not error_message == "": 176 gp.print_error_report(error_message) 177 return False 178 return True 179 180############################################################################### 181