blob: 516ef982d6730ffc38b10db71ab1ff29ed2a4cce [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 r"""
Michael Walshbec416d2016-11-10 08:54:52 -060019 Return an empty string if var_value is a valid value. Otherwise, return
20 an error string.
Michael Walsh7423c012016-10-04 10:27:21 -050021
22 Description of arguments:
23 var_value The value being validated.
24 invalid_values A list of invalid values. If var_value is
25 equal to any of these, it is invalid.
26 Note that if you specify anything for
27 invalid_values (below), the valid_values
28 list is not even processed.
29 valid_values A list of invalid values. var_value must
30 be equal to one of these values to be
31 considered valid.
Michael Walshbec416d2016-11-10 08:54:52 -060032 var_name The name of the variable whose value is
33 passed in var_value. This parameter is
34 normally unnecessary as this function can
35 figure out the var_name. This is provided
36 for Robot callers. In this scenario, we
37 are unable to get the variable name
38 ourselves.
Michael Walsh7423c012016-10-04 10:27:21 -050039 """
40
Michael Walshbec416d2016-11-10 08:54:52 -060041 success_message = ""
42 error_message = ""
43 stack_frame_ix = 3
44
Michael Walsh7423c012016-10-04 10:27:21 -050045 len_valid_values = len(valid_values)
46 len_invalid_values = len(invalid_values)
47 if len_valid_values > 0 and len_invalid_values > 0:
Michael Walshbec416d2016-11-10 08:54:52 -060048 error_message += "Programmer error - You must provide either an" +\
49 " invalid_values list or a valid_values" +\
50 " list but NOT both.\n" +\
51 gp.sprint_var(invalid_values) +\
52 gp.sprint_var(valid_values)
53 return error_message
Michael Walsh7423c012016-10-04 10:27:21 -050054
Michael Walshbec416d2016-11-10 08:54:52 -060055 show_blanks = 1
Michael Walsh7423c012016-10-04 10:27:21 -050056 if len_valid_values > 0:
57 # Processing the valid_values list.
58 if var_value in valid_values:
Michael Walshbec416d2016-11-10 08:54:52 -060059 return success_message
60 if var_name == "":
61 var_name = gp.get_arg_name(0, 1, stack_frame_ix)
62 error_message += "The following variable has an invalid" +\
63 " value:\n" +\
64 gp.sprint_varx(var_name, var_value, show_blanks) +\
65 "\nIt must be one of the following values:\n" +\
66 gp.sprint_varx("valid_values", valid_values,
67 show_blanks)
68 return error_message
Michael Walsh7423c012016-10-04 10:27:21 -050069
70 if len_invalid_values == 0:
Michael Walshbec416d2016-11-10 08:54:52 -060071 # Assign default value.
72 invalid_values = [""]
Michael Walsh7423c012016-10-04 10:27:21 -050073
74 # Assertion: We have an invalid_values list. Processing it now.
75 if var_value not in invalid_values:
Michael Walshbec416d2016-11-10 08:54:52 -060076 return success_message
Michael Walsh7423c012016-10-04 10:27:21 -050077
Michael Walshbec416d2016-11-10 08:54:52 -060078 if var_name == "":
79 var_name = gp.get_arg_name(0, 1, stack_frame_ix)
80 error_message += "The following variable has an invalid value:\n" +\
81 gp.sprint_varx(var_name, var_value, show_blanks) +\
82 "\nIt must NOT be one of the following values:\n" +\
83 gp.sprint_varx("invalid_values", invalid_values,
84 show_blanks)
85 return error_message
Michael Walsh7423c012016-10-04 10:27:21 -050086
Michael Walsh7423c012016-10-04 10:27:21 -050087
Michael Walshbec416d2016-11-10 08:54:52 -060088def valid_value(var_value,
89 invalid_values=[],
90 valid_values=[],
91 var_name=""):
Michael Walshbec416d2016-11-10 08:54:52 -060092 r"""
93 Return True if var_value is a valid value. Otherwise, return False and
94 print an error message to stderr.
95
96 Description of arguments:
Michael Walsh78bdfdd2017-01-10 11:27:30 -060097 (See description of arguments for svalid_value (above)).
Michael Walshbec416d2016-11-10 08:54:52 -060098 """
99
100 error_message = svalid_value(var_value, invalid_values, valid_values,
101 var_name)
102
103 if not error_message == "":
104 gp.print_error_report(error_message)
105 return False
106 return True
107
Michael Walshbec416d2016-11-10 08:54:52 -0600108
Michael Walshbec416d2016-11-10 08:54:52 -0600109def svalid_integer(var_value,
110 var_name=""):
Michael Walshbec416d2016-11-10 08:54:52 -0600111 r"""
112 Return an empty string if var_value is a valid integer. Otherwise, return
113 an error string.
114
115 Description of arguments:
116 var_value The value being validated.
117 var_name The name of the variable whose value is
118 passed in var_value. This parameter is
119 normally unnecessary as this function can
120 figure out the var_name. This is provided
121 for Robot callers. In this scenario, we
122 are unable to get the variable name
123 ourselves.
124 """
125
Michael Walshbec416d2016-11-10 08:54:52 -0600126 success_message = ""
127 error_message = ""
128 try:
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600129 if type(int(str(var_value), 0)) is int:
Michael Walshbec416d2016-11-10 08:54:52 -0600130 return success_message
131 except ValueError:
132 pass
133
134 # If we get to this point, the validation has failed.
135 if var_name is "":
136 stack_index = 3
137 var_name = gp.get_arg_name(0, 1, stack_index)
138
139 show_blanks = 1
140 error_message += "Invalid integer value:\n" +\
141 gp.sprint_varx(var_name, var_value, show_blanks)
142
143 return error_message
144
Michael Walshbec416d2016-11-10 08:54:52 -0600145
Michael Walshbec416d2016-11-10 08:54:52 -0600146def valid_integer(var_value,
147 var_name=""):
Michael Walsh7423c012016-10-04 10:27:21 -0500148 r"""
149 Return True if var_value is a valid integer. Otherwise, return False and
150 print an error message to stderr.
151
152 Description of arguments:
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600153 (See description of arguments for svalid_value (above)).
Michael Walsh7423c012016-10-04 10:27:21 -0500154 """
155
Michael Walshbec416d2016-11-10 08:54:52 -0600156 error_message = svalid_integer(var_value, var_name)
Michael Walsh7423c012016-10-04 10:27:21 -0500157
Michael Walshbec416d2016-11-10 08:54:52 -0600158 if not error_message == "":
159 gp.print_error_report(error_message)
160 return False
161 return True
Michael Walsh7423c012016-10-04 10:27:21 -0500162
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600163
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600164def svalid_dir_path(var_value,
165 var_name=""):
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600166 r"""
167 Return an empty string if var_value is a valid directory path. Otherwise,
168 return an error string.
169
170 Description of arguments:
171 var_value The value being validated.
172 var_name The name of the variable whose value is
173 passed in var_value. This parameter is
174 normally unnecessary as this function can
175 figure out the var_name. This is provided
176 for Robot callers. In this scenario, we
177 are unable to get the variable name
178 ourselves.
179 """
180
181 error_message = ""
182 if not os.path.isdir(str(var_value)):
183 if var_name is "":
184 stack_index = 3
185 var_name = gp.get_arg_name(0, 1, stack_index)
186 error_message += "The following directory does not exist:\n" +\
187 gp.sprint_varx(var_name, var_value)
188
189 return error_message
190
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600191
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600192def valid_dir_path(var_value,
193 var_name=""):
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600194 r"""
195 Return True if var_value is a valid integer. Otherwise, return False and
196 print an error message to stderr.
197
198 Description of arguments:
199 (See description of arguments for svalid_value (above)).
200 """
201
202 error_message = svalid_dir_path(var_value, var_name)
203
204 if not error_message == "":
205 gp.print_error_report(error_message)
206 return False
207
208 return True
209
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600210
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600211def svalid_file_path(var_value,
212 var_name=""):
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600213 r"""
214 Return an empty string if var_value is a valid file path. Otherwise,
215 return an error string.
216
217 Description of arguments:
218 var_value The value being validated.
219 var_name The name of the variable whose value is
220 passed in var_value. This parameter is
221 normally unnecessary as this function can
222 figure out the var_name. This is provided
223 for Robot callers. In this scenario, we
224 are unable to get the variable name
225 ourselves.
226 """
227
228 error_message = ""
229 if not os.path.isfile(str(var_value)):
230 if var_name is "":
231 stack_index = 3
232 var_name = gp.get_arg_name(0, 1, stack_index)
233 error_message += "Invalid file (does not exist):\n" +\
234 gp.sprint_varx(var_name, var_value)
235
236 return error_message
237
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600238
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600239def valid_file_path(var_value,
240 var_name=""):
Michael Walsh78bdfdd2017-01-10 11:27:30 -0600241 r"""
242 Return True if var_value is a valid integer. Otherwise, return False and
243 print an error message to stderr.
244
245 Description of arguments:
246 (See description of arguments for svalid_value (above)).
247 """
248
249 error_message = svalid_file_path(var_value, var_name)
250
251 if not error_message == "":
252 gp.print_error_report(error_message)
253 return False
254
255 return True