George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 2 | |
| 3 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 4 | This module provides validation functions like valid_value(), valid_integer(), etc. for robot programs. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 5 | """ |
| 6 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 7 | import re |
Michael Walsh | c108e42 | 2019-03-28 12:27:18 -0500 | [diff] [blame] | 8 | import gen_print as gp |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 9 | import gen_valid as gv |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 10 | import func_args as fa |
| 11 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 12 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 13 | |
| 14 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 15 | def valid_var_name(var_name): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 16 | r""" |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 17 | Validate the robot variable name and return its value. |
Michael Walsh | 3e26e10 | 2017-01-10 11:29:28 -0600 | [diff] [blame] | 18 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 19 | If the variable is undefined, this function will print an error message and call BuiltIn().fail(). |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 20 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 21 | Description of arguments(): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 22 | var_name The name of the robot variable (e.g. "var1"). Do not include "${}" (e.g. |
| 23 | "${var1}". Just provide the simple name of the variable. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 24 | """ |
| 25 | |
| 26 | # Note: get_variable_value() seems to have no trouble with local variables. |
| 27 | var_value = BuiltIn().get_variable_value("${" + var_name + "}") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 28 | if var_value is None: |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 29 | var_value = "<undefined>" |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 30 | error_message = gv.valid_value(var_value, invalid_values=[var_value], |
| 31 | var_name=var_name) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 32 | BuiltIn().fail(error_message) |
| 33 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 34 | return var_value |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 35 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 36 | |
| 37 | def valid_init(var_name, *args, **kwargs): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 38 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 39 | Do initialization for variable validation and return var_name, args and kwargs. |
Michael Walsh | 3e26e10 | 2017-01-10 11:29:28 -0600 | [diff] [blame] | 40 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 41 | This function is to be called by all of the various validation functions in this module. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 42 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 43 | This function is designed solely for use by other functions in this file. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 44 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 45 | Description of argument(s): |
| 46 | var_name The name of the variable to be validated. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 47 | args The positional arguments to be passed to a validation function. |
| 48 | kwargs The keyword arguments to be passed to a validation function. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 49 | """ |
| 50 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 51 | var_value = valid_var_name(var_name) |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 52 | # Convert python string object definitions to objects (useful for robot callers). |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 53 | args = fa.args_to_objects(args) |
| 54 | kwargs = fa.args_to_objects(kwargs) |
| 55 | return var_value, args, kwargs |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 56 | |
| 57 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 58 | def process_error_message(error_message): |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 59 | r""" |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 60 | Process an error message. |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 61 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 62 | If error_message is non-blank, fail. Otherwise, do nothing. |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 63 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 64 | This function is designed solely for use by other functions in this file. |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 65 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 66 | Description of argument(s): |
| 67 | error_message The error message to be processed. |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 68 | """ |
| 69 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 70 | if error_message: |
Michael Walsh | c108e42 | 2019-03-28 12:27:18 -0500 | [diff] [blame] | 71 | error_message = gp.sprint_error_report(error_message) |
Michael Walsh | 2c687e9 | 2018-05-09 11:47:56 -0500 | [diff] [blame] | 72 | BuiltIn().fail(error_message) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 73 | |
| 74 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 75 | # The docstring header will be pre-pended to each validation function's existing docstring. |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 76 | docstring_header = \ |
| 77 | r""" |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 78 | Fail if the variable named by var_name is invalid. |
| 79 | """ |
| 80 | |
| 81 | |
| 82 | def customize_doc_string(doc_string): |
| 83 | r""" |
| 84 | Customize a gen_valid function docstring and return the result. |
| 85 | |
| 86 | This function is designed solely for use by other functions in this file. |
| 87 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 88 | The caller should pass a docstring from a gen_valid.py validation function. This docstring will be |
| 89 | changed to make a suitable docstring for this module's corresponding validation function. |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 90 | |
| 91 | For example: |
| 92 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 93 | Let's suppose that gen_valid.py has a function called "valid_value()". This module could make the |
| 94 | following call to essentially copy gen_valid's "valid_value()" function, modify it and then assign it to |
| 95 | the local version of the valid_value() function. |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 96 | |
| 97 | valid.__doc__ = customize_doc_string(gv.valid.__doc__) |
| 98 | |
| 99 | Description of argument(s): |
| 100 | doc_string The docstring to be customized. |
| 101 | """ |
| 102 | |
| 103 | doc_string = docstring_header + doc_string |
| 104 | doc_string = doc_string.split("\n") |
| 105 | |
| 106 | start_ix = 0 |
| 107 | # Find the "var_value" line. |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 108 | start_ix = next((index for index, value in |
| 109 | enumerate(doc_string[start_ix:], start_ix) |
| 110 | if re.match("[ ]+var_value ", value)), None) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 111 | # Replace the "var_value" line with our "var_name" line. |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 112 | doc_string[start_ix] = " var_name " \ |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 113 | + "The name of the variable to be validated." |
| 114 | |
| 115 | return "\n".join(doc_string) |
| 116 | |
| 117 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 118 | # All of the following functions are robot wrappers for the equivalent functions defined in gen_valid.py. |
| 119 | # Note that the only difference between any two of these locally defined functions is the function name and |
| 120 | # the gv.<function name> which they call. Also, note that the docstring for each is created by modifying the |
| 121 | # docstring from the supporting gen_valid.py function. |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 122 | |
Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 123 | def valid_type(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 124 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 125 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 126 | error_message = \ |
| 127 | gv.valid_type(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 128 | process_error_message(error_message) |
| 129 | |
| 130 | |
| 131 | def valid_value(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 132 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 133 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 134 | error_message = \ |
| 135 | gv.valid_value(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 136 | process_error_message(error_message) |
| 137 | |
| 138 | |
| 139 | def valid_range(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 140 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 141 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 142 | error_message = \ |
| 143 | gv.valid_range(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 144 | process_error_message(error_message) |
| 145 | |
| 146 | |
| 147 | def valid_integer(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 148 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 149 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 150 | error_message = \ |
| 151 | gv.valid_integer(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 152 | process_error_message(error_message) |
| 153 | |
| 154 | |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 155 | def valid_float(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 156 | |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 157 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 158 | error_message = \ |
| 159 | gv.valid_float(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 160 | process_error_message(error_message) |
| 161 | |
| 162 | |
| 163 | def valid_date_time(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 164 | |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 165 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 166 | error_message = \ |
| 167 | gv.valid_date_time(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 8333a18 | 2019-10-22 16:19:00 -0500 | [diff] [blame] | 168 | process_error_message(error_message) |
| 169 | |
| 170 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 171 | def valid_dir_path(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 172 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 173 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 174 | error_message = \ |
| 175 | gv.valid_dir_path(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 176 | process_error_message(error_message) |
| 177 | |
| 178 | |
| 179 | def valid_file_path(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 180 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 181 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 182 | error_message = \ |
| 183 | gv.valid_file_path(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 184 | process_error_message(error_message) |
| 185 | |
| 186 | |
| 187 | def valid_path(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 188 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 189 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 190 | error_message = \ |
| 191 | gv.valid_path(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 192 | process_error_message(error_message) |
| 193 | |
| 194 | |
| 195 | def valid_list(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 196 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 197 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 198 | error_message = \ |
| 199 | gv.valid_list(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 200 | process_error_message(error_message) |
| 201 | |
| 202 | |
| 203 | def valid_dict(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 204 | |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 205 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 206 | error_message = \ |
| 207 | gv.valid_dict(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 208 | process_error_message(error_message) |
| 209 | |
| 210 | |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 211 | def valid_program(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 212 | |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 213 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 214 | error_message = \ |
| 215 | gv.valid_program(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | be3a815 | 2019-08-20 16:38:19 -0500 | [diff] [blame] | 216 | process_error_message(error_message) |
| 217 | |
| 218 | |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 219 | def valid_length(var_name, *args, **kwargs): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 220 | |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 221 | var_value, args, kwargs = valid_init(var_name, *args, **kwargs) |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 222 | error_message = \ |
| 223 | gv.valid_length(var_value, *args, var_name=var_name, **kwargs) |
Michael Walsh | b9d8dfd | 2019-09-11 11:11:12 -0500 | [diff] [blame] | 224 | process_error_message(error_message) |
| 225 | |
| 226 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 227 | # Modify the validation function docstrings by calling customize_doc_string for each function in the |
| 228 | # func_names list. |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 229 | func_names = [ |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 230 | "valid_type", "valid_value", "valid_range", "valid_integer", |
| 231 | "valid_dir_path", "valid_file_path", "valid_path", "valid_list", |
| 232 | "valid_dict", "valid_program", "valid_length", "valid_float", |
| 233 | "valid_date_time" |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 234 | ] |
| 235 | |
| 236 | for func_name in func_names: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 237 | cmd_buf = func_name \ |
| 238 | + ".__doc__ = customize_doc_string(gv.raw_doc_strings['" \ |
| 239 | + func_name + "'])" |
Michael Walsh | 84230c2 | 2019-08-01 12:23:07 -0500 | [diff] [blame] | 240 | exec(cmd_buf) |