blob: ac456627cc74c29f33826614be921b0e4770cf65 [file] [log] [blame]
Michael Walsh7423c012016-10-04 10:27:21 -05001#!/usr/bin/env python
2
3r"""
4This module provides valuable argument processing functions like
5gen_get_options and sprint_args.
6"""
7
8import sys
Michael Walsh78bdfdd2017-01-10 11:27:30 -06009import os
Michael Walsh7423c012016-10-04 10:27:21 -050010
11import gen_print as gp
12
13
Michael Walshbec416d2016-11-10 08:54:52 -060014def svalid_value(var_value,
15 invalid_values=[],
16 valid_values=[],
17 var_name=""):
Michael Walsh7423c012016-10-04 10:27:21 -050018
19 r"""
Michael Walshbec416d2016-11-10 08:54:52 -060020 Return an empty string if var_value is a valid value. Otherwise, return
21 an error string.
Michael Walsh7423c012016-10-04 10:27:21 -050022
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 Walshbec416d2016-11-10 08:54:52 -060033 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 Walsh7423c012016-10-04 10:27:21 -050040 """
41
Michael Walshbec416d2016-11-10 08:54:52 -060042 success_message = ""
43 error_message = ""
44 stack_frame_ix = 3
45
Michael Walsh7423c012016-10-04 10:27:21 -050046 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 Walshbec416d2016-11-10 08:54:52 -060049 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 Walsh7423c012016-10-04 10:27:21 -050055
Michael Walshbec416d2016-11-10 08:54:52 -060056 show_blanks = 1
Michael Walsh7423c012016-10-04 10:27:21 -050057 if len_valid_values > 0:
58 # Processing the valid_values list.
59 if var_value in valid_values:
Michael Walshbec416d2016-11-10 08:54:52 -060060 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 Walsh7423c012016-10-04 10:27:21 -050070
71 if len_invalid_values == 0:
Michael Walshbec416d2016-11-10 08:54:52 -060072 # Assign default value.
73 invalid_values = [""]
Michael Walsh7423c012016-10-04 10:27:21 -050074
75 # Assertion: We have an invalid_values list. Processing it now.
76 if var_value not in invalid_values:
Michael Walshbec416d2016-11-10 08:54:52 -060077 return success_message
Michael Walsh7423c012016-10-04 10:27:21 -050078
Michael Walshbec416d2016-11-10 08:54:52 -060079 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 Walsh7423c012016-10-04 10:27:21 -050087
Michael Walsh7423c012016-10-04 10:27:21 -050088
Michael Walshbec416d2016-11-10 08:54:52 -060089def valid_value(var_value,
90 invalid_values=[],
91 valid_values=[],
92 var_name=""):
93
94 r"""
95 Return True if var_value is a valid value. Otherwise, return False and
96 print an error message to stderr.
97
98 Description of arguments:
Michael Walsh78bdfdd2017-01-10 11:27:30 -060099 (See description of arguments for svalid_value (above)).
Michael Walshbec416d2016-11-10 08:54:52 -0600100 """
101
102 error_message = svalid_value(var_value, invalid_values, valid_values,
103 var_name)
104
105 if not error_message == "":
106 gp.print_error_report(error_message)
107 return False
108 return True
109
Michael Walshbec416d2016-11-10 08:54:52 -0600110
Michael Walshbec416d2016-11-10 08:54:52 -0600111def svalid_integer(var_value,
112 var_name=""):
113
114 r"""
115 Return an empty string if var_value is a valid integer. Otherwise, return
116 an error string.
117
118 Description of arguments:
119 var_value The value being validated.
120 var_name The name of the variable whose value is
121 passed in var_value. This parameter is
122 normally unnecessary as this function can
123 figure out the var_name. This is provided
124 for Robot callers. In this scenario, we
125 are unable to get the variable name
126 ourselves.
127 """
128
Michael Walshbec416d2016-11-10 08:54:52 -0600129 success_message = ""
130 error_message = ""
131 try:
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600132 if type(int(str(var_value), 0)) is int:
Michael Walshbec416d2016-11-10 08:54:52 -0600133 return success_message
134 except ValueError:
135 pass
136
137 # If we get to this point, the validation has failed.
138 if var_name is "":
139 stack_index = 3
140 var_name = gp.get_arg_name(0, 1, stack_index)
141
142 show_blanks = 1
143 error_message += "Invalid integer value:\n" +\
144 gp.sprint_varx(var_name, var_value, show_blanks)
145
146 return error_message
147
Michael Walshbec416d2016-11-10 08:54:52 -0600148
Michael Walshbec416d2016-11-10 08:54:52 -0600149def valid_integer(var_value,
150 var_name=""):
Michael Walsh7423c012016-10-04 10:27:21 -0500151
152 r"""
153 Return True if var_value is a valid integer. Otherwise, return False and
154 print an error message to stderr.
155
156 Description of arguments:
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600157 (See description of arguments for svalid_value (above)).
Michael Walsh7423c012016-10-04 10:27:21 -0500158 """
159
Michael Walshbec416d2016-11-10 08:54:52 -0600160 error_message = svalid_integer(var_value, var_name)
Michael Walsh7423c012016-10-04 10:27:21 -0500161
Michael Walshbec416d2016-11-10 08:54:52 -0600162 if not error_message == "":
163 gp.print_error_report(error_message)
164 return False
165 return True
Michael Walsh7423c012016-10-04 10:27:21 -0500166
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600167
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600168def svalid_dir_path(var_value,
169 var_name=""):
170
171 r"""
172 Return an empty string if var_value is a valid directory path. Otherwise,
173 return an error string.
174
175 Description of arguments:
176 var_value The value being validated.
177 var_name The name of the variable whose value is
178 passed in var_value. This parameter is
179 normally unnecessary as this function can
180 figure out the var_name. This is provided
181 for Robot callers. In this scenario, we
182 are unable to get the variable name
183 ourselves.
184 """
185
186 error_message = ""
187 if not os.path.isdir(str(var_value)):
188 if var_name is "":
189 stack_index = 3
190 var_name = gp.get_arg_name(0, 1, stack_index)
191 error_message += "The following directory does not exist:\n" +\
192 gp.sprint_varx(var_name, var_value)
193
194 return error_message
195
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600196
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600197def valid_dir_path(var_value,
198 var_name=""):
199
200 r"""
201 Return True if var_value is a valid integer. Otherwise, return False and
202 print an error message to stderr.
203
204 Description of arguments:
205 (See description of arguments for svalid_value (above)).
206 """
207
208 error_message = svalid_dir_path(var_value, var_name)
209
210 if not error_message == "":
211 gp.print_error_report(error_message)
212 return False
213
214 return True
215
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600216
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600217def svalid_file_path(var_value,
218 var_name=""):
219
220 r"""
221 Return an empty string if var_value is a valid file path. Otherwise,
222 return an error string.
223
224 Description of arguments:
225 var_value The value being validated.
226 var_name The name of the variable whose value is
227 passed in var_value. This parameter is
228 normally unnecessary as this function can
229 figure out the var_name. This is provided
230 for Robot callers. In this scenario, we
231 are unable to get the variable name
232 ourselves.
233 """
234
235 error_message = ""
236 if not os.path.isfile(str(var_value)):
237 if var_name is "":
238 stack_index = 3
239 var_name = gp.get_arg_name(0, 1, stack_index)
240 error_message += "Invalid file (does not exist):\n" +\
241 gp.sprint_varx(var_name, var_value)
242
243 return error_message
244
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600245
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600246def valid_file_path(var_value,
247 var_name=""):
248
249 r"""
250 Return True if var_value is a valid integer. Otherwise, return False and
251 print an error message to stderr.
252
253 Description of arguments:
254 (See description of arguments for svalid_value (above)).
255 """
256
257 error_message = svalid_file_path(var_value, var_name)
258
259 if not error_message == "":
260 gp.print_error_report(error_message)
261 return False
262
263 return True