1e7e9171eSGeorge Keishing#!/usr/bin/env python3 2de79173eSMichael Walsh 3de79173eSMichael Walshr""" 4de79173eSMichael WalshThis file contains functions useful for printing to stdout from robot programs. 5de79173eSMichael Walsh""" 6de79173eSMichael Walsh 7e635ddc0SGeorge Keishingimport os 8*20f38712SPatrick Williamsimport re 97423c01aSMichael Walsh 10e635ddc0SGeorge Keishingimport func_args as fa 11*20f38712SPatrick Williamsimport gen_print as gp 12de79173eSMichael Walshfrom robot.libraries.BuiltIn import BuiltIn 13de79173eSMichael Walsh 14*20f38712SPatrick Williamsgen_robot_print_debug = int(os.environ.get("GEN_ROBOT_PRINT_DEBUG", "0")) 15de79173eSMichael Walsh 16de79173eSMichael Walsh 17ff790b6dSMichael Walshdef sprint_vars(*args, **kwargs): 18de79173eSMichael Walsh r""" 196e64698aSMichael Walsh Sprint the values of one or more variables to the console. 206e64698aSMichael Walsh 21410b1787SMichael Walsh This is a robot re-definition of the sprint_vars function in gen_print.py. Given a list of variable 22410b1787SMichael Walsh names, this keyword will string print each variable name and value such that each value lines up in the 23410b1787SMichael Walsh same column as messages printed with sprint_time(). 2418176322SMichael Walsh 25ff790b6dSMichael Walsh Description of argument(s): 26410b1787SMichael Walsh args The names of the variables to be printed (e.g. var1 rather than ${var1}). 27410b1787SMichael Walsh kwargs See sprint_varx in gen_print.py for descriptions of all other arguments. 2818176322SMichael Walsh """ 2918176322SMichael Walsh 30*20f38712SPatrick Williams if "fmt" in kwargs: 31410b1787SMichael Walsh # Find format option names in kwargs['fmt'] and wrap them with "gp." and "()" to make them into 32410b1787SMichael Walsh # function calls. For example, verbose would be converted to "gp.verbose()". This allows the user 33410b1787SMichael Walsh # to simply specify "fmt=verbose" (vs. fmt=gp.verbose()). 34410b1787SMichael Walsh # Note "terse" has been explicitly added for backward compatibility. Once the repo has been purged 35410b1787SMichael Walsh # of its use, this code can return to its original form. 366bed4d34SMichael Walsh regex = "(" + "|".join(gp.valid_fmts()) + "|terse)" 37*20f38712SPatrick Williams kwargs["fmt"] = re.sub(regex, "gp.\\1()", kwargs["fmt"]) 38ff790b6dSMichael Walsh kwargs = fa.args_to_objects(kwargs) 3918176322SMichael Walsh buffer = "" 40ff790b6dSMichael Walsh for var_name in args: 410fa47624SMichael Walsh var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}") 42ff790b6dSMichael Walsh buffer += gp.sprint_varx(var_name, var_value, **kwargs) 4318176322SMichael Walsh 4418176322SMichael Walsh return buffer 4518176322SMichael Walsh 4618176322SMichael Walsh 4718176322SMichael Walshdef sprint_auto_vars(headers=0): 4818176322SMichael Walsh r""" 49410b1787SMichael Walsh String print all of the Automatic Variables described in the Robot User's Guide using sprint_vars. 50de79173eSMichael Walsh 51de79173eSMichael Walsh NOTE: Not all automatic variables are guaranteed to exist. 52de79173eSMichael Walsh 53ff790b6dSMichael Walsh Description of argument(s): 54410b1787SMichael Walsh headers This indicates that a header and footer should be printed. 55de79173eSMichael Walsh """ 56de79173eSMichael Walsh 5718176322SMichael Walsh buffer = "" 58de79173eSMichael Walsh if int(headers) == 1: 5918176322SMichael Walsh buffer += gp.sprint_dashes() 6018176322SMichael Walsh buffer += "Automatic Variables:" 61de79173eSMichael Walsh 62*20f38712SPatrick Williams buffer += sprint_vars( 63*20f38712SPatrick Williams "TEST_NAME", 64*20f38712SPatrick Williams "TEST_TAGS", 65*20f38712SPatrick Williams "TEST_DOCUMENTATION", 66*20f38712SPatrick Williams "TEST_STATUS", 67*20f38712SPatrick Williams "TEST_DOCUMENTATION", 68*20f38712SPatrick Williams "TEST_STATUS", 69*20f38712SPatrick Williams "TEST_MESSAGE", 70*20f38712SPatrick Williams "PREV_TEST_NAME", 71*20f38712SPatrick Williams "PREV_TEST_STATUS", 72*20f38712SPatrick Williams "PREV_TEST_MESSAGE", 73*20f38712SPatrick Williams "SUITE_NAME", 74*20f38712SPatrick Williams "SUITE_SOURCE", 75*20f38712SPatrick Williams "SUITE_DOCUMENTATION", 76*20f38712SPatrick Williams "SUITE_METADATA", 77*20f38712SPatrick Williams "SUITE_STATUS", 78*20f38712SPatrick Williams "SUITE_MESSAGE", 79*20f38712SPatrick Williams "KEYWORD_STATUS", 80*20f38712SPatrick Williams "KEYWORD_MESSAGE", 81*20f38712SPatrick Williams "LOG_LEVEL", 82*20f38712SPatrick Williams "OUTPUT_FILE", 83*20f38712SPatrick Williams "LOG_FILE", 84*20f38712SPatrick Williams "REPORT_FILE", 85*20f38712SPatrick Williams "DEBUG_FILE", 86*20f38712SPatrick Williams "OUTPUT_DIR", 87*20f38712SPatrick Williams ) 88de79173eSMichael Walsh 89de79173eSMichael Walsh if int(headers) == 1: 9018176322SMichael Walsh buffer += gp.sprint_dashes() 9118176322SMichael Walsh 9218176322SMichael Walsh return buffer 93de79173eSMichael Walsh 94de79173eSMichael Walsh 956e64698aSMichael Walshdef gp_debug_print(buffer): 966e64698aSMichael Walsh r""" 97ff790b6dSMichael Walsh Print the buffer value only if gen_robot_print_debug is set. 98de79173eSMichael Walsh 996e64698aSMichael Walsh This function is intended for use only by other functions in this module. 100de79173eSMichael Walsh 101ff790b6dSMichael Walsh Description of argument(s): 1026e64698aSMichael Walsh buffer The string to be printed. 1036e64698aSMichael Walsh """ 104de79173eSMichael Walsh 1056e64698aSMichael Walsh if not gen_robot_print_debug: 1066e64698aSMichael Walsh return 1076e64698aSMichael Walsh gp.gp_print(buffer) 108a6723f27SMichael Walsh 109afa7a1b8SMichael Walsh 110410b1787SMichael Walsh# In the following section of code, we will dynamically create print versions for several of the sprint 111410b1787SMichael Walsh# functions defined above. For example, where we have an sprint_vars() function defined above that returns 112410b1787SMichael Walsh# formatted variable print outs in a string, we will create a corresponding rprint_vars() function that will 113410b1787SMichael Walsh# print that string directly to stdout. 114a6723f27SMichael Walsh 115410b1787SMichael Walsh# It can be complicated to follow what's being created below. Here is an example of the rprint_vars() 116410b1787SMichael Walsh# function that will be created: 117a6723f27SMichael Walsh 118ff790b6dSMichael Walsh# def rprint_vars(*args, **kwargs): 119410b1787SMichael Walsh# gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout') 120ff790b6dSMichael Walsh 121ff790b6dSMichael Walsh# For sprint_vars (defined above), the following functions will be created: 122ff790b6dSMichael Walsh 123ff790b6dSMichael Walsh# rprint_vars Robot Print Vars 124410b1787SMichael Walsh# rqprint_vars Robot Print Vars if ${quiet} is set to ${0}. 125410b1787SMichael Walsh# rdprint_vars Robot Print Vars if ${debug} is set to ${1}. 126410b1787SMichael Walsh# rlprint_vars Robot Print Vars to the log instead of to the console. 127ff790b6dSMichael Walsh 128ff790b6dSMichael Walsh# Abbreviated names are created for all of the preceding function names: 129ff790b6dSMichael Walsh# rpvars 130ff790b6dSMichael Walsh# rqpvars 131ff790b6dSMichael Walsh# rdpvars 132ff790b6dSMichael Walsh# rlpvars 133ff790b6dSMichael Walsh 134410b1787SMichael Walsh# Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to 135410b1787SMichael Walsh# full names. 136ff790b6dSMichael Walsh# Rprint Vars (instead of Rpvars) 137afa7a1b8SMichael Walsh 138*20f38712SPatrick Williamsreplace_dict = {"output_stream": "stdout", "mod_qualifier": "gp."} 1396e64698aSMichael Walsh 1406e64698aSMichael Walshgp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n") 1416e64698aSMichael Walsh 142410b1787SMichael Walsh# func_names contains a list of all rprint functions which should be created from their sprint counterparts. 143*20f38712SPatrick Williamsfunc_names = ["print_vars", "print_auto_vars"] 144de79173eSMichael Walsh 145410b1787SMichael Walsh# stderr_func_names is a list of functions whose output should go to stderr rather than stdout. 1466e64698aSMichael Walshstderr_func_names = [] 14718176322SMichael Walsh 148*20f38712SPatrick Williamsfunc_defs = gp.create_print_wrapper_funcs( 149*20f38712SPatrick Williams func_names, stderr_func_names, replace_dict, "r" 150*20f38712SPatrick Williams) 1516e64698aSMichael Walshgp_debug_print(func_defs) 1526e64698aSMichael Walshexec(func_defs) 15318176322SMichael Walsh 154410b1787SMichael Walsh# Define an alias. rpvar is just a special case of rpvars where the args list contains only one element. 15518176322SMichael Walshcmd_buf = "rpvar = rpvars" 1566e64698aSMichael Walshgp_debug_print("\n" + cmd_buf + "\n") 15718176322SMichael Walshexec(cmd_buf) 158