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