blob: 770c634d0fe230c78febdc5f276b5a6656b855ef [file] [log] [blame]
Michael Walsha2a553c2017-01-10 11:17:38 -06001#!/usr/bin/env python
2
3r"""
4This module is the python counterpart to run_keyword.robot.
5"""
6
Michael Walsha2a553c2017-01-10 11:17:38 -06007import gen_robot_print as grp
8import gen_robot_valid as grv
Michael Walsh16cbb7f2017-02-02 15:54:16 -06009import gen_robot_utils as gru
10
Michael Walsha2a553c2017-01-10 11:17:38 -060011from robot.libraries.BuiltIn import BuiltIn
Michael Walshe7a7a182017-01-19 10:37:10 -060012import re
Michael Walsha2a553c2017-01-10 11:17:38 -060013
14
15###############################################################################
Michael Walsha2a553c2017-01-10 11:17:38 -060016def setup():
17
18 r"""
19 Do general program setup tasks.
20 """
21
22 grp.rqprintn()
23
24 validate_parms()
25
26 grp.rqprint_pgm_header()
27
28###############################################################################
29
30
31###############################################################################
32def validate_parms():
33
34 r"""
35 Validate all program parameters.
36 """
37
38 grv.rvalid_value("keyword_string")
39
40 return True
41
42###############################################################################
43
44
45###############################################################################
46def program_teardown():
47
48 r"""
49 Clean up after this program.
50 """
51
52 grp.rqprint_pgm_footer()
53
54###############################################################################
Michael Walshe9d78c42017-03-02 14:41:15 -060055
56
57###############################################################################
58def my_run_keywords(lib_file_path,
59 keyword_string,
60 quiet=0,
61 test_mode=0):
62
63 r"""
64 Run the keywords in the keyword string.
65
66 Description of arguments:
67 lib_file_path The path to a library or resource needed to run the
68 keywords. This may contain a colon-delimited list of
69 library/resource paths.
70 keyword_string The keyword string to be run by this function. If this
71 keyword string contains " ; " anywhere, it will be taken to
72 be multiple keyword strings. Each keyword may also include
73 a variable assignment. Example:
74 ${my_var}= My Keyword
75 quiet If this parameter is set to "1", this program will print
76 only essential information, i.e. it will not echo
77 parameters, echo commands, print the total run time, etc.
78 test_mode This means that this program should go through all the
79 motions but not actually do anything substantial.
80 """
81
82 # NOTE: During code review the following question was raised: Why support
83 # 1) variable assignments 2) multiple keywords? Couldn't a user simply
84 # call this program twice to get what they need. If necessary, the user
85 # could take the output of the first call and specify it as a literal on
86 # the second call.
87 #
88 # However, this approach would not work in all cases. The following case
89 # would be such an example:
90 # Let's say the first keyword string is as follows:
91 # Create Dictionary foo=bar
92 # You wish to take the output of that call and specify it as a literal
93 # value when running the following:
94 # Want Dictionary parm=<literal dictionary specification>
95 # The problem is that there is no way to specify a dictionary as a
96 # literal in Robot Framework.
97 # By having this program support variable assignments and multiple
98 # keywords, the user can invoke it with the following keyword string.
99 # ${my_dict}= Create Dictionary foo=bar ; Want Dictionary ${my_dict}
100
101
102 # The user can pass multiple lib/resource paths by separating them with a
103 # colon.
104 lib_file_path_list = lib_file_path.split(":")
105 # Get rid of empty entry if it exists.
106 if lib_file_path_list[0] == "":
107 del lib_file_path_list[0]
108 for lib_file_path in lib_file_path_list:
109 if lib_file_path.endswith(".py"):
110 grp.rdprint_issuing("import_library(\"" + lib_file_path + "\")")
111 BuiltIn().import_library(lib_file_path)
112 else:
113 grp.rdprint_issuing("my_import_resource(\"" + lib_file_path +
114 "\")")
115 gru.my_import_resource(lib_file_path)
116
117 # The user can pass multiple keyword strings by separating them with " ; ".
118 keyword_list = keyword_string.split(" ; ")
119 for keyword_string in keyword_list:
120 cmd_buf = keyword_string.split(" ")
121 if re.match(r"\$\{", cmd_buf[0]):
122 # This looks like an assignment (e.g. ${var}= <keyword>).
123 # We'll extract the variable name, remove element 0 from
124 # cmd_buf and set the global variable with the results
125 # after running the keyword.
126 var_name = cmd_buf[0].strip("${}=")
127 del cmd_buf[0]
128 else:
129 var_name = ""
130
131 if not quiet:
132 grp.rprint_issuing_keyword(cmd_buf, test_mode)
133 if test_mode:
134 continue
135
136 output = BuiltIn().run_keyword(*cmd_buf)
137
138 if var_name != "":
139 BuiltIn().set_global_variable("${" + var_name + "}", output)
140 else:
141 if output is not None:
142 grp.rprint(output)
143
144###############################################################################
145
146
147###############################################################################
148def main_py():
149
150 r"""
151 Do main program processing.
152 """
153
154 setup()
155
156 lib_file_path = BuiltIn().get_variable_value("${lib_file_path}")
157 keyword_string = BuiltIn().get_variable_value("${keyword_string}")
158 quiet = int(BuiltIn().get_variable_value("${quiet}"))
159 test_mode = int(BuiltIn().get_variable_value("${test_mode}"))
160
161 my_run_keywords(lib_file_path, keyword_string, quiet, test_mode)
162
163###############################################################################