File: calls.py

package info (click to toggle)
pcs 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,148 kB
  • sloc: python: 238,810; xml: 20,833; ruby: 13,203; makefile: 1,595; sh: 484
file content (254 lines) | stat: -rw-r--r-- 7,785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
def format_call(call):
    if hasattr(call, "format"):
        return call.format()
    return call


def show_calls(name_list, call_list):
    return "\n".join(
        [
            "  {0}. '{1}': {2}".format(i, x[0], format_call(x[1]))
            for i, x in enumerate(zip(name_list, call_list, strict=False))
        ]
    )


class Queue:
    def __init__(self, call_list_builder=None):
        if not call_list_builder:
            call_list_builder = CallListBuilder()

        self.__call_list = call_list_builder.calls
        self.__name_list = call_list_builder.names

        self.__index = 0

    def take(self, type_of_call, real_call_info=None):
        if self.__index >= len(self.__call_list):
            raise self.__extra_call(type_of_call, real_call_info)

        call = self.__call_list[self.__index]

        if call.type != type_of_call:
            raise self.__unexpected_type(call, type_of_call, real_call_info)

        self.__index += 1
        return self.__index, call

    def has_type(self, call_type):
        return any(call.type == call_type for call in self.__call_list)

    @property
    def remaining(self):
        return self.__call_list[self.__index :]

    @property
    def taken(self):
        return self.__call_list[: self.__index]

    def error_with_context(self, message):
        return AssertionError(
            "{0}\nAll calls in queue (current index={1}):\n{2}".format(
                message,
                self.__index,
                show_calls(self.__name_list, self.__call_list),
            )
        )

    def __unexpected_type(self, call, real_type, real_call_info):
        return self.error_with_context(
            (
                "{0}. call was expected as '{1}' type but was '{2}' type"
                "\n  expected call: {3}{4}"
                "\nHint: check call compatibility: for example if you use"
                " env.push_cib() then runner.cib.push() will be never launched"
            ).format(
                self.__index,
                call.type,
                real_type,
                call,
                (
                    "\n  real call: {0}".format(real_call_info)
                    if real_call_info
                    else ""
                ),
            )
        )

    def __extra_call(self, type_of_call, real_call_info):
        return self.error_with_context(
            "No next call expected, but was ({0}):\n    '{1}'".format(
                type_of_call, real_call_info
            )
        )


class CallListBuilder:
    def __init__(self):
        self.__call_list = []
        self.__name_list = []

    @property
    def calls(self):
        return list(self.__call_list)

    @property
    def names(self):
        return list(self.__name_list)

    def __set(self, instead_name, name, call):
        """
        Replace call that has key instead_name with new call that has key name

        string name -- key of the call
        Call call
        string instead_name -- key of call instead of which this new call is to
            be placed
        """
        if instead_name not in self.__name_list:
            raise self.__cannot_put("instead of", instead_name, name, call)

        for i, current_name in enumerate(self.__name_list):
            if current_name == instead_name:
                self.__call_list[i] = call
                # yes we change the name as well
                self.__name_list[i] = name
                return

    def __append(self, name, call):
        """
        Append call.

        string name -- key of the call
        Call call
        """
        self.__name_list.append(name)
        self.__call_list.append(call)

    def __insert(self, before_name, name, call):
        """
        Insert call before call with before_name.

        string before_name -- key of call before which this new call is to be
            placed
        string name -- key of the call
        Call call
        """
        if before_name not in self.__name_list:
            raise self.__cannot_put("before", before_name, name, call)

        index = self.__name_list.index(before_name)
        self.__name_list.insert(index, name)
        self.__call_list.insert(index, call)

    def remove(self, name):
        """
        Remove a call with the specified name
        """
        try:
            index = self.__name_list.index(name)
            del self.__call_list[index]
            del self.__name_list[index]
        except ValueError as e:
            raise self.__name_not_exists(name) from e

    def trim_before(self, name):
        """
        Remove a call with the specified name and all calls after it from the list
        """
        try:
            index = self.__name_list.index(name)
            self.__call_list = self.__call_list[:index]
            self.__name_list = self.__name_list[:index]
        except ValueError as e:
            raise self.__name_not_exists(name) from e

    def get(self, name):
        """
        Get first call with name.

        string name -- key of the call
        """
        try:
            return self.__call_list[self.__name_list.index(name)]
        except ValueError as e:
            raise self.__name_not_exists(name) from e

    def place(self, name, call, before=None, instead=None):
        """
        Place call into calllist.

        string name -- key of the call
        Call call
        string before -- key of call before which this new call is to be placed
        string instead -- key of call instead of which this new call is to be
            placed
        """
        if name and name in self.__name_list and instead != name:
            raise self.__name_exists_already(name)

        if before and instead:
            raise self.__cannot_use_before_and_instead(
                name,
                call,
                before,
                instead,
            )

        if not hasattr(call, "type") or not call.type:
            raise self.__type_of_call_is_not_specified(call)

        if before:
            self.__insert(before, name, call)
        elif instead:
            self.__set(instead, name, call)
        else:
            self.__append(name, call)

    def __error_with_context(self, message):
        return AssertionError(
            "{0}\nCalls in the configuration call collection are:\n{1}".format(
                message,
                show_calls(self.__name_list, self.__call_list),
            )
        )

    @staticmethod
    def __type_of_call_is_not_specified(call):
        return AssertionError(
            (
                "Class {0}.{1} must have the attribute 'type' with no-falsy "
                "value."
            ).format(call.__module__, call.__class__.__name__)
        )

    def __name_not_exists(self, name):
        return self.__error_with_context(
            "Call named '{0}' does not exist.".format(name)
        )

    def __name_exists_already(self, name):
        return self.__error_with_context(
            "Name '{0}' is in this configuration already.".format(name)
        )

    def __cannot_use_before_and_instead(self, name, call, before, instead):
        return self.__error_with_context(
            (
                "Args 'before' ({0}) and 'instead' ({1}) cannot be used"
                " together\n  '{2}': {3}"
            ).format(before, instead, name, call)
        )

    def __cannot_put(self, where_type, where_name, name, call):
        return self.__error_with_context(
            (
                "Cannot put call named '{0}' ({1}) {2} '{3}'"
                " because '{3}' does not exist."
            ).format(
                name,
                call,
                where_type,
                where_name,
            )
        )