Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 4 | This module provides validation functions like valid_value(), valid_integer(), etc. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 5 | """ |
| 6 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 7 | import os |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 8 | import gen_print as gp |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 9 | import gen_cmd as gc |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 10 | import func_args as fa |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 11 | import datetime |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 12 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 13 | exit_on_error = False |
| 14 | |
| 15 | |
| 16 | def set_exit_on_error(value): |
| 17 | r""" |
| 18 | Set the exit_on_error value to either True or False. |
| 19 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 20 | If exit_on_error is set, validation functions like valid_value() will exit the program on error instead |
| 21 | of returning False. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 22 | |
| 23 | Description of argument(s): |
| 24 | value Value to set global exit_on_error to. |
| 25 | """ |
| 26 | |
| 27 | global exit_on_error |
| 28 | exit_on_error = value |
| 29 | |
| 30 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 31 | def get_var_name(var_name): |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 32 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 33 | If var_name is not None, simply return its value. Otherwise, get the variable name of the first argument |
| 34 | used to call the validation function (e.g. valid, valid_integer, etc.) and return it. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 35 | |
| 36 | This function is designed solely for use by other functions in this file. |
| 37 | |
| 38 | Example: |
| 39 | |
| 40 | A programmer codes this: |
| 41 | |
| 42 | valid_value(last_name) |
| 43 | |
| 44 | Which results in the following call stack: |
| 45 | |
| 46 | valid_value(last_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 47 | -> get_var_name(var_name) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 48 | |
| 49 | In this example, this function will return "last_name". |
| 50 | |
| 51 | Example: |
| 52 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 53 | err_msg = valid_value(last_name, var_name="some_other_name") |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 54 | |
| 55 | Which results in the following call stack: |
| 56 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 57 | valid_value(var_value, var_name="some_other_name") |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 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 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 66 | return var_name or gp.get_arg_name(0, 1, stack_frame_ix=3) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def process_error_message(error_message): |
| 70 | r""" |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 71 | Process the error_message in the manner described below. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 72 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 73 | This function is designed solely for use by other functions in this file. |
| 74 | |
| 75 | NOTE: A blank error_message means that there is no error. |
| 76 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 77 | For the following explanations, assume the caller of this function is a function with the following |
| 78 | definition: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 79 | valid_value(var_value, valid_values=[], invalid_values=[], var_name=None): |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 80 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 81 | If the user of valid_value() is assigning the valid_value() return value to a variable, |
| 82 | process_error_message() will simply return the error_message. This mode of usage is illustrated by the |
| 83 | following example: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 84 | |
| 85 | error_message = valid_value(var1) |
| 86 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 87 | This mode is useful for callers who wish to validate a variable and then decide for themselves what to do |
| 88 | with the error_message (e.g. raise(error_message), BuiltIn().fail(error_message), etc.). |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 89 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 90 | If the user of valid_value() is NOT assigning the valid_value() return value to a variable, |
| 91 | process_error_message() will behave as follows. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 92 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 93 | First, if error_message is non-blank, it will be printed to stderr via a call to |
| 94 | gp.print_error_report(error_message). |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 95 | |
| 96 | If exit_on_error is set: |
| 97 | - If the error_message is blank, simply return. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 98 | - If the error_message is non-blank, exit the program with a return code of 1. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 99 | |
| 100 | If exit_on_error is NOT set: |
| 101 | - If the error_message is blank, return True. |
| 102 | - If the error_message is non-blank, return False. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 103 | |
| 104 | Description of argument(s): |
| 105 | error_message An error message. |
| 106 | """ |
| 107 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 108 | # Determine whether the caller's caller is assigning the result to a variable. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 109 | l_value = gp.get_arg_name(None, -1, stack_frame_ix=3) |
| 110 | if l_value: |
| 111 | return error_message |
| 112 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 113 | if error_message == "": |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 114 | if exit_on_error: |
| 115 | return |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 116 | return True |
| 117 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 118 | gp.print_error_report(error_message, stack_frame_ix=4) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 119 | if exit_on_error: |
Michael Walsh | 10a4d98 | 2018-10-30 13:07:46 -0500 | [diff] [blame] | 120 | exit(1) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 121 | return False |
| 122 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 123 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 124 | # Note to programmers: All of the validation functions in this module should follow the same basic template: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 125 | # def valid_value(var_value, var1, var2, varn, var_name=None): |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 126 | # |
| 127 | # error_message = "" |
| 128 | # if not valid: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 129 | # var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 130 | # error_message += "The following variable is invalid because...:\n" |
| 131 | # error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
| 132 | # |
| 133 | # return process_error_message(error_message) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 134 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 135 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 136 | # The docstring header and footer will be added to each validation function's existing docstring. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 137 | docstring_header = \ |
| 138 | r""" |
| 139 | Determine whether var_value is valid, construct an error_message and call |
| 140 | process_error_message(error_message). |
| 141 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 142 | See the process_error_message() function defined in this module for a description of how error messages |
| 143 | are processed. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 144 | """ |
| 145 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 146 | additional_args_docstring_footer = \ |
| 147 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 148 | var_name The name of the variable whose value is passed in var_value. For the |
| 149 | general case, this argument is unnecessary as this function can figure |
| 150 | out the var_name. This is provided for Robot callers in which case, this |
| 151 | function lacks the ability to determine the variable name. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 152 | """ |
| 153 | |
| 154 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 155 | def valid_type(var_value, required_type, var_name=None): |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 156 | r""" |
| 157 | The variable value is valid if it is of the required type. |
| 158 | |
| 159 | Examples: |
| 160 | |
| 161 | valid_type(var1, int) |
| 162 | |
| 163 | valid_type(var1, (list, dict)) |
| 164 | |
| 165 | Description of argument(s): |
| 166 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 167 | required_type A type or a tuple of types (e.g. str, int, etc.). |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 168 | """ |
| 169 | |
| 170 | error_message = "" |
| 171 | if type(required_type) is tuple: |
| 172 | if type(var_value) in required_type: |
| 173 | return process_error_message(error_message) |
| 174 | else: |
| 175 | if type(var_value) is required_type: |
| 176 | return process_error_message(error_message) |
| 177 | |
| 178 | # If we get to this point, the validation has failed. |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 179 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 180 | error_message += "Invalid variable type:\n" |
| 181 | error_message += gp.sprint_varx(var_name, var_value, |
| 182 | gp.blank() | gp.show_type()) |
| 183 | error_message += "\n" |
| 184 | error_message += gp.sprint_var(required_type) |
| 185 | |
| 186 | return process_error_message(error_message) |
| 187 | |
| 188 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 189 | def valid_value(var_value, valid_values=[], invalid_values=[], var_name=None): |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 190 | |
| 191 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 192 | The variable value is valid if it is either contained in the valid_values list or if it is NOT contained |
| 193 | in the invalid_values list. If the caller specifies nothing for either of these 2 arguments, |
| 194 | invalid_values will be initialized to ['', None]. This is a good way to fail on variables which contain |
| 195 | blank values. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 196 | |
| 197 | It is illegal to specify both valid_values and invalid values. |
| 198 | |
| 199 | Example: |
| 200 | |
| 201 | var1 = '' |
| 202 | valid_value(var1) |
| 203 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 204 | This code would fail because var1 is blank and the default value for invalid_values is ['', None]. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 205 | |
| 206 | Example: |
| 207 | var1 = 'yes' |
| 208 | valid_value(var1, valid_values=['yes', 'true']) |
| 209 | |
| 210 | This code would pass. |
| 211 | |
| 212 | Description of argument(s): |
| 213 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 214 | valid_values A list of valid values. The variable value must be equal to one of these |
| 215 | values to be considered valid. |
| 216 | invalid_values A list of invalid values. If the variable value is equal to any of |
| 217 | these, it is considered invalid. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 218 | """ |
| 219 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 220 | error_message = "" |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 221 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 222 | # Validate this function's arguments. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 223 | len_valid_values = len(valid_values) |
| 224 | len_invalid_values = len(invalid_values) |
| 225 | if len_valid_values > 0 and len_invalid_values > 0: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 226 | error_message += "Programmer error - You must provide either an" |
| 227 | error_message += " invalid_values list or a valid_values" |
| 228 | error_message += " list but NOT both:\n" |
| 229 | error_message += gp.sprint_var(invalid_values) |
| 230 | error_message += gp.sprint_var(valid_values) |
| 231 | return process_error_message(error_message) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 232 | |
Michael Walsh | 0f5fa53 | 2020-02-13 10:51:40 -0600 | [diff] [blame] | 233 | error_message = valid_type(valid_values, list, var_name='valid_values') |
| 234 | if error_message: |
| 235 | return process_error_message(error_message) |
| 236 | |
| 237 | error_message = valid_type(invalid_values, list, var_name='invalid_values') |
| 238 | if error_message: |
| 239 | return process_error_message(error_message) |
| 240 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 241 | if len_valid_values > 0: |
| 242 | # Processing the valid_values list. |
| 243 | if var_value in valid_values: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 244 | return process_error_message(error_message) |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 245 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 246 | error_message += "Invalid variable value:\n" |
| 247 | error_message += gp.sprint_varx(var_name, var_value, |
| 248 | gp.blank() | gp.verbose() |
| 249 | | gp.show_type()) |
| 250 | error_message += "\n" |
| 251 | error_message += "It must be one of the following values:\n" |
| 252 | error_message += "\n" |
| 253 | error_message += gp.sprint_var(valid_values, |
| 254 | gp.blank() | gp.show_type()) |
| 255 | return process_error_message(error_message) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 256 | |
| 257 | if len_invalid_values == 0: |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 258 | # Assign default value. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 259 | invalid_values = ["", None] |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 260 | |
| 261 | # Assertion: We have an invalid_values list. Processing it now. |
| 262 | if var_value not in invalid_values: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 263 | return process_error_message(error_message) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 264 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 265 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 266 | error_message += "Invalid variable value:\n" |
| 267 | error_message += gp.sprint_varx(var_name, var_value, |
| 268 | gp.blank() | gp.verbose() |
| 269 | | gp.show_type()) |
| 270 | error_message += "\n" |
| 271 | error_message += "It must NOT be one of the following values:\n" |
| 272 | error_message += "\n" |
| 273 | error_message += gp.sprint_var(invalid_values, |
| 274 | gp.blank() | gp.show_type()) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 275 | return process_error_message(error_message) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 276 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 277 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 278 | def valid_range(var_value, lower=None, upper=None, var_name=None): |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 279 | r""" |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 280 | The variable value is valid if it is within the specified range. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 281 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 282 | This function can be used with any type of operands where they can have a greater than/less than |
| 283 | relationship to each other (e.g. int, float, str). |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 284 | |
| 285 | Description of argument(s): |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 286 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 287 | lower The lower end of the range. If not None, the var_value must be greater |
| 288 | than or equal to lower. |
| 289 | upper The upper end of the range. If not None, the var_value must be less than |
| 290 | or equal to upper. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 291 | """ |
| 292 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 293 | error_message = "" |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 294 | if lower is None and upper is None: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 295 | return process_error_message(error_message) |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 296 | if lower is None and var_value <= upper: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 297 | return process_error_message(error_message) |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 298 | if upper is None and var_value >= lower: |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 299 | return process_error_message(error_message) |
| 300 | if lower and upper: |
| 301 | if lower > upper: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 302 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 303 | error_message += "Programmer error - the lower value is greater" |
| 304 | error_message += " than the upper value:\n" |
| 305 | error_message += gp.sprint_vars(lower, upper, fmt=gp.show_type()) |
| 306 | return process_error_message(error_message) |
| 307 | if lower <= var_value <= upper: |
| 308 | return process_error_message(error_message) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 309 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 310 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 311 | error_message += "The following variable is not within the expected" |
| 312 | error_message += " range:\n" |
| 313 | error_message += gp.sprint_varx(var_name, var_value, gp.show_type()) |
| 314 | error_message += "\n" |
| 315 | error_message += "range:\n" |
| 316 | error_message += gp.sprint_vars(lower, upper, fmt=gp.show_type(), indent=2) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 317 | return process_error_message(error_message) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 318 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 319 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 320 | def valid_integer(var_value, lower=None, upper=None, var_name=None): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 321 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 322 | The variable value is valid if it is an integer or can be interpreted as an integer (e.g. 7, "7", etc.). |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 323 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 324 | This function also calls valid_range to make sure the integer value is within the specified range (if |
| 325 | any). |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 326 | |
| 327 | Description of argument(s): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 328 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 329 | lower The lower end of the range. If not None, the var_value must be greater |
| 330 | than or equal to lower. |
| 331 | upper The upper end of the range. If not None, the var_value must be less than |
| 332 | or equal to upper. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 333 | """ |
| 334 | |
| 335 | error_message = "" |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 336 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 337 | try: |
| 338 | var_value = int(str(var_value), 0) |
| 339 | except ValueError: |
| 340 | error_message += "Invalid integer value:\n" |
| 341 | error_message += gp.sprint_varx(var_name, var_value, |
| 342 | gp.blank() | gp.show_type()) |
| 343 | return process_error_message(error_message) |
| 344 | |
| 345 | # Check the range (if any). |
| 346 | if lower: |
| 347 | lower = int(str(lower), 0) |
| 348 | if upper: |
| 349 | upper = int(str(upper), 0) |
| 350 | error_message = valid_range(var_value, lower, upper, var_name=var_name) |
| 351 | |
| 352 | return process_error_message(error_message) |
| 353 | |
| 354 | |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 355 | def valid_float(var_value, lower=None, upper=None, var_name=None): |
| 356 | r""" |
| 357 | The variable value is valid if it is a floating point value or can be interpreted as a floating point |
| 358 | value (e.g. 7.5, "7.5", etc.). |
| 359 | |
| 360 | This function also calls valid_range to make sure the float value is within the specified range (if any). |
| 361 | |
| 362 | Description of argument(s): |
| 363 | var_value The value being validated. |
| 364 | lower The lower end of the range. If not None, the var_value must be greater |
| 365 | than or equal to lower. |
| 366 | upper The upper end of the range. If not None, the var_value must be less than |
| 367 | or equal to upper. |
| 368 | """ |
| 369 | |
| 370 | error_message = "" |
| 371 | var_name = get_var_name(var_name) |
| 372 | try: |
| 373 | var_value = float(str(var_value)) |
| 374 | except ValueError: |
| 375 | error_message += "Invalid float value:\n" |
| 376 | error_message += gp.sprint_varx(var_name, var_value, |
| 377 | gp.blank() | gp.show_type()) |
| 378 | return process_error_message(error_message) |
| 379 | |
| 380 | # Check the range (if any). |
| 381 | if lower: |
| 382 | lower = float(str(lower)) |
| 383 | if upper: |
| 384 | upper = float(str(upper)) |
| 385 | error_message = valid_range(var_value, lower, upper, var_name=var_name) |
| 386 | |
| 387 | return process_error_message(error_message) |
| 388 | |
| 389 | |
| 390 | def valid_date_time(var_value, var_name=None): |
| 391 | r""" |
| 392 | The variable value is valid if it can be interpreted as a date/time (e.g. "14:49:49.981", "tomorrow", |
| 393 | etc.) by the linux date command. |
| 394 | |
| 395 | Description of argument(s): |
| 396 | var_value The value being validated. |
| 397 | """ |
| 398 | |
| 399 | error_message = "" |
| 400 | rc, out_buf = gc.shell_cmd("date -d '" + str(var_value) + "'", quiet=1, show_err=0, ignore_err=1) |
| 401 | if rc: |
| 402 | var_name = get_var_name(var_name) |
| 403 | error_message += "Invalid date/time value:\n" |
| 404 | error_message += gp.sprint_varx(var_name, var_value, |
| 405 | gp.blank() | gp.show_type()) |
| 406 | return process_error_message(error_message) |
| 407 | |
| 408 | return process_error_message(error_message) |
| 409 | |
| 410 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 411 | def valid_dir_path(var_value, var_name=None): |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 412 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 413 | The variable value is valid if it contains the path of an existing directory. |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 414 | |
| 415 | Description of argument(s): |
| 416 | var_value The value being validated. |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 417 | """ |
| 418 | |
| 419 | error_message = "" |
| 420 | if not os.path.isdir(str(var_value)): |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 421 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 422 | error_message += "The following directory does not exist:\n" |
Michael Walsh | 0024434 | 2019-08-29 10:35:50 -0500 | [diff] [blame] | 423 | error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 424 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 425 | return process_error_message(error_message) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 426 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 427 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 428 | def valid_file_path(var_value, var_name=None): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 429 | r""" |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 430 | The variable value is valid if it contains the path of an existing file. |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 431 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 432 | Description of argument(s): |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 433 | var_value The value being validated. |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 434 | """ |
| 435 | |
| 436 | error_message = "" |
| 437 | if not os.path.isfile(str(var_value)): |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 438 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 439 | error_message += "The following file does not exist:\n" |
Michael Walsh | 0024434 | 2019-08-29 10:35:50 -0500 | [diff] [blame] | 440 | error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 441 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 442 | return process_error_message(error_message) |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 443 | |
Michael Walsh | 78bdfdd | 2017-01-10 11:27:30 -0600 | [diff] [blame] | 444 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 445 | def valid_path(var_value, var_name=None): |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 446 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 447 | The variable value is valid if it contains the path of an existing file or directory. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 448 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 449 | Description of argument(s): |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 450 | var_value The value being validated. |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 451 | """ |
| 452 | |
| 453 | error_message = "" |
| 454 | if not (os.path.isfile(str(var_value)) or os.path.isdir(str(var_value))): |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 455 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 456 | error_message += "Invalid path (file or directory does not exist):\n" |
Michael Walsh | 0024434 | 2019-08-29 10:35:50 -0500 | [diff] [blame] | 457 | error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 458 | |
Michael Walsh | e23b5ad | 2018-06-01 14:54:35 -0500 | [diff] [blame] | 459 | return process_error_message(error_message) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 460 | |
| 461 | |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 462 | def valid_list(var_value, valid_values=[], invalid_values=[], |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 463 | required_values=[], fail_on_empty=False, var_name=None): |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 464 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 465 | The variable value is valid if it is a list where each entry can be found in the valid_values list or if |
| 466 | none of its values can be found in the invalid_values list or if all of the values in the required_values |
| 467 | list can be found in var_value. |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 468 | |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 469 | The caller may only specify one of these 3 arguments: valid_values, invalid_values, required_values. |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 470 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 471 | Description of argument(s): |
| 472 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 473 | valid_values A list of valid values. Each element in the var_value list must be equal |
| 474 | to one of these values to be considered valid. |
| 475 | invalid_values A list of invalid values. If any element in var_value is equal to any of |
| 476 | the values in this argument, var_value is considered invalid. |
| 477 | required_values Every value in required_values must be found in var_value. Otherwise, |
| 478 | var_value is considered invalid. |
| 479 | fail_on_empty Indicates that an empty list for the variable value should be considered |
| 480 | an error. |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 481 | """ |
| 482 | |
| 483 | error_message = "" |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 484 | |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 485 | # Validate this function's arguments. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 486 | if not (bool(len(valid_values)) ^ bool(len(invalid_values)) ^ bool(len(required_values))): |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 487 | error_message += "Programmer error - You must provide only one of the" |
| 488 | error_message += " following: valid_values, invalid_values," |
| 489 | error_message += " required_values.\n" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 490 | error_message += gp.sprint_var(invalid_values, gp.show_type()) |
| 491 | error_message += gp.sprint_var(valid_values, gp.show_type()) |
| 492 | error_message += gp.sprint_var(required_values, gp.show_type()) |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 493 | return process_error_message(error_message) |
| 494 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 495 | if type(var_value) is not list: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 496 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 497 | error_message = valid_type(var_value, list, var_name=var_name) |
| 498 | if error_message: |
| 499 | return process_error_message(error_message) |
| 500 | |
| 501 | if fail_on_empty and len(var_value) == 0: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 502 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 503 | error_message += "Invalid empty list:\n" |
| 504 | error_message += gp.sprint_varx(var_name, var_value, gp.show_type()) |
| 505 | return process_error_message(error_message) |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 506 | |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 507 | if len(required_values): |
| 508 | found_error = 0 |
| 509 | display_required_values = list(required_values) |
| 510 | for ix in range(0, len(required_values)): |
| 511 | if required_values[ix] not in var_value: |
| 512 | found_error = 1 |
| 513 | display_required_values[ix] = \ |
| 514 | str(display_required_values[ix]) + "*" |
| 515 | if found_error: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 516 | var_name = get_var_name(var_name) |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 517 | error_message += "The following list is invalid:\n" |
| 518 | error_message += gp.sprint_varx(var_name, var_value, |
| 519 | gp.blank() | gp.show_type()) |
| 520 | error_message += "\n" |
| 521 | error_message += "Because some of the values in the " |
| 522 | error_message += "required_values list are not present (see" |
| 523 | error_message += " entries marked with \"*\"):\n" |
| 524 | error_message += "\n" |
| 525 | error_message += gp.sprint_varx('required_values', |
| 526 | display_required_values, |
| 527 | gp.blank() | gp.show_type()) |
| 528 | error_message += "\n" |
| 529 | |
| 530 | return process_error_message(error_message) |
| 531 | |
| 532 | if len(invalid_values): |
| 533 | found_error = 0 |
| 534 | display_var_value = list(var_value) |
| 535 | for ix in range(0, len(var_value)): |
| 536 | if var_value[ix] in invalid_values: |
| 537 | found_error = 1 |
| 538 | display_var_value[ix] = str(var_value[ix]) + "*" |
| 539 | |
| 540 | if found_error: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 541 | var_name = get_var_name(var_name) |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 542 | error_message += "The following list is invalid (see entries" |
| 543 | error_message += " marked with \"*\"):\n" |
| 544 | error_message += gp.sprint_varx(var_name, display_var_value, |
| 545 | gp.blank() | gp.show_type()) |
| 546 | error_message += "\n" |
| 547 | error_message += gp.sprint_var(invalid_values, gp.show_type()) |
| 548 | return process_error_message(error_message) |
| 549 | |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 550 | found_error = 0 |
| 551 | display_var_value = list(var_value) |
| 552 | for ix in range(0, len(var_value)): |
| 553 | if var_value[ix] not in valid_values: |
| 554 | found_error = 1 |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 555 | display_var_value[ix] = str(var_value[ix]) + "*" |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 556 | |
| 557 | if found_error: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 558 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 559 | error_message += "The following list is invalid (see entries marked" |
| 560 | error_message += " with \"*\"):\n" |
| 561 | error_message += gp.sprint_varx(var_name, display_var_value, |
| 562 | gp.blank() | gp.show_type()) |
| 563 | error_message += "\n" |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 564 | error_message += gp.sprint_var(valid_values, gp.show_type()) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 565 | return process_error_message(error_message) |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 566 | |
Michael Walsh | ca19399 | 2018-08-02 17:20:00 -0500 | [diff] [blame] | 567 | return process_error_message(error_message) |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 568 | |
| 569 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 570 | def valid_dict(var_value, required_keys=[], var_name=None): |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 571 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 572 | The variable value is valid if it is a dictionary containing all of the required keys. |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 573 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 574 | Description of argument(s): |
| 575 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 576 | required_keys A list of keys which must be found in the dictionary for it to be |
| 577 | considered valid. |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 578 | """ |
| 579 | |
| 580 | error_message = "" |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 581 | missing_keys = list(set(required_keys) - set(var_value.keys())) |
| 582 | if len(missing_keys) > 0: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 583 | var_name = get_var_name(var_name) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 584 | error_message += "The following dictionary is invalid because it is" |
| 585 | error_message += " missing required keys:\n" |
| 586 | error_message += gp.sprint_varx(var_name, var_value, |
| 587 | gp.blank() | gp.show_type()) |
| 588 | error_message += "\n" |
Michael Walsh | 35026be | 2019-08-14 17:13:39 -0500 | [diff] [blame] | 589 | error_message += gp.sprint_var(missing_keys, gp.show_type()) |
Michael Walsh | 7ac5fd8 | 2018-10-11 16:58:49 -0500 | [diff] [blame] | 590 | return process_error_message(error_message) |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 591 | |
| 592 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 593 | def valid_program(var_value, var_name=None): |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 594 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 595 | The variable value is valid if it contains the name of a program which can be located using the "which" |
| 596 | command. |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 597 | |
| 598 | Description of argument(s): |
| 599 | var_value The value being validated. |
| 600 | """ |
| 601 | |
| 602 | error_message = "" |
| 603 | rc, out_buf = gc.shell_cmd("which " + var_value, quiet=1, show_err=0, |
| 604 | ignore_err=1) |
| 605 | if rc: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 606 | var_name = get_var_name(var_name) |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 607 | error_message += "The following required program could not be found" |
| 608 | error_message += " using the $PATH environment variable:\n" |
Michael Walsh | 0024434 | 2019-08-29 10:35:50 -0500 | [diff] [blame] | 609 | error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 610 | PATH = os.environ.get("PATH", "").split(":") |
| 611 | error_message += "\n" |
| 612 | error_message += gp.sprint_var(PATH) |
| 613 | return process_error_message(error_message) |
| 614 | |
| 615 | |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 616 | def valid_length(var_value, min_length=None, max_length=None, var_name=None): |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 617 | r""" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 618 | The variable value is valid if it is an object (e.g. list, dictionary) whose length is within the |
| 619 | specified range. |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 620 | |
| 621 | Description of argument(s): |
| 622 | var_value The value being validated. |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 623 | min_length The minimum length of the object. If not None, the length of var_value |
| 624 | must be greater than or equal to min_length. |
| 625 | max_length The maximum length of the object. If not None, the length of var_value |
| 626 | must be less than or equal to min_length. |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 627 | """ |
| 628 | |
| 629 | error_message = "" |
| 630 | length = len(var_value) |
| 631 | error_message = valid_range(length, min_length, max_length) |
| 632 | if error_message: |
Michael Walsh | 89eff54 | 2019-09-13 16:24:51 -0500 | [diff] [blame] | 633 | var_name = get_var_name(var_name) |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 634 | error_message = "The length of the following object is not within the" |
| 635 | error_message += " expected range:\n" |
Michael Walsh | da20f84 | 2019-10-16 11:36:40 -0500 | [diff] [blame] | 636 | error_message += gp.sprint_vars(min_length, max_length) |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 637 | error_message += gp.sprint_var(length) |
| 638 | error_message += gp.sprint_varx(var_name, var_value, gp.blank()) |
| 639 | error_message += "\n" |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 640 | return process_error_message(error_message) |
| 641 | |
| 642 | return process_error_message(error_message) |
| 643 | |
| 644 | |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 645 | # Modify selected function docstrings by adding headers/footers. |
| 646 | |
| 647 | func_names = [ |
| 648 | "valid_type", "valid_value", "valid_range", "valid_integer", |
| 649 | "valid_dir_path", "valid_file_path", "valid_path", "valid_list", |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 650 | "valid_dict", "valid_program", "valid_length", "valid_float", |
| 651 | "valid_date_time" |
Michael Walsh | 018e25f | 2019-08-01 11:27:12 -0500 | [diff] [blame] | 652 | ] |
| 653 | |
| 654 | raw_doc_strings = {} |
| 655 | |
| 656 | for func_name in func_names: |
| 657 | cmd_buf = "raw_doc_strings['" + func_name + "'] = " + func_name |
| 658 | cmd_buf += ".__doc__" |
| 659 | exec(cmd_buf) |
| 660 | cmd_buf = func_name + ".__doc__ = docstring_header + " + func_name |
| 661 | cmd_buf += ".__doc__.rstrip(\" \\n\") + additional_args_docstring_footer" |
| 662 | exec(cmd_buf) |