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