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