autodisc: improve logging
[babeltrace.git] / tests / data / auto-source-discovery / grouping / bt_plugin_test.py
CommitLineData
73760435
SM
1import bt2
2import os
3
216b7cc7
SM
4# Source component classes in this file recognize and group inputs in
5# various ways. One stream is created by each component, with a name
6# derived from the component class and the inputs. This is then checked
7# using the `sink.text.details` sink.
8
73760435
SM
9
10class TestIter(bt2._UserMessageIterator):
216b7cc7
SM
11 def __init__(self, output_port):
12 inputs = output_port.user_data['inputs']
13 sc = output_port.user_data['sc']
14 tc = sc.trace_class
15 t = tc()
16 s = t.create_stream(sc, name=self._make_stream_name(inputs))
17
18 self._msgs = [
19 self._create_stream_beginning_message(s),
20 self._create_stream_end_message(s),
21 ]
22
23 def _make_stream_name(self, inputs):
24 comp_cls_name = self._component.__class__.__name__
25 return (
26 comp_cls_name
27 + ': '
28 + ', '.join(sorted([os.path.basename(str(x)) for x in inputs]))
29 )
30
31 def __next__(self):
32 if len(self._msgs) == 0:
33 raise StopIteration
34
35 return self._msgs.pop(0)
73760435
SM
36
37
38class Base:
216b7cc7
SM
39 def __init__(self, params):
40 tc = self._create_trace_class()
41 sc = tc.create_stream_class()
42
43 self._add_output_port('out', {'inputs': params['inputs'], 'sc': sc})
73760435
SM
44
45
46@bt2.plugin_component_class
47class TestSourceExt(Base, bt2._UserSourceComponent, message_iterator_class=TestIter):
48 """
49 Recognize files whose name start with 'aaa', 'bbb' or 'ccc'.
50
51 'aaa' files are grouped together, 'bbb' files are grouped together, 'ccc'
52 files are not grouped.
53 """
54
66964f3f 55 def __init__(self, params, obj):
216b7cc7 56 super().__init__(params)
73760435
SM
57
58 @staticmethod
7c14d641 59 def _user_query(priv_query_exec, obj, params, method_obj):
1a29b831 60 if obj == 'babeltrace.support-info':
73760435
SM
61 if params['type'] == 'file':
62 name = os.path.basename(str(params['input']))
63
64 if name.startswith('aaa'):
65 return {'weight': 1, 'group': 'aaa'}
66 elif name.startswith('bbb'):
67 return {'weight': 0.5, 'group': 'bbb'}
68 elif name.startswith('ccc'):
69 # Try two different ways of returning "no group", and two
70 # different ways of returning 1 (an int and a float).
71 if name[3] == '1':
72 return {'weight': 1, 'group': None}
73 elif name[3] == '2':
74 return {'weight': 1.0, 'group': None}
75 elif name[3] == '3':
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
86class 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
66964f3f 93 def __init__(self, params, obj):
216b7cc7 94 super().__init__(params)
73760435
SM
95
96 @staticmethod
7c14d641 97 def _user_query(priv_query_exec, obj, params, method_obj):
1a29b831 98 if obj == 'babeltrace.support-info':
73760435
SM
99 if params['type'] == 'directory':
100 name = os.path.basename(str(params['input']))
101 return 1 if name == 'some-dir' else 0
102 else:
103 return 0
104 else:
76b6c2f7 105 raise bt2.UnknownObject
73760435
SM
106
107
108@bt2.plugin_component_class
109class TestSourceABCDE(Base, bt2._UserSourceComponent, message_iterator_class=TestIter):
110 """A source that recognizes the arbitrary string input "ABCDE"."""
111
66964f3f 112 def __init__(self, params, obj):
216b7cc7 113 super().__init__(params)
73760435
SM
114
115 @staticmethod
7c14d641 116 def _user_query(priv_query_exec, obj, params, method_obj):
1a29b831 117 if obj == 'babeltrace.support-info':
73760435
SM
118 return (
119 1.0
120 if params['type'] == 'string' and params['input'] == 'ABCDE'
121 else 0.0
122 )
123 else:
76b6c2f7 124 raise bt2.UnknownObject
73760435
SM
125
126
127class TestSourceNoQuery(bt2._UserSourceComponent, message_iterator_class=TestIter):
128 """A source that does not implement _query at all."""
129
130
131bt2.register_plugin(module_name=__name__, name="test")
This page took 0.031448 seconds and 4 git commands to generate.