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 | |
| 7 | import sys |
| 8 | import re |
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 | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 11 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 12 | from robot.libraries.BuiltIn import BuiltIn |
| 13 | from robot.api import logger |
| 14 | |
| 15 | |
| 16 | ############################################################################### |
| 17 | # In the following section of code, we will dynamically create robot versions |
| 18 | # of print functions for each of the sprint functions defined in the |
| 19 | # gen_print.py module. So, for example, where we have an sprint_time() |
| 20 | # function defined above that returns the time to the caller in a string, we |
| 21 | # will create a corresponding rprint_time() function that will print that |
| 22 | # string directly to stdout. |
| 23 | |
| 24 | # It can be complicated to follow what's being creaed by the exec statement |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 25 | # below. Here is an example of the rprint_time() function that will be |
| 26 | # created (as of the time of this writing): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 27 | |
| 28 | # def rprint_time(*args): |
| 29 | # s_func = getattr(gp, "sprint_time") |
| 30 | # BuiltIn().log_to_console(s_func(*args), |
| 31 | # stream='STDIN', |
| 32 | # no_newline=True) |
| 33 | |
| 34 | # Here are comments describing the lines in the body of the created function. |
| 35 | # Put a reference to the "s" version of this function in s_func. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 36 | # Call the "s" version of this function passing it all of our arguments. |
| 37 | # Write the result to stdout. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 38 | |
| 39 | robot_prefix = "r" |
| 40 | for func_name in gp.func_names: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 41 | # The print_var function's job is to figure out the name of arg 1 and |
| 42 | # then call print_varx. This is not currently supported for robot |
| 43 | # programs. Though it IS supported for python modules. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 44 | if func_name == "print_error": |
| 45 | output_stream = "STDERR" |
| 46 | else: |
| 47 | output_stream = "STDIN" |
| 48 | func_def = \ |
| 49 | [ |
| 50 | "def " + robot_prefix + func_name + "(*args):", |
| 51 | " s_func = getattr(gp, \"s" + func_name + "\")", |
| 52 | " BuiltIn().log_to_console(s_func(*args)," |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 53 | " stream='" + output_stream + "'," |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 54 | " no_newline=True)" |
| 55 | ] |
| 56 | |
| 57 | pgm_definition_string = '\n'.join(func_def) |
| 58 | exec(pgm_definition_string) |
| 59 | |
| 60 | # Create abbreviated aliases (e.g. rpvarx is an alias for rprint_varx). |
| 61 | alias = re.sub("print_", "p", func_name) |
| 62 | exec(robot_prefix + alias + " = " + robot_prefix + func_name) |
| 63 | |
| 64 | |
| 65 | ############################################################################### |
| 66 | |
| 67 | |
| 68 | ############################################################################### |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 69 | def rprint(buffer="", |
| 70 | stream="STDOUT"): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 71 | |
| 72 | r""" |
| 73 | rprint stands for "Robot Print". This keyword will print the user's |
| 74 | buffer to the console. This keyword does not write a linefeed. It is the |
| 75 | responsibility of the caller to include a line feed if desired. This |
| 76 | keyword is essentially an alias for "Log to Console <string> |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 77 | <stream>". |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 78 | |
| 79 | Description of arguments: |
| 80 | buffer The value that is to written to stdout. |
| 81 | """ |
| 82 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 83 | BuiltIn().log_to_console(buffer, no_newline=True, stream=stream) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 84 | |
| 85 | ############################################################################### |
| 86 | |
| 87 | |
| 88 | ############################################################################### |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 89 | def rprintn(buffer="", |
| 90 | stream='STDOUT'): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 91 | |
| 92 | r""" |
| 93 | rprintn stands for "Robot print with linefeed". This keyword will print |
| 94 | the user's buffer to the console along with a linefeed. It is basically |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 95 | an abbreviated form of "Log go Console <string> <stream>" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 96 | |
| 97 | Description of arguments: |
| 98 | buffer The value that is to written to stdout. |
| 99 | """ |
| 100 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 101 | BuiltIn().log_to_console(buffer, no_newline=False, stream=stream) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 102 | |
| 103 | ############################################################################### |
| 104 | |
| 105 | |
| 106 | ############################################################################### |
| 107 | def rprint_auto_vars(headers=0): |
| 108 | |
| 109 | r""" |
| 110 | This keyword will print all of the Automatic Variables described in the |
| 111 | Robot User's Guide using rpvars. |
| 112 | |
| 113 | NOTE: Not all automatic variables are guaranteed to exist. |
| 114 | |
| 115 | Description of arguments: |
| 116 | headers This indicates that a header and footer |
| 117 | will be printed. |
| 118 | """ |
| 119 | |
| 120 | if int(headers) == 1: |
| 121 | BuiltIn().log_to_console(gp.sprint_dashes(), no_newline=True) |
| 122 | BuiltIn().log_to_console("Automatic Variables:", no_newline=False) |
| 123 | |
| 124 | rpvars("TEST_NAME", "TEST_TAGS", "TEST_DOCUMENTATION", "TEST_STATUS", |
| 125 | "TEST_DOCUMENTATION", "TEST_STATUS", "TEST_MESSAGE", |
| 126 | "PREV_TEST_NAME", "PREV_TEST_STATUS", "PREV_TEST_MESSAGE", |
| 127 | "SUITE_NAME", "SUITE_SOURCE", "SUITE_DOCUMENTATION", |
| 128 | "SUITE_METADATA", "SUITE_STATUS", "SUITE_MESSAGE", "KEYWORD_STATUS", |
| 129 | "KEYWORD_MESSAGE", "LOG_LEVEL", "OUTPUT_FILE", "LOG_FILE", |
| 130 | "REPORT_FILE", "DEBUG_FILE", "OUTPUT_DIR") |
| 131 | |
| 132 | if int(headers) == 1: |
| 133 | BuiltIn().log_to_console(gp.sprint_dashes(), no_newline=True) |
| 134 | |
| 135 | ############################################################################### |
| 136 | |
| 137 | |
| 138 | ############################################################################### |
| 139 | def rpvars(*var_names): |
| 140 | |
| 141 | r""" |
| 142 | rpvars stands for "Robot Print Vars". Given a list of variable names, |
| 143 | this keyword will print each variable name and value such that the value |
| 144 | lines up in the same column as messages printed with rptime. |
| 145 | |
| 146 | NOTE: This function should NOT be called for local variables. It is |
| 147 | incapable of obtaining their values. |
| 148 | |
| 149 | NOTE: I intend to add code to allow the last several parms to be |
| 150 | recognized as hex, indent, etc. and passed on to rpvarx function. See the |
| 151 | sprint_var() function in gen_print.py for details. |
| 152 | |
| 153 | Description of arguments: |
| 154 | var_names A list of the names of variables to be |
| 155 | printed. |
| 156 | """ |
| 157 | |
| 158 | for var_name in var_names: |
| 159 | var_value = BuiltIn().get_variable_value("${" + var_name + "}") |
| 160 | rpvarx(var_name, var_value) |
| 161 | |
| 162 | ############################################################################### |
| 163 | |
| 164 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 165 | # Define an alias. rpvar is just a special case of rpvars where the |
| 166 | # var_names list contains only one element. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 167 | rpvar = rpvars |