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