George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | This file contains functions useful for printing to stdout from robot programs. |
| 5 | """ |
| 6 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 7 | import os |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 8 | import re |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 9 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 10 | import func_args as fa |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 11 | import gen_print as gp |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 12 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 13 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 14 | gen_robot_print_debug = int(os.environ.get("GEN_ROBOT_PRINT_DEBUG", "0")) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 15 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 16 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 17 | def sprint_vars(*args, **kwargs): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 18 | r""" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 19 | Sprint the values of one or more variables to the console. |
| 20 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 21 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 24 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 25 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 26 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 28 | """ |
| 29 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 30 | if "fmt" in kwargs: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 31 | # 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 Walsh | 6bed4d3 | 2019-07-10 14:11:30 -0500 | [diff] [blame] | 36 | regex = "(" + "|".join(gp.valid_fmts()) + "|terse)" |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 37 | kwargs["fmt"] = re.sub(regex, "gp.\\1()", kwargs["fmt"]) |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 38 | kwargs = fa.args_to_objects(kwargs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 39 | buffer = "" |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 40 | for var_name in args: |
Michael Walsh | 0fa4762 | 2017-02-20 16:11:34 -0600 | [diff] [blame] | 41 | var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}") |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 42 | buffer += gp.sprint_varx(var_name, var_value, **kwargs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 43 | |
| 44 | return buffer |
| 45 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 46 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 47 | def sprint_auto_vars(headers=0): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 48 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 49 | String print all of the Automatic Variables described in the Robot User's Guide using sprint_vars. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 50 | |
| 51 | NOTE: Not all automatic variables are guaranteed to exist. |
| 52 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 53 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 54 | headers This indicates that a header and footer should be printed. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 55 | """ |
| 56 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 57 | buffer = "" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 58 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 59 | buffer += gp.sprint_dashes() |
| 60 | buffer += "Automatic Variables:" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 61 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 62 | 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 Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 88 | |
| 89 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 90 | buffer += gp.sprint_dashes() |
| 91 | |
| 92 | return buffer |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 93 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 94 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 95 | def gp_debug_print(buffer): |
| 96 | r""" |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 97 | Print the buffer value only if gen_robot_print_debug is set. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 98 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 99 | This function is intended for use only by other functions in this module. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 100 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 101 | Description of argument(s): |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 102 | buffer The string to be printed. |
| 103 | """ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 104 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 105 | if not gen_robot_print_debug: |
| 106 | return |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 107 | gp.gp_print(buffer) |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 108 | |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 109 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 110 | # 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 Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 114 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 115 | # 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 Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 117 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 118 | # def rprint_vars(*args, **kwargs): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 119 | # gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout') |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 120 | |
| 121 | # For sprint_vars (defined above), the following functions will be created: |
| 122 | |
| 123 | # rprint_vars Robot Print Vars |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 124 | # 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 Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 127 | |
| 128 | # Abbreviated names are created for all of the preceding function names: |
| 129 | # rpvars |
| 130 | # rqpvars |
| 131 | # rdpvars |
| 132 | # rlpvars |
| 133 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 134 | # Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to |
| 135 | # full names. |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 136 | # Rprint Vars (instead of Rpvars) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 137 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 138 | replace_dict = {"output_stream": "stdout", "mod_qualifier": "gp."} |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 139 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 140 | gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 141 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 142 | # func_names contains a list of all rprint functions which should be created from their sprint counterparts. |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 143 | func_names = ["print_vars", "print_auto_vars"] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 144 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 145 | # stderr_func_names is a list of functions whose output should go to stderr rather than stdout. |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 146 | stderr_func_names = [] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 147 | |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 148 | func_defs = gp.create_print_wrapper_funcs( |
| 149 | func_names, stderr_func_names, replace_dict, "r" |
| 150 | ) |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 151 | gp_debug_print(func_defs) |
| 152 | exec(func_defs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 153 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 154 | # Define an alias. rpvar is just a special case of rpvars where the args list contains only one element. |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 155 | cmd_buf = "rpvar = rpvars" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 156 | gp_debug_print("\n" + cmd_buf + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 157 | exec(cmd_buf) |