George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | Define the var_stack class. |
| 5 | """ |
| 6 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame^] | 7 | import sys |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 8 | import collections |
Michael Walsh | 514fad7 | 2019-01-21 14:34:01 -0600 | [diff] [blame] | 9 | import copy |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 10 | |
| 11 | try: |
| 12 | from robot.utils import DotDict |
| 13 | except ImportError: |
| 14 | pass |
| 15 | |
| 16 | import gen_print as gp |
| 17 | |
| 18 | |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 19 | class var_stack: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame^] | 20 | |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 21 | r""" |
| 22 | Define the variable stack class. |
| 23 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 24 | An object of this class can be used to push variable name/variable value pairs which may be popped off |
| 25 | the stack at a later time. The most obvious use for this is for saving variables that are to be restored |
| 26 | later. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 27 | |
| 28 | Example code: |
| 29 | |
| 30 | save_stack = var_stack('save_stack') |
| 31 | var1 = "johnson" |
| 32 | save_stack.push(var1) |
| 33 | var1 = "smith" |
| 34 | ... |
| 35 | var1 = save_stack.pop('var1') |
| 36 | # var1 has now been restored to the value "johnson". |
| 37 | |
| 38 | |
| 39 | Example use: |
| 40 | |
| 41 | var1 = "mike" |
| 42 | save_stack.push(var1) |
| 43 | var1 = "james" |
| 44 | save_stack.push(var1) |
| 45 | save_stack.print_obj() |
| 46 | |
| 47 | # The print-out of the object would then look like this: |
| 48 | |
| 49 | save_stack: |
| 50 | stack_dict: |
| 51 | [var1]: |
| 52 | [var1][0]: mike |
| 53 | [var1][1]: james |
| 54 | |
| 55 | # Continuing with this code... |
| 56 | |
| 57 | var1 = save_stack.pop('var1') |
| 58 | save_stack.print_obj() |
| 59 | |
| 60 | # The print-out of the object would then look like this: |
| 61 | |
| 62 | save_stack: |
| 63 | stack_dict: |
| 64 | [var1]: |
| 65 | [var1][0]: mike |
| 66 | """ |
| 67 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame^] | 68 | def __init__(self, |
| 69 | obj_name='var_stack'): |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 70 | r""" |
| 71 | Initialize a new object of this class type. |
| 72 | |
| 73 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 74 | obj_name The name of the object. This is useful for printing out the object. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 75 | """ |
| 76 | |
| 77 | self.__obj_name = obj_name |
| 78 | # Create a stack dictionary. |
| 79 | try: |
| 80 | self.__stack_dict = collections.OrderedDict() |
| 81 | except AttributeError: |
| 82 | self.__stack_dict = DotDict() |
| 83 | |
| 84 | def sprint_obj(self): |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 85 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 86 | sprint the fields of this object. This would normally be for debug purposes. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 87 | """ |
| 88 | |
| 89 | buffer = "" |
| 90 | |
| 91 | buffer += self.__obj_name + ":\n" |
| 92 | indent = 2 |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame^] | 93 | buffer += gp.sprint_varx('stack_dict', self.__stack_dict, indent) |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 94 | |
| 95 | return buffer |
| 96 | |
| 97 | def print_obj(self): |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 98 | r""" |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 99 | print the fields of this object to stdout. This would normally be for debug purposes. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 100 | """ |
| 101 | |
| 102 | sys.stdout.write(self.sprint_obj()) |
| 103 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame^] | 104 | def push(self, |
| 105 | var_value, |
| 106 | var_name=""): |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 107 | r""" |
| 108 | push the var_name/var_value pair onto the stack. |
| 109 | |
| 110 | Description of argument(s): |
| 111 | var_value The value being pushed. |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 112 | var_name The name of the variable containing the value to be pushed. This |
| 113 | parameter is normally unnecessary as this function can figure out the |
| 114 | var_name. This is provided for Robot callers. In this scenario, we are |
| 115 | unable to get the variable name ourselves. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 116 | """ |
| 117 | |
| 118 | if var_name == "": |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 119 | # The caller has not passed a var_name so we will try to figure it out. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 120 | stack_frame_ix = 2 |
| 121 | var_name = gp.get_arg_name(0, 1, stack_frame_ix) |
| 122 | if var_name in self.__stack_dict: |
| 123 | self.__stack_dict[var_name].append(var_value) |
| 124 | else: |
Michael Walsh | 514fad7 | 2019-01-21 14:34:01 -0600 | [diff] [blame] | 125 | self.__stack_dict[var_name] = copy.deepcopy([var_value]) |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 126 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame^] | 127 | def pop(self, |
| 128 | var_name=""): |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 129 | r""" |
| 130 | Pop the value for the given var_name from the stack and return it. |
| 131 | |
| 132 | Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 133 | var_name The name of the variable whose value is to be popped. |
Michael Walsh | 05cd10e | 2017-08-29 10:53:28 -0500 | [diff] [blame] | 134 | """ |
| 135 | |
| 136 | return self.__stack_dict[var_name].pop() |