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
14def svalid_value(var_value,
15                 invalid_values=[],
16                 valid_values=[],
17                 var_name=""):
18
19    r"""
20    Return an empty string if var_value is a valid value.  Otherwise, return
21    an error string.
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.
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.
40    """
41
42    success_message = ""
43    error_message = ""
44    stack_frame_ix = 3
45
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:
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
55
56    show_blanks = 1
57    if len_valid_values > 0:
58        # Processing the valid_values list.
59        if var_value in valid_values:
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
70
71    if len_invalid_values == 0:
72        # Assign default value.
73        invalid_values = [""]
74
75    # Assertion: We have an invalid_values list.  Processing it now.
76    if var_value not in invalid_values:
77        return success_message
78
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
87
88
89def 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:
99    (See description of arguments for svalid_value (above)).
100    """
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
110
111def 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
129    success_message = ""
130    error_message = ""
131    try:
132        if type(int(str(var_value), 0)) is int:
133            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
148
149def valid_integer(var_value,
150                  var_name=""):
151
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:
157    (See description of arguments for svalid_value (above)).
158    """
159
160    error_message = svalid_integer(var_value, var_name)
161
162    if not error_message == "":
163        gp.print_error_report(error_message)
164        return False
165    return True
166
167
168def 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
196
197def 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
216
217def 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
245
246def 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
264