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 | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 11 | import func_args as fa |
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 | 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 | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 22 | This is a robot re-definition of the sprint_vars function in gen_print.py. |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 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 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 27 | 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 Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 32 | """ |
| 33 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 34 | if 'fmt' in kwargs: |
| 35 | # Find format option names in kwargs['fmt'] and wrap them with "gp." |
| 36 | # and "()" to make them into function calls. For example, terse would |
| 37 | # be converted to "gp.terse()". This allows the user to simply |
| 38 | # specify "fmt=terse" (vs. fmt=gp.terse()). |
| 39 | regex = "(" + "|".join(gp.valid_fmts()) + ")" |
| 40 | kwargs['fmt'] = \ |
| 41 | re.sub(regex, "gp.\\1()", kwargs['fmt']) |
| 42 | kwargs = fa.args_to_objects(kwargs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 43 | buffer = "" |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 44 | for var_name in args: |
Michael Walsh | 0fa4762 | 2017-02-20 16:11:34 -0600 | [diff] [blame] | 45 | var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}") |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 46 | buffer += gp.sprint_varx(var_name, var_value, **kwargs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 47 | |
| 48 | return buffer |
| 49 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 50 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 51 | def sprint_auto_vars(headers=0): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 52 | r""" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 53 | String print all of the Automatic Variables described in the Robot User's |
| 54 | Guide using sprint_vars. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 55 | |
| 56 | NOTE: Not all automatic variables are guaranteed to exist. |
| 57 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 58 | Description of argument(s): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 59 | headers This indicates that a header and footer |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 60 | should be printed. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 61 | """ |
| 62 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 63 | buffer = "" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 64 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 65 | buffer += gp.sprint_dashes() |
| 66 | buffer += "Automatic Variables:" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 67 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 68 | buffer += \ |
| 69 | sprint_vars( |
| 70 | "TEST_NAME", "TEST_TAGS", "TEST_DOCUMENTATION", "TEST_STATUS", |
| 71 | "TEST_DOCUMENTATION", "TEST_STATUS", "TEST_MESSAGE", |
| 72 | "PREV_TEST_NAME", "PREV_TEST_STATUS", "PREV_TEST_MESSAGE", |
| 73 | "SUITE_NAME", "SUITE_SOURCE", "SUITE_DOCUMENTATION", |
| 74 | "SUITE_METADATA", "SUITE_STATUS", "SUITE_MESSAGE", |
| 75 | "KEYWORD_STATUS", "KEYWORD_MESSAGE", "LOG_LEVEL", "OUTPUT_FILE", |
| 76 | "LOG_FILE", "REPORT_FILE", "DEBUG_FILE", "OUTPUT_DIR") |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 77 | |
| 78 | if int(headers) == 1: |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 79 | buffer += gp.sprint_dashes() |
| 80 | |
| 81 | return buffer |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 82 | |
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 | def gp_debug_print(buffer): |
| 85 | r""" |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 86 | Print the buffer value only if gen_robot_print_debug is set. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 87 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 88 | 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] | 89 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 90 | Description of argument(s): |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 91 | buffer The string to be printed. |
| 92 | """ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 93 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 94 | if not gen_robot_print_debug: |
| 95 | return |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 96 | gp.gp_print(buffer) |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 97 | |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 98 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 99 | # In the following section of code, we will dynamically create print versions |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 100 | # for several of the sprint functions defined above. For example, where we |
| 101 | # have an sprint_vars() function defined above that returns formatted variable |
| 102 | # print outs in a string, we will create a corresponding rprint_vars() |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 103 | # function that will print that string directly to stdout. |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 104 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 105 | # It can be complicated to follow what's being created below. Here is an |
| 106 | # example of the rprint_vars() function that will be created: |
Michael Walsh | a6723f2 | 2016-11-22 11:12:01 -0600 | [diff] [blame] | 107 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 108 | # def rprint_vars(*args, **kwargs): |
| 109 | # gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), |
| 110 | # stream='stdout') |
| 111 | |
| 112 | # For sprint_vars (defined above), the following functions will be created: |
| 113 | |
| 114 | # rprint_vars Robot Print Vars |
| 115 | # rqprint_vars Robot Print Vars if ${quiet} is set to |
| 116 | # ${0}. |
| 117 | # rdprint_vars Robot Print Vars if ${debug} is set to |
| 118 | # ${1}. |
| 119 | # rlprint_vars Robot Print Vars to the log instead of to |
| 120 | # the console. |
| 121 | |
| 122 | # Abbreviated names are created for all of the preceding function names: |
| 123 | # rpvars |
| 124 | # rqpvars |
| 125 | # rdpvars |
| 126 | # rlpvars |
| 127 | |
| 128 | # Users are encouraged to only use the abbreviated forms for development but |
| 129 | # to then ultimately switch to full names. |
| 130 | # Rprint Vars (instead of Rpvars) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 131 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 132 | replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'} |
Michael Walsh | afa7a1b | 2016-12-09 14:02:48 -0600 | [diff] [blame] | 133 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 134 | gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 135 | |
Michael Walsh | ff790b6 | 2019-05-17 16:31:16 -0500 | [diff] [blame] | 136 | # func_names contains a list of all rprint functions which should be created |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 137 | # from their sprint counterparts. |
| 138 | func_names = [ |
| 139 | 'print_vars', 'print_auto_vars' |
| 140 | ] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 141 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 142 | # stderr_func_names is a list of functions whose output should go to stderr |
| 143 | # rather than stdout. |
| 144 | stderr_func_names = [] |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 145 | |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 146 | func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names, |
| 147 | replace_dict, "r") |
| 148 | gp_debug_print(func_defs) |
| 149 | exec(func_defs) |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 150 | |
| 151 | # Define an alias. rpvar is just a special case of rpvars where the args |
| 152 | # list contains only one element. |
| 153 | cmd_buf = "rpvar = rpvars" |
Michael Walsh | 6e64698 | 2019-03-28 12:55:31 -0500 | [diff] [blame] | 154 | gp_debug_print("\n" + cmd_buf + "\n") |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 155 | exec(cmd_buf) |