xref: /openbmc/openbmc-test-automation/lib/gen_robot_print.py (revision d0fd8838ea51df528b7c6f658b40b180eab35bd5)
1#!/usr/bin/env python
2
3r"""
4This file contains functions useful for printing to stdout from robot programs.
5"""
6
7import sys
8import re
9
10import gen_print as gp
11
12from robot.libraries.BuiltIn import BuiltIn
13from 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
25# below.  Here is an example of the rprint_time() function that will be
26# created (as of the time of this writing):
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.
36# Call the "s" version of this function passing it all of our arguments.
37# Write the result to stdout.
38
39robot_prefix = "r"
40for func_name in gp.func_names:
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.
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),"
53            " stream='" + output_stream + "',"
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###############################################################################
69def rprint(buffer="",
70           stream="STDOUT"):
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>
77    <stream>".
78
79    Description of arguments:
80    buffer                          The value that is to written to stdout.
81    """
82
83    BuiltIn().log_to_console(buffer, no_newline=True, stream=stream)
84
85###############################################################################
86
87
88###############################################################################
89def rprintn(buffer="",
90            stream='STDOUT'):
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
95    an abbreviated form of "Log go Console  <string>  <stream>"
96
97    Description of arguments:
98    buffer                          The value that is to written to stdout.
99    """
100
101    BuiltIn().log_to_console(buffer, no_newline=False, stream=stream)
102
103###############################################################################
104
105
106###############################################################################
107def 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###############################################################################
139def 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
165# Define an alias.  rpvar is just a special case of rpvars where the
166# var_names list contains only one element.
167rpvar = rpvars
168