Adding new function and fixing some bugs in general purpose py files.
gen_args.py:
sprint_args()
I made corrections to col1_width processing when indent is not zero.
gen_print.py:
get_arg_name:
I fixed a bug.
sprint_varx:
I added support for printing OrderedDict objects and robot DotDict objects.
I added support for having the hex arg double as a "print None" for string objects.
sprint_pgm_header:
I added support for linefeed arg.
sissuing:
I added support for test_mode parm.
sprintn:
New function.
gen_valid.py:
svalid_value:
New function:
valid_value:
Now calls svalid_value.
svalid_integer:
New function:
valid_integer:
Now calls svalid_integer.
Change-Id: I161086d1148e4559fcc57b7d749cc3fb810dc19f
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/gen_valid.py b/lib/gen_valid.py
index 1a52ace..e0337b4 100755
--- a/lib/gen_valid.py
+++ b/lib/gen_valid.py
@@ -10,15 +10,15 @@
import gen_print as gp
-
###############################################################################
-def valid_value(var_value,
- invalid_values=[""],
- valid_values=[]):
+def svalid_value(var_value,
+ invalid_values=[],
+ valid_values=[],
+ var_name=""):
r"""
- Return True if var_value is a valid value. Otherwise, return False and
- print an error message to stderr.
+ Return an empty string if var_value is a valid value. Otherwise, return
+ an error string.
Description of arguments:
var_value The value being validated.
@@ -30,50 +30,135 @@
valid_values A list of invalid values. var_value must
be equal to one of these values to be
considered valid.
+ var_name The name of the variable whose value is
+ passed in var_value. This parameter is
+ normally unnecessary as this function can
+ figure out the var_name. This is provided
+ for Robot callers. In this scenario, we
+ are unable to get the variable name
+ ourselves.
"""
+ success_message = ""
+ error_message = ""
+ stack_frame_ix = 3
+
len_valid_values = len(valid_values)
len_invalid_values = len(invalid_values)
if len_valid_values > 0 and len_invalid_values > 0:
- gp.print_error_report("Programmer error - You must provide either an" +
- " invalid_values list or a valid_values" +
- " list but NOT both.")
- return False
+ error_message += "Programmer error - You must provide either an" +\
+ " invalid_values list or a valid_values" +\
+ " list but NOT both.\n" +\
+ gp.sprint_var(invalid_values) +\
+ gp.sprint_var(valid_values)
+ return error_message
+ show_blanks = 1
if len_valid_values > 0:
# Processing the valid_values list.
if var_value in valid_values:
- return True
- var_name = gp.get_arg_name(0, 1, 2)
- gp.print_error_report("The following variable has an invalid" +
- " value:\n" +
- gp.sprint_varx(var_name, var_value) +
- "\nIt must be one of the following values:\n" +
- gp.sprint_varx("valid_values", valid_values))
- return False
+ return success_message
+ if var_name == "":
+ var_name = gp.get_arg_name(0, 1, stack_frame_ix)
+ error_message += "The following variable has an invalid" +\
+ " value:\n" +\
+ gp.sprint_varx(var_name, var_value, show_blanks) +\
+ "\nIt must be one of the following values:\n" +\
+ gp.sprint_varx("valid_values", valid_values,
+ show_blanks)
+ return error_message
if len_invalid_values == 0:
- gp.print_error_report("Programmer error - You must provide either an" +
- " invalid_values list or a valid_values" +
- " list. Both are empty.")
- return False
+ # Assign default value.
+ invalid_values = [""]
# Assertion: We have an invalid_values list. Processing it now.
if var_value not in invalid_values:
- return True
+ return success_message
- var_name = gp.get_arg_name(0, 1, 2)
- gp.print_error_report("The following variable has an invalid value:\n" +
- gp.sprint_varx(var_name, var_value) + "\nIt must" +
- " NOT be one of the following values:\n" +
- gp.sprint_varx("invalid_values", invalid_values))
- return False
+ if var_name == "":
+ var_name = gp.get_arg_name(0, 1, stack_frame_ix)
+ error_message += "The following variable has an invalid value:\n" +\
+ gp.sprint_varx(var_name, var_value, show_blanks) +\
+ "\nIt must NOT be one of the following values:\n" +\
+ gp.sprint_varx("invalid_values", invalid_values,
+ show_blanks)
+ return error_message
###############################################################################
###############################################################################
-def valid_integer(var_value):
+def valid_value(var_value,
+ invalid_values=[],
+ valid_values=[],
+ var_name=""):
+
+ r"""
+ Return True if var_value is a valid value. Otherwise, return False and
+ print an error message to stderr.
+
+ Description of arguments:
+ (See description of arguments for svalid_value (above).
+ """
+
+ error_message = svalid_value(var_value, invalid_values, valid_values,
+ var_name)
+
+ if not error_message == "":
+ gp.print_error_report(error_message)
+ return False
+ return True
+
+###############################################################################
+
+
+###############################################################################
+def svalid_integer(var_value,
+ var_name=""):
+
+ r"""
+ Return an empty string if var_value is a valid integer. Otherwise, return
+ an error string.
+
+ Description of arguments:
+ var_value The value being validated.
+ var_name The name of the variable whose value is
+ passed in var_value. This parameter is
+ normally unnecessary as this function can
+ figure out the var_name. This is provided
+ for Robot callers. In this scenario, we
+ are unable to get the variable name
+ ourselves.
+ """
+
+ # This currently allows floats which is not good.
+
+ success_message = ""
+ error_message = ""
+ try:
+ if type(int(var_value)) is int:
+ return success_message
+ except ValueError:
+ pass
+
+ # If we get to this point, the validation has failed.
+ if var_name is "":
+ stack_index = 3
+ var_name = gp.get_arg_name(0, 1, stack_index)
+
+ show_blanks = 1
+ error_message += "Invalid integer value:\n" +\
+ gp.sprint_varx(var_name, var_value, show_blanks)
+
+ return error_message
+
+###############################################################################
+
+
+###############################################################################
+def valid_integer(var_value,
+ var_name=""):
r"""
Return True if var_value is a valid integer. Otherwise, return False and
@@ -85,20 +170,11 @@
# This currently allows floats which is not good.
- try:
- if type(int(var_value)) is int:
- return True
- except ValueError:
- pass
+ error_message = svalid_integer(var_value, var_name)
- # If we get to this point, the validation has failed.
-
- var_name = gp.get_arg_name(0, 1, 2)
- gp.print_varx("var_name", var_name)
-
- gp.print_error_report("Invalid integer value:\n" +
- gp.sprint_varx(var_name, var_value))
-
- return False
+ if not error_message == "":
+ gp.print_error_report(error_message)
+ return False
+ return True
###############################################################################