Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module provides valuable argument processing functions like |
| 5 | gen_get_options and sprint_args. |
| 6 | """ |
| 7 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 8 | import os |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 9 | import gen_print as gp |
| 10 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 11 | exit_on_error = False |
| 12 | |
| 13 | |
| 14 | def set_exit_on_error(value): |
| 15 | r""" |
| 16 | Set the exit_on_error value to either True or False. |
| 17 | |
| 18 | If exit_on_error is set, validation functions like valid_value will exit |
| 19 | the program on error instead of returning False. |
| 20 | |
| 21 | Description of argument(s): |
| 22 | value Value to set global exit_on_error to. |
| 23 | """ |
| 24 | |
| 25 | global exit_on_error |
| 26 | exit_on_error = value |
| 27 | |
| 28 | |
| 29 | def get_var_name(var_name): |
| 30 | r""" |
| 31 | If var_name has a value, simply return it. Otherwise, get the variable |
Gunnar Mills | 7732c7e | 2018-08-14 11:54:24 -0500 | [diff] [blame] | 32 | name of the first argument used to call the validation function (e.g. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 33 | valid_value, valid_integer, etc.) and return it. |
| 34 | |
| 35 | This function is designed solely for use by other functions in this file. |
| 36 | |
| 37 | Example: |
| 38 | |
| 39 | A programmer codes this: |
| 40 | |
| 41 | valid_value(last_name) |
| 42 | |
| 43 | Which results in the following call stack: |
| 44 | |
| 45 | valid_value(last_name) |
| 46 | -> svalid_value(var_value...) |
| 47 | -> get_var_name(var_name) |
| 48 | |
| 49 | In this example, this function will return "last_name". |
| 50 | |
| 51 | Example: |
| 52 | |
| 53 | err_msg = svalid_value(last_name, var_name="some_other_name") |
| 54 | |
| 55 | Which results in the following call stack: |
| 56 | |
| 57 | svalid_value(var_value, var_name="some_other_name") |
| 58 | -> get_var_name(var_name) |
| 59 | |
| 60 | In this example, this function will return "some_other_name". |
| 61 | |
| 62 | Description of argument(s): |
| 63 | var_name The name of the variable. |
| 64 | """ |
| 65 | |
| 66 | if var_name != "": |
| 67 | return var_name |
| 68 | # Calculate stack_frame_ix. The validation functions in this file come |
| 69 | # in pairs. There is an "s" version of each validation function (e.g. |
| 70 | # svalid_value) whose job is to return an error message string. Then |
| 71 | # there is a wrapper function (e.g. valid_value) that will call the "s" |
| 72 | # version and print the result if there is an error. See examples 1 and 2 |
| 73 | # above for illustration. This function must be cognizant of both |
| 74 | # scenarios to accurately determine the name of the variable being |
| 75 | # validated. Where the "s" function is being called directly, the |
| 76 | # stack_frame_ix should be set to 3. Where the wrapper function is being |
| 77 | # called, the stack_frame_ix should be incremented to 4. |
| 78 | stack_frame_ix = 3 |
| 79 | parent_func_name = gp.sprint_func_name(2) |
| 80 | grandparent_func_name = gp.sprint_func_name(3) |
| 81 | if parent_func_name == "s" + grandparent_func_name: |
| 82 | stack_frame_ix += 1 |
| 83 | var_name = gp.get_arg_name(0, 1, stack_frame_ix) |
| 84 | return var_name |
| 85 | |
| 86 | |
| 87 | def process_error_message(error_message): |
| 88 | r""" |
| 89 | Process the error_message as follows: |
| 90 | - If the error_message is blank, return True. |
| 91 | - If the error_message contains a value: |
| 92 | - Print the error_message as part of a full error report. |
| 93 | - If global exit_on_error is set, then exit the program with a return |
Michael Walsh | faafa9c | 2018-06-27 16:39:31 -0500 | [diff] [blame] | 94 | code of 1. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 95 | - If exit_on_error is not set, return False. |
| 96 | |
| 97 | This function is designed solely for use by wrapper functions in this file |
| 98 | (e.g. "valid_value"). |
| 99 | |
| 100 | Description of argument(s): |
| 101 | error_message An error message. |
| 102 | """ |
| 103 | |
| 104 | if error_message == "": |
| 105 | return True |
| 106 | |
| 107 | gp.print_error_report(error_message) |
| 108 | if exit_on_error: |
Michael Walsh | 10a4d98 | 2018-10-30 13:07:46 -0500 | [diff] [blame] | 109 | exit(1) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 110 | return False |
| 111 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 112 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 113 | def svalid_value(var_value, |
| 114 | invalid_values=[], |
| 115 | valid_values=[], |
| 116 | var_name=""): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 117 | r""" |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 118 | Return an empty string if var_value is a valid value. Otherwise, return |
| 119 | an error string. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 120 | |
| 121 | Description of arguments: |
| 122 | var_value The value being validated. |
| 123 | invalid_values A list of invalid values. If var_value is |
| 124 | equal to any of these, it is invalid. |
| 125 | Note that if you specify anything for |
| 126 | invalid_values (below), the valid_values |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 127 | list is not even processed. If you |
| 128 | specify nothing for both invalid_values |
| 129 | and valid_values, invalid_values will be |
| 130 | set to a default value of [""]. |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 131 | valid_values A list of valid values. var_value must be |
| 132 | equal to one of these values to be |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 133 | considered valid. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 134 | var_name The name of the variable whose value is |
| 135 | passed in var_value. This parameter is |
| 136 | normally unnecessary as this function can |
| 137 | figure out the var_name. This is provided |
| 138 | for Robot callers. In this scenario, we |
| 139 | are unable to get the variable name |
| 140 | ourselves. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 141 | """ |
| 142 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 143 | success_message = "" |
| 144 | error_message = "" |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 145 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 146 | # Validate this function's arguments. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 147 | len_valid_values = len(valid_values) |
| 148 | len_invalid_values = len(invalid_values) |
| 149 | if len_valid_values > 0 and len_invalid_values > 0: |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 150 | error_message += "Programmer error - You must provide either an" +\ |
| 151 | " invalid_values list or a valid_values" +\ |
| 152 | " list but NOT both.\n" +\ |
| 153 | gp.sprint_var(invalid_values) +\ |
| 154 | gp.sprint_var(valid_values) |
| 155 | return error_message |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 156 | |
| 157 | if len_valid_values > 0: |
| 158 | # Processing the valid_values list. |
| 159 | if var_value in valid_values: |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 160 | return success_message |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 161 | error_message += "The following variable has an invalid" +\ |
| 162 | " value:\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 163 | gp.sprint_varx(get_var_name(var_name), var_value, |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 164 | gp.blank()) +\ |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 165 | "\nIt must be one of the following values:\n" +\ |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 166 | gp.sprint_var(valid_values, gp.blank()) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 167 | return error_message |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 168 | |
| 169 | if len_invalid_values == 0: |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 170 | # Assign default value. |
| 171 | invalid_values = [""] |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 172 | |
| 173 | # Assertion: We have an invalid_values list. Processing it now. |
| 174 | if var_value not in invalid_values: |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 175 | return success_message |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 176 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 177 | error_message += "The following variable has an invalid value:\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 178 | gp.sprint_varx(get_var_name(var_name), var_value, |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 179 | gp.blank()) +\ |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 180 | "\nIt must NOT be one of the following values:\n" +\ |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 181 | gp.sprint_var(invalid_values, gp.blank()) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 182 | return error_message |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 183 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 184 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 185 | def valid_value(var_value, |
| 186 | invalid_values=[], |
| 187 | valid_values=[], |
| 188 | var_name=""): |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 189 | r""" |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 190 | Return True if var_value is valid. Otherwise, print an error message and |
| 191 | either return False or exit(1) depending on the value of exit_on_error. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 192 | |
| 193 | Description of arguments: |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 194 | (See description of arguments for svalid_value (above)). |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 195 | """ |
| 196 | |
| 197 | error_message = svalid_value(var_value, invalid_values, valid_values, |
| 198 | var_name) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 199 | return process_error_message(error_message) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 200 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 201 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 202 | def svalid_integer(var_value, |
| 203 | var_name=""): |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 204 | r""" |
| 205 | Return an empty string if var_value is a valid integer. Otherwise, return |
| 206 | an error string. |
| 207 | |
| 208 | Description of arguments: |
| 209 | var_value The value being validated. |
| 210 | var_name The name of the variable whose value is |
| 211 | passed in var_value. This parameter is |
| 212 | normally unnecessary as this function can |
| 213 | figure out the var_name. This is provided |
| 214 | for Robot callers. In this scenario, we |
| 215 | are unable to get the variable name |
| 216 | ourselves. |
| 217 | """ |
| 218 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 219 | success_message = "" |
| 220 | error_message = "" |
| 221 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 222 | if isinstance(int(str(var_value), 0), int): |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 223 | return success_message |
| 224 | except ValueError: |
| 225 | pass |
| 226 | |
| 227 | # If we get to this point, the validation has failed. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 228 | error_message +=\ |
| 229 | "Invalid integer value:\n" +\ |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 230 | gp.sprint_varx(get_var_name(var_name), var_value, gp.blank()) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 231 | |
| 232 | return error_message |
| 233 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 234 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 235 | def valid_integer(var_value, |
| 236 | var_name=""): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 237 | r""" |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 238 | Return True if var_value is a valid integer. Otherwise, print an error |
| 239 | message and either return False or exit(1) depending on the value of |
| 240 | exit_on_error. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 241 | |
| 242 | Description of arguments: |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 243 | (See description of arguments for svalid_integer (above)). |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 244 | """ |
| 245 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 246 | error_message = svalid_integer(var_value, var_name) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 247 | return process_error_message(error_message) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 248 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 249 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 250 | def svalid_dir_path(var_value, |
| 251 | var_name=""): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 252 | r""" |
| 253 | Return an empty string if var_value is a valid directory path. Otherwise, |
| 254 | return an error string. |
| 255 | |
| 256 | Description of arguments: |
| 257 | var_value The value being validated. |
| 258 | var_name The name of the variable whose value is |
| 259 | passed in var_value. This parameter is |
| 260 | normally unnecessary as this function can |
| 261 | figure out the var_name. This is provided |
| 262 | for Robot callers. In this scenario, we |
| 263 | are unable to get the variable name |
| 264 | ourselves. |
| 265 | """ |
| 266 | |
| 267 | error_message = "" |
| 268 | if not os.path.isdir(str(var_value)): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 269 | error_message += "The following directory does not exist:\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 270 | gp.sprint_varx(get_var_name(var_name), var_value) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 271 | |
| 272 | return error_message |
| 273 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 274 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 275 | def valid_dir_path(var_value, |
| 276 | var_name=""): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 277 | r""" |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 278 | Return True if var_value is a valid directory path. Otherwise, print an |
| 279 | error message and either return False or exit(1) depending on the value of |
| 280 | exit_on_error. |
| 281 | |
| 282 | Valid means that the directory path exists. |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 283 | |
| 284 | Description of arguments: |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 285 | (See description of arguments for svalid_dir_path (above)). |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 286 | """ |
| 287 | |
| 288 | error_message = svalid_dir_path(var_value, var_name) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 289 | return process_error_message(error_message) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 290 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 291 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 292 | def svalid_file_path(var_value, |
| 293 | var_name=""): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 294 | r""" |
| 295 | Return an empty string if var_value is a valid file path. Otherwise, |
| 296 | return an error string. |
| 297 | |
| 298 | Description of arguments: |
| 299 | var_value The value being validated. |
| 300 | var_name The name of the variable whose value is |
| 301 | passed in var_value. This parameter is |
| 302 | normally unnecessary as this function can |
| 303 | figure out the var_name. This is provided |
| 304 | for Robot callers. In this scenario, we |
| 305 | are unable to get the variable name |
| 306 | ourselves. |
| 307 | """ |
| 308 | |
| 309 | error_message = "" |
| 310 | if not os.path.isfile(str(var_value)): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 311 | error_message += "Invalid file (does not exist):\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 312 | gp.sprint_varx(get_var_name(var_name), var_value) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 313 | |
| 314 | return error_message |
| 315 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 316 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 317 | def valid_file_path(var_value, |
| 318 | var_name=""): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 319 | r""" |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 320 | Return True if var_value is a valid file path. Otherwise, print an error |
| 321 | message and either return False or exit(1) depending on the value of |
| 322 | exit_on_error. |
| 323 | |
| 324 | Valid means that the file exists. |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 325 | |
| 326 | Description of arguments: |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 327 | (See description of arguments for svalid_file_path (above)). |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 328 | """ |
| 329 | |
| 330 | error_message = svalid_file_path(var_value, var_name) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 331 | return process_error_message(error_message) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 332 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 333 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 334 | def svalid_path(var_value, |
| 335 | var_name=""): |
| 336 | r""" |
| 337 | Return an empty string if var_value is either a valid file path or |
| 338 | directory path. Otherwise, return an error string. |
| 339 | |
| 340 | Description of arguments: |
| 341 | var_value The value being validated. |
| 342 | var_name The name of the variable whose value is |
| 343 | passed in var_value. This parameter is |
| 344 | normally unnecessary as this function can |
| 345 | figure out the var_name. This is provided |
| 346 | for Robot callers. In this scenario, we |
| 347 | are unable to get the variable name |
| 348 | ourselves. |
| 349 | """ |
| 350 | |
| 351 | error_message = "" |
| 352 | if not (os.path.isfile(str(var_value)) or os.path.isdir(str(var_value))): |
| 353 | error_message = "Invalid path (file or directory does not exist):\n" +\ |
| 354 | gp.sprint_varx(get_var_name(var_name), var_value) |
| 355 | |
| 356 | return error_message |
| 357 | |
| 358 | |
| 359 | def valid_path(var_value, |
| 360 | var_name=""): |
| 361 | r""" |
| 362 | Return True if var_value is a valid file path. Otherwise, print an error |
| 363 | message and either return False or exit(1) depending on the value of |
| 364 | exit_on_error. |
| 365 | |
| 366 | Valid means that the file exists. |
| 367 | |
| 368 | Description of arguments: |
| 369 | (See description of arguments for svalid_path (above)). |
| 370 | """ |
| 371 | |
| 372 | error_message = svalid_path(var_value, var_name) |
| 373 | return process_error_message(error_message) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 374 | |
| 375 | |
| 376 | def svalid_range(var_value, |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 377 | valid_range=[], |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 378 | var_name=""): |
| 379 | r""" |
| 380 | Return an empty string if var_value is within the range. Otherwise, |
| 381 | return an error string. |
| 382 | |
| 383 | Description of arguments: |
| 384 | var_value The value being validated. This value |
| 385 | must be an integer. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 386 | valid_range A list comprised of one or two elements |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 387 | which are the lower and upper ends of a |
| 388 | range. These values must be integers |
| 389 | except where noted. Valid specifications |
| 390 | may be of the following forms: [lower, |
| 391 | upper], [lower] or [None, upper]. |
| 392 | var_name The name of the variable whose value is |
| 393 | passed in var_value. This parameter is |
| 394 | normally unnecessary as this function can |
| 395 | figure out the var_name. This is provided |
| 396 | for Robot callers. In this scenario, we |
| 397 | are unable to get the variable name |
| 398 | ourselves. |
| 399 | """ |
| 400 | |
| 401 | error_message = "" |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 402 | |
| 403 | # Validate this function's parms: |
| 404 | # First, ensure that the value is an integer. |
| 405 | error_message = svalid_integer(var_value, var_name) |
| 406 | if not error_message == "": |
| 407 | return error_message |
| 408 | var_value = int(var_value) |
| 409 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 410 | len_valid_range = len(valid_range) |
| 411 | if len_valid_range == 0 or len_valid_range > 2: |
| 412 | error_message += "Programmer error - For the valid_range parameter," +\ |
| 413 | " you must provide a list consisting of one or two" +\ |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 414 | " elements.\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 415 | gp.sprint_var(valid_range) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 416 | return error_message |
| 417 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 418 | if len_valid_range == 1 or valid_range[0] is not None: |
| 419 | # Make sure lower valid_range value is an integer. |
| 420 | error_message = svalid_integer(valid_range[0], "valid_range[0]") |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 421 | if not error_message == "": |
| 422 | error_message = "Programmer error:\n" + error_message |
| 423 | return error_message |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 424 | if valid_range[0] is not None: |
| 425 | valid_range[0] = int(valid_range[0]) |
| 426 | if len_valid_range == 2: |
| 427 | # Make sure upper valid_range value is an integer. |
| 428 | error_message = svalid_integer(valid_range[1], "valid_range[1]") |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 429 | if not error_message == "": |
| 430 | error_message = "Programmer error:\n" + error_message |
| 431 | return error_message |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 432 | valid_range[1] = int(valid_range[1]) |
| 433 | if valid_range[0] is not None and valid_range[0] > valid_range[1]: |
| 434 | error_message = "Programmer error - In the following range, the" +\ |
| 435 | " lower limit is greater than the upper" +\ |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 436 | " limit:\n" + gp.sprint_var(valid_range) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 437 | return error_message |
| 438 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 439 | if len_valid_range == 1: |
| 440 | if var_value < valid_range[0]: |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 441 | error_message += "The following variable is not within the" +\ |
| 442 | " expected range:\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 443 | gp.sprint_varx(get_var_name(var_name), |
| 444 | var_value) +\ |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 445 | gp.sprint_varx("valid_range", |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 446 | str(valid_range[0]) + "..") |
| 447 | return error_message |
| 448 | return error_message |
| 449 | |
| 450 | if valid_range[0] is None: |
| 451 | if var_value > valid_range[1]: |
| 452 | error_message += "The following variable is not within the" +\ |
| 453 | " expected range:\n" +\ |
| 454 | gp.sprint_varx(get_var_name(var_name), |
| 455 | var_value) +\ |
| 456 | gp.sprint_varx("valid_range", |
| 457 | ".." + str(valid_range[1])) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 458 | return error_message |
| 459 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 460 | if var_value < valid_range[0] or var_value > valid_range[1]: |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 461 | error_message += "The following variable is not within the expected" +\ |
| 462 | " range:\n" +\ |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 463 | gp.sprint_varx(get_var_name(var_name), var_value) +\ |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 464 | gp.sprint_varx("valid_range", |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 465 | str(valid_range[0]) + ".." |
| 466 | + str(valid_range[1])) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 467 | return error_message |
| 468 | |
| 469 | return error_message |
| 470 | |
| 471 | |
| 472 | def valid_range(var_value, |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 473 | valid_range=[], |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 474 | var_name=""): |
| 475 | r""" |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 476 | Return True if var_value is within range. Otherwise, print an error |
| 477 | message and either return False or exit(1) depending on the value of |
| 478 | exit_on_error. |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 479 | |
| 480 | Description of arguments: |
| 481 | (See description of arguments for svalid_range (above)). |
| 482 | """ |
| 483 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 484 | error_message = svalid_range(var_value, valid_range, var_name) |
| 485 | return process_error_message(error_message) |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 486 | |
| 487 | |
| 488 | def svalid_list(var_value, |
| 489 | valid_values=[], |
| 490 | var_name=""): |
| 491 | r""" |
| 492 | Return an empty string if var_value is a valid list. Otherwise, return an |
| 493 | error string. |
| 494 | |
| 495 | Description of arguments: |
| 496 | var_value The value (i.e. list) being validated. |
| 497 | valid_values A list of valid values. Each element in |
| 498 | the var_value list must be equal to one of |
| 499 | these values to be considered valid. |
| 500 | var_name The name of the variable whose value is |
| 501 | passed in var_value. This parameter is |
| 502 | normally unnecessary as this function can |
| 503 | figure out the var_name. This is provided |
| 504 | for Robot callers. In this scenario, we |
| 505 | are unable to get the variable name |
| 506 | ourselves. |
| 507 | """ |
| 508 | |
| 509 | error_message = "" |
| 510 | if len(var_value) == 0: |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 511 | error_message += "The \"" + get_var_name(var_name) |
| 512 | error_message += "\" list is empty and is therefore invalid:\n" |
| 513 | return error_message |
| 514 | |
| 515 | found_error = 0 |
| 516 | display_var_value = list(var_value) |
| 517 | for ix in range(0, len(var_value)): |
| 518 | if var_value[ix] not in valid_values: |
| 519 | found_error = 1 |
| 520 | display_var_value[ix] = var_value[ix] + "*" |
| 521 | |
| 522 | if found_error: |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 523 | error_message += "The list entries marked with \"*\" are not valid:\n" |
| 524 | error_message += gp.sprint_varx(get_var_name(var_name), |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 525 | display_var_value, gp.blank()) |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 526 | error_message += gp.sprint_var(valid_values) |
| 527 | return error_message |
| 528 | |
| 529 | return "" |
| 530 | |
| 531 | |
| 532 | def valid_list(var_value, |
| 533 | valid_values=[], |
| 534 | var_name=""): |
| 535 | r""" |
| 536 | Return True if var_value is a valid list. Otherwise, print an error |
| 537 | message and either return False or exit(1) depending on the value of |
| 538 | exit_on_error. |
| 539 | |
| 540 | Description of arguments: |
| 541 | (See description of arguments for svalid_list (above)). |
| 542 | """ |
| 543 | |
| 544 | error_message = svalid_list(var_value, valid_values, var_name) |
| 545 | return process_error_message(error_message) |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 546 | |
| 547 | |
| 548 | def svalid_dict(var_value, |
| 549 | required_keys=[], |
| 550 | var_name=""): |
| 551 | r""" |
| 552 | Return an empty string if var_value is a valid dictionary. Otherwise, |
| 553 | return an error string. |
| 554 | |
| 555 | Description of arguments: |
| 556 | var_value The value (i.e. dictionary) being |
| 557 | validated. |
| 558 | required_keys A list of keys which must be found in the |
| 559 | dictionary for it to be considered valid. |
| 560 | var_name The name of the variable whose value is |
| 561 | passed in var_value. This parameter is |
| 562 | normally unnecessary as this function can |
| 563 | figure out the var_name. This is provided |
| 564 | for Robot callers. In this scenario, we |
| 565 | are unable to get the variable name |
| 566 | ourselves. |
| 567 | """ |
| 568 | |
| 569 | error_message = "" |
| 570 | |
| 571 | keys_missing = list(set(required_keys) - set(var_value.keys())) |
| 572 | if len(keys_missing) > 0: |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 573 | var_name = get_var_name(var_name) |
| 574 | error_message = "The following key fields are missing from " |
| 575 | error_message += var_name + ":\n" |
| 576 | error_message += gp.sprint_var(keys_missing) |
Michael Walsh | 0d5f96a | 2019-05-20 10:09:57 -0500 | [diff] [blame] | 577 | error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 578 | return error_message |
| 579 | |
| 580 | return "" |
| 581 | |
| 582 | |
| 583 | def valid_dict(var_value, |
| 584 | required_keys=[], |
| 585 | var_name=""): |
| 586 | r""" |
| 587 | Return True if var_value is a valid dictionary. Otherwise, print an error |
| 588 | message and either return False or exit(1) depending on the value of |
| 589 | exit_on_error. |
| 590 | |
| 591 | Description of arguments: |
| 592 | (See description of arguments for svalid_list (above)). |
| 593 | """ |
| 594 | |
| 595 | error_message = svalid_dict(var_value, required_keys, var_name) |
| 596 | return process_error_message(error_message) |