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