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