1#!/usr/bin/env python
2
3r"""
4This module provides valuable argument processing functions like
5gen_get_options and sprint_args.
6"""
7
8import sys
9import os
10
11import gen_print as gp
12
13
14###############################################################################
15def svalid_value(var_value,
16                 invalid_values=[],
17                 valid_values=[],
18                 var_name=""):
19
20    r"""
21    Return an empty string if var_value is a valid value.  Otherwise, return
22    an error string.
23
24    Description of arguments:
25    var_value                       The value being validated.
26    invalid_values                  A list of invalid values.  If var_value is
27                                    equal to any of these, it is invalid.
28                                    Note that if you specify anything for
29                                    invalid_values (below), the valid_values
30                                    list is not even processed.
31    valid_values                    A list of invalid values.  var_value must
32                                    be equal to one of these values to be
33                                    considered valid.
34    var_name                        The name of the variable whose value is
35                                    passed in var_value.  This parameter is
36                                    normally unnecessary as this function can
37                                    figure out the var_name.  This is provided
38                                    for Robot callers.  In this scenario, we
39                                    are unable to get the variable name
40                                    ourselves.
41    """
42
43    success_message = ""
44    error_message = ""
45    stack_frame_ix = 3
46
47    len_valid_values = len(valid_values)
48    len_invalid_values = len(invalid_values)
49    if len_valid_values > 0 and len_invalid_values > 0:
50        error_message += "Programmer error - You must provide either an" +\
51                         " invalid_values list or a valid_values" +\
52                         " list but NOT both.\n" +\
53                         gp.sprint_var(invalid_values) +\
54                         gp.sprint_var(valid_values)
55        return error_message
56
57    show_blanks = 1
58    if len_valid_values > 0:
59        # Processing the valid_values list.
60        if var_value in valid_values:
61            return success_message
62        if var_name == "":
63            var_name = gp.get_arg_name(0, 1, stack_frame_ix)
64        error_message += "The following variable has an invalid" +\
65                         " value:\n" +\
66                         gp.sprint_varx(var_name, var_value, show_blanks) +\
67                         "\nIt must be one of the following values:\n" +\
68                         gp.sprint_varx("valid_values", valid_values,
69                                        show_blanks)
70        return error_message
71
72    if len_invalid_values == 0:
73        # Assign default value.
74        invalid_values = [""]
75
76    # Assertion: We have an invalid_values list.  Processing it now.
77    if var_value not in invalid_values:
78        return success_message
79
80    if var_name == "":
81        var_name = gp.get_arg_name(0, 1, stack_frame_ix)
82    error_message += "The following variable has an invalid value:\n" +\
83                     gp.sprint_varx(var_name, var_value, show_blanks) +\
84                     "\nIt must NOT be one of the following values:\n" +\
85                     gp.sprint_varx("invalid_values", invalid_values,
86                                    show_blanks)
87    return error_message
88
89###############################################################################
90
91
92###############################################################################
93def valid_value(var_value,
94                invalid_values=[],
95                valid_values=[],
96                var_name=""):
97
98    r"""
99    Return True if var_value is a valid value.  Otherwise, return False and
100    print an error message to stderr.
101
102    Description of arguments:
103    (See description of arguments for svalid_value (above)).
104    """
105
106    error_message = svalid_value(var_value, invalid_values, valid_values,
107                                 var_name)
108
109    if not error_message == "":
110        gp.print_error_report(error_message)
111        return False
112    return True
113
114###############################################################################
115
116
117###############################################################################
118def svalid_integer(var_value,
119                   var_name=""):
120
121    r"""
122    Return an empty string if var_value is a valid integer.  Otherwise, return
123    an error string.
124
125    Description of arguments:
126    var_value                       The value being validated.
127    var_name                        The name of the variable whose value is
128                                    passed in var_value.  This parameter is
129                                    normally unnecessary as this function can
130                                    figure out the var_name.  This is provided
131                                    for Robot callers.  In this scenario, we
132                                    are unable to get the variable name
133                                    ourselves.
134    """
135
136    success_message = ""
137    error_message = ""
138    try:
139        if type(int(str(var_value), 0)) is int:
140            return success_message
141    except ValueError:
142        pass
143
144    # If we get to this point, the validation has failed.
145    if var_name is "":
146        stack_index = 3
147        var_name = gp.get_arg_name(0, 1, stack_index)
148
149    show_blanks = 1
150    error_message += "Invalid integer value:\n" +\
151                     gp.sprint_varx(var_name, var_value, show_blanks)
152
153    return error_message
154
155###############################################################################
156
157
158###############################################################################
159def valid_integer(var_value,
160                  var_name=""):
161
162    r"""
163    Return True if var_value is a valid integer.  Otherwise, return False and
164    print an error message to stderr.
165
166    Description of arguments:
167    (See description of arguments for svalid_value (above)).
168    """
169
170    error_message = svalid_integer(var_value, var_name)
171
172    if not error_message == "":
173        gp.print_error_report(error_message)
174        return False
175    return True
176
177###############################################################################
178
179
180###############################################################################
181def svalid_dir_path(var_value,
182                    var_name=""):
183
184    r"""
185    Return an empty string if var_value is a valid directory path.  Otherwise,
186    return an error string.
187
188    Description of arguments:
189    var_value                       The value being validated.
190    var_name                        The name of the variable whose value is
191                                    passed in var_value.  This parameter is
192                                    normally unnecessary as this function can
193                                    figure out the var_name.  This is provided
194                                    for Robot callers.  In this scenario, we
195                                    are unable to get the variable name
196                                    ourselves.
197    """
198
199    error_message = ""
200    if not os.path.isdir(str(var_value)):
201        if var_name is "":
202            stack_index = 3
203            var_name = gp.get_arg_name(0, 1, stack_index)
204        error_message += "The following directory does not exist:\n" +\
205                         gp.sprint_varx(var_name, var_value)
206
207    return error_message
208
209###############################################################################
210
211
212###############################################################################
213def valid_dir_path(var_value,
214                   var_name=""):
215
216    r"""
217    Return True if var_value is a valid integer.  Otherwise, return False and
218    print an error message to stderr.
219
220    Description of arguments:
221    (See description of arguments for svalid_value (above)).
222    """
223
224    error_message = svalid_dir_path(var_value, var_name)
225
226    if not error_message == "":
227        gp.print_error_report(error_message)
228        return False
229
230    return True
231
232###############################################################################
233
234
235###############################################################################
236def svalid_file_path(var_value,
237                     var_name=""):
238
239    r"""
240    Return an empty string if var_value is a valid file path.  Otherwise,
241    return an error string.
242
243    Description of arguments:
244    var_value                       The value being validated.
245    var_name                        The name of the variable whose value is
246                                    passed in var_value.  This parameter is
247                                    normally unnecessary as this function can
248                                    figure out the var_name.  This is provided
249                                    for Robot callers.  In this scenario, we
250                                    are unable to get the variable name
251                                    ourselves.
252    """
253
254    error_message = ""
255    if not os.path.isfile(str(var_value)):
256        if var_name is "":
257            stack_index = 3
258            var_name = gp.get_arg_name(0, 1, stack_index)
259        error_message += "Invalid file (does not exist):\n" +\
260                         gp.sprint_varx(var_name, var_value)
261
262    return error_message
263
264###############################################################################
265
266
267###############################################################################
268def valid_file_path(var_value,
269                    var_name=""):
270
271    r"""
272    Return True if var_value is a valid integer.  Otherwise, return False and
273    print an error message to stderr.
274
275    Description of arguments:
276    (See description of arguments for svalid_value (above)).
277    """
278
279    error_message = svalid_file_path(var_value, var_name)
280
281    if not error_message == "":
282        gp.print_error_report(error_message)
283        return False
284
285    return True
286
287###############################################################################
288