blob: 6d2d0f52a6da4a9bacb78df686c263a57ef9ab3b [file] [log] [blame]
Michael Walshbcc404d2017-04-12 15:54:58 -05001#!/usr/bin/env python
2
3r"""
4This module provides robot keyword execution functions such as run_key..
5"""
6
7import gen_print as gp
8from robot.libraries.BuiltIn import BuiltIn
9
10
11###############################################################################
12def run_key(keyword_buf,
13 quiet=None,
14 test_mode=None,
15 ignore=0):
16
17 r"""
18 Run the given keyword, return the status and the keyword return values.
19
20 The advantage of using this function verses having robot simply run your
21 keyword is the handling of parameters like quiet, test_mode and ignore.
22
23 Description of arguments:
24 keyword_buf The keyword string to be run.
25 quiet Indicates whether this function should run
26 the pissuing function to print 'Issuing:
27 <keyword string>' to stdout.
28 test_mode If test_mode is set, this function will
29 not actually run the command. If quiet is
30 0, it will print a message indicating what
31 it would have run (e.g. "Issuing:
32 (test_mode) your command").
33 ignore Ignore errors from running keyword. If
34 this is 0, this function will fail with
35 whatever error occurred when running the
36 keyword.
37
38 Example usage from a robot script:
39
40 ${status} ${ret_values}= Run Key My Keyword \ Arg1 \ Arg2
41
42 Note that to get robot to pass your command + args as a single string to
43 this function, you must escape extra spaces with a backslash.
44
45 Also note that ret_values is a python list:
46 ret_values:
47 ret_values[0]: value1
48 ret_values[1]: value2
49 """
50
51 # Set these vars to default values if they are None.
52 quiet = int(gp.get_var_value(quiet, 0))
53 test_mode = int(gp.get_var_value(test_mode, 0))
54 ignore = int(ignore)
55
56 # Convert the keyword_buf into a list split wherever 2 or more spaces are
57 # found.
58 keyword_list = keyword_buf.split(' ')
59 # Strip spaces from each argument to make the output look clean and
60 # uniform.
61 keyword_list = [item.strip(' ') for item in keyword_list]
62
63 if not quiet:
64 # Join the list back into keyword_buf for the sake of output.
65 keyword_buf = ' '.join(keyword_list)
66 gp.pissuing(keyword_buf, test_mode)
67
68 if test_mode:
69 return 'PASS', ""
70
71 status, ret_values = \
72 BuiltIn().run_keyword_and_ignore_error(*keyword_list)
73
74 if not (status == 'PASS' or ignore):
75 # Output the error message to stderr.
76 BuiltIn().log_to_console(ret_values, stream='STDERR')
77 # Fail with the given error message.
78 BuiltIn().fail(ret_values)
79
80 return status, ret_values
81
82###############################################################################
83
84
85###############################################################################
86def run_key_u(keyword_buf,
87 quiet=None,
88 ignore=0):
89
90 r"""
91 Run keyword unconditionally (i.e. without regard to global test_mode
92 setting).
93
94 This function will simply call the run_key function passing on all of the
95 callers parameters except test_mode which will be hard-coded to 0. See
96 run_key (above) for details.
97
98 See the proglog of "run_key" function above for description of arguments.
99 """
100
101 return run_key(keyword_buf, test_mode=0, quiet=quiet, ignore=ignore)
102
103###############################################################################