Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This file contains functions useful for printing to stdout from robot programs. |
| 5 | """ |
| 6 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 7 | import re |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 8 | import os |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 9 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 10 | import gen_print as gp |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 11 | import wrap_utils as wu |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 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 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [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 | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 18 | def sprint_vars(*args): |
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 | |
| 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 26 | |
| 27 | Description of arguments: |
| 28 | args: |
| 29 | If the first argument is an integer, it will be interpreted to be the |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 30 | "hex" value. |
Michael Walsh | c4da0cb | 2017-01-10 11:31:05 -0600 | [diff] [blame] | 31 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 35 | 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 Walsh | c4da0cb | 2017-01-10 11:31:05 -0600 | [diff] [blame] | 45 | # See if parm 1 is to be interpreted as "hex". |
| 46 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 47 | if isinstance(int(args_list[0]), int): |
Michael Walsh | c4da0cb | 2017-01-10 11:31:05 -0600 | [diff] [blame] | 48 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 54 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 55 | if isinstance(int(args_list[0]), int): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 56 | indent = int(args_list[0]) |
| 57 | args_list.pop(0) |
| 58 | except ValueError: |
| 59 | indent = 0 |
| 60 | |
Michael Walsh | c4da0cb | 2017-01-10 11:31:05 -0600 | [diff] [blame] | 61 | # See if parm 3 is to be interpreted as "col1_width". |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 62 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 63 | if isinstance(int(args_list[0]), int): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 64 | loc_col1_width = int(args_list[0]) |
| 65 | args_list.pop(0) |
| 66 | except ValueError: |
| 67 | loc_col1_width = gp.col1_width |
| 68 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 69 | buffer = "" |
| 70 | for var_name in args_list: |
Michael Walsh | 0fa4762 | 2017-02-20 16:11:34 -0600 | [diff] [blame] | 71 | var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 72 | buffer += gp.sprint_varx(var_name, var_value, hex, indent, |
| 73 | loc_col1_width) |
| 74 | |
| 75 | return buffer |
| 76 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 77 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 78 | def sprint_auto_vars(headers=0): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 79 | r""" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 80 | String print all of the Automatic Variables described in the Robot User's |
| 81 | Guide using sprint_vars. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 82 | |
| 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 87 | should be printed. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 88 | """ |
| 89 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 90 | buffer = "" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 91 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 92 | buffer += gp.sprint_dashes() |
| 93 | buffer += "Automatic Variables:" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 94 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 95 | 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 Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 104 | |
| 105 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 106 | buffer += gp.sprint_dashes() |
| 107 | |
| 108 | return buffer |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 109 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 110 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 111 | def gp_debug_print(buffer): |
| 112 | r""" |
| 113 | Print the buffer value only if gen_print_debug is set. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 114 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 115 | 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] | 116 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 117 | Description of arguments: |
| 118 | buffer The string to be printed. |
| 119 | """ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 120 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 121 | if not gen_robot_print_debug: |
| 122 | return |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 123 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 124 | gp.gp_print(buffer) |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 125 | |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 126 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 127 | # 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 Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 132 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 133 | # 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 Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 135 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 136 | # def rprint_vars(*args): |
| 137 | # gp.gp_print(gp.replace_passwords(sprint_vars(*args)), stream='stdout') |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 138 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 139 | replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'} |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 140 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 141 | gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 142 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 143 | # func_names contains a list of all print functions which should be created |
| 144 | # from their sprint counterparts. |
| 145 | func_names = [ |
| 146 | 'print_vars', 'print_auto_vars' |
| 147 | ] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 148 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 149 | # stderr_func_names is a list of functions whose output should go to stderr |
| 150 | # rather than stdout. |
| 151 | stderr_func_names = [] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 152 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 153 | func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names, |
| 154 | replace_dict, "r") |
| 155 | gp_debug_print(func_defs) |
| 156 | exec(func_defs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 157 | |
| 158 | # Define an alias. rpvar is just a special case of rpvars where the args |
| 159 | # list contains only one element. |
| 160 | cmd_buf = "rpvar = rpvars" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 161 | gp_debug_print("\n" + cmd_buf + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 162 | exec(cmd_buf) |