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