bt2: pass custom Python object to Python component's __init__()
[babeltrace.git] / tests / data / cli / convert / auto-source-discovery-grouping / bt_plugin_test.py
CommitLineData
a1040187
SM
1import bt2
2import os
3
4
5class TestIter(bt2._UserMessageIterator):
6 pass
7
8
9class Base:
10 @classmethod
11 def _print_params(cls, params):
12 inputs = sorted([str(x) for x in params['inputs']])
13 print('{}: {}'.format(cls.__name__, ', '.join(inputs)))
14
15
16@bt2.plugin_component_class
17class TestSourceExt(Base, bt2._UserSourceComponent, message_iterator_class=TestIter):
18 """
19 Recognize files whose name start with 'aaa', 'bbb' or 'ccc'.
20
21 'aaa' files are grouped together, 'bbb' files are grouped together, 'ccc'
22 files are not grouped.
23 """
24
b20382e2 25 def __init__(self, params, obj):
a1040187
SM
26 self._print_params(params)
27
28 @staticmethod
bf403eb2 29 def _user_query(priv_query_exec, obj, params):
9e534aae 30 if obj == 'babeltrace.support-info':
a1040187
SM
31 if params['type'] == 'file':
32 name = os.path.basename(str(params['input']))
33
34 if name.startswith('aaa'):
35 return {'weight': 1, 'group': 'aaa'}
36 elif name.startswith('bbb'):
37 return {'weight': 0.5, 'group': 'bbb'}
38 elif name.startswith('ccc'):
39 # Try two different ways of returning "no group", and two
40 # different ways of returning 1 (an int and a float).
41 if name[3] == '1':
42 return {'weight': 1, 'group': None}
43 elif name[3] == '2':
44 return {'weight': 1.0, 'group': None}
45 elif name[3] == '3':
46 return 1
47 else:
48 return 1.0
49 else:
50 return 0
51 else:
7dd3e712 52 raise bt2.UnknownObject
a1040187
SM
53
54
55@bt2.plugin_component_class
56class TestSourceSomeDir(
57 Base, bt2._UserSourceComponent, message_iterator_class=TestIter
58):
59 """Recognizes directories named "some-dir". The file "aaa10" in the
60 directory "some-dir" won't be found by TestSourceExt, because we won't
61 recurse in "some-dir"."""
62
b20382e2 63 def __init__(self, params, obj):
a1040187
SM
64 self._print_params(params)
65
66 @staticmethod
bf403eb2 67 def _user_query(priv_query_exec, obj, params):
9e534aae 68 if obj == 'babeltrace.support-info':
a1040187
SM
69 if params['type'] == 'directory':
70 name = os.path.basename(str(params['input']))
71 return 1 if name == 'some-dir' else 0
72 else:
73 return 0
74 else:
7dd3e712 75 raise bt2.UnknownObject
a1040187
SM
76
77
78@bt2.plugin_component_class
79class TestSourceABCDE(Base, bt2._UserSourceComponent, message_iterator_class=TestIter):
80 """A source that recognizes the arbitrary string input "ABCDE"."""
81
b20382e2 82 def __init__(self, params, obj):
a1040187
SM
83 self._print_params(params)
84
85 @staticmethod
bf403eb2 86 def _user_query(priv_query_exec, obj, params):
9e534aae 87 if obj == 'babeltrace.support-info':
a1040187
SM
88 return (
89 1.0
90 if params['type'] == 'string' and params['input'] == 'ABCDE'
91 else 0.0
92 )
93 else:
7dd3e712 94 raise bt2.UnknownObject
a1040187
SM
95
96
97class TestSourceNoQuery(bt2._UserSourceComponent, message_iterator_class=TestIter):
98 """A source that does not implement _query at all."""
99
100
101bt2.register_plugin(module_name=__name__, name="test")
This page took 0.02852 seconds and 4 git commands to generate.