Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
81447b5b | 4 | |
5995b304 | 5 | import sys |
81447b5b | 6 | import collections.abc |
5995b304 SM |
7 | |
8 | from bt2 import port as bt2_port | |
9 | from bt2 import error as bt2_error | |
10 | from bt2 import utils as bt2_utils | |
3fb99a22 | 11 | from bt2 import value as bt2_value |
5995b304 SM |
12 | from bt2 import object as bt2_object |
13 | from bt2 import native_bt | |
3fb99a22 | 14 | from bt2 import clock_class as bt2_clock_class |
5995b304 | 15 | from bt2 import trace_class as bt2_trace_class |
3fb99a22 | 16 | from bt2 import query_executor as bt2_query_executor |
5995b304 | 17 | from bt2 import message_iterator as bt2_message_iterator |
c345b078 | 18 | from bt2 import integer_range_set as bt2_integer_range_set |
811644b8 PP |
19 | |
20 | ||
0feb43aa SM |
21 | class _IncompleteUserClass(Exception): |
22 | pass | |
23 | ||
24 | ||
81447b5b PP |
25 | # This class wraps a component class pointer. This component class could |
26 | # have been created by Python code, but since we only have the pointer, | |
27 | # we can only wrap it in a generic way and lose the original Python | |
28 | # class. | |
601c0026 SM |
29 | # |
30 | # Subclasses must implement some methods that this base class uses: | |
31 | # | |
85906b6b | 32 | # - _bt_as_component_class_ptr: static method, convert the passed component class |
601c0026 SM |
33 | # pointer to a 'bt_component_class *'. |
34 | ||
cfbd7cf3 | 35 | |
e5914347 | 36 | class _ComponentClassConst(bt2_object._SharedObject): |
81447b5b PP |
37 | @property |
38 | def name(self): | |
85906b6b | 39 | ptr = self._bt_as_component_class_ptr(self._ptr) |
601c0026 SM |
40 | name = native_bt.component_class_get_name(ptr) |
41 | assert name is not None | |
811644b8 | 42 | return name |
81447b5b PP |
43 | |
44 | @property | |
45 | def description(self): | |
85906b6b | 46 | ptr = self._bt_as_component_class_ptr(self._ptr) |
601c0026 | 47 | return native_bt.component_class_get_description(ptr) |
81447b5b | 48 | |
40910fbb PP |
49 | @property |
50 | def help(self): | |
85906b6b | 51 | ptr = self._bt_as_component_class_ptr(self._ptr) |
601c0026 SM |
52 | return native_bt.component_class_get_help(ptr) |
53 | ||
85906b6b FD |
54 | def _bt_component_class_ptr(self): |
55 | return self._bt_as_component_class_ptr(self._ptr) | |
40910fbb | 56 | |
811644b8 | 57 | def __eq__(self, other): |
615238be | 58 | if not isinstance(other, _ComponentClassConst): |
811644b8 PP |
59 | try: |
60 | if not issubclass(other, _UserComponent): | |
61 | return False | |
62 | except TypeError: | |
63 | return False | |
81447b5b | 64 | |
811644b8 | 65 | return self.addr == other.addr |
81447b5b PP |
66 | |
67 | ||
615238be | 68 | class _SourceComponentClassConst(_ComponentClassConst): |
9dee90bd SM |
69 | @staticmethod |
70 | def _get_ref(ptr): | |
71 | native_bt.component_class_source_get_ref(ptr) | |
72 | ||
73 | @staticmethod | |
74 | def _put_ref(ptr): | |
75 | native_bt.component_class_source_put_ref(ptr) | |
76 | ||
cfbd7cf3 FD |
77 | _bt_as_component_class_ptr = staticmethod( |
78 | native_bt.component_class_source_as_component_class | |
79 | ) | |
81447b5b PP |
80 | |
81 | ||
615238be | 82 | class _FilterComponentClassConst(_ComponentClassConst): |
9dee90bd SM |
83 | @staticmethod |
84 | def _get_ref(ptr): | |
85 | native_bt.component_class_filter_get_ref(ptr) | |
86 | ||
87 | @staticmethod | |
88 | def _put_ref(ptr): | |
89 | native_bt.component_class_filter_put_ref(ptr) | |
90 | ||
cfbd7cf3 FD |
91 | _bt_as_component_class_ptr = staticmethod( |
92 | native_bt.component_class_filter_as_component_class | |
93 | ) | |
81447b5b PP |
94 | |
95 | ||
615238be | 96 | class _SinkComponentClassConst(_ComponentClassConst): |
9dee90bd SM |
97 | @staticmethod |
98 | def _get_ref(ptr): | |
99 | native_bt.component_class_sink_get_ref(ptr) | |
100 | ||
101 | @staticmethod | |
102 | def _put_ref(ptr): | |
103 | native_bt.component_class_sink_put_ref(ptr) | |
104 | ||
cfbd7cf3 FD |
105 | _bt_as_component_class_ptr = staticmethod( |
106 | native_bt.component_class_sink_as_component_class | |
107 | ) | |
81447b5b PP |
108 | |
109 | ||
811644b8 PP |
110 | class _PortIterator(collections.abc.Iterator): |
111 | def __init__(self, comp_ports): | |
112 | self._comp_ports = comp_ports | |
113 | self._at = 0 | |
114 | ||
115 | def __next__(self): | |
116 | if self._at == len(self._comp_ports): | |
117 | raise StopIteration | |
118 | ||
119 | comp_ports = self._comp_ports | |
894a8df5 SM |
120 | comp_ptr = comp_ports._component_ptr |
121 | ||
122 | port_ptr = comp_ports._borrow_port_ptr_at_index(comp_ptr, self._at) | |
123 | assert port_ptr is not None | |
124 | ||
125 | name = native_bt.port_get_name(comp_ports._port_pycls._as_port_ptr(port_ptr)) | |
126 | assert name is not None | |
811644b8 | 127 | |
811644b8 PP |
128 | self._at += 1 |
129 | return name | |
130 | ||
131 | ||
132 | class _ComponentPorts(collections.abc.Mapping): | |
894a8df5 SM |
133 | # component_ptr is a bt_component_source *, bt_component_filter * or |
134 | # bt_component_sink *. Its type must match the type expected by the | |
135 | # functions passed as arguments. | |
136 | ||
cfbd7cf3 FD |
137 | def __init__( |
138 | self, | |
139 | component_ptr, | |
140 | borrow_port_ptr_by_name, | |
141 | borrow_port_ptr_at_index, | |
142 | get_port_count, | |
143 | port_pycls, | |
144 | ): | |
894a8df5 SM |
145 | self._component_ptr = component_ptr |
146 | self._borrow_port_ptr_by_name = borrow_port_ptr_by_name | |
147 | self._borrow_port_ptr_at_index = borrow_port_ptr_at_index | |
148 | self._get_port_count = get_port_count | |
149 | self._port_pycls = port_pycls | |
811644b8 PP |
150 | |
151 | def __getitem__(self, key): | |
e5914347 | 152 | bt2_utils._check_str(key) |
894a8df5 | 153 | port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key) |
811644b8 PP |
154 | |
155 | if port_ptr is None: | |
156 | raise KeyError(key) | |
157 | ||
894a8df5 | 158 | return self._port_pycls._create_from_ptr_and_get_ref(port_ptr) |
811644b8 PP |
159 | |
160 | def __len__(self): | |
894a8df5 SM |
161 | count = self._get_port_count(self._component_ptr) |
162 | assert count >= 0 | |
811644b8 PP |
163 | return count |
164 | ||
165 | def __iter__(self): | |
166 | return _PortIterator(self) | |
167 | ||
168 | ||
81447b5b | 169 | # This class holds the methods which are common to both generic |
601c0026 SM |
170 | # component objects and Python user component objects. |
171 | # | |
172 | # Subclasses must provide these methods or property: | |
173 | # | |
85906b6b | 174 | # - _bt_borrow_component_class_ptr: static method, must return a pointer to the |
601c0026 SM |
175 | # specialized component class (e.g. 'bt_component_class_sink *') of the |
176 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
85906b6b | 177 | # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* |
601c0026 | 178 | # constants. |
85906b6b | 179 | # - _bt_as_component_ptr: static method, must return the passed specialized |
1c9ed2ff | 180 | # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'. |
601c0026 | 181 | |
cfbd7cf3 | 182 | |
615238be | 183 | class _ComponentConst: |
81447b5b PP |
184 | @property |
185 | def name(self): | |
85906b6b | 186 | ptr = self._bt_as_component_ptr(self._ptr) |
1c9ed2ff SM |
187 | name = native_bt.component_get_name(ptr) |
188 | assert name is not None | |
811644b8 PP |
189 | return name |
190 | ||
e874da19 PP |
191 | @property |
192 | def logging_level(self): | |
85906b6b | 193 | ptr = self._bt_as_component_ptr(self._ptr) |
e874da19 PP |
194 | return native_bt.component_get_logging_level(ptr) |
195 | ||
81447b5b | 196 | @property |
e8ac1aae | 197 | def cls(self): |
85906b6b | 198 | cc_ptr = self._bt_borrow_component_class_ptr(self._ptr) |
601c0026 | 199 | assert cc_ptr is not None |
615238be | 200 | return _create_component_class_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
201 | cc_ptr, self._bt_comp_cls_type |
202 | ) | |
81447b5b | 203 | |
811644b8 | 204 | def __eq__(self, other): |
f5567ea8 | 205 | if not hasattr(other, "addr"): |
811644b8 | 206 | return False |
81447b5b | 207 | |
811644b8 | 208 | return self.addr == other.addr |
81447b5b | 209 | |
81447b5b | 210 | |
615238be | 211 | class _SourceComponentConst(_ComponentConst): |
cfbd7cf3 FD |
212 | _bt_borrow_component_class_ptr = staticmethod( |
213 | native_bt.component_source_borrow_class_const | |
214 | ) | |
85906b6b | 215 | _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
cfbd7cf3 FD |
216 | _bt_as_component_class_ptr = staticmethod( |
217 | native_bt.component_class_source_as_component_class | |
218 | ) | |
85906b6b | 219 | _bt_as_component_ptr = staticmethod(native_bt.component_source_as_component_const) |
81447b5b | 220 | |
81447b5b | 221 | |
615238be | 222 | class _FilterComponentConst(_ComponentConst): |
cfbd7cf3 FD |
223 | _bt_borrow_component_class_ptr = staticmethod( |
224 | native_bt.component_filter_borrow_class_const | |
225 | ) | |
85906b6b | 226 | _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
cfbd7cf3 FD |
227 | _bt_as_component_class_ptr = staticmethod( |
228 | native_bt.component_class_filter_as_component_class | |
229 | ) | |
85906b6b | 230 | _bt_as_component_ptr = staticmethod(native_bt.component_filter_as_component_const) |
81447b5b | 231 | |
81447b5b | 232 | |
615238be | 233 | class _SinkComponentConst(_ComponentConst): |
cfbd7cf3 FD |
234 | _bt_borrow_component_class_ptr = staticmethod( |
235 | native_bt.component_sink_borrow_class_const | |
236 | ) | |
85906b6b | 237 | _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
cfbd7cf3 FD |
238 | _bt_as_component_class_ptr = staticmethod( |
239 | native_bt.component_class_sink_as_component_class | |
240 | ) | |
85906b6b | 241 | _bt_as_component_ptr = staticmethod(native_bt.component_sink_as_component_const) |
81447b5b PP |
242 | |
243 | ||
615238be | 244 | # This is analogous to _SourceComponentClassConst, but for source |
81447b5b | 245 | # component objects. |
e5914347 | 246 | class _GenericSourceComponentConst(bt2_object._SharedObject, _SourceComponentConst): |
9dee90bd SM |
247 | @staticmethod |
248 | def _get_ref(ptr): | |
249 | native_bt.component_source_get_ref(ptr) | |
250 | ||
251 | @staticmethod | |
252 | def _put_ref(ptr): | |
253 | native_bt.component_source_put_ref(ptr) | |
894a8df5 | 254 | |
811644b8 PP |
255 | @property |
256 | def output_ports(self): | |
cfbd7cf3 FD |
257 | return _ComponentPorts( |
258 | self._ptr, | |
259 | native_bt.component_source_borrow_output_port_by_name_const, | |
260 | native_bt.component_source_borrow_output_port_by_index_const, | |
261 | native_bt.component_source_get_output_port_count, | |
5813b3a3 | 262 | bt2_port._OutputPortConst, |
cfbd7cf3 | 263 | ) |
81447b5b PP |
264 | |
265 | ||
615238be | 266 | # This is analogous to _FilterComponentClassConst, but for filter |
81447b5b | 267 | # component objects. |
e5914347 | 268 | class _GenericFilterComponentConst(bt2_object._SharedObject, _FilterComponentConst): |
9dee90bd SM |
269 | @staticmethod |
270 | def _get_ref(ptr): | |
271 | native_bt.component_filter_get_ref(ptr) | |
272 | ||
273 | @staticmethod | |
274 | def _put_ref(ptr): | |
275 | native_bt.component_filter_put_ref(ptr) | |
894a8df5 | 276 | |
811644b8 PP |
277 | @property |
278 | def output_ports(self): | |
cfbd7cf3 FD |
279 | return _ComponentPorts( |
280 | self._ptr, | |
281 | native_bt.component_filter_borrow_output_port_by_name_const, | |
282 | native_bt.component_filter_borrow_output_port_by_index_const, | |
283 | native_bt.component_filter_get_output_port_count, | |
5813b3a3 | 284 | bt2_port._OutputPortConst, |
cfbd7cf3 | 285 | ) |
811644b8 PP |
286 | |
287 | @property | |
288 | def input_ports(self): | |
cfbd7cf3 FD |
289 | return _ComponentPorts( |
290 | self._ptr, | |
291 | native_bt.component_filter_borrow_input_port_by_name_const, | |
292 | native_bt.component_filter_borrow_input_port_by_index_const, | |
293 | native_bt.component_filter_get_input_port_count, | |
5813b3a3 | 294 | bt2_port._InputPortConst, |
cfbd7cf3 | 295 | ) |
81447b5b PP |
296 | |
297 | ||
615238be | 298 | # This is analogous to _SinkComponentClassConst, but for sink |
81447b5b | 299 | # component objects. |
e5914347 | 300 | class _GenericSinkComponentConst(bt2_object._SharedObject, _SinkComponentConst): |
9dee90bd SM |
301 | @staticmethod |
302 | def _get_ref(ptr): | |
303 | native_bt.component_sink_get_ref(ptr) | |
304 | ||
305 | @staticmethod | |
306 | def _put_ref(ptr): | |
307 | native_bt.component_sink_put_ref(ptr) | |
601c0026 | 308 | |
811644b8 PP |
309 | @property |
310 | def input_ports(self): | |
cfbd7cf3 FD |
311 | return _ComponentPorts( |
312 | self._ptr, | |
313 | native_bt.component_sink_borrow_input_port_by_name_const, | |
314 | native_bt.component_sink_borrow_input_port_by_index_const, | |
315 | native_bt.component_sink_get_input_port_count, | |
5813b3a3 | 316 | bt2_port._InputPortConst, |
cfbd7cf3 | 317 | ) |
81447b5b PP |
318 | |
319 | ||
811644b8 | 320 | _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = { |
615238be FD |
321 | native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponentConst, |
322 | native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponentConst, | |
323 | native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponentConst, | |
81447b5b PP |
324 | } |
325 | ||
326 | ||
811644b8 | 327 | _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = { |
615238be FD |
328 | native_bt.COMPONENT_CLASS_TYPE_SOURCE: _SourceComponentClassConst, |
329 | native_bt.COMPONENT_CLASS_TYPE_FILTER: _FilterComponentClassConst, | |
330 | native_bt.COMPONENT_CLASS_TYPE_SINK: _SinkComponentClassConst, | |
81447b5b PP |
331 | } |
332 | ||
333 | ||
615238be FD |
334 | # Create a component Python object of type _GenericSourceComponentConst, |
335 | # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on | |
601c0026 SM |
336 | # comp_cls_type. |
337 | # | |
338 | # Steals the reference to ptr from the caller. | |
339 | ||
cfbd7cf3 | 340 | |
615238be | 341 | def _create_component_from_const_ptr(ptr, comp_cls_type): |
811644b8 | 342 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr) |
81447b5b | 343 | |
5f25509b SM |
344 | |
345 | # Same as the above, but acquire a new reference instead of stealing the | |
346 | # reference from the caller. | |
347 | ||
cfbd7cf3 | 348 | |
615238be | 349 | def _create_component_from_const_ptr_and_get_ref(ptr, comp_cls_type): |
cfbd7cf3 FD |
350 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[ |
351 | comp_cls_type | |
352 | ]._create_from_ptr_and_get_ref(ptr) | |
5f25509b SM |
353 | |
354 | ||
601c0026 | 355 | # Create a component class Python object of type |
615238be FD |
356 | # _SourceComponentClassConst, _FilterComponentClassConst or |
357 | # _SinkComponentClassConst, depending on comp_cls_type. | |
601c0026 SM |
358 | # |
359 | # Acquires a new reference to ptr. | |
81447b5b | 360 | |
cfbd7cf3 | 361 | |
615238be | 362 | def _create_component_class_from_const_ptr_and_get_ref(ptr, comp_cls_type): |
cfbd7cf3 FD |
363 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[ |
364 | comp_cls_type | |
365 | ]._create_from_ptr_and_get_ref(ptr) | |
81447b5b PP |
366 | |
367 | ||
40910fbb PP |
368 | def _trim_docstring(docstring): |
369 | lines = docstring.expandtabs().splitlines() | |
ade5c95e PP |
370 | |
371 | if len(lines) == 0: | |
f5567ea8 | 372 | return "" |
ade5c95e | 373 | |
40910fbb PP |
374 | indent = sys.maxsize |
375 | ||
ade5c95e PP |
376 | if len(lines) > 1: |
377 | for line in lines[1:]: | |
378 | stripped = line.lstrip() | |
40910fbb | 379 | |
ade5c95e PP |
380 | if stripped: |
381 | indent = min(indent, len(line) - len(stripped)) | |
40910fbb PP |
382 | |
383 | trimmed = [lines[0].strip()] | |
384 | ||
ade5c95e | 385 | if indent < sys.maxsize and len(lines) > 1: |
40910fbb PP |
386 | for line in lines[1:]: |
387 | trimmed.append(line[indent:].rstrip()) | |
388 | ||
389 | while trimmed and not trimmed[-1]: | |
390 | trimmed.pop() | |
391 | ||
392 | while trimmed and not trimmed[0]: | |
393 | trimmed.pop(0) | |
394 | ||
f5567ea8 | 395 | return "\n".join(trimmed) |
40910fbb PP |
396 | |
397 | ||
81447b5b PP |
398 | # Metaclass for component classes defined by Python code. |
399 | # | |
400 | # The Python user can create a standard Python class which inherits one | |
811644b8 PP |
401 | # of the three base classes (_UserSourceComponent, _UserFilterComponent, |
402 | # or _UserSinkComponent). Those base classes set this class | |
81447b5b PP |
403 | # (_UserComponentType) as their metaclass. |
404 | # | |
405 | # Once the body of a user-defined component class is executed, this | |
406 | # metaclass is used to create and initialize the class. The metaclass | |
407 | # creates a native BT component class of the corresponding type and | |
408 | # associates it with this user-defined class. The metaclass also defines | |
409 | # class methods like the `name` and `description` properties to match | |
615238be | 410 | # the _ComponentClassConst interface. |
81447b5b PP |
411 | # |
412 | # The component class name which is used is either: | |
413 | # | |
414 | # * The `name` parameter of the class: | |
415 | # | |
416 | # class MySink(bt2.SinkComponent, name='my-custom-sink'): | |
417 | # ... | |
418 | # | |
419 | # * If the `name` class parameter is not used: the name of the class | |
420 | # itself (`MySink` in the example above). | |
421 | # | |
422 | # The component class description which is used is the user-defined | |
423 | # class's docstring: | |
424 | # | |
425 | # class MySink(bt2.SinkComponent): | |
426 | # 'Description goes here' | |
427 | # ... | |
428 | # | |
429 | # A user-defined Python component class can have an __init__() method | |
101fde11 | 430 | # which must accept the following parameters: |
81447b5b | 431 | # |
101fde11 | 432 | # def __init__(self, config, params, obj): |
81447b5b PP |
433 | # ... |
434 | # | |
101fde11 SM |
435 | # The value of the `obj` parameter is what was passed as the `obj` |
436 | # parameter if the component was instantiated from Python with | |
437 | # Graph.add_component(). If the component was not instantiated from | |
438 | # Python, is is always `None`. | |
439 | # | |
440 | # The user-defined component class can also have a _user_finalize() | |
441 | # method (do NOT use __del__()) to be notified when the component object | |
442 | # is finalized. | |
81447b5b PP |
443 | # |
444 | # User-defined source and filter component classes must use the | |
5602ef81 SM |
445 | # `message_iterator_class` class parameter to specify the |
446 | # message iterator class to use for this component class: | |
81447b5b | 447 | # |
5602ef81 | 448 | # class MyMessageIterator(bt2._UserMessageIterator): |
81447b5b PP |
449 | # ... |
450 | # | |
811644b8 | 451 | # class MySource(bt2._UserSourceComponent, |
5602ef81 | 452 | # message_iterator_class=MyMessageIterator): |
81447b5b PP |
453 | # ... |
454 | # | |
101fde11 SM |
455 | # This message iterator class must inherit bt2._UserMessageIterator. |
456 | # It can implement the __init__() method, which must accept the | |
457 | # following parameters: | |
458 | # | |
459 | # def __init__(self, config, port): | |
460 | # ... | |
461 | # | |
462 | # It can also implement the __next__() and _user_finalize() methods | |
463 | # (again, do NOT use __del__()), which don't accept any parameters | |
464 | # other than `self`. | |
81447b5b PP |
465 | # |
466 | # When the user-defined class is destroyed, this metaclass's __del__() | |
467 | # method is called: the native BT component class pointer is put (not | |
468 | # needed anymore, at least not by any Python code since all references | |
469 | # are dropped for __del__() to be called). | |
470 | class _UserComponentType(type): | |
471 | # __new__() is used to catch custom class parameters | |
472 | def __new__(meta_cls, class_name, bases, attrs, **kwargs): | |
473 | return super().__new__(meta_cls, class_name, bases, attrs) | |
474 | ||
475 | def __init__(cls, class_name, bases, namespace, **kwargs): | |
476 | super().__init__(class_name, bases, namespace) | |
477 | ||
478 | # skip our own bases; they are never directly instantiated by the user | |
811644b8 | 479 | own_bases = ( |
f5567ea8 FD |
480 | "_UserComponent", |
481 | "_UserFilterSinkComponent", | |
482 | "_UserSourceComponent", | |
483 | "_UserFilterComponent", | |
484 | "_UserSinkComponent", | |
811644b8 PP |
485 | ) |
486 | ||
487 | if class_name in own_bases: | |
81447b5b PP |
488 | return |
489 | ||
f5567ea8 | 490 | comp_cls_name = kwargs.get("name", class_name) |
e5914347 | 491 | bt2_utils._check_str(comp_cls_name) |
40910fbb PP |
492 | comp_cls_descr = None |
493 | comp_cls_help = None | |
494 | ||
f5567ea8 | 495 | if hasattr(cls, "__doc__") and cls.__doc__ is not None: |
e5914347 | 496 | bt2_utils._check_str(cls.__doc__) |
40910fbb PP |
497 | docstring = _trim_docstring(cls.__doc__) |
498 | lines = docstring.splitlines() | |
499 | ||
500 | if len(lines) >= 1: | |
501 | comp_cls_descr = lines[0] | |
502 | ||
503 | if len(lines) >= 3: | |
f5567ea8 | 504 | comp_cls_help = "\n".join(lines[2:]) |
40910fbb | 505 | |
f5567ea8 | 506 | iter_cls = kwargs.get("message_iterator_class") |
81447b5b | 507 | |
811644b8 | 508 | if _UserSourceComponent in bases: |
85906b6b | 509 | _UserComponentType._bt_set_iterator_class(cls, iter_cls) |
cfbd7cf3 FD |
510 | cc_ptr = native_bt.bt2_component_class_source_create( |
511 | cls, comp_cls_name, comp_cls_descr, comp_cls_help | |
512 | ) | |
811644b8 | 513 | elif _UserFilterComponent in bases: |
85906b6b | 514 | _UserComponentType._bt_set_iterator_class(cls, iter_cls) |
cfbd7cf3 FD |
515 | cc_ptr = native_bt.bt2_component_class_filter_create( |
516 | cls, comp_cls_name, comp_cls_descr, comp_cls_help | |
517 | ) | |
811644b8 | 518 | elif _UserSinkComponent in bases: |
f5567ea8 | 519 | if not hasattr(cls, "_user_consume"): |
c345b078 | 520 | raise _IncompleteUserClass( |
6a91742b | 521 | "cannot create component class '{}': missing a _user_consume() method".format( |
cfbd7cf3 FD |
522 | class_name |
523 | ) | |
524 | ) | |
525 | ||
526 | cc_ptr = native_bt.bt2_component_class_sink_create( | |
527 | cls, comp_cls_name, comp_cls_descr, comp_cls_help | |
528 | ) | |
81447b5b | 529 | else: |
c345b078 | 530 | raise _IncompleteUserClass( |
cfbd7cf3 FD |
531 | "cannot find a known component class base in the bases of '{}'".format( |
532 | class_name | |
533 | ) | |
534 | ) | |
81447b5b PP |
535 | |
536 | if cc_ptr is None: | |
c345b078 | 537 | raise bt2_error._MemoryError( |
cfbd7cf3 FD |
538 | "cannot create component class '{}'".format(class_name) |
539 | ) | |
81447b5b | 540 | |
85906b6b | 541 | cls._bt_cc_ptr = cc_ptr |
81447b5b | 542 | |
66964f3f | 543 | def _bt_init_from_native(cls, comp_ptr, params_ptr, obj): |
811644b8 | 544 | # create instance, not user-initialized yet |
81447b5b PP |
545 | self = cls.__new__(cls) |
546 | ||
59225a3e SM |
547 | # config object |
548 | config = cls._config_pycls() | |
549 | ||
601c0026 | 550 | # pointer to native self component object (weak/borrowed) |
85906b6b | 551 | self._bt_ptr = comp_ptr |
81447b5b | 552 | |
811644b8 PP |
553 | # call user's __init__() method |
554 | if params_ptr is not None: | |
e42e1587 | 555 | params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr) |
811644b8 PP |
556 | else: |
557 | params = None | |
81447b5b | 558 | |
59225a3e | 559 | self.__init__(config, params, obj) |
81447b5b PP |
560 | return self |
561 | ||
811644b8 | 562 | def __call__(cls, *args, **kwargs): |
ce4923b0 | 563 | raise RuntimeError( |
f5567ea8 | 564 | "cannot directly instantiate a user component from a Python module" |
cfbd7cf3 | 565 | ) |
81447b5b PP |
566 | |
567 | @staticmethod | |
85906b6b | 568 | def _bt_set_iterator_class(cls, iter_cls): |
81447b5b | 569 | if iter_cls is None: |
c345b078 | 570 | raise _IncompleteUserClass( |
cfbd7cf3 FD |
571 | "cannot create component class '{}': missing message iterator class".format( |
572 | cls.__name__ | |
573 | ) | |
574 | ) | |
81447b5b | 575 | |
3fb99a22 | 576 | if not issubclass(iter_cls, bt2_message_iterator._UserMessageIterator): |
c345b078 | 577 | raise _IncompleteUserClass( |
cfbd7cf3 FD |
578 | "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format( |
579 | cls.__name__ | |
580 | ) | |
581 | ) | |
81447b5b | 582 | |
f5567ea8 | 583 | if not hasattr(iter_cls, "__next__"): |
c345b078 | 584 | raise _IncompleteUserClass( |
cfbd7cf3 FD |
585 | "cannot create component class '{}': message iterator class is missing a __next__() method".format( |
586 | cls.__name__ | |
587 | ) | |
588 | ) | |
81447b5b | 589 | |
f5567ea8 FD |
590 | if hasattr(iter_cls, "_user_can_seek_ns_from_origin") and not hasattr( |
591 | iter_cls, "_user_seek_ns_from_origin" | |
2e1b5615 | 592 | ): |
c345b078 | 593 | raise _IncompleteUserClass( |
2e1b5615 SM |
594 | "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format( |
595 | cls.__name__ | |
596 | ) | |
597 | ) | |
598 | ||
f5567ea8 FD |
599 | if hasattr(iter_cls, "_user_can_seek_beginning") and not hasattr( |
600 | iter_cls, "_user_seek_beginning" | |
2e1b5615 | 601 | ): |
c345b078 | 602 | raise _IncompleteUserClass( |
2e1b5615 SM |
603 | "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format( |
604 | cls.__name__ | |
605 | ) | |
606 | ) | |
607 | ||
81447b5b PP |
608 | cls._iter_cls = iter_cls |
609 | ||
610 | @property | |
611 | def name(cls): | |
85906b6b | 612 | ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 613 | return native_bt.component_class_get_name(ptr) |
81447b5b PP |
614 | |
615 | @property | |
616 | def description(cls): | |
85906b6b | 617 | ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 618 | return native_bt.component_class_get_description(ptr) |
81447b5b | 619 | |
40910fbb PP |
620 | @property |
621 | def help(cls): | |
85906b6b | 622 | ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 623 | return native_bt.component_class_get_help(ptr) |
40910fbb | 624 | |
81447b5b PP |
625 | @property |
626 | def addr(cls): | |
85906b6b | 627 | return int(cls._bt_cc_ptr) |
81447b5b | 628 | |
fd57054b PP |
629 | def _bt_get_supported_mip_versions_from_native(cls, params_ptr, obj, log_level): |
630 | # this can raise, but the native side checks the exception | |
631 | if params_ptr is not None: | |
e42e1587 | 632 | params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr) |
fd57054b PP |
633 | else: |
634 | params = None | |
635 | ||
636 | # this can raise, but the native side checks the exception | |
637 | range_set = cls._user_get_supported_mip_versions(params, obj, log_level) | |
638 | ||
c345b078 | 639 | if type(range_set) is not bt2_integer_range_set.UnsignedIntegerRangeSet: |
fd57054b | 640 | # this can raise, but the native side checks the exception |
c345b078 | 641 | range_set = bt2_integer_range_set.UnsignedIntegerRangeSet(range_set) |
fd57054b PP |
642 | |
643 | # return new reference | |
644 | range_set._get_ref(range_set._ptr) | |
645 | return int(range_set._ptr) | |
646 | ||
647 | def _user_get_supported_mip_versions(cls, params, obj, log_level): | |
648 | return [0] | |
649 | ||
0b9ddc00 PP |
650 | def _bt_query_from_native( |
651 | cls, priv_query_exec_ptr, object_name, params_ptr, method_obj | |
652 | ): | |
fd57054b | 653 | # this can raise, but the native side checks the exception |
811644b8 | 654 | if params_ptr is not None: |
e42e1587 | 655 | params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr) |
811644b8 PP |
656 | else: |
657 | params = None | |
658 | ||
3c729b9a | 659 | priv_query_exec = bt2_query_executor._PrivateQueryExecutor(priv_query_exec_ptr) |
811644b8 | 660 | |
3c729b9a PP |
661 | try: |
662 | # this can raise, but the native side checks the exception | |
0b9ddc00 | 663 | results = cls._user_query(priv_query_exec, object_name, params, method_obj) |
3c729b9a PP |
664 | finally: |
665 | # the private query executor is a private view on the query | |
666 | # executor; it's not a shared object (the library does not | |
667 | # offer an API to get/put a reference, just like "self" | |
668 | # objects) from this query's point of view, so invalidate | |
669 | # the object in case the user kept a reference and uses it | |
670 | # later | |
671 | priv_query_exec._invalidate() | |
811644b8 | 672 | |
c7eee084 | 673 | # this can raise, but the native side checks the exception |
c345b078 | 674 | results = bt2_value.create_value(results) |
911dec08 PP |
675 | |
676 | if results is None: | |
b5947615 | 677 | results_ptr = native_bt.value_null |
911dec08 | 678 | else: |
b5947615 | 679 | results_ptr = results._ptr |
911dec08 | 680 | |
3c729b9a | 681 | # return new reference |
3fb99a22 | 682 | bt2_value._Value._get_ref(results_ptr) |
b5947615 | 683 | return int(results_ptr) |
911dec08 | 684 | |
0b9ddc00 | 685 | def _user_query(cls, priv_query_executor, object_name, params, method_obj): |
c345b078 | 686 | raise bt2_utils.UnknownObject |
811644b8 | 687 | |
85906b6b FD |
688 | def _bt_component_class_ptr(self): |
689 | return self._bt_as_component_class_ptr(self._bt_cc_ptr) | |
911dec08 | 690 | |
81447b5b | 691 | def __del__(cls): |
f5567ea8 | 692 | if hasattr(cls, "_bt_cc_ptr"): |
85906b6b | 693 | cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 694 | native_bt.component_class_put_ref(cc_ptr) |
ab1cea3f | 695 | native_bt.bt2_unregister_cc_ptr_to_py_cls(cc_ptr) |
81447b5b | 696 | |
cfbd7cf3 | 697 | |
59225a3e SM |
698 | # Configuration objects for components. |
699 | # | |
700 | # These are passed in __init__ to allow components to change some configuration | |
701 | # parameters during initialization and not after. As you can see, they are not | |
702 | # used at the moment, but are there in case we want to add such parameters. | |
703 | ||
704 | ||
705 | class _UserComponentConfiguration: | |
706 | pass | |
707 | ||
708 | ||
709 | class _UserSourceComponentConfiguration(_UserComponentConfiguration): | |
710 | pass | |
711 | ||
712 | ||
713 | class _UserFilterComponentConfiguration(_UserComponentConfiguration): | |
714 | pass | |
715 | ||
716 | ||
717 | class _UserSinkComponentConfiguration(_UserComponentConfiguration): | |
718 | pass | |
719 | ||
720 | ||
1c9ed2ff SM |
721 | # Subclasses must provide these methods or property: |
722 | # | |
85906b6b | 723 | # - _bt_as_not_self_specific_component_ptr: static method, must return the passed |
1c9ed2ff SM |
724 | # specialized self component pointer (e.g. 'bt_self_component_sink *') as a |
725 | # specialized non-self pointer (e.g. 'bt_component_sink *'). | |
85906b6b | 726 | # - _bt_borrow_component_class_ptr: static method, must return a pointer to the |
1c9ed2ff SM |
727 | # specialized component class (e.g. 'bt_component_class_sink *') of the |
728 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
85906b6b | 729 | # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* |
1c9ed2ff | 730 | # constants. |
81447b5b | 731 | |
cfbd7cf3 | 732 | |
811644b8 PP |
733 | class _UserComponent(metaclass=_UserComponentType): |
734 | @property | |
735 | def name(self): | |
85906b6b FD |
736 | ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr) |
737 | ptr = self._bt_as_component_ptr(ptr) | |
1c9ed2ff SM |
738 | name = native_bt.component_get_name(ptr) |
739 | assert name is not None | |
811644b8 | 740 | return name |
81447b5b | 741 | |
e874da19 PP |
742 | @property |
743 | def logging_level(self): | |
85906b6b FD |
744 | ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr) |
745 | ptr = self._bt_as_component_ptr(ptr) | |
e874da19 PP |
746 | return native_bt.component_get_logging_level(ptr) |
747 | ||
811644b8 | 748 | @property |
e8ac1aae | 749 | def cls(self): |
85906b6b FD |
750 | comp_ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr) |
751 | cc_ptr = self._bt_borrow_component_class_ptr(comp_ptr) | |
615238be | 752 | return _create_component_class_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
753 | cc_ptr, self._bt_comp_cls_type |
754 | ) | |
81447b5b | 755 | |
81447b5b PP |
756 | @property |
757 | def addr(self): | |
85906b6b | 758 | return int(self._bt_ptr) |
81447b5b | 759 | |
056deb59 PP |
760 | @property |
761 | def _graph_mip_version(self): | |
762 | ptr = self._bt_as_self_component_ptr(self._bt_ptr) | |
763 | return native_bt.self_component_get_graph_mip_version(ptr) | |
764 | ||
59225a3e | 765 | def __init__(self, config, params, obj): |
81447b5b PP |
766 | pass |
767 | ||
6a91742b | 768 | def _user_finalize(self): |
81447b5b PP |
769 | pass |
770 | ||
6a91742b | 771 | def _user_port_connected(self, port, other_port): |
811644b8 | 772 | pass |
81447b5b | 773 | |
cfbd7cf3 FD |
774 | def _bt_port_connected_from_native( |
775 | self, self_port_ptr, self_port_type, other_port_ptr | |
776 | ): | |
3fb99a22 | 777 | port = bt2_port._create_self_from_ptr_and_get_ref(self_port_ptr, self_port_type) |
2c67587a SM |
778 | |
779 | if self_port_type == native_bt.PORT_TYPE_OUTPUT: | |
780 | other_port_type = native_bt.PORT_TYPE_INPUT | |
781 | else: | |
782 | other_port_type = native_bt.PORT_TYPE_OUTPUT | |
783 | ||
5813b3a3 | 784 | other_port = bt2_port._create_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
785 | other_port_ptr, other_port_type |
786 | ) | |
6a91742b | 787 | self._user_port_connected(port, other_port) |
81447b5b | 788 | |
5783664e PP |
789 | def _create_trace_class( |
790 | self, user_attributes=None, assigns_automatic_stream_class_id=True | |
791 | ): | |
85906b6b | 792 | ptr = self._bt_as_self_component_ptr(self._bt_ptr) |
fbbe9302 SM |
793 | tc_ptr = native_bt.trace_class_create(ptr) |
794 | ||
795 | if tc_ptr is None: | |
c345b078 | 796 | raise bt2_error._MemoryError("could not create trace class") |
fbbe9302 | 797 | |
3fb99a22 | 798 | tc = bt2_trace_class._TraceClass._create_from_ptr(tc_ptr) |
fbbe9302 SM |
799 | tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id |
800 | ||
5783664e PP |
801 | if user_attributes is not None: |
802 | tc._user_attributes = user_attributes | |
803 | ||
fbbe9302 SM |
804 | return tc |
805 | ||
cfbd7cf3 FD |
806 | def _create_clock_class( |
807 | self, | |
808 | frequency=None, | |
809 | name=None, | |
5783664e | 810 | user_attributes=None, |
cfbd7cf3 FD |
811 | description=None, |
812 | precision=None, | |
813 | offset=None, | |
814 | origin_is_unix_epoch=True, | |
815 | uuid=None, | |
816 | ): | |
85906b6b | 817 | ptr = self._bt_as_self_component_ptr(self._bt_ptr) |
3cdfbaea SM |
818 | cc_ptr = native_bt.clock_class_create(ptr) |
819 | ||
820 | if cc_ptr is None: | |
c345b078 | 821 | raise bt2_error._MemoryError("could not create clock class") |
3cdfbaea | 822 | |
3fb99a22 | 823 | cc = bt2_clock_class._ClockClass._create_from_ptr(cc_ptr) |
2ae9f48c SM |
824 | |
825 | if frequency is not None: | |
826 | cc._frequency = frequency | |
827 | ||
be7bbff9 SM |
828 | if name is not None: |
829 | cc._name = name | |
830 | ||
5783664e PP |
831 | if user_attributes is not None: |
832 | cc._user_attributes = user_attributes | |
833 | ||
be7bbff9 SM |
834 | if description is not None: |
835 | cc._description = description | |
836 | ||
837 | if precision is not None: | |
838 | cc._precision = precision | |
839 | ||
840 | if offset is not None: | |
841 | cc._offset = offset | |
842 | ||
843 | cc._origin_is_unix_epoch = origin_is_unix_epoch | |
844 | ||
845 | if uuid is not None: | |
846 | cc._uuid = uuid | |
847 | ||
2ae9f48c | 848 | return cc |
3cdfbaea | 849 | |
81447b5b | 850 | |
615238be | 851 | class _UserSourceComponent(_UserComponent, _SourceComponentConst): |
cfbd7cf3 FD |
852 | _bt_as_not_self_specific_component_ptr = staticmethod( |
853 | native_bt.self_component_source_as_component_source | |
854 | ) | |
855 | _bt_as_self_component_ptr = staticmethod( | |
856 | native_bt.self_component_source_as_self_component | |
857 | ) | |
59225a3e | 858 | _config_pycls = _UserSourceComponentConfiguration |
1c9ed2ff | 859 | |
81447b5b | 860 | @property |
811644b8 | 861 | def _output_ports(self): |
894a8df5 | 862 | def get_output_port_count(self_ptr): |
85906b6b | 863 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
864 | return native_bt.component_source_get_output_port_count(ptr) |
865 | ||
cfbd7cf3 FD |
866 | return _ComponentPorts( |
867 | self._bt_ptr, | |
868 | native_bt.self_component_source_borrow_output_port_by_name, | |
869 | native_bt.self_component_source_borrow_output_port_by_index, | |
870 | get_output_port_count, | |
3fb99a22 | 871 | bt2_port._UserComponentOutputPort, |
cfbd7cf3 | 872 | ) |
811644b8 | 873 | |
2e00bc76 | 874 | def _add_output_port(self, name, user_data=None): |
e5914347 | 875 | bt2_utils._check_str(name) |
157a98ed SM |
876 | |
877 | if name in self._output_ports: | |
878 | raise ValueError( | |
f5567ea8 | 879 | "source component `{}` already contains an output port named `{}`".format( |
157a98ed SM |
880 | self.name, name |
881 | ) | |
882 | ) | |
883 | ||
894a8df5 | 884 | fn = native_bt.self_component_source_add_output_port |
85906b6b | 885 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
e5914347 | 886 | bt2_utils._handle_func_status( |
f5567ea8 | 887 | comp_status, "cannot add output port to source component object" |
cfbd7cf3 | 888 | ) |
894a8df5 | 889 | assert self_port_ptr is not None |
a91462c9 PP |
890 | return bt2_port._UserComponentOutputPort._create_from_ptr_and_get_ref( |
891 | self_port_ptr | |
892 | ) | |
811644b8 PP |
893 | |
894 | ||
615238be | 895 | class _UserFilterComponent(_UserComponent, _FilterComponentConst): |
cfbd7cf3 FD |
896 | _bt_as_not_self_specific_component_ptr = staticmethod( |
897 | native_bt.self_component_filter_as_component_filter | |
898 | ) | |
899 | _bt_as_self_component_ptr = staticmethod( | |
900 | native_bt.self_component_filter_as_self_component | |
901 | ) | |
59225a3e | 902 | _config_pycls = _UserFilterComponentConfiguration |
1c9ed2ff | 903 | |
811644b8 PP |
904 | @property |
905 | def _output_ports(self): | |
894a8df5 | 906 | def get_output_port_count(self_ptr): |
85906b6b | 907 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
908 | return native_bt.component_filter_get_output_port_count(ptr) |
909 | ||
cfbd7cf3 FD |
910 | return _ComponentPorts( |
911 | self._bt_ptr, | |
912 | native_bt.self_component_filter_borrow_output_port_by_name, | |
913 | native_bt.self_component_filter_borrow_output_port_by_index, | |
914 | get_output_port_count, | |
3fb99a22 | 915 | bt2_port._UserComponentOutputPort, |
cfbd7cf3 | 916 | ) |
81447b5b | 917 | |
811644b8 PP |
918 | @property |
919 | def _input_ports(self): | |
894a8df5 | 920 | def get_input_port_count(self_ptr): |
85906b6b | 921 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
922 | return native_bt.component_filter_get_input_port_count(ptr) |
923 | ||
cfbd7cf3 FD |
924 | return _ComponentPorts( |
925 | self._bt_ptr, | |
926 | native_bt.self_component_filter_borrow_input_port_by_name, | |
927 | native_bt.self_component_filter_borrow_input_port_by_index, | |
928 | get_input_port_count, | |
3fb99a22 | 929 | bt2_port._UserComponentInputPort, |
cfbd7cf3 | 930 | ) |
811644b8 | 931 | |
2e00bc76 | 932 | def _add_output_port(self, name, user_data=None): |
e5914347 | 933 | bt2_utils._check_str(name) |
157a98ed SM |
934 | |
935 | if name in self._output_ports: | |
936 | raise ValueError( | |
f5567ea8 | 937 | "filter component `{}` already contains an output port named `{}`".format( |
157a98ed SM |
938 | self.name, name |
939 | ) | |
940 | ) | |
941 | ||
894a8df5 | 942 | fn = native_bt.self_component_filter_add_output_port |
85906b6b | 943 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
e5914347 | 944 | bt2_utils._handle_func_status( |
f5567ea8 | 945 | comp_status, "cannot add output port to filter component object" |
cfbd7cf3 | 946 | ) |
894a8df5 | 947 | assert self_port_ptr |
a91462c9 PP |
948 | return bt2_port._UserComponentOutputPort._create_from_ptr_and_get_ref( |
949 | self_port_ptr | |
950 | ) | |
811644b8 | 951 | |
2e00bc76 | 952 | def _add_input_port(self, name, user_data=None): |
e5914347 | 953 | bt2_utils._check_str(name) |
157a98ed SM |
954 | |
955 | if name in self._input_ports: | |
956 | raise ValueError( | |
f5567ea8 | 957 | "filter component `{}` already contains an input port named `{}`".format( |
157a98ed SM |
958 | self.name, name |
959 | ) | |
960 | ) | |
961 | ||
894a8df5 | 962 | fn = native_bt.self_component_filter_add_input_port |
85906b6b | 963 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
e5914347 | 964 | bt2_utils._handle_func_status( |
f5567ea8 | 965 | comp_status, "cannot add input port to filter component object" |
cfbd7cf3 | 966 | ) |
894a8df5 | 967 | assert self_port_ptr |
a91462c9 PP |
968 | return bt2_port._UserComponentInputPort._create_from_ptr_and_get_ref( |
969 | self_port_ptr | |
970 | ) | |
811644b8 PP |
971 | |
972 | ||
615238be | 973 | class _UserSinkComponent(_UserComponent, _SinkComponentConst): |
cfbd7cf3 FD |
974 | _bt_as_not_self_specific_component_ptr = staticmethod( |
975 | native_bt.self_component_sink_as_component_sink | |
976 | ) | |
977 | _bt_as_self_component_ptr = staticmethod( | |
978 | native_bt.self_component_sink_as_self_component | |
979 | ) | |
59225a3e | 980 | _config_pycls = _UserSinkComponentConfiguration |
1c9ed2ff | 981 | |
d14a864a | 982 | def _bt_graph_is_configured_from_native(self): |
6a91742b | 983 | self._user_graph_is_configured() |
d14a864a | 984 | |
6a91742b | 985 | def _user_graph_is_configured(self): |
cd1ef6f2 PP |
986 | pass |
987 | ||
811644b8 PP |
988 | @property |
989 | def _input_ports(self): | |
894a8df5 | 990 | def get_input_port_count(self_ptr): |
85906b6b | 991 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
992 | return native_bt.component_sink_get_input_port_count(ptr) |
993 | ||
cfbd7cf3 FD |
994 | return _ComponentPorts( |
995 | self._bt_ptr, | |
996 | native_bt.self_component_sink_borrow_input_port_by_name, | |
997 | native_bt.self_component_sink_borrow_input_port_by_index, | |
998 | get_input_port_count, | |
3fb99a22 | 999 | bt2_port._UserComponentInputPort, |
cfbd7cf3 | 1000 | ) |
811644b8 | 1001 | |
2e00bc76 | 1002 | def _add_input_port(self, name, user_data=None): |
e5914347 | 1003 | bt2_utils._check_str(name) |
157a98ed SM |
1004 | |
1005 | if name in self._input_ports: | |
1006 | raise ValueError( | |
f5567ea8 | 1007 | "sink component `{}` already contains an input port named `{}`".format( |
157a98ed SM |
1008 | self.name, name |
1009 | ) | |
1010 | ) | |
1011 | ||
894a8df5 | 1012 | fn = native_bt.self_component_sink_add_input_port |
85906b6b | 1013 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
e5914347 | 1014 | bt2_utils._handle_func_status( |
f5567ea8 | 1015 | comp_status, "cannot add input port to sink component object" |
cfbd7cf3 | 1016 | ) |
5f25509b | 1017 | assert self_port_ptr |
a91462c9 PP |
1018 | return bt2_port._UserComponentInputPort._create_from_ptr_and_get_ref( |
1019 | self_port_ptr | |
1020 | ) | |
ca02df0a | 1021 | |
9a2c8b8e | 1022 | def _create_message_iterator(self, input_port): |
e5914347 | 1023 | bt2_utils._check_type(input_port, bt2_port._UserComponentInputPort) |
ca02df0a | 1024 | |
415d43a1 | 1025 | if not input_port.is_connected: |
f5567ea8 | 1026 | raise ValueError("input port is not connected") |
415d43a1 | 1027 | |
75882e97 FD |
1028 | ( |
1029 | status, | |
1030 | msg_iter_ptr, | |
9a2c8b8e | 1031 | ) = native_bt.bt2_message_iterator_create_from_sink_component( |
ca02df0a PP |
1032 | self._bt_ptr, input_port._ptr |
1033 | ) | |
e5914347 | 1034 | bt2_utils._handle_func_status(status, "cannot create message iterator object") |
e803df70 | 1035 | assert msg_iter_ptr is not None |
ca02df0a | 1036 | |
3fb99a22 | 1037 | return bt2_message_iterator._UserComponentInputPortMessageIterator(msg_iter_ptr) |
9b4f9b42 PP |
1038 | |
1039 | @property | |
1040 | def _is_interrupted(self): | |
1041 | return bool(native_bt.self_component_sink_is_interrupted(self._bt_ptr)) |