blob: fb958e02ef14636608c0ac3a7836d601b1982ee2 [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
Michael Walshde791732016-09-06 14:25:24 -05007import re
Michael Walsh18176322016-11-15 15:11:21 -06008import os
Michael Walsh7423c012016-10-04 10:27:21 -05009
Michael Walshde791732016-09-06 14:25:24 -050010import gen_print as gp
Michael Walshff790b62019-05-17 16:31:16 -050011import func_args as fa
Michael Walsh7423c012016-10-04 10:27:21 -050012
Michael Walshde791732016-09-06 14:25:24 -050013from robot.libraries.BuiltIn import BuiltIn
Michael Walshde791732016-09-06 14:25:24 -050014
Michael Walsh6e646982019-03-28 12:55:31 -050015gen_robot_print_debug = int(os.environ.get('GEN_ROBOT_PRINT_DEBUG', '0'))
Michael Walshde791732016-09-06 14:25:24 -050016
Michael Walshde791732016-09-06 14:25:24 -050017
Michael Walshff790b62019-05-17 16:31:16 -050018def sprint_vars(*args, **kwargs):
Michael Walshde791732016-09-06 14:25:24 -050019 r"""
Michael Walsh6e646982019-03-28 12:55:31 -050020 Sprint the values of one or more variables to the console.
21
Michael Walsh410b1782019-10-22 15:56:18 -050022 This is a robot re-definition of the sprint_vars function in gen_print.py. Given a list of variable
23 names, this keyword will string print each variable name and value such that each value lines up in the
24 same column as messages printed with sprint_time().
Michael Walsh18176322016-11-15 15:11:21 -060025
Michael Walshff790b62019-05-17 16:31:16 -050026 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050027 args The names of the variables to be printed (e.g. var1 rather than ${var1}).
28 kwargs See sprint_varx in gen_print.py for descriptions of all other arguments.
Michael Walsh18176322016-11-15 15:11:21 -060029 """
30
Michael Walshff790b62019-05-17 16:31:16 -050031 if 'fmt' in kwargs:
Michael Walsh410b1782019-10-22 15:56:18 -050032 # Find format option names in kwargs['fmt'] and wrap them with "gp." and "()" to make them into
33 # function calls. For example, verbose would be converted to "gp.verbose()". This allows the user
34 # to simply specify "fmt=verbose" (vs. fmt=gp.verbose()).
35 # Note "terse" has been explicitly added for backward compatibility. Once the repo has been purged
36 # of its use, this code can return to its original form.
Michael Walsh6bed4d32019-07-10 14:11:30 -050037 regex = "(" + "|".join(gp.valid_fmts()) + "|terse)"
Michael Walshff790b62019-05-17 16:31:16 -050038 kwargs['fmt'] = \
39 re.sub(regex, "gp.\\1()", kwargs['fmt'])
40 kwargs = fa.args_to_objects(kwargs)
Michael Walsh18176322016-11-15 15:11:21 -060041 buffer = ""
Michael Walshff790b62019-05-17 16:31:16 -050042 for var_name in args:
Michael Walsh0fa47622017-02-20 16:11:34 -060043 var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}")
Michael Walshff790b62019-05-17 16:31:16 -050044 buffer += gp.sprint_varx(var_name, var_value, **kwargs)
Michael Walsh18176322016-11-15 15:11:21 -060045
46 return buffer
47
Michael Walsh18176322016-11-15 15:11:21 -060048
Michael Walsh18176322016-11-15 15:11:21 -060049def sprint_auto_vars(headers=0):
Michael Walsh18176322016-11-15 15:11:21 -060050 r"""
Michael Walsh410b1782019-10-22 15:56:18 -050051 String print all of the Automatic Variables described in the Robot User's Guide using sprint_vars.
Michael Walshde791732016-09-06 14:25:24 -050052
53 NOTE: Not all automatic variables are guaranteed to exist.
54
Michael Walshff790b62019-05-17 16:31:16 -050055 Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050056 headers This indicates that a header and footer should be printed.
Michael Walshde791732016-09-06 14:25:24 -050057 """
58
Michael Walsh18176322016-11-15 15:11:21 -060059 buffer = ""
Michael Walshde791732016-09-06 14:25:24 -050060 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060061 buffer += gp.sprint_dashes()
62 buffer += "Automatic Variables:"
Michael Walshde791732016-09-06 14:25:24 -050063
Michael Walsh18176322016-11-15 15:11:21 -060064 buffer += \
65 sprint_vars(
66 "TEST_NAME", "TEST_TAGS", "TEST_DOCUMENTATION", "TEST_STATUS",
67 "TEST_DOCUMENTATION", "TEST_STATUS", "TEST_MESSAGE",
68 "PREV_TEST_NAME", "PREV_TEST_STATUS", "PREV_TEST_MESSAGE",
69 "SUITE_NAME", "SUITE_SOURCE", "SUITE_DOCUMENTATION",
70 "SUITE_METADATA", "SUITE_STATUS", "SUITE_MESSAGE",
71 "KEYWORD_STATUS", "KEYWORD_MESSAGE", "LOG_LEVEL", "OUTPUT_FILE",
72 "LOG_FILE", "REPORT_FILE", "DEBUG_FILE", "OUTPUT_DIR")
Michael Walshde791732016-09-06 14:25:24 -050073
74 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060075 buffer += gp.sprint_dashes()
76
77 return buffer
Michael Walshde791732016-09-06 14:25:24 -050078
Michael Walshde791732016-09-06 14:25:24 -050079
Michael Walsh6e646982019-03-28 12:55:31 -050080def gp_debug_print(buffer):
81 r"""
Michael Walshff790b62019-05-17 16:31:16 -050082 Print the buffer value only if gen_robot_print_debug is set.
Michael Walshde791732016-09-06 14:25:24 -050083
Michael Walsh6e646982019-03-28 12:55:31 -050084 This function is intended for use only by other functions in this module.
Michael Walshde791732016-09-06 14:25:24 -050085
Michael Walshff790b62019-05-17 16:31:16 -050086 Description of argument(s):
Michael Walsh6e646982019-03-28 12:55:31 -050087 buffer The string to be printed.
88 """
Michael Walshde791732016-09-06 14:25:24 -050089
Michael Walsh6e646982019-03-28 12:55:31 -050090 if not gen_robot_print_debug:
91 return
Michael Walsh6e646982019-03-28 12:55:31 -050092 gp.gp_print(buffer)
Michael Walsha6723f22016-11-22 11:12:01 -060093
Michael Walshafa7a1b2016-12-09 14:02:48 -060094
Michael Walsh410b1782019-10-22 15:56:18 -050095# In the following section of code, we will dynamically create print versions for several of the sprint
96# functions defined above. For example, where we have an sprint_vars() function defined above that returns
97# formatted variable print outs in a string, we will create a corresponding rprint_vars() function that will
98# print that string directly to stdout.
Michael Walsha6723f22016-11-22 11:12:01 -060099
Michael Walsh410b1782019-10-22 15:56:18 -0500100# It can be complicated to follow what's being created below. Here is an example of the rprint_vars()
101# function that will be created:
Michael Walsha6723f22016-11-22 11:12:01 -0600102
Michael Walshff790b62019-05-17 16:31:16 -0500103# def rprint_vars(*args, **kwargs):
Michael Walsh410b1782019-10-22 15:56:18 -0500104# gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout')
Michael Walshff790b62019-05-17 16:31:16 -0500105
106# For sprint_vars (defined above), the following functions will be created:
107
108# rprint_vars Robot Print Vars
Michael Walsh410b1782019-10-22 15:56:18 -0500109# rqprint_vars Robot Print Vars if ${quiet} is set to ${0}.
110# rdprint_vars Robot Print Vars if ${debug} is set to ${1}.
111# rlprint_vars Robot Print Vars to the log instead of to the console.
Michael Walshff790b62019-05-17 16:31:16 -0500112
113# Abbreviated names are created for all of the preceding function names:
114# rpvars
115# rqpvars
116# rdpvars
117# rlpvars
118
Michael Walsh410b1782019-10-22 15:56:18 -0500119# Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to
120# full names.
Michael Walshff790b62019-05-17 16:31:16 -0500121# Rprint Vars (instead of Rpvars)
Michael Walshde791732016-09-06 14:25:24 -0500122
Michael Walsh6e646982019-03-28 12:55:31 -0500123replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'}
Michael Walshafa7a1b2016-12-09 14:02:48 -0600124
Michael Walsh6e646982019-03-28 12:55:31 -0500125gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600126
Michael Walsh410b1782019-10-22 15:56:18 -0500127# func_names contains a list of all rprint functions which should be created from their sprint counterparts.
Michael Walsh6e646982019-03-28 12:55:31 -0500128func_names = [
129 'print_vars', 'print_auto_vars'
130]
Michael Walsh18176322016-11-15 15:11:21 -0600131
Michael Walsh410b1782019-10-22 15:56:18 -0500132# stderr_func_names is a list of functions whose output should go to stderr rather than stdout.
Michael Walsh6e646982019-03-28 12:55:31 -0500133stderr_func_names = []
Michael Walsh18176322016-11-15 15:11:21 -0600134
Michael Walsh6e646982019-03-28 12:55:31 -0500135func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names,
136 replace_dict, "r")
137gp_debug_print(func_defs)
138exec(func_defs)
Michael Walsh18176322016-11-15 15:11:21 -0600139
Michael Walsh410b1782019-10-22 15:56:18 -0500140# 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 -0600141cmd_buf = "rpvar = rpvars"
Michael Walsh6e646982019-03-28 12:55:31 -0500142gp_debug_print("\n" + cmd_buf + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600143exec(cmd_buf)