Commit | Line | Data |
---|---|---|
0235b0db MJ |
1 | # SPDX-License-Identifier: GPL-2.0-only |
2 | # | |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
5 | ||
73760435 SM |
6 | import bt2 |
7 | import os | |
8 | ||
216b7cc7 SM |
9 | # Source component classes in this file recognize and group inputs in |
10 | # various ways. One stream is created by each component, with a name | |
11 | # derived from the component class and the inputs. This is then checked | |
12 | # using the `sink.text.details` sink. | |
13 | ||
73760435 SM |
14 | |
15 | class TestIter(bt2._UserMessageIterator): | |
8d8b141d | 16 | def __init__(self, config, output_port): |
f5567ea8 FD |
17 | inputs = output_port.user_data["inputs"] |
18 | sc = output_port.user_data["sc"] | |
216b7cc7 SM |
19 | tc = sc.trace_class |
20 | t = tc() | |
21 | s = t.create_stream(sc, name=self._make_stream_name(inputs)) | |
22 | ||
23 | self._msgs = [ | |
24 | self._create_stream_beginning_message(s), | |
25 | self._create_stream_end_message(s), | |
26 | ] | |
27 | ||
28 | def _make_stream_name(self, inputs): | |
29 | comp_cls_name = self._component.__class__.__name__ | |
30 | return ( | |
31 | comp_cls_name | |
f5567ea8 FD |
32 | + ": " |
33 | + ", ".join(sorted([os.path.basename(str(x)) for x in inputs])) | |
216b7cc7 SM |
34 | ) |
35 | ||
36 | def __next__(self): | |
37 | if len(self._msgs) == 0: | |
38 | raise StopIteration | |
39 | ||
40 | return self._msgs.pop(0) | |
73760435 SM |
41 | |
42 | ||
43 | class Base: | |
216b7cc7 SM |
44 | def __init__(self, params): |
45 | tc = self._create_trace_class() | |
46 | sc = tc.create_stream_class() | |
47 | ||
f5567ea8 | 48 | self._add_output_port("out", {"inputs": params["inputs"], "sc": sc}) |
73760435 SM |
49 | |
50 | ||
51 | @bt2.plugin_component_class | |
52 | class TestSourceExt(Base, bt2._UserSourceComponent, message_iterator_class=TestIter): | |
53 | """ | |
54 | Recognize files whose name start with 'aaa', 'bbb' or 'ccc'. | |
55 | ||
56 | 'aaa' files are grouped together, 'bbb' files are grouped together, 'ccc' | |
57 | files are not grouped. | |
58 | """ | |
59 | ||
59225a3e | 60 | def __init__(self, config, params, obj): |
216b7cc7 | 61 | super().__init__(params) |
73760435 SM |
62 | |
63 | @staticmethod | |
7c14d641 | 64 | def _user_query(priv_query_exec, obj, params, method_obj): |
f5567ea8 FD |
65 | if obj == "babeltrace.support-info": |
66 | if params["type"] == "file": | |
67 | name = os.path.basename(str(params["input"])) | |
68 | ||
69 | if name.startswith("aaa"): | |
70 | return {"weight": 1, "group": "aaa"} | |
71 | elif name.startswith("bbb"): | |
72 | return {"weight": 0.5, "group": "bbb"} | |
73 | elif name.startswith("ccc"): | |
282a37f5 | 74 | # Try two different ways of returning 1 (an int and a float). |
f5567ea8 | 75 | if name[3] == "1": |
73760435 SM |
76 | return 1 |
77 | else: | |
78 | return 1.0 | |
75cae5af SM |
79 | |
80 | return 0 | |
73760435 | 81 | else: |
76b6c2f7 | 82 | raise bt2.UnknownObject |
73760435 SM |
83 | |
84 | ||
85 | @bt2.plugin_component_class | |
86 | class TestSourceSomeDir( | |
87 | Base, bt2._UserSourceComponent, message_iterator_class=TestIter | |
88 | ): | |
89 | """Recognizes directories named "some-dir". The file "aaa10" in the | |
90 | directory "some-dir" won't be found by TestSourceExt, because we won't | |
91 | recurse in "some-dir".""" | |
92 | ||
59225a3e | 93 | def __init__(self, config, params, obj): |
216b7cc7 | 94 | super().__init__(params) |
73760435 SM |
95 | |
96 | @staticmethod | |
7c14d641 | 97 | def _user_query(priv_query_exec, obj, params, method_obj): |
f5567ea8 FD |
98 | if obj == "babeltrace.support-info": |
99 | if params["type"] == "directory": | |
100 | name = os.path.basename(str(params["input"])) | |
101 | return 1 if name == "some-dir" else 0 | |
73760435 SM |
102 | else: |
103 | return 0 | |
104 | else: | |
76b6c2f7 | 105 | raise bt2.UnknownObject |
73760435 SM |
106 | |
107 | ||
108 | @bt2.plugin_component_class | |
109 | class TestSourceABCDE(Base, bt2._UserSourceComponent, message_iterator_class=TestIter): | |
110 | """A source that recognizes the arbitrary string input "ABCDE".""" | |
111 | ||
59225a3e | 112 | def __init__(self, config, params, obj): |
216b7cc7 | 113 | super().__init__(params) |
73760435 SM |
114 | |
115 | @staticmethod | |
7c14d641 | 116 | def _user_query(priv_query_exec, obj, params, method_obj): |
f5567ea8 | 117 | if obj == "babeltrace.support-info": |
73760435 SM |
118 | return ( |
119 | 1.0 | |
f5567ea8 | 120 | if params["type"] == "string" and params["input"] == "ABCDE" |
73760435 SM |
121 | else 0.0 |
122 | ) | |
123 | else: | |
76b6c2f7 | 124 | raise bt2.UnknownObject |
73760435 SM |
125 | |
126 | ||
127 | class TestSourceNoQuery(bt2._UserSourceComponent, message_iterator_class=TestIter): | |
128 | """A source that does not implement _query at all.""" | |
129 | ||
130 | ||
131 | bt2.register_plugin(module_name=__name__, name="test") |