blob: c03c9a3f9145de60e764a80469cf6d01f883054c [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 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 Walshff790b62019-05-17 16:31:16 -050022 This is a robot re-definition of the sprint_vars function in gen_print.py.
Michael Walsh6e646982019-03-28 12:55:31 -050023 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
Michael Walshff790b62019-05-17 16:31:16 -050027 Description of argument(s):
28 args The names of the variables to be printed
29 (e.g. var1 rather than ${var1}).
30 kwargs See sprint_varx in gen_print.py for
31 descriptions of all other arguments.
Michael Walsh18176322016-11-15 15:11:21 -060032 """
33
Michael Walshff790b62019-05-17 16:31:16 -050034 if 'fmt' in kwargs:
35 # Find format option names in kwargs['fmt'] and wrap them with "gp."
Michael Walsh6bed4d32019-07-10 14:11:30 -050036 # and "()" to make them into function calls. For example, verbose
37 # would be converted to "gp.verbose()". This allows the user to
38 # simply specify "fmt=verbose" (vs. fmt=gp.verbose()).
39 # Note "terse" has been explicitly added for backward compatibility.
40 # Once the repo has been purged of its use, this code can return to
41 # its original form.
42 regex = "(" + "|".join(gp.valid_fmts()) + "|terse)"
Michael Walshff790b62019-05-17 16:31:16 -050043 kwargs['fmt'] = \
44 re.sub(regex, "gp.\\1()", kwargs['fmt'])
45 kwargs = fa.args_to_objects(kwargs)
Michael Walsh18176322016-11-15 15:11:21 -060046 buffer = ""
Michael Walshff790b62019-05-17 16:31:16 -050047 for var_name in args:
Michael Walsh0fa47622017-02-20 16:11:34 -060048 var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}")
Michael Walshff790b62019-05-17 16:31:16 -050049 buffer += gp.sprint_varx(var_name, var_value, **kwargs)
Michael Walsh18176322016-11-15 15:11:21 -060050
51 return buffer
52
Michael Walsh18176322016-11-15 15:11:21 -060053
Michael Walsh18176322016-11-15 15:11:21 -060054def sprint_auto_vars(headers=0):
Michael Walsh18176322016-11-15 15:11:21 -060055 r"""
Michael Walsh6e646982019-03-28 12:55:31 -050056 String print all of the Automatic Variables described in the Robot User's
57 Guide using sprint_vars.
Michael Walshde791732016-09-06 14:25:24 -050058
59 NOTE: Not all automatic variables are guaranteed to exist.
60
Michael Walshff790b62019-05-17 16:31:16 -050061 Description of argument(s):
Michael Walshde791732016-09-06 14:25:24 -050062 headers This indicates that a header and footer
Michael Walsh18176322016-11-15 15:11:21 -060063 should be printed.
Michael Walshde791732016-09-06 14:25:24 -050064 """
65
Michael Walsh18176322016-11-15 15:11:21 -060066 buffer = ""
Michael Walshde791732016-09-06 14:25:24 -050067 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060068 buffer += gp.sprint_dashes()
69 buffer += "Automatic Variables:"
Michael Walshde791732016-09-06 14:25:24 -050070
Michael Walsh18176322016-11-15 15:11:21 -060071 buffer += \
72 sprint_vars(
73 "TEST_NAME", "TEST_TAGS", "TEST_DOCUMENTATION", "TEST_STATUS",
74 "TEST_DOCUMENTATION", "TEST_STATUS", "TEST_MESSAGE",
75 "PREV_TEST_NAME", "PREV_TEST_STATUS", "PREV_TEST_MESSAGE",
76 "SUITE_NAME", "SUITE_SOURCE", "SUITE_DOCUMENTATION",
77 "SUITE_METADATA", "SUITE_STATUS", "SUITE_MESSAGE",
78 "KEYWORD_STATUS", "KEYWORD_MESSAGE", "LOG_LEVEL", "OUTPUT_FILE",
79 "LOG_FILE", "REPORT_FILE", "DEBUG_FILE", "OUTPUT_DIR")
Michael Walshde791732016-09-06 14:25:24 -050080
81 if int(headers) == 1:
Michael Walsh18176322016-11-15 15:11:21 -060082 buffer += gp.sprint_dashes()
83
84 return buffer
Michael Walshde791732016-09-06 14:25:24 -050085
Michael Walshde791732016-09-06 14:25:24 -050086
Michael Walsh6e646982019-03-28 12:55:31 -050087def gp_debug_print(buffer):
88 r"""
Michael Walshff790b62019-05-17 16:31:16 -050089 Print the buffer value only if gen_robot_print_debug is set.
Michael Walshde791732016-09-06 14:25:24 -050090
Michael Walsh6e646982019-03-28 12:55:31 -050091 This function is intended for use only by other functions in this module.
Michael Walshde791732016-09-06 14:25:24 -050092
Michael Walshff790b62019-05-17 16:31:16 -050093 Description of argument(s):
Michael Walsh6e646982019-03-28 12:55:31 -050094 buffer The string to be printed.
95 """
Michael Walshde791732016-09-06 14:25:24 -050096
Michael Walsh6e646982019-03-28 12:55:31 -050097 if not gen_robot_print_debug:
98 return
Michael Walsh6e646982019-03-28 12:55:31 -050099 gp.gp_print(buffer)
Michael Walsha6723f22016-11-22 11:12:01 -0600100
Michael Walshafa7a1b2016-12-09 14:02:48 -0600101
Michael Walsh6e646982019-03-28 12:55:31 -0500102# In the following section of code, we will dynamically create print versions
Michael Walshff790b62019-05-17 16:31:16 -0500103# for several of the sprint functions defined above. For example, where we
104# have an sprint_vars() function defined above that returns formatted variable
105# print outs in a string, we will create a corresponding rprint_vars()
Michael Walsh6e646982019-03-28 12:55:31 -0500106# function that will print that string directly to stdout.
Michael Walsha6723f22016-11-22 11:12:01 -0600107
Michael Walsh6e646982019-03-28 12:55:31 -0500108# It can be complicated to follow what's being created below. Here is an
109# example of the rprint_vars() function that will be created:
Michael Walsha6723f22016-11-22 11:12:01 -0600110
Michael Walshff790b62019-05-17 16:31:16 -0500111# def rprint_vars(*args, **kwargs):
112# gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)),
113# stream='stdout')
114
115# For sprint_vars (defined above), the following functions will be created:
116
117# rprint_vars Robot Print Vars
118# rqprint_vars Robot Print Vars if ${quiet} is set to
119# ${0}.
120# rdprint_vars Robot Print Vars if ${debug} is set to
121# ${1}.
122# rlprint_vars Robot Print Vars to the log instead of to
123# the console.
124
125# Abbreviated names are created for all of the preceding function names:
126# rpvars
127# rqpvars
128# rdpvars
129# rlpvars
130
131# Users are encouraged to only use the abbreviated forms for development but
132# to then ultimately switch to full names.
133# Rprint Vars (instead of Rpvars)
Michael Walshde791732016-09-06 14:25:24 -0500134
Michael Walsh6e646982019-03-28 12:55:31 -0500135replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'}
Michael Walshafa7a1b2016-12-09 14:02:48 -0600136
Michael Walsh6e646982019-03-28 12:55:31 -0500137gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600138
Michael Walshff790b62019-05-17 16:31:16 -0500139# func_names contains a list of all rprint functions which should be created
Michael Walsh6e646982019-03-28 12:55:31 -0500140# from their sprint counterparts.
141func_names = [
142 'print_vars', 'print_auto_vars'
143]
Michael Walsh18176322016-11-15 15:11:21 -0600144
Michael Walsh6e646982019-03-28 12:55:31 -0500145# stderr_func_names is a list of functions whose output should go to stderr
146# rather than stdout.
147stderr_func_names = []
Michael Walsh18176322016-11-15 15:11:21 -0600148
Michael Walsh6e646982019-03-28 12:55:31 -0500149func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names,
150 replace_dict, "r")
151gp_debug_print(func_defs)
152exec(func_defs)
Michael Walsh18176322016-11-15 15:11:21 -0600153
154# Define an alias. rpvar is just a special case of rpvars where the args
155# list contains only one element.
156cmd_buf = "rpvar = rpvars"
Michael Walsh6e646982019-03-28 12:55:31 -0500157gp_debug_print("\n" + cmd_buf + "\n")
Michael Walsh18176322016-11-15 15:11:21 -0600158exec(cmd_buf)