xref: /openbmc/openbmc-test-automation/lib/var_stack.py (revision 1eb85f572b7aefc3f1c0d0237731e493e2f99cd3)
1#!/usr/bin/env python
2
3r"""
4Define the var_stack class.
5"""
6
7import sys
8import collections
9
10try:
11    from robot.utils import DotDict
12except ImportError:
13    pass
14
15import gen_print as gp
16
17
18###############################################################################
19class var_stack:
20
21    r"""
22    Define the variable stack class.
23
24    An object of this class can be used to push variable name/variable value
25    pairs which may be popped off the stack at a later time.  The most obvious
26    use for this is for saving variables that are to be restored later.
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
68    def __init__(self,
69                 obj_name='var_stack'):
70
71        r"""
72        Initialize a new object of this class type.
73
74        Description of argument(s):
75        obj_name                    The name of the object.  This is useful
76                                    for printing out the object.
77        """
78
79        self.__obj_name = obj_name
80        # Create a stack dictionary.
81        try:
82            self.__stack_dict = collections.OrderedDict()
83        except AttributeError:
84            self.__stack_dict = DotDict()
85
86    def sprint_obj(self):
87
88        r"""
89        sprint the fields of this object.  This would normally be for debug
90        purposes.
91        """
92
93        buffer = ""
94
95        buffer += self.__obj_name + ":\n"
96        indent = 2
97        buffer += gp.sprint_varx('stack_dict', self.__stack_dict, 1, indent)
98
99        return buffer
100
101    def print_obj(self):
102
103        r"""
104        print the fields of this object to stdout.  This would normally be for
105        debug purposes.
106        """
107
108        sys.stdout.write(self.sprint_obj())
109
110    def push(self,
111             var_value,
112             var_name=""):
113
114        r"""
115        push the var_name/var_value pair onto the stack.
116
117        Description of argument(s):
118        var_value                   The value being pushed.
119        var_name                    The name of the variable containing the
120                                    value to be pushed.  This parameter is
121                                    normally unnecessary as this function can
122                                    figure out the var_name.  This is provided
123                                    for Robot callers.  In this scenario, we
124                                    are unable to get the variable name
125                                    ourselves.
126        """
127
128        if var_name == "":
129            # The caller has not passed a var_name so we will try to figure
130            # it out.
131            stack_frame_ix = 2
132            var_name = gp.get_arg_name(0, 1, stack_frame_ix)
133        if var_name in self.__stack_dict:
134            self.__stack_dict[var_name].append(var_value)
135        else:
136            self.__stack_dict[var_name] = [var_value]
137
138    def pop(self,
139            var_name=""):
140
141        r"""
142        Pop the value for the given var_name from the stack and return it.
143
144        Description of argument(s):
145        var_name                    The name of the variable whose value is to
146                                    be popped.
147        """
148
149        return self.__stack_dict[var_name].pop()
150
151###############################################################################
152