blob: d6c95064dc5975a4589022137592250141ac5717 [file] [log] [blame]
Michael Walshde791732016-09-06 14:25:24 -05001#!/usr/bin/env python
2
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 Walsh6e646982019-03-28 12:55:31 -050011import wrap_utils as wu
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 Walsh18176322016-11-15 15:11:21 -060018def sprint_vars(*args):
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
22 This is a robot re=definition of the sprint_vars function in gen_print.py.
23 Given a list of variable names, this keyword will string print each
24 variable name and value such that each value lines up in the same column
25 as messages printed with sprint_time().
Michael Walsh18176322016-11-15 15:11:21 -060026
27 Description of arguments:
28 args:
29 If the first argument is an integer, it will be interpreted to be the
Michael Walsh18176322016-11-15 15:11:21 -060030 "hex" value.
Michael Walshc4da0cb2017-01-10 11:31:05 -060031 If the second argument is an integer, it will be interpreted to be the
32 "indent" value.
33 If the third argument is an integer, it will be interpreted to be the
34 "col1_width" value.
Michael Walsh18176322016-11-15 15:11:21 -060035 All remaining parms are considered variable names which are to be
36 sprinted.
37 """
38
39 if len(args) == 0:
40 return
41
42 # Create list from args (which is a tuple) so that it can be modified.
43 args_list = list(args)
44
Michael Walshc4da0cb2017-01-10 11:31:05 -060045 # See if parm 1 is to be interpreted as "hex".
46 try:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050047 if isinstance(int(args_list[0]), int):
Michael Walshc4da0cb2017-01-10 11:31:05 -060048 hex = int(args_list[0])
49 args_list.pop(0)
50 except ValueError:
51 hex = 0
52
53 # See if parm 2 is to be interpreted as "indent".
Michael Walsh18176322016-11-15 15:11:21 -060054 try:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050055 if isinstance(int(args_list[0]), int):
Michael Walsh18176322016-11-15 15:11:21 -060056 indent = int(args_list[0])
57 args_list.pop(0)
58 except ValueError:
59 indent = 0
60
Michael Walshc4da0cb2017-01-10 11:31:05 -060061 # See if parm 3 is to be interpreted as "col1_width".
Michael Walsh18176322016-11-15 15:11:21 -060062 try:
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -050063 if isinstance(int(args_list[0]), int):
Michael Walsh18176322016-11-15 15:11:21 -060064 loc_col1_width = int(args_list[0])
65 args_list.pop(0)
66 except ValueError:
67 loc_col1_width = gp.col1_width
68
Michael Walsh18176322016-11-15 15:11:21 -060069 buffer = ""
70 for var_name in args_list:
Michael Walsh0fa47622017-02-20 16:11:34 -060071 var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}")
Michael Walsh18176322016-11-15 15:11:21 -060072 buffer += gp.sprint_varx(var_name, var_value, hex, indent,
73 loc_col1_width)
74
75 return buffer
76
Michael Walsh18176322016-11-15 15:11:21 -060077
Michael Walsh18176322016-11-15 15:11:21 -060078def sprint_auto_vars(headers=0):
Michael Walsh18176322016-11-15 15:11:21 -060079 r"""
Michael Walsh6e646982019-03-28 12:55:31 -050080 String print all of the Automatic Variables described in the Robot User's
81 Guide using sprint_vars.
Michael Walshde791732016-09-06 14:25:24 -050082
83 NOTE: Not all automatic variables are guaranteed to exist.
84
85 Description of arguments:
86 headers This indicates that a header and footer
Michael Walsh18176322016-11-15 15:11:21 -060087 should be printed.
Michael Walshde791732016-09-06 14:25:24 -050088 """
89
Michael Walsh18176322016-11-15 15:11:21 -060090 buffer = ""
Michael Walshde791732016-09-06 14:25:24 -050091 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060092 buffer += gp.sprint_dashes()
93 buffer += "Automatic Variables:"
Michael Walshde791732016-09-06 14:25:24 -050094
Michael Walsh18176322016-11-15 15:11:21 -060095 buffer += \
96 sprint_vars(
97 "TEST_NAME", "TEST_TAGS", "TEST_DOCUMENTATION", "TEST_STATUS",
98 "TEST_DOCUMENTATION", "TEST_STATUS", "TEST_MESSAGE",
99 "PREV_TEST_NAME", "PREV_TEST_STATUS", "PREV_TEST_MESSAGE",
100 "SUITE_NAME", "SUITE_SOURCE", "SUITE_DOCUMENTATION",
101 "SUITE_METADATA", "SUITE_STATUS", "SUITE_MESSAGE",
102 "KEYWORD_STATUS", "KEYWORD_MESSAGE", "LOG_LEVEL", "OUTPUT_FILE",
103 "LOG_FILE", "REPORT_FILE", "DEBUG_FILE", "OUTPUT_DIR")
Michael Walshde791732016-09-06 14:25:24 -0500104
105 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -0600106 buffer += gp.sprint_dashes()
107
108 return buffer
Michael Walshde791732016-09-06 14:25:24 -0500109
Michael Walshde791732016-09-06 14:25:24 -0500110
Michael Walsh6e646982019-03-28 12:55:31 -0500111def gp_debug_print(buffer):
112 r"""
113 Print the buffer value only if gen_print_debug is set.
Michael Walshde791732016-09-06 14:25:24 -0500114
Michael Walsh6e646982019-03-28 12:55:31 -0500115 This function is intended for use only by other functions in this module.
Michael Walshde791732016-09-06 14:25:24 -0500116
Michael Walsh6e646982019-03-28 12:55:31 -0500117 Description of arguments:
118 buffer The string to be printed.
119 """
Michael Walshde791732016-09-06 14:25:24 -0500120
Michael Walsh6e646982019-03-28 12:55:31 -0500121 if not gen_robot_print_debug:
122 return
Michael Walshde791732016-09-06 14:25:24 -0500123
Michael Walsh6e646982019-03-28 12:55:31 -0500124 gp.gp_print(buffer)
Michael Walsha6723f22016-11-22 11:12:01 -0600125
Michael Walshafa7a1b2016-12-09 14:02:48 -0600126
Michael Walsh6e646982019-03-28 12:55:31 -0500127# In the following section of code, we will dynamically create print versions
128# for several of the sprint functions defined above. So, for example, where
129# we have an sprint_vars() function defined above that returns formatted
130# variable print outs in a string, we will create a corresponding print_vars()
131# function that will print that string directly to stdout.
Michael Walsha6723f22016-11-22 11:12:01 -0600132
Michael Walsh6e646982019-03-28 12:55:31 -0500133# It can be complicated to follow what's being created below. Here is an
134# example of the rprint_vars() function that will be created:
Michael Walsha6723f22016-11-22 11:12:01 -0600135
Michael Walsh6e646982019-03-28 12:55:31 -0500136# def rprint_vars(*args):
137# gp.gp_print(gp.replace_passwords(sprint_vars(*args)), stream='stdout')
Michael Walshde791732016-09-06 14:25:24 -0500138
Michael Walsh6e646982019-03-28 12:55:31 -0500139replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'}
Michael Walshafa7a1b2016-12-09 14:02:48 -0600140
Michael Walsh6e646982019-03-28 12:55:31 -0500141gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600142
Michael Walsh6e646982019-03-28 12:55:31 -0500143# func_names contains a list of all print functions which should be created
144# from their sprint counterparts.
145func_names = [
146 'print_vars', 'print_auto_vars'
147]
Michael Walsh18176322016-11-15 15:11:21 -0600148
Michael Walsh6e646982019-03-28 12:55:31 -0500149# stderr_func_names is a list of functions whose output should go to stderr
150# rather than stdout.
151stderr_func_names = []
Michael Walsh18176322016-11-15 15:11:21 -0600152
Michael Walsh6e646982019-03-28 12:55:31 -0500153func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names,
154 replace_dict, "r")
155gp_debug_print(func_defs)
156exec(func_defs)
Michael Walsh18176322016-11-15 15:11:21 -0600157
158# Define an alias. rpvar is just a special case of rpvars where the args
159# list contains only one element.
160cmd_buf = "rpvar = rpvars"
Michael Walsh6e646982019-03-28 12:55:31 -0500161gp_debug_print("\n" + cmd_buf + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600162exec(cmd_buf)