blob: b0e6a944194369d336fd9159d9ad8fd94cfe7190 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Michael Walshde791732016-09-06 14:25:24 -05002
3r"""
4This file contains functions useful for printing to stdout from robot programs.
5"""
6
George Keishinge635ddc2022-12-08 07:38:02 -06007import os
Patrick Williams20f38712022-12-08 06:18:26 -06008import re
Michael Walsh7423c012016-10-04 10:27:21 -05009
George Keishinge635ddc2022-12-08 07:38:02 -060010import func_args as fa
Patrick Williams20f38712022-12-08 06:18:26 -060011import gen_print as gp
Michael Walshde791732016-09-06 14:25:24 -050012from robot.libraries.BuiltIn import BuiltIn
Michael Walshde791732016-09-06 14:25:24 -050013
Patrick Williams20f38712022-12-08 06:18:26 -060014gen_robot_print_debug = int(os.environ.get("GEN_ROBOT_PRINT_DEBUG", "0"))
Michael Walshde791732016-09-06 14:25:24 -050015
Michael Walshde791732016-09-06 14:25:24 -050016
Michael Walshff790b62019-05-17 16:31:16 -050017def sprint_vars(*args, **kwargs):
Michael Walshde791732016-09-06 14:25:24 -050018 r"""
Michael Walsh6e646982019-03-28 12:55:31 -050019 Sprint the values of one or more variables to the console.
20
Michael Walsh410b1782019-10-22 15:56:18 -050021 This is a robot re-definition of the sprint_vars function in gen_print.py. Given a list of variable
22 names, this keyword will string print each variable name and value such that each value lines up in the
23 same column as messages printed with sprint_time().
Michael Walsh18176322016-11-15 15:11:21 -060024
Michael Walshff790b62019-05-17 16:31:16 -050025 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050026 args The names of the variables to be printed (e.g. var1 rather than ${var1}).
27 kwargs See sprint_varx in gen_print.py for descriptions of all other arguments.
Michael Walsh18176322016-11-15 15:11:21 -060028 """
29
Patrick Williams20f38712022-12-08 06:18:26 -060030 if "fmt" in kwargs:
Michael Walsh410b1782019-10-22 15:56:18 -050031 # Find format option names in kwargs['fmt'] and wrap them with "gp." and "()" to make them into
32 # function calls. For example, verbose would be converted to "gp.verbose()". This allows the user
33 # to simply specify "fmt=verbose" (vs. fmt=gp.verbose()).
34 # Note "terse" has been explicitly added for backward compatibility. Once the repo has been purged
35 # of its use, this code can return to its original form.
Michael Walsh6bed4d32019-07-10 14:11:30 -050036 regex = "(" + "|".join(gp.valid_fmts()) + "|terse)"
Patrick Williams20f38712022-12-08 06:18:26 -060037 kwargs["fmt"] = re.sub(regex, "gp.\\1()", kwargs["fmt"])
Michael Walshff790b62019-05-17 16:31:16 -050038 kwargs = fa.args_to_objects(kwargs)
Michael Walsh18176322016-11-15 15:11:21 -060039 buffer = ""
Michael Walshff790b62019-05-17 16:31:16 -050040 for var_name in args:
Michael Walsh0fa47622017-02-20 16:11:34 -060041 var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}")
Michael Walshff790b62019-05-17 16:31:16 -050042 buffer += gp.sprint_varx(var_name, var_value, **kwargs)
Michael Walsh18176322016-11-15 15:11:21 -060043
44 return buffer
45
Michael Walsh18176322016-11-15 15:11:21 -060046
Michael Walsh18176322016-11-15 15:11:21 -060047def sprint_auto_vars(headers=0):
Michael Walsh18176322016-11-15 15:11:21 -060048 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050049 String print all of the Automatic Variables described in the Robot User's Guide using sprint_vars.
Michael Walshde791732016-09-06 14:25:24 -050050
51 NOTE: Not all automatic variables are guaranteed to exist.
52
Michael Walshff790b62019-05-17 16:31:16 -050053 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050054 headers This indicates that a header and footer should be printed.
Michael Walshde791732016-09-06 14:25:24 -050055 """
56
Michael Walsh18176322016-11-15 15:11:21 -060057 buffer = ""
Michael Walshde791732016-09-06 14:25:24 -050058 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060059 buffer += gp.sprint_dashes()
60 buffer += "Automatic Variables:"
Michael Walshde791732016-09-06 14:25:24 -050061
Patrick Williams20f38712022-12-08 06:18:26 -060062 buffer += sprint_vars(
63 "TEST_NAME",
64 "TEST_TAGS",
65 "TEST_DOCUMENTATION",
66 "TEST_STATUS",
67 "TEST_DOCUMENTATION",
68 "TEST_STATUS",
69 "TEST_MESSAGE",
70 "PREV_TEST_NAME",
71 "PREV_TEST_STATUS",
72 "PREV_TEST_MESSAGE",
73 "SUITE_NAME",
74 "SUITE_SOURCE",
75 "SUITE_DOCUMENTATION",
76 "SUITE_METADATA",
77 "SUITE_STATUS",
78 "SUITE_MESSAGE",
79 "KEYWORD_STATUS",
80 "KEYWORD_MESSAGE",
81 "LOG_LEVEL",
82 "OUTPUT_FILE",
83 "LOG_FILE",
84 "REPORT_FILE",
85 "DEBUG_FILE",
86 "OUTPUT_DIR",
87 )
Michael Walshde791732016-09-06 14:25:24 -050088
89 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060090 buffer += gp.sprint_dashes()
91
92 return buffer
Michael Walshde791732016-09-06 14:25:24 -050093
Michael Walshde791732016-09-06 14:25:24 -050094
Michael Walsh6e646982019-03-28 12:55:31 -050095def gp_debug_print(buffer):
96 r"""
Michael Walshff790b62019-05-17 16:31:16 -050097 Print the buffer value only if gen_robot_print_debug is set.
Michael Walshde791732016-09-06 14:25:24 -050098
Michael Walsh6e646982019-03-28 12:55:31 -050099 This function is intended for use only by other functions in this module.
Michael Walshde791732016-09-06 14:25:24 -0500100
Michael Walshff790b62019-05-17 16:31:16 -0500101 Description of argument(s):
Michael Walsh6e646982019-03-28 12:55:31 -0500102 buffer The string to be printed.
103 """
Michael Walshde791732016-09-06 14:25:24 -0500104
Michael Walsh6e646982019-03-28 12:55:31 -0500105 if not gen_robot_print_debug:
106 return
Michael Walsh6e646982019-03-28 12:55:31 -0500107 gp.gp_print(buffer)
Michael Walsha6723f22016-11-22 11:12:01 -0600108
Michael Walshafa7a1b2016-12-09 14:02:48 -0600109
Michael Walsh410b1782019-10-22 15:56:18 -0500110# In the following section of code, we will dynamically create print versions for several of the sprint
111# functions defined above. For example, where we have an sprint_vars() function defined above that returns
112# formatted variable print outs in a string, we will create a corresponding rprint_vars() function that will
113# print that string directly to stdout.
Michael Walsha6723f22016-11-22 11:12:01 -0600114
Michael Walsh410b1782019-10-22 15:56:18 -0500115# It can be complicated to follow what's being created below. Here is an example of the rprint_vars()
116# function that will be created:
Michael Walsha6723f22016-11-22 11:12:01 -0600117
Michael Walshff790b62019-05-17 16:31:16 -0500118# def rprint_vars(*args, **kwargs):
Michael Walsh410b1782019-10-22 15:56:18 -0500119# gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout')
Michael Walshff790b62019-05-17 16:31:16 -0500120
121# For sprint_vars (defined above), the following functions will be created:
122
123# rprint_vars Robot Print Vars
Michael Walsh410b1782019-10-22 15:56:18 -0500124# rqprint_vars Robot Print Vars if ${quiet} is set to ${0}.
125# rdprint_vars Robot Print Vars if ${debug} is set to ${1}.
126# rlprint_vars Robot Print Vars to the log instead of to the console.
Michael Walshff790b62019-05-17 16:31:16 -0500127
128# Abbreviated names are created for all of the preceding function names:
129# rpvars
130# rqpvars
131# rdpvars
132# rlpvars
133
Michael Walsh410b1782019-10-22 15:56:18 -0500134# Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to
135# full names.
Michael Walshff790b62019-05-17 16:31:16 -0500136# Rprint Vars (instead of Rpvars)
Michael Walshde791732016-09-06 14:25:24 -0500137
Patrick Williams20f38712022-12-08 06:18:26 -0600138replace_dict = {"output_stream": "stdout", "mod_qualifier": "gp."}
Michael Walshafa7a1b2016-12-09 14:02:48 -0600139
Michael Walsh6e646982019-03-28 12:55:31 -0500140gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600141
Michael Walsh410b1782019-10-22 15:56:18 -0500142# func_names contains a list of all rprint functions which should be created from their sprint counterparts.
Patrick Williams20f38712022-12-08 06:18:26 -0600143func_names = ["print_vars", "print_auto_vars"]
Michael Walsh18176322016-11-15 15:11:21 -0600144
Michael Walsh410b1782019-10-22 15:56:18 -0500145# stderr_func_names is a list of functions whose output should go to stderr rather than stdout.
Michael Walsh6e646982019-03-28 12:55:31 -0500146stderr_func_names = []
Michael Walsh18176322016-11-15 15:11:21 -0600147
Patrick Williams20f38712022-12-08 06:18:26 -0600148func_defs = gp.create_print_wrapper_funcs(
149 func_names, stderr_func_names, replace_dict, "r"
150)
Michael Walsh6e646982019-03-28 12:55:31 -0500151gp_debug_print(func_defs)
152exec(func_defs)
Michael Walsh18176322016-11-15 15:11:21 -0600153
Michael Walsh410b1782019-10-22 15:56:18 -0500154# Define an alias. rpvar is just a special case of rpvars where the args list contains only one element.
Michael Walsh18176322016-11-15 15:11:21 -0600155cmd_buf = "rpvar = rpvars"
Michael Walsh6e646982019-03-28 12:55:31 -0500156gp_debug_print("\n" + cmd_buf + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600157exec(cmd_buf)