Commit | Line | Data |
---|---|---|
0235b0db MJ |
1 | # SPDX-License-Identifier: GPL-2.0-only |
2 | # | |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
5 | ||
1ead9076 SM |
6 | import os |
7 | ||
5995b304 SM |
8 | import bt2 |
9 | ||
dc807017 SM |
10 | # This file defines source component classes to help verify the parameters an |
11 | # log levels passed to components. Each component creates one stream, with a | |
12 | # name derived from either: | |
1b2b6649 | 13 | # |
dc807017 SM |
14 | # - the received params that start with `test-` |
15 | # - the received log level | |
16 | # | |
17 | # The `what` parameter determines what is used. | |
1ead9076 SM |
18 | |
19 | ||
20 | class TestIter(bt2._UserMessageIterator): | |
8d8b141d | 21 | def __init__(self, config, output_port): |
f5567ea8 FD |
22 | params = output_port.user_data["params"] |
23 | obj = output_port.user_data["obj"] | |
1ead9076 | 24 | |
dc807017 | 25 | comp_cls_name = self._component.__class__.__name__ |
1ead9076 | 26 | |
f5567ea8 FD |
27 | if params["what"] == "test-params": |
28 | items = sorted([str(x) for x in params.items() if x[0].startswith("test-")]) | |
29 | stream_name = "{}: {}".format(comp_cls_name, ", ".join(items)) | |
30 | elif params["what"] == "log-level": | |
dc807017 | 31 | log_level = self._component.logging_level |
f5567ea8 FD |
32 | stream_name = "{}: {}".format(comp_cls_name, log_level) |
33 | elif params["what"] == "python-obj": | |
f20175d1 | 34 | assert type(obj) is str or obj is None |
f5567ea8 | 35 | stream_name = "{}: {}".format(comp_cls_name, obj) |
1b2b6649 SM |
36 | else: |
37 | assert False | |
38 | ||
f5567ea8 | 39 | sc = output_port.user_data["sc"] |
dc807017 SM |
40 | tc = sc.trace_class |
41 | t = tc() | |
42 | s = t.create_stream(sc, name=stream_name) | |
43 | ||
44 | self._msgs = [ | |
45 | self._create_stream_beginning_message(s), | |
46 | self._create_stream_end_message(s), | |
47 | ] | |
48 | ||
49 | def __next__(self): | |
50 | if len(self._msgs) == 0: | |
51 | raise StopIteration | |
52 | ||
53 | return self._msgs.pop(0) | |
54 | ||
55 | ||
56 | class Base: | |
f3c9a159 | 57 | def __init__(self, params, obj): |
dc807017 SM |
58 | tc = self._create_trace_class() |
59 | sc = tc.create_stream_class() | |
60 | ||
f5567ea8 | 61 | self._add_output_port("out", {"params": params, "obj": obj, "sc": sc}) |
dc807017 | 62 | |
1ead9076 SM |
63 | |
64 | @bt2.plugin_component_class | |
65 | class TestSourceA(Base, bt2._UserSourceComponent, message_iterator_class=TestIter): | |
59225a3e | 66 | def __init__(self, config, params, obj): |
f3c9a159 | 67 | super().__init__(params, obj) |
1ead9076 SM |
68 | |
69 | @staticmethod | |
7c14d641 | 70 | def _user_query(priv_query_exec, obj, params, method_obj): |
1ead9076 SM |
71 | # Match files starting with 'aaa'. |
72 | ||
f5567ea8 FD |
73 | if obj == "babeltrace.support-info": |
74 | if params["type"] != "file": | |
1ead9076 SM |
75 | return 0 |
76 | ||
f5567ea8 | 77 | name = os.path.basename(str(params["input"])) |
1ead9076 | 78 | |
f5567ea8 FD |
79 | if name.startswith("aaa"): |
80 | return {"weight": 1, "group": "aaa"} | |
1ead9076 SM |
81 | else: |
82 | return 0 | |
83 | else: | |
84 | raise bt2.UnknownObject | |
85 | ||
86 | ||
87 | @bt2.plugin_component_class | |
88 | class TestSourceB(Base, bt2._UserSourceComponent, message_iterator_class=TestIter): | |
59225a3e | 89 | def __init__(self, config, params, obj): |
f3c9a159 | 90 | super().__init__(params, obj) |
1ead9076 SM |
91 | |
92 | @staticmethod | |
7c14d641 | 93 | def _user_query(priv_query_exec, obj, params, method_obj): |
1ead9076 SM |
94 | # Match files starting with 'bbb'. |
95 | ||
f5567ea8 FD |
96 | if obj == "babeltrace.support-info": |
97 | if params["type"] != "file": | |
1ead9076 SM |
98 | return 0 |
99 | ||
f5567ea8 | 100 | name = os.path.basename(str(params["input"])) |
1ead9076 | 101 | |
f5567ea8 FD |
102 | if name.startswith("bbb"): |
103 | return {"weight": 1, "group": "bbb"} | |
1ead9076 SM |
104 | else: |
105 | return 0 | |
106 | else: | |
107 | raise bt2.UnknownObject | |
108 | ||
109 | ||
110 | bt2.register_plugin(module_name=__name__, name="test") |