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