Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
micwalsh committed Jul 21, 2020
1 parent c0a7a2a commit 9ce40f1
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 385 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
.idea/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# mw_robot_toolkit
Useful support for robot programs
# mw_robot_toolkit
Useful support for robot programs
91 changes: 91 additions & 0 deletions lib/gen_robot_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python

r"""
This module provides robot keyword execution functions such as run_key..
"""

import gen_print as gp
from robot.libraries.BuiltIn import BuiltIn


def run_key(keyword_buf,
quiet=None,
test_mode=None,
ignore=0):
r"""
Run the given keyword, return the status and the keyword return values.
The advantage of using this function verses having robot simply run your keyword is the handling of
parameters like quiet, test_mode and ignore.
Description of arguments:
keyword_buf The keyword string to be run.
quiet Indicates whether this function should run the pissuing function to print
'Issuing: <keyword string>' to stdout.
test_mode If test_mode is set, this function will not actually run the command. If
quiet is 0, it will print a message indicating what it would have run
(e.g. "Issuing: (test_mode) your command").
ignore Ignore errors from running keyword. If this is 0, this function will
fail with whatever error occurred when running the keyword.
Example usage from a robot script:
${status} ${ret_values}= Run Key My Keyword \ Arg1 \ Arg2
Note that to get robot to pass your command + args as a single string to this function, you must escape
extra spaces with a backslash.
Also note that ret_values is a python list:
ret_values:
ret_values[0]: value1
ret_values[1]: value2
"""

# Set these vars to default values if they are None.
quiet = int(gp.get_var_value(quiet, 0))
test_mode = int(gp.get_var_value(test_mode, 0))
ignore = int(ignore)

# Convert the keyword_buf into a list split wherever 2 or more spaces are found.
keyword_list = keyword_buf.split(' ')
# Strip spaces from each argument to make the output look clean and uniform.
keyword_list = [item.strip(' ') for item in keyword_list]

if not quiet:
# Join the list back into keyword_buf for the sake of output.
keyword_buf = ' '.join(keyword_list)
gp.pissuing(keyword_buf, test_mode)

if test_mode:
return 'PASS', ""

try:
status, ret_values = \
BuiltIn().run_keyword_and_ignore_error(*keyword_list)
except Exception as my_assertion_error:
status = "FAIL"
ret_values = my_assertion_error.args[0]

if status != 'PASS':
# Output the error message to stderr.
BuiltIn().log_to_console(ret_values, stream='STDERR')
if not ignore:
# Fail with the given error message.
BuiltIn().fail(ret_values)

return status, ret_values


def run_key_u(keyword_buf,
quiet=None,
ignore=0):
r"""
Run keyword unconditionally (i.e. without regard to global test_mode setting).
This function will simply call the run_key function passing on all of the callers parameters except
test_mode which will be hard-coded to 0. See run_key (above) for details.
See the proglog of "run_key" function above for description of arguments.
"""

return run_key(keyword_buf, test_mode=0, quiet=quiet, ignore=ignore)
286 changes: 143 additions & 143 deletions lib/gen_robot_print.py
Original file line number Diff line number Diff line change
@@ -1,143 +1,143 @@
#!/usr/bin/env python

r"""
This file contains functions useful for printing to stdout from robot programs.
"""

import re
import os

import gen_print as gp
import func_args as fa

from robot.libraries.BuiltIn import BuiltIn

gen_robot_print_debug = int(os.environ.get('GEN_ROBOT_PRINT_DEBUG', '0'))


def sprint_vars(*args, **kwargs):
r"""
Sprint the values of one or more variables to the console.
This is a robot re-definition of the sprint_vars function in gen_print.py. Given a list of variable
names, this keyword will string print each variable name and value such that each value lines up in the
same column as messages printed with sprint_time().
Description of argument(s):
args The names of the variables to be printed (e.g. var1 rather than ${var1}).
kwargs See sprint_varx in gen_print.py for descriptions of all other arguments.
"""

if 'fmt' in kwargs:
# Find format option names in kwargs['fmt'] and wrap them with "gp." and "()" to make them into
# function calls. For example, verbose would be converted to "gp.verbose()". This allows the user
# to simply specify "fmt=verbose" (vs. fmt=gp.verbose()).
# Note "terse" has been explicitly added for backward compatibility. Once the repo has been purged
# of its use, this code can return to its original form.
regex = "(" + "|".join(gp.valid_fmts()) + "|terse)"
kwargs['fmt'] = \
re.sub(regex, "gp.\\1()", kwargs['fmt'])
kwargs = fa.args_to_objects(kwargs)
buffer = ""
for var_name in args:
var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}")
buffer += gp.sprint_varx(var_name, var_value, **kwargs)

return buffer


def sprint_auto_vars(headers=0):
r"""
String print all of the Automatic Variables described in the Robot User's Guide using sprint_vars.
NOTE: Not all automatic variables are guaranteed to exist.
Description of argument(s):
headers This indicates that a header and footer should be printed.
"""

buffer = ""
if int(headers) == 1:
buffer += gp.sprint_dashes()
buffer += "Automatic Variables:"

buffer += \
sprint_vars(
"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:
buffer += gp.sprint_dashes()

return buffer


def gp_debug_print(buffer):
r"""
Print the buffer value only if gen_robot_print_debug is set.
This function is intended for use only by other functions in this module.
Description of argument(s):
buffer The string to be printed.
"""

if not gen_robot_print_debug:
return
gp.gp_print(buffer)


# In the following section of code, we will dynamically create print versions for several of the sprint
# functions defined above. For example, where we have an sprint_vars() function defined above that returns
# formatted variable print outs in a string, we will create a corresponding rprint_vars() function that will
# print that string directly to stdout.

# It can be complicated to follow what's being created below. Here is an example of the rprint_vars()
# function that will be created:

# def rprint_vars(*args, **kwargs):
# gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout')

# For sprint_vars (defined above), the following functions will be created:

# rprint_vars Robot Print Vars
# rqprint_vars Robot Print Vars if ${quiet} is set to ${0}.
# rdprint_vars Robot Print Vars if ${debug} is set to ${1}.
# rlprint_vars Robot Print Vars to the log instead of to the console.

# Abbreviated names are created for all of the preceding function names:
# rpvars
# rqpvars
# rdpvars
# rlpvars

# Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to
# full names.
# Rprint Vars (instead of Rpvars)

replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'}

gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n")

# func_names contains a list of all rprint functions which should be created from their sprint counterparts.
func_names = [
'print_vars', 'print_auto_vars'
]

# stderr_func_names is a list of functions whose output should go to stderr rather than stdout.
stderr_func_names = []

func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names,
replace_dict, "r")
gp_debug_print(func_defs)
exec(func_defs)

# Define an alias. rpvar is just a special case of rpvars where the args list contains only one element.
cmd_buf = "rpvar = rpvars"
gp_debug_print("\n" + cmd_buf + "\n")
exec(cmd_buf)
#!/usr/bin/env python

r"""
This file contains functions useful for printing to stdout from robot programs.
"""

import re
import os

import gen_print as gp
import func_args as fa

from robot.libraries.BuiltIn import BuiltIn

gen_robot_print_debug = int(os.environ.get('GEN_ROBOT_PRINT_DEBUG', '0'))


def sprint_vars(*args, **kwargs):
r"""
Sprint the values of one or more variables to the console.
This is a robot re-definition of the sprint_vars function in gen_print.py. Given a list of variable
names, this keyword will string print each variable name and value such that each value lines up in the
same column as messages printed with sprint_time().
Description of argument(s):
args The names of the variables to be printed (e.g. var1 rather than ${var1}).
kwargs See sprint_varx in gen_print.py for descriptions of all other arguments.
"""

if 'fmt' in kwargs:
# Find format option names in kwargs['fmt'] and wrap them with "gp." and "()" to make them into
# function calls. For example, verbose would be converted to "gp.verbose()". This allows the user
# to simply specify "fmt=verbose" (vs. fmt=gp.verbose()).
# Note "terse" has been explicitly added for backward compatibility. Once the repo has been purged
# of its use, this code can return to its original form.
regex = "(" + "|".join(gp.valid_fmts()) + "|terse)"
kwargs['fmt'] = \
re.sub(regex, "gp.\\1()", kwargs['fmt'])
kwargs = fa.args_to_objects(kwargs)
buffer = ""
for var_name in args:
var_value = BuiltIn().get_variable_value("${" + str(var_name) + "}")
buffer += gp.sprint_varx(var_name, var_value, **kwargs)

return buffer


def sprint_auto_vars(headers=0):
r"""
String print all of the Automatic Variables described in the Robot User's Guide using sprint_vars.
NOTE: Not all automatic variables are guaranteed to exist.
Description of argument(s):
headers This indicates that a header and footer should be printed.
"""

buffer = ""
if int(headers) == 1:
buffer += gp.sprint_dashes()
buffer += "Automatic Variables:"

buffer += \
sprint_vars(
"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:
buffer += gp.sprint_dashes()

return buffer


def gp_debug_print(buffer):
r"""
Print the buffer value only if gen_robot_print_debug is set.
This function is intended for use only by other functions in this module.
Description of argument(s):
buffer The string to be printed.
"""

if not gen_robot_print_debug:
return
gp.gp_print(buffer)


# In the following section of code, we will dynamically create print versions for several of the sprint
# functions defined above. For example, where we have an sprint_vars() function defined above that returns
# formatted variable print outs in a string, we will create a corresponding rprint_vars() function that will
# print that string directly to stdout.

# It can be complicated to follow what's being created below. Here is an example of the rprint_vars()
# function that will be created:

# def rprint_vars(*args, **kwargs):
# gp.gp_print(gp.replace_passwords(sprint_vars(*args, **kwargs)), stream='stdout')

# For sprint_vars (defined above), the following functions will be created:

# rprint_vars Robot Print Vars
# rqprint_vars Robot Print Vars if ${quiet} is set to ${0}.
# rdprint_vars Robot Print Vars if ${debug} is set to ${1}.
# rlprint_vars Robot Print Vars to the log instead of to the console.

# Abbreviated names are created for all of the preceding function names:
# rpvars
# rqpvars
# rdpvars
# rlpvars

# Users are encouraged to only use the abbreviated forms for development but to then ultimately switch to
# full names.
# Rprint Vars (instead of Rpvars)

replace_dict = {'output_stream': 'stdout', 'mod_qualifier': 'gp.'}

gp_debug_print("gp.robot_env: " + str(gp.robot_env) + "\n")

# func_names contains a list of all rprint functions which should be created from their sprint counterparts.
func_names = [
'print_vars', 'print_auto_vars'
]

# stderr_func_names is a list of functions whose output should go to stderr rather than stdout.
stderr_func_names = []

func_defs = gp.create_print_wrapper_funcs(func_names, stderr_func_names,
replace_dict, "r")
gp_debug_print(func_defs)
exec(func_defs)

# Define an alias. rpvar is just a special case of rpvars where the args list contains only one element.
cmd_buf = "rpvar = rpvars"
gp_debug_print("\n" + cmd_buf + "\n")
exec(cmd_buf)
Loading

0 comments on commit 9ce40f1

Please sign in to comment.