valid_list: enforce only one of three args
- valid_values allows only one of the following three arguments:
- valid_values
- invalid_values
- required_values
It was incorrectly enforcing this rule.
- Change to allow 110 chars vs. 79 chars for comments and docstrings.
Change-Id: Idcd3b27fa2445f22698365b81c1b96014d141d4d
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/gen_valid.py b/lib/gen_valid.py
index 91a68fb..34c406b 100755
--- a/lib/gen_valid.py
+++ b/lib/gen_valid.py
@@ -1,8 +1,7 @@
#!/usr/bin/env python
r"""
-This module provides validation functions like valid_value(), valid_integer(),
-etc.
+This module provides validation functions like valid_value(), valid_integer(), etc.
"""
import os
@@ -17,8 +16,8 @@
r"""
Set the exit_on_error value to either True or False.
- If exit_on_error is set, validation functions like valid_value() will exit
- the program on error instead of returning False.
+ If exit_on_error is set, validation functions like valid_value() will exit the program on error instead
+ of returning False.
Description of argument(s):
value Value to set global exit_on_error to.
@@ -30,9 +29,8 @@
def get_var_name(var_name):
r"""
- If var_name is not None, simply return its value. Otherwise, get the
- variable name of the first argument used to call the validation function
- (e.g. valid, valid_integer, etc.) and return it.
+ If var_name is not None, simply return its value. Otherwise, get the variable name of the first argument
+ used to call the validation function (e.g. valid, valid_integer, etc.) and return it.
This function is designed solely for use by other functions in this file.
@@ -75,30 +73,28 @@
NOTE: A blank error_message means that there is no error.
- For the following explanations, assume the caller of this function is a
- function with the following definition:
+ For the following explanations, assume the caller of this function is a function with the following
+ definition:
valid_value(var_value, valid_values=[], invalid_values=[], var_name=None):
- If the user of valid_value() is assigning the valid_value() return value
- to a variable, process_error_message() will simply return the
- error_message. This mode of usage is illustrated by the following example:
+ If the user of valid_value() is assigning the valid_value() return value to a variable,
+ process_error_message() will simply return the error_message. This mode of usage is illustrated by the
+ following example:
error_message = valid_value(var1)
- This mode is useful for callers who wish to validate a variable and then
- decide for themselves what to do with the error_message (e.g.
- raise(error_message), BuiltIn().fail(error_message), etc.).
+ This mode is useful for callers who wish to validate a variable and then decide for themselves what to do
+ with the error_message (e.g. raise(error_message), BuiltIn().fail(error_message), etc.).
- If the user of valid_value() is NOT assigning the valid_value() return
- value to a variable, process_error_message() will behave as follows.
+ If the user of valid_value() is NOT assigning the valid_value() return value to a variable,
+ process_error_message() will behave as follows.
- First, if error_message is non-blank, it will be printed to stderr via a
- call to gp.print_error_report(error_message).
+ First, if error_message is non-blank, it will be printed to stderr via a call to
+ gp.print_error_report(error_message).
If exit_on_error is set:
- If the error_message is blank, simply return.
- - If the error_message is non-blank, exit the program with a return code
- of 1.
+ - If the error_message is non-blank, exit the program with a return code of 1.
If exit_on_error is NOT set:
- If the error_message is blank, return True.
@@ -108,8 +104,7 @@
error_message An error message.
"""
- # Determine whether the caller's caller is assigning the result to a
- # variable.
+ # Determine whether the caller's caller is assigning the result to a variable.
l_value = gp.get_arg_name(None, -1, stack_frame_ix=3)
if l_value:
return error_message
@@ -125,8 +120,7 @@
return False
-# Note to programmers: All of the validation functions in this module should
-# follow the same basic template:
+# Note to programmers: All of the validation functions in this module should follow the same basic template:
# def valid_value(var_value, var1, var2, varn, var_name=None):
#
# error_message = ""
@@ -138,26 +132,22 @@
# return process_error_message(error_message)
-# The docstring header and footer will be added to each validation function's
-# existing docstring.
+# The docstring header and footer will be added to each validation function's existing docstring.
docstring_header = \
r"""
Determine whether var_value is valid, construct an error_message and call
process_error_message(error_message).
- See the process_error_message() function defined in this module for a
- description of how error messages are processed.
+ See the process_error_message() function defined in this module for a description of how error messages
+ are processed.
"""
additional_args_docstring_footer = \
r"""
- var_name The name of the variable whose value is
- passed in var_value. For the general
- case, this argument is unnecessary as this
- function can figure out the var_name.
- This is provided for Robot callers in
- which case, this function lacks the
- ability to determine the variable name.
+ var_name The name of the variable whose value is passed in var_value. For the
+ general case, this argument is unnecessary as this function can figure
+ out the var_name. This is provided for Robot callers in which case, this
+ function lacks the ability to determine the variable name.
"""
@@ -173,8 +163,7 @@
Description of argument(s):
var_value The value being validated.
- required_type A type or a tuple of types (e.g. str, int,
- etc.).
+ required_type A type or a tuple of types (e.g. str, int, etc.).
"""
error_message = ""
@@ -199,11 +188,10 @@
def valid_value(var_value, valid_values=[], invalid_values=[], var_name=None):
r"""
- The variable value is valid if it is either contained in the valid_values
- list or if it is NOT contained in the invalid_values list. If the caller
- specifies nothing for either of these 2 arguments, invalid_values will be
- initialized to ['', None]. This is a good way to fail on variables which
- contain blank values.
+ The variable value is valid if it is either contained in the valid_values list or if it is NOT contained
+ in the invalid_values list. If the caller specifies nothing for either of these 2 arguments,
+ invalid_values will be initialized to ['', None]. This is a good way to fail on variables which contain
+ blank values.
It is illegal to specify both valid_values and invalid values.
@@ -212,8 +200,7 @@
var1 = ''
valid_value(var1)
- This code would fail because var1 is blank and the default value for
- invalid_values is ['', None].
+ This code would fail because var1 is blank and the default value for invalid_values is ['', None].
Example:
var1 = 'yes'
@@ -223,12 +210,10 @@
Description of argument(s):
var_value The value being validated.
- valid_values A list of valid values. The variable
- value must be equal to one of these values
- to be considered valid.
- invalid_values A list of invalid values. If the variable
- value is equal to any of these, it is
- considered invalid.
+ valid_values A list of valid values. The variable value must be equal to one of these
+ values to be considered valid.
+ invalid_values A list of invalid values. If the variable value is equal to any of
+ these, it is considered invalid.
"""
error_message = ""
@@ -285,17 +270,15 @@
r"""
The variable value is valid if it is within the specified range.
- This function can be used with any type of operands where they can have a
- greater than/less than relationship to each other (e.g. int, float, str).
+ This function can be used with any type of operands where they can have a greater than/less than
+ relationship to each other (e.g. int, float, str).
Description of argument(s):
var_value The value being validated.
- lower The lower end of the range. If not None,
- the var_value must be greater than or
- equal to lower.
- upper The upper end of the range. If not None,
- the var_value must be less than or equal
- to upper.
+ lower The lower end of the range. If not None, the var_value must be greater
+ than or equal to lower.
+ upper The upper end of the range. If not None, the var_value must be less than
+ or equal to upper.
"""
error_message = ""
@@ -327,20 +310,17 @@
def valid_integer(var_value, lower=None, upper=None, var_name=None):
r"""
- The variable value is valid if it is an integer or can be interpreted as
- an integer (e.g. 7, "7", etc.).
+ The variable value is valid if it is an integer or can be interpreted as an integer (e.g. 7, "7", etc.).
- This function also calls valid_range to make sure the integer value is
- within the specified range (if any).
+ This function also calls valid_range to make sure the integer value is within the specified range (if
+ any).
Description of argument(s):
var_value The value being validated.
- lower The lower end of the range. If not None,
- the var_value must be greater than or
- equal to lower.
- upper The upper end of the range. If not None,
- the var_value must be less than or equal
- to upper.
+ lower The lower end of the range. If not None, the var_value must be greater
+ than or equal to lower.
+ upper The upper end of the range. If not None, the var_value must be less than
+ or equal to upper.
"""
error_message = ""
@@ -365,8 +345,7 @@
def valid_dir_path(var_value, var_name=None):
r"""
- The variable value is valid if it contains the path of an existing
- directory.
+ The variable value is valid if it contains the path of an existing directory.
Description of argument(s):
var_value The value being validated.
@@ -400,8 +379,7 @@
def valid_path(var_value, var_name=None):
r"""
- The variable value is valid if it contains the path of an existing file or
- directory.
+ The variable value is valid if it contains the path of an existing file or directory.
Description of argument(s):
var_value The value being validated.
@@ -419,44 +397,34 @@
def valid_list(var_value, valid_values=[], invalid_values=[],
required_values=[], fail_on_empty=False, var_name=None):
r"""
- The variable value is valid if it is a list where each entry can be found
- in the valid_values list or if none of its values can be found in the
- invalid_values list or if all of the values in the required_values list
- can be found in var_value.
+ The variable value is valid if it is a list where each entry can be found in the valid_values list or if
+ none of its values can be found in the invalid_values list or if all of the values in the required_values
+ list can be found in var_value.
- The caller may only specify one of these 3 arguments: valid_values,
- invalid_values, required_values.
+ The caller may only specify one of these 3 arguments: valid_values, invalid_values, required_values.
Description of argument(s):
var_value The value being validated.
- valid_values A list of valid values. Each element in
- the var_value list must be equal to one of
- these values to be considered valid.
- invalid_values A list of invalid values. If any element
- in var_value is equal to any of the values
- in this argument, var_value is considered
- invalid.
- required_values Every value in required_values must be
- found in var_value. Otherwise, var_value
- is considered invalid.
- fail_on_empty Indicates that an empty list for the
- variable value should be considered an
- error.
+ valid_values A list of valid values. Each element in the var_value list must be equal
+ to one of these values to be considered valid.
+ invalid_values A list of invalid values. If any element in var_value is equal to any of
+ the values in this argument, var_value is considered invalid.
+ required_values Every value in required_values must be found in var_value. Otherwise,
+ var_value is considered invalid.
+ fail_on_empty Indicates that an empty list for the variable value should be considered
+ an error.
"""
error_message = ""
# Validate this function's arguments.
- len_valid_values = len(valid_values)
- len_invalid_values = len(invalid_values)
- len_required_values = len(required_values)
- if (len_valid_values + len_invalid_values + len_required_values) > 1:
+ if not (bool(len(valid_values)) ^ bool(len(invalid_values)) ^ bool(len(required_values))):
error_message += "Programmer error - You must provide only one of the"
error_message += " following: valid_values, invalid_values,"
error_message += " required_values.\n"
- error_message += gp.sprint_var(invalid_values)
- error_message += gp.sprint_var(valid_values)
- error_message += gp.sprint_var(required_values)
+ error_message += gp.sprint_var(invalid_values, gp.show_type())
+ error_message += gp.sprint_var(valid_values, gp.show_type())
+ error_message += gp.sprint_var(required_values, gp.show_type())
return process_error_message(error_message)
if type(var_value) is not list:
@@ -536,13 +504,12 @@
def valid_dict(var_value, required_keys=[], var_name=None):
r"""
- The variable value is valid if it is a dictionary containing all of the
- required keys.
+ The variable value is valid if it is a dictionary containing all of the required keys.
Description of argument(s):
var_value The value being validated.
- required_keys A list of keys which must be found in the
- dictionary for it to be considered valid.
+ required_keys A list of keys which must be found in the dictionary for it to be
+ considered valid.
"""
error_message = ""
@@ -560,8 +527,8 @@
def valid_program(var_value, var_name=None):
r"""
- The variable value is valid if it contains the name of a program which can
- be located using the "which" command.
+ The variable value is valid if it contains the name of a program which can be located using the "which"
+ command.
Description of argument(s):
var_value The value being validated.
@@ -583,17 +550,15 @@
def valid_length(var_value, min_length=None, max_length=None, var_name=None):
r"""
- The variable value is valid if it is an object (e.g. list, dictionary)
- whose length is within the specified range.
+ The variable value is valid if it is an object (e.g. list, dictionary) whose length is within the
+ specified range.
Description of argument(s):
var_value The value being validated.
- min_length The minimum length of the object. If not
- None, the length of var_value must be
- greater than or equal to min_length.
- max_length The maximum length of the object. If not
- None, the length of var_value must be less
- than or equal to min_length.
+ min_length The minimum length of the object. If not None, the length of var_value
+ must be greater than or equal to min_length.
+ max_length The maximum length of the object. If not None, the length of var_value
+ must be less than or equal to min_length.
"""
error_message = ""
@@ -603,10 +568,10 @@
var_name = get_var_name(var_name)
error_message = "The length of the following object is not within the"
error_message += " expected range:\n"
+ error_message += gp.sprint_vars(min_length, max_length)
error_message += gp.sprint_var(length)
error_message += gp.sprint_varx(var_name, var_value, gp.blank())
error_message += "\n"
- error_message += gp.sprint_vars(min_length, max_length)
return process_error_message(error_message)
return process_error_message(error_message)