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