| Michael Walsh | bdb3965 | 2017-01-27 15:44:49 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python | 
|  | 2 |  | 
|  | 3 | r""" | 
|  | 4 | This module contains utility and wrapper functions useful to robot python | 
|  | 5 | programs. | 
|  | 6 | """ | 
|  | 7 |  | 
|  | 8 | import re | 
|  | 9 | from robot.libraries.BuiltIn import BuiltIn | 
|  | 10 |  | 
|  | 11 |  | 
|  | 12 | ############################################################################### | 
|  | 13 | def my_import_resource(path): | 
|  | 14 |  | 
|  | 15 | r""" | 
|  | 16 | Import the resource file specified in path. | 
|  | 17 |  | 
|  | 18 | Description of arguments: | 
|  | 19 | path   The path to your resource file. | 
|  | 20 |  | 
|  | 21 | This function is a wrapper for BuiltIn().import_resource() and provides | 
|  | 22 | the following benefits: | 
|  | 23 | - When you invoke a robot program from a command line, you may specify | 
|  | 24 | program parameters as follows: | 
|  | 25 |  | 
|  | 26 | -v --variable name:values | 
|  | 27 |  | 
|  | 28 | For example: | 
|  | 29 |  | 
|  | 30 | robot -v parm_x:1 file_x.robot | 
|  | 31 |  | 
|  | 32 | When you do "Resource utils_x.robot" in a .robot program, it processes | 
|  | 33 | "utils_x.robot" BEFORE your command line parms are processed, as one | 
|  | 34 | might expect.  On the other hand, if one of your python library files | 
|  | 35 | were to run BuiltIn().import_resource("utils_x.robot"), it will process | 
|  | 36 | "utils_x.robot" AFTER your program parms are processed.  Let's suppose | 
|  | 37 | that utils_x.robot contains the following: | 
|  | 38 |  | 
|  | 39 | *** Variables *** | 
|  | 40 | ${parm_x}  ${0} | 
|  | 41 |  | 
|  | 42 | If your program is invoked like this: | 
|  | 43 |  | 
|  | 44 | robot -v parm_x:3 file_x.robot | 
|  | 45 |  | 
|  | 46 | And if your program has a python library file that invokes | 
|  | 47 | BuiltIn().import_resource("utils_x.robot"), then parm_x will get set to | 
|  | 48 | ${0}.  In other words, instead of utils_x.robot serving to set a default | 
|  | 49 | value for parm_x, it actually causes the user's specification of | 
|  | 50 | "-v parm_x:3" to be overwritten. | 
|  | 51 |  | 
|  | 52 | This function will remedy that problem by keeping your -v parms intact. | 
|  | 53 |  | 
|  | 54 | - The problems with -v parms mentioned above are also found with variables | 
|  | 55 | from your file_x.robot "** Variables **" section.  Namely, they may get | 
|  | 56 | overwritten when import_resource() is used.  This function will likewise | 
|  | 57 | remedy that problem. | 
|  | 58 |  | 
|  | 59 | """ | 
|  | 60 |  | 
|  | 61 | # Retrieve the values of all current variables into a dictionary. | 
|  | 62 | pre_var_dict = BuiltIn().get_variables() | 
|  | 63 | # Do the import. | 
|  | 64 | BuiltIn().import_resource(path) | 
|  | 65 | # Once again, retrieve the values of all current variables into a | 
|  | 66 | # dictionary. | 
|  | 67 | post_var_dict = BuiltIn().get_variables() | 
|  | 68 |  | 
|  | 69 | # If any variable values were changed due to the prior import, set them | 
|  | 70 | # back to their original values. | 
|  | 71 | for key, value in post_var_dict.iteritems(): | 
|  | 72 | if key in pre_var_dict: | 
|  | 73 | if value != pre_var_dict[key]: | 
|  | 74 | global_var_name = re.sub("[@&]", "$", key) | 
|  | 75 | BuiltIn().set_global_variable(global_var_name, | 
|  | 76 | pre_var_dict[key]) | 
|  | 77 |  | 
|  | 78 | ############################################################################### |