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 | |
Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 7 | import re |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 8 | import os |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 9 | |
Patrick Williams | 5731818 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 10 | import gen_print as gp |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 11 | import func_args as fa |
| 12 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 13 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 14 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 15 | 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] | 16 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 17 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 18 | def sprint_vars(*args, **kwargs): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 19 | r""" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 20 | Sprint the values of one or more variables to the console. |
| 21 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 22 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 25 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 26 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 27 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 29 | """ |
| 30 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 31 | if 'fmt' in kwargs: |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 32 | # 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 Walsh | 6bed4d3 | 2019-07-10 14:11:30 -0500 | [diff] [blame] | 37 | regex = "(" + "|".join(gp.valid_fmts()) + "|terse)" |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 38 | kwargs['fmt'] = \ |
| 39 | re.sub(regex, "gp.\\1()", kwargs['fmt']) |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 40 | kwargs = fa.args_to_objects(kwargs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 41 | buffer = "" |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 42 | for var_name in args: |
Michael Walsh | 0fa4762 | 2017-02-20 16:11:34 -0600 | [diff] [blame] | 43 | var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}") |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 44 | buffer += gp.sprint_varx(var_name, var_value, **kwargs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 45 | |
| 46 | return buffer |
| 47 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 48 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 49 | def sprint_auto_vars(headers=0): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 50 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 51 | 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] | 52 | |
| 53 | NOTE: Not all automatic variables are guaranteed to exist. |
| 54 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 55 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 56 | headers This indicates that a header and footer should be printed. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 57 | """ |
| 58 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 59 | buffer = "" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 60 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 61 | buffer += gp.sprint_dashes() |
| 62 | buffer += "Automatic Variables:" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 63 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 64 | 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 Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 73 | |
| 74 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 75 | buffer += gp.sprint_dashes() |
| 76 | |
| 77 | return buffer |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 78 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 79 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 80 | def gp_debug_print(buffer): |
| 81 | r""" |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 82 | Print the buffer value only if gen_robot_print_debug is set. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 83 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 84 | 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] | 85 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 86 | Description of argument(s): |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 87 | buffer The string to be printed. |
| 88 | """ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 89 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 90 | if not gen_robot_print_debug: |
| 91 | return |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 92 | gp.gp_print(buffer) |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 93 | |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 94 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 95 | # 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 Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 99 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 100 | # 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 Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 102 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 103 | # def rprint_vars(*args, **kwargs): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 104 | # gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout') |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 105 | |
| 106 | # For sprint_vars (defined above), the following functions will be created: |
| 107 | |
| 108 | # rprint_vars Robot Print Vars |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 109 | # 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 Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 112 | |
| 113 | # Abbreviated names are created for all of the preceding function names: |
| 114 | # rpvars |
| 115 | # rqpvars |
| 116 | # rdpvars |
| 117 | # rlpvars |
| 118 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 119 | # Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to |
| 120 | # full names. |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 121 | # Rprint Vars (instead of Rpvars) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 122 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 123 | replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'} |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 124 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 125 | gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 126 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 127 | # func_names contains a list of all rprint functions which should be created from their sprint counterparts. |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 128 | func_names = [ |
| 129 | 'print_vars', 'print_auto_vars' |
| 130 | ] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 131 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 132 | # 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] | 133 | stderr_func_names = [] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 134 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 135 | func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names, |
| 136 | replace_dict, "r") |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 137 | gp_debug_print(func_defs) |
| 138 | exec(func_defs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 139 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 140 | # 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] | 141 | cmd_buf = "rpvar = rpvars" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 142 | gp_debug_print("\n" + cmd_buf + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 143 | exec(cmd_buf) |