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