Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module is the python counterpart to run_keyword.robot. |
| 5 | """ |
| 6 | |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 7 | import gen_robot_print as grp |
| 8 | import gen_robot_valid as grv |
Michael Walsh | 16cbb7f | 2017-02-02 15:54:16 -0600 | [diff] [blame^] | 9 | import gen_robot_utils as gru |
| 10 | |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 11 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 12 | import re |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 13 | |
| 14 | |
| 15 | ############################################################################### |
| 16 | def main_py(): |
| 17 | |
| 18 | r""" |
| 19 | Do main program processing. |
| 20 | """ |
| 21 | |
| 22 | setup() |
| 23 | |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 24 | # NOTE: During code review the following question was raised: Why support |
| 25 | # 1) variable assignments 2) multiple keywords? Couldn't a user simply |
| 26 | # call this program twice to get what they need. If necessary, the user |
| 27 | # could take the output of the first call and specify it as a literal on |
| 28 | # the second call. |
| 29 | # |
| 30 | # However, this approach would not work in all cases. The following case |
| 31 | # would be such an example: |
| 32 | # Let's say the first keyword string is as follows: |
| 33 | # Create Dictionary foo=bar |
| 34 | # You wish to take the output of that call and specify it as a literal |
| 35 | # value when running the following: |
| 36 | # Want Dictionary parm=<literal dictionary specification> |
| 37 | # The problem is that there is no way to specify a dictionary as a |
| 38 | # literal in Robot Framework. |
| 39 | # By having this program support variable assignments and multiple |
| 40 | # keywords, the user can invoke it with the following keyword string. |
| 41 | # ${my_dict}= Create Dictionary foo=bar ; Want Dictionary ${my_dict} |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 42 | |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 43 | # The user can pass multiple lib/resource paths by separating them with a |
| 44 | # colon. |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 45 | |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 46 | lib_file_path_list = \ |
| 47 | BuiltIn().get_variable_value("${lib_file_path}").split(":") |
| 48 | # Get rid of empty entry if it exists. |
| 49 | if lib_file_path_list[0] == "": |
| 50 | del lib_file_path_list[0] |
| 51 | for lib_file_path in lib_file_path_list: |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 52 | # We don't want global variable getting changed when an import is done |
| 53 | # so we'll save it and restore it. |
| 54 | quiet = int(BuiltIn().get_variable_value("${quiet}")) |
| 55 | if lib_file_path.endswith(".py"): |
| 56 | grp.rdprint_issuing("import_library(\"" + lib_file_path + "\")") |
| 57 | BuiltIn().import_library(lib_file_path) |
| 58 | else: |
Michael Walsh | 16cbb7f | 2017-02-02 15:54:16 -0600 | [diff] [blame^] | 59 | grp.rdprint_issuing("my_import_resource(\"" + lib_file_path + |
| 60 | "\")") |
| 61 | gru.my_import_resource(lib_file_path) |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 62 | BuiltIn().set_global_variable("${quiet}", quiet) |
| 63 | |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 64 | # The user can pass multiple keyword strings by separating them with " ; ". |
| 65 | keyword_list = \ |
| 66 | BuiltIn().get_variable_value("${keyword_string}").split(" ; ") |
| 67 | for keyword_string in keyword_list: |
| 68 | cmd_buf = keyword_string.split(" ") |
| 69 | if re.match(r"\$\{", cmd_buf[0]): |
| 70 | # This looks like an assignment (e.g. ${var}= <keyword>). |
| 71 | # We'll extract the variable name, remove element 0 from |
| 72 | # cmd_buf and set the global variable with the results |
| 73 | # after running the keyword. |
| 74 | var_name = cmd_buf[0].strip("${}=") |
| 75 | del cmd_buf[0] |
| 76 | else: |
| 77 | var_name = "" |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 78 | |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 79 | test_mode = int(BuiltIn().get_variable_value("${test_mode}")) |
| 80 | grp.rqprint_issuing_keyword(cmd_buf, test_mode) |
| 81 | if test_mode: |
| 82 | return |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 83 | |
Michael Walsh | e7a7a18 | 2017-01-19 10:37:10 -0600 | [diff] [blame] | 84 | output = BuiltIn().run_keyword(*cmd_buf) |
| 85 | |
| 86 | if var_name != "": |
| 87 | BuiltIn().set_global_variable("${" + var_name + "}", output) |
| 88 | else: |
| 89 | if output is not None: |
| 90 | grp.rprint_var(output) |
Michael Walsh | a2a553c | 2017-01-10 11:17:38 -0600 | [diff] [blame] | 91 | |
| 92 | ############################################################################### |
| 93 | |
| 94 | |
| 95 | ############################################################################### |
| 96 | def setup(): |
| 97 | |
| 98 | r""" |
| 99 | Do general program setup tasks. |
| 100 | """ |
| 101 | |
| 102 | grp.rqprintn() |
| 103 | |
| 104 | validate_parms() |
| 105 | |
| 106 | grp.rqprint_pgm_header() |
| 107 | |
| 108 | ############################################################################### |
| 109 | |
| 110 | |
| 111 | ############################################################################### |
| 112 | def validate_parms(): |
| 113 | |
| 114 | r""" |
| 115 | Validate all program parameters. |
| 116 | """ |
| 117 | |
| 118 | grv.rvalid_value("keyword_string") |
| 119 | |
| 120 | return True |
| 121 | |
| 122 | ############################################################################### |
| 123 | |
| 124 | |
| 125 | ############################################################################### |
| 126 | def program_teardown(): |
| 127 | |
| 128 | r""" |
| 129 | Clean up after this program. |
| 130 | """ |
| 131 | |
| 132 | grp.rqprint_pgm_footer() |
| 133 | |
| 134 | ############################################################################### |