blob: 803aa4a821b0007577b6e33cc935558b702481b4 [file] [log] [blame]
#!/usr/bin/env python
r"""
This file contains functions useful for printing to stdout from robot programs.
"""
import sys
import re
import gen_print as gp
from robot.libraries.BuiltIn import BuiltIn
from robot.api import logger
###############################################################################
# In the following section of code, we will dynamically create robot versions
# of print functions for each of the sprint functions defined in the
# gen_print.py module. So, for example, where we have an sprint_time()
# function defined above that returns the time to the caller in a string, we
# will create a corresponding rprint_time() function that will print that
# string directly to stdout.
# It can be complicated to follow what's being creaed by the exec statement
# below. Here is an example of the rprint_time() function that will be created
# (as of the time of this writing):
# def rprint_time(*args):
# s_func = getattr(gp, "sprint_time")
# BuiltIn().log_to_console(s_func(*args),
# stream='STDIN',
# no_newline=True)
# Here are comments describing the lines in the body of the created function.
# Put a reference to the "s" version of this function in s_func.
# Call the "s" version of this function passing it all of our arguments. Write
# the result to stdout.
robot_prefix = "r"
for func_name in gp.func_names:
# The print_var function's job is to figure out the name of arg 1 and then
# call print_varx. This is not currently supported for robot programs.
if func_name == "print_error":
output_stream = "STDERR"
else:
output_stream = "STDIN"
func_def = \
[
"def " + robot_prefix + func_name + "(*args):",
" s_func = getattr(gp, \"s" + func_name + "\")",
" BuiltIn().log_to_console(s_func(*args),"
" stream = '" + output_stream + "',"
" no_newline=True)"
]
pgm_definition_string = '\n'.join(func_def)
exec(pgm_definition_string)
# Create abbreviated aliases (e.g. rpvarx is an alias for rprint_varx).
alias = re.sub("print_", "p", func_name)
exec(robot_prefix + alias + " = " + robot_prefix + func_name)
###############################################################################
###############################################################################
def rprint(buffer=""):
r"""
rprint stands for "Robot Print". This keyword will print the user's
buffer to the console. This keyword does not write a linefeed. It is the
responsibility of the caller to include a line feed if desired. This
keyword is essentially an alias for "Log to Console <string>
no_newline=True".
Description of arguments:
buffer The value that is to written to stdout.
"""
BuiltIn().log_to_console(buffer, no_newline=True)
###############################################################################
###############################################################################
def rprintn(buffer=""):
r"""
rprintn stands for "Robot print with linefeed". This keyword will print
the user's buffer to the console along with a linefeed. It is basically
an abbreviated form of "Log go Console <string>"
Description of arguments:
buffer The value that is to written to stdout.
"""
BuiltIn().log_to_console(buffer, no_newline=False)
###############################################################################
###############################################################################
def rprint_auto_vars(headers=0):
r"""
This keyword will print all of the Automatic Variables described in the
Robot User's Guide using rpvars.
NOTE: Not all automatic variables are guaranteed to exist.
Description of arguments:
headers This indicates that a header and footer
will be printed.
"""
if int(headers) == 1:
BuiltIn().log_to_console(gp.sprint_dashes(), no_newline=True)
BuiltIn().log_to_console("Automatic Variables:", no_newline=False)
rpvars("TEST_NAME", "TEST_TAGS", "TEST_DOCUMENTATION", "TEST_STATUS",
"TEST_DOCUMENTATION", "TEST_STATUS", "TEST_MESSAGE",
"PREV_TEST_NAME", "PREV_TEST_STATUS", "PREV_TEST_MESSAGE",
"SUITE_NAME", "SUITE_SOURCE", "SUITE_DOCUMENTATION",
"SUITE_METADATA", "SUITE_STATUS", "SUITE_MESSAGE", "KEYWORD_STATUS",
"KEYWORD_MESSAGE", "LOG_LEVEL", "OUTPUT_FILE", "LOG_FILE",
"REPORT_FILE", "DEBUG_FILE", "OUTPUT_DIR")
if int(headers) == 1:
BuiltIn().log_to_console(gp.sprint_dashes(), no_newline=True)
###############################################################################
###############################################################################
def rpvars(*var_names):
r"""
rpvars stands for "Robot Print Vars". Given a list of variable names,
this keyword will print each variable name and value such that the value
lines up in the same column as messages printed with rptime.
NOTE: This function should NOT be called for local variables. It is
incapable of obtaining their values.
NOTE: I intend to add code to allow the last several parms to be
recognized as hex, indent, etc. and passed on to rpvarx function. See the
sprint_var() function in gen_print.py for details.
Description of arguments:
var_names A list of the names of variables to be
printed.
"""
for var_name in var_names:
var_value = BuiltIn().get_variable_value("${" + var_name + "}")
rpvarx(var_name, var_value)
###############################################################################
# Define an alias. rpvar is just a special case of rpvars where the var_names
# list contains only one element.
rpvar = rpvars