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