blob: 3bb89c2e18f8d6d586149f1dbe360bc7b2908aff [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 Walshedb5c942019-03-28 12:40:50 -05007import gen_print as gp
Michael Walsha2a553c2017-01-10 11:17:38 -06008import 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():
Michael Walsha2a553c2017-01-10 11:17:38 -060016 r"""
17 Do general program setup tasks.
18 """
19
Michael Walshedb5c942019-03-28 12:40:50 -050020 gp.qprintn()
Michael Walsha2a553c2017-01-10 11:17:38 -060021
22 validate_parms()
23
Michael Walshedb5c942019-03-28 12:40:50 -050024 gp.qprint_pgm_header()
Michael Walsha2a553c2017-01-10 11:17:38 -060025
Michael Walsha2a553c2017-01-10 11:17:38 -060026
Michael Walsha2a553c2017-01-10 11:17:38 -060027def validate_parms():
Michael Walsha2a553c2017-01-10 11:17:38 -060028 r"""
29 Validate all program parameters.
30 """
31
Michael Walshe7edb222019-08-19 17:39:38 -050032 grv.valid_value("keyword_string")
Michael Walsha2a553c2017-01-10 11:17:38 -060033
34 return True
35
Michael Walsha2a553c2017-01-10 11:17:38 -060036
Michael Walsha2a553c2017-01-10 11:17:38 -060037def program_teardown():
Michael Walsha2a553c2017-01-10 11:17:38 -060038 r"""
39 Clean up after this program.
40 """
41
Michael Walshedb5c942019-03-28 12:40:50 -050042 gp.qprint_pgm_footer()
Michael Walsha2a553c2017-01-10 11:17:38 -060043
Michael Walshe9d78c42017-03-02 14:41:15 -060044
Michael Walshe9d78c42017-03-02 14:41:15 -060045def my_run_keywords(lib_file_path,
46 keyword_string,
47 quiet=0,
48 test_mode=0):
Michael Walshe9d78c42017-03-02 14:41:15 -060049 r"""
50 Run the keywords in the keyword string.
51
52 Description of arguments:
53 lib_file_path The path to a library or resource needed to run the
54 keywords. This may contain a colon-delimited list of
55 library/resource paths.
56 keyword_string The keyword string to be run by this function. If this
57 keyword string contains " ; " anywhere, it will be taken to
58 be multiple keyword strings. Each keyword may also include
59 a variable assignment. Example:
60 ${my_var}= My Keyword
61 quiet If this parameter is set to "1", this program will print
62 only essential information, i.e. it will not echo
63 parameters, echo commands, print the total run time, etc.
64 test_mode This means that this program should go through all the
65 motions but not actually do anything substantial.
66 """
67
68 # NOTE: During code review the following question was raised: Why support
69 # 1) variable assignments 2) multiple keywords? Couldn't a user simply
70 # call this program twice to get what they need. If necessary, the user
71 # could take the output of the first call and specify it as a literal on
72 # the second call.
73 #
74 # However, this approach would not work in all cases. The following case
75 # would be such an example:
76 # Let's say the first keyword string is as follows:
77 # Create Dictionary foo=bar
78 # You wish to take the output of that call and specify it as a literal
79 # value when running the following:
80 # Want Dictionary parm=<literal dictionary specification>
81 # The problem is that there is no way to specify a dictionary as a
82 # literal in Robot Framework.
83 # By having this program support variable assignments and multiple
84 # keywords, the user can invoke it with the following keyword string.
85 # ${my_dict}= Create Dictionary foo=bar ; Want Dictionary ${my_dict}
86
Michael Walshe9d78c42017-03-02 14:41:15 -060087 # The user can pass multiple lib/resource paths by separating them with a
88 # colon.
89 lib_file_path_list = lib_file_path.split(":")
90 # Get rid of empty entry if it exists.
91 if lib_file_path_list[0] == "":
92 del lib_file_path_list[0]
93 for lib_file_path in lib_file_path_list:
94 if lib_file_path.endswith(".py"):
Michael Walshedb5c942019-03-28 12:40:50 -050095 gp.dprint_issuing("import_library(\"" + lib_file_path + "\")")
Michael Walshe9d78c42017-03-02 14:41:15 -060096 BuiltIn().import_library(lib_file_path)
97 else:
Michael Walshedb5c942019-03-28 12:40:50 -050098 gp.dprint_issuing("my_import_resource(\"" + lib_file_path + "\")")
Michael Walshe9d78c42017-03-02 14:41:15 -060099 gru.my_import_resource(lib_file_path)
100
101 # The user can pass multiple keyword strings by separating them with " ; ".
102 keyword_list = keyword_string.split(" ; ")
103 for keyword_string in keyword_list:
104 cmd_buf = keyword_string.split(" ")
105 if re.match(r"\$\{", cmd_buf[0]):
106 # This looks like an assignment (e.g. ${var}= <keyword>).
107 # We'll extract the variable name, remove element 0 from
108 # cmd_buf and set the global variable with the results
109 # after running the keyword.
110 var_name = cmd_buf[0].strip("${}=")
111 del cmd_buf[0]
112 else:
113 var_name = ""
114
115 if not quiet:
Michael Walshedb5c942019-03-28 12:40:50 -0500116 gp.print_issuing(cmd_buf, test_mode)
Michael Walshe9d78c42017-03-02 14:41:15 -0600117 if test_mode:
118 continue
119
120 output = BuiltIn().run_keyword(*cmd_buf)
121
122 if var_name != "":
123 BuiltIn().set_global_variable("${" + var_name + "}", output)
124 else:
125 if output is not None:
Michael Walshedb5c942019-03-28 12:40:50 -0500126 gp.gp_print(output)
Michael Walshe9d78c42017-03-02 14:41:15 -0600127
Michael Walshe9d78c42017-03-02 14:41:15 -0600128
Michael Walshe9d78c42017-03-02 14:41:15 -0600129def main_py():
Michael Walshe9d78c42017-03-02 14:41:15 -0600130 r"""
131 Do main program processing.
132 """
133
134 setup()
135
136 lib_file_path = BuiltIn().get_variable_value("${lib_file_path}")
137 keyword_string = BuiltIn().get_variable_value("${keyword_string}")
138 quiet = int(BuiltIn().get_variable_value("${quiet}"))
139 test_mode = int(BuiltIn().get_variable_value("${test_mode}"))
140
141 my_run_keywords(lib_file_path, keyword_string, quiet, test_mode)