lib: rename all `*_STATUS_OVERFLOW` -> `*_STATUS_OVERFLOW_ERROR`
[babeltrace.git] / src / bindings / python / bt2 / bt2 / component.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
23from bt2 import native_bt, object, utils
fa4c33e3 24import bt2.message_iterator
81447b5b 25import collections.abc
ae0bfae8 26import bt2.value
d016011d 27import bt2.trace_class
f6a5e476
PP
28import traceback
29import bt2.port
40910fbb 30import sys
81447b5b 31import bt2
f6a5e476
PP
32import os
33
34
81447b5b
PP
35# This class wraps a component class pointer. This component class could
36# have been created by Python code, but since we only have the pointer,
37# we can only wrap it in a generic way and lose the original Python
38# class.
bbb3650f
SM
39#
40# Subclasses must implement some methods that this base class uses:
41#
deec48a6 42# - _bt_as_component_class_ptr: static method, convert the passed component class
bbb3650f
SM
43# pointer to a 'bt_component_class *'.
44
61d96b89 45
ede525fd 46class _ComponentClass(object._SharedObject):
81447b5b
PP
47 @property
48 def name(self):
deec48a6 49 ptr = self._bt_as_component_class_ptr(self._ptr)
bbb3650f
SM
50 name = native_bt.component_class_get_name(ptr)
51 assert name is not None
f6a5e476 52 return name
81447b5b
PP
53
54 @property
55 def description(self):
deec48a6 56 ptr = self._bt_as_component_class_ptr(self._ptr)
bbb3650f 57 return native_bt.component_class_get_description(ptr)
81447b5b 58
40910fbb
PP
59 @property
60 def help(self):
deec48a6 61 ptr = self._bt_as_component_class_ptr(self._ptr)
bbb3650f
SM
62 return native_bt.component_class_get_help(ptr)
63
deec48a6
FD
64 def _bt_component_class_ptr(self):
65 return self._bt_as_component_class_ptr(self._ptr)
40910fbb 66
f6a5e476 67 def __eq__(self, other):
ede525fd 68 if not isinstance(other, _ComponentClass):
f6a5e476
PP
69 try:
70 if not issubclass(other, _UserComponent):
71 return False
72 except TypeError:
73 return False
81447b5b 74
f6a5e476 75 return self.addr == other.addr
81447b5b
PP
76
77
ede525fd 78class _SourceComponentClass(_ComponentClass):
a49e2cc3
PP
79 _get_ref = staticmethod(native_bt.component_class_source_get_ref)
80 _put_ref = staticmethod(native_bt.component_class_source_put_ref)
61d96b89
FD
81 _bt_as_component_class_ptr = staticmethod(
82 native_bt.component_class_source_as_component_class
83 )
81447b5b
PP
84
85
ede525fd 86class _FilterComponentClass(_ComponentClass):
a49e2cc3
PP
87 _get_ref = staticmethod(native_bt.component_class_filter_get_ref)
88 _put_ref = staticmethod(native_bt.component_class_filter_put_ref)
61d96b89
FD
89 _bt_as_component_class_ptr = staticmethod(
90 native_bt.component_class_filter_as_component_class
91 )
81447b5b
PP
92
93
ede525fd 94class _SinkComponentClass(_ComponentClass):
a49e2cc3
PP
95 _get_ref = staticmethod(native_bt.component_class_sink_get_ref)
96 _put_ref = staticmethod(native_bt.component_class_sink_put_ref)
61d96b89
FD
97 _bt_as_component_class_ptr = staticmethod(
98 native_bt.component_class_sink_as_component_class
99 )
81447b5b
PP
100
101
f6a5e476
PP
102class _PortIterator(collections.abc.Iterator):
103 def __init__(self, comp_ports):
104 self._comp_ports = comp_ports
105 self._at = 0
106
107 def __next__(self):
108 if self._at == len(self._comp_ports):
109 raise StopIteration
110
111 comp_ports = self._comp_ports
bc5c9924
SM
112 comp_ptr = comp_ports._component_ptr
113
114 port_ptr = comp_ports._borrow_port_ptr_at_index(comp_ptr, self._at)
115 assert port_ptr is not None
116
117 name = native_bt.port_get_name(comp_ports._port_pycls._as_port_ptr(port_ptr))
118 assert name is not None
f6a5e476 119
f6a5e476
PP
120 self._at += 1
121 return name
122
123
124class _ComponentPorts(collections.abc.Mapping):
bc5c9924
SM
125
126 # component_ptr is a bt_component_source *, bt_component_filter * or
127 # bt_component_sink *. Its type must match the type expected by the
128 # functions passed as arguments.
129
61d96b89
FD
130 def __init__(
131 self,
132 component_ptr,
133 borrow_port_ptr_by_name,
134 borrow_port_ptr_at_index,
135 get_port_count,
136 port_pycls,
137 ):
bc5c9924
SM
138 self._component_ptr = component_ptr
139 self._borrow_port_ptr_by_name = borrow_port_ptr_by_name
140 self._borrow_port_ptr_at_index = borrow_port_ptr_at_index
141 self._get_port_count = get_port_count
142 self._port_pycls = port_pycls
f6a5e476
PP
143
144 def __getitem__(self, key):
145 utils._check_str(key)
bc5c9924 146 port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key)
f6a5e476
PP
147
148 if port_ptr is None:
149 raise KeyError(key)
150
bc5c9924 151 return self._port_pycls._create_from_ptr_and_get_ref(port_ptr)
f6a5e476
PP
152
153 def __len__(self):
bc5c9924
SM
154 count = self._get_port_count(self._component_ptr)
155 assert count >= 0
f6a5e476
PP
156 return count
157
158 def __iter__(self):
159 return _PortIterator(self)
160
161
81447b5b 162# This class holds the methods which are common to both generic
bbb3650f
SM
163# component objects and Python user component objects.
164#
165# Subclasses must provide these methods or property:
166#
deec48a6 167# - _bt_borrow_component_class_ptr: static method, must return a pointer to the
bbb3650f
SM
168# specialized component class (e.g. 'bt_component_class_sink *') of the
169# passed specialized component pointer (e.g. 'bt_component_sink *').
deec48a6 170# - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
bbb3650f 171# constants.
deec48a6 172# - _bt_as_component_ptr: static method, must return the passed specialized
e34fb94a 173# component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
bbb3650f 174
61d96b89 175
f6a5e476 176class _Component:
81447b5b
PP
177 @property
178 def name(self):
deec48a6 179 ptr = self._bt_as_component_ptr(self._ptr)
e34fb94a
SM
180 name = native_bt.component_get_name(ptr)
181 assert name is not None
f6a5e476
PP
182 return name
183
cc81b5ab
PP
184 @property
185 def logging_level(self):
deec48a6 186 ptr = self._bt_as_component_ptr(self._ptr)
cc81b5ab
PP
187 return native_bt.component_get_logging_level(ptr)
188
81447b5b 189 @property
c88be1c8 190 def cls(self):
deec48a6 191 cc_ptr = self._bt_borrow_component_class_ptr(self._ptr)
bbb3650f 192 assert cc_ptr is not None
61d96b89
FD
193 return _create_component_class_from_ptr_and_get_ref(
194 cc_ptr, self._bt_comp_cls_type
195 )
81447b5b 196
f6a5e476
PP
197 def __eq__(self, other):
198 if not hasattr(other, 'addr'):
199 return False
81447b5b 200
f6a5e476 201 return self.addr == other.addr
81447b5b 202
81447b5b 203
f6a5e476 204class _SourceComponent(_Component):
61d96b89
FD
205 _bt_borrow_component_class_ptr = staticmethod(
206 native_bt.component_source_borrow_class_const
207 )
deec48a6 208 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
61d96b89
FD
209 _bt_as_component_class_ptr = staticmethod(
210 native_bt.component_class_source_as_component_class
211 )
deec48a6 212 _bt_as_component_ptr = staticmethod(native_bt.component_source_as_component_const)
81447b5b 213
81447b5b 214
f6a5e476 215class _FilterComponent(_Component):
61d96b89
FD
216 _bt_borrow_component_class_ptr = staticmethod(
217 native_bt.component_filter_borrow_class_const
218 )
deec48a6 219 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
61d96b89
FD
220 _bt_as_component_class_ptr = staticmethod(
221 native_bt.component_class_filter_as_component_class
222 )
deec48a6 223 _bt_as_component_ptr = staticmethod(native_bt.component_filter_as_component_const)
81447b5b 224
81447b5b 225
f6a5e476 226class _SinkComponent(_Component):
61d96b89
FD
227 _bt_borrow_component_class_ptr = staticmethod(
228 native_bt.component_sink_borrow_class_const
229 )
deec48a6 230 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
61d96b89
FD
231 _bt_as_component_class_ptr = staticmethod(
232 native_bt.component_class_sink_as_component_class
233 )
deec48a6 234 _bt_as_component_ptr = staticmethod(native_bt.component_sink_as_component_const)
81447b5b
PP
235
236
ede525fd 237# This is analogous to _SourceComponentClass, but for source
81447b5b 238# component objects.
c3044a97 239class _GenericSourceComponent(object._SharedObject, _SourceComponent):
bc5c9924
SM
240 _get_ref = staticmethod(native_bt.component_source_get_ref)
241 _put_ref = staticmethod(native_bt.component_source_put_ref)
242
f6a5e476
PP
243 @property
244 def output_ports(self):
61d96b89
FD
245 return _ComponentPorts(
246 self._ptr,
247 native_bt.component_source_borrow_output_port_by_name_const,
248 native_bt.component_source_borrow_output_port_by_index_const,
249 native_bt.component_source_get_output_port_count,
250 bt2.port._OutputPort,
251 )
81447b5b
PP
252
253
ede525fd 254# This is analogous to _FilterComponentClass, but for filter
81447b5b 255# component objects.
c3044a97 256class _GenericFilterComponent(object._SharedObject, _FilterComponent):
bc5c9924
SM
257 _get_ref = staticmethod(native_bt.component_filter_get_ref)
258 _put_ref = staticmethod(native_bt.component_filter_put_ref)
259
f6a5e476
PP
260 @property
261 def output_ports(self):
61d96b89
FD
262 return _ComponentPorts(
263 self._ptr,
264 native_bt.component_filter_borrow_output_port_by_name_const,
265 native_bt.component_filter_borrow_output_port_by_index_const,
266 native_bt.component_filter_get_output_port_count,
267 bt2.port._OutputPort,
268 )
f6a5e476
PP
269
270 @property
271 def input_ports(self):
61d96b89
FD
272 return _ComponentPorts(
273 self._ptr,
274 native_bt.component_filter_borrow_input_port_by_name_const,
275 native_bt.component_filter_borrow_input_port_by_index_const,
276 native_bt.component_filter_get_input_port_count,
277 bt2.port._InputPort,
278 )
81447b5b
PP
279
280
ede525fd 281# This is analogous to _SinkComponentClass, but for sink
81447b5b 282# component objects.
c3044a97 283class _GenericSinkComponent(object._SharedObject, _SinkComponent):
a49e2cc3
PP
284 _get_ref = staticmethod(native_bt.component_sink_get_ref)
285 _put_ref = staticmethod(native_bt.component_sink_put_ref)
bbb3650f 286
f6a5e476
PP
287 @property
288 def input_ports(self):
61d96b89
FD
289 return _ComponentPorts(
290 self._ptr,
291 native_bt.component_sink_borrow_input_port_by_name_const,
292 native_bt.component_sink_borrow_input_port_by_index_const,
293 native_bt.component_sink_get_input_port_count,
294 bt2.port._InputPort,
295 )
81447b5b
PP
296
297
f6a5e476 298_COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = {
81447b5b
PP
299 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponent,
300 native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponent,
301 native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponent,
302}
303
304
f6a5e476 305_COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = {
ede525fd
PP
306 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _SourceComponentClass,
307 native_bt.COMPONENT_CLASS_TYPE_FILTER: _FilterComponentClass,
308 native_bt.COMPONENT_CLASS_TYPE_SINK: _SinkComponentClass,
81447b5b
PP
309}
310
311
bbb3650f
SM
312# Create a component Python object of type _GenericSourceComponent,
313# _GenericFilterComponent or _GenericSinkComponent, depending on
314# comp_cls_type.
315#
316# Steals the reference to ptr from the caller.
317
61d96b89 318
bbb3650f 319def _create_component_from_ptr(ptr, comp_cls_type):
f6a5e476 320 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr)
81447b5b 321
871a292a
SM
322
323# Same as the above, but acquire a new reference instead of stealing the
324# reference from the caller.
325
61d96b89 326
871a292a 327def _create_component_from_ptr_and_get_ref(ptr, comp_cls_type):
61d96b89
FD
328 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[
329 comp_cls_type
330 ]._create_from_ptr_and_get_ref(ptr)
871a292a
SM
331
332
bbb3650f 333# Create a component class Python object of type
ede525fd
PP
334# _SourceComponentClass, _FilterComponentClass or
335# _SinkComponentClass, depending on comp_cls_type.
bbb3650f
SM
336#
337# Acquires a new reference to ptr.
81447b5b 338
61d96b89 339
bbb3650f 340def _create_component_class_from_ptr_and_get_ref(ptr, comp_cls_type):
61d96b89
FD
341 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[
342 comp_cls_type
343 ]._create_from_ptr_and_get_ref(ptr)
81447b5b
PP
344
345
40910fbb
PP
346def _trim_docstring(docstring):
347 lines = docstring.expandtabs().splitlines()
348 indent = sys.maxsize
349
350 for line in lines[1:]:
351 stripped = line.lstrip()
352
353 if stripped:
354 indent = min(indent, len(line) - len(stripped))
355
356 trimmed = [lines[0].strip()]
357
358 if indent < sys.maxsize:
359 for line in lines[1:]:
360 trimmed.append(line[indent:].rstrip())
361
362 while trimmed and not trimmed[-1]:
363 trimmed.pop()
364
365 while trimmed and not trimmed[0]:
366 trimmed.pop(0)
367
368 return '\n'.join(trimmed)
369
370
81447b5b
PP
371# Metaclass for component classes defined by Python code.
372#
373# The Python user can create a standard Python class which inherits one
f6a5e476
PP
374# of the three base classes (_UserSourceComponent, _UserFilterComponent,
375# or _UserSinkComponent). Those base classes set this class
81447b5b
PP
376# (_UserComponentType) as their metaclass.
377#
378# Once the body of a user-defined component class is executed, this
379# metaclass is used to create and initialize the class. The metaclass
380# creates a native BT component class of the corresponding type and
381# associates it with this user-defined class. The metaclass also defines
382# class methods like the `name` and `description` properties to match
ede525fd 383# the _ComponentClass interface.
81447b5b
PP
384#
385# The component class name which is used is either:
386#
387# * The `name` parameter of the class:
388#
389# class MySink(bt2.SinkComponent, name='my-custom-sink'):
390# ...
391#
392# * If the `name` class parameter is not used: the name of the class
393# itself (`MySink` in the example above).
394#
395# The component class description which is used is the user-defined
396# class's docstring:
397#
398# class MySink(bt2.SinkComponent):
399# 'Description goes here'
400# ...
401#
402# A user-defined Python component class can have an __init__() method
403# which must at least accept the `params` and `name` arguments:
404#
405# def __init__(self, params, name, something_else):
406# ...
407#
f6a5e476 408# The user-defined component class can also have a _finalize() method
81447b5b 409# (do NOT use __del__()) to be notified when the component object is
f6a5e476 410# finalized.
81447b5b
PP
411#
412# User-defined source and filter component classes must use the
fa4c33e3
SM
413# `message_iterator_class` class parameter to specify the
414# message iterator class to use for this component class:
81447b5b 415#
fa4c33e3 416# class MyMessageIterator(bt2._UserMessageIterator):
81447b5b
PP
417# ...
418#
f6a5e476 419# class MySource(bt2._UserSourceComponent,
fa4c33e3 420# message_iterator_class=MyMessageIterator):
81447b5b
PP
421# ...
422#
fa4c33e3
SM
423# This message iterator class must inherit
424# bt2._UserMessageIterator, and it must define the _get() and
425# _next() methods. The message iterator class can also define an
81447b5b
PP
426# __init__() method: this method has access to the original Python
427# component object which was used to create it as the `component`
fa4c33e3 428# property. The message iterator class can also define a
f6a5e476 429# _finalize() method (again, do NOT use __del__()): this is called when
fa4c33e3 430# the message iterator is (really) destroyed.
81447b5b
PP
431#
432# When the user-defined class is destroyed, this metaclass's __del__()
433# method is called: the native BT component class pointer is put (not
434# needed anymore, at least not by any Python code since all references
435# are dropped for __del__() to be called).
436class _UserComponentType(type):
437 # __new__() is used to catch custom class parameters
438 def __new__(meta_cls, class_name, bases, attrs, **kwargs):
439 return super().__new__(meta_cls, class_name, bases, attrs)
440
441 def __init__(cls, class_name, bases, namespace, **kwargs):
442 super().__init__(class_name, bases, namespace)
443
444 # skip our own bases; they are never directly instantiated by the user
f6a5e476
PP
445 own_bases = (
446 '_UserComponent',
447 '_UserFilterSinkComponent',
448 '_UserSourceComponent',
449 '_UserFilterComponent',
450 '_UserSinkComponent',
451 )
452
453 if class_name in own_bases:
81447b5b
PP
454 return
455
456 comp_cls_name = kwargs.get('name', class_name)
f6a5e476 457 utils._check_str(comp_cls_name)
40910fbb
PP
458 comp_cls_descr = None
459 comp_cls_help = None
460
461 if hasattr(cls, '__doc__') and cls.__doc__ is not None:
f6a5e476 462 utils._check_str(cls.__doc__)
40910fbb
PP
463 docstring = _trim_docstring(cls.__doc__)
464 lines = docstring.splitlines()
465
466 if len(lines) >= 1:
467 comp_cls_descr = lines[0]
468
469 if len(lines) >= 3:
470 comp_cls_help = '\n'.join(lines[2:])
471
fa4c33e3 472 iter_cls = kwargs.get('message_iterator_class')
81447b5b 473
f6a5e476 474 if _UserSourceComponent in bases:
deec48a6 475 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
61d96b89
FD
476 cc_ptr = native_bt.bt2_component_class_source_create(
477 cls, comp_cls_name, comp_cls_descr, comp_cls_help
478 )
f6a5e476 479 elif _UserFilterComponent in bases:
deec48a6 480 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
61d96b89
FD
481 cc_ptr = native_bt.bt2_component_class_filter_create(
482 cls, comp_cls_name, comp_cls_descr, comp_cls_help
483 )
f6a5e476 484 elif _UserSinkComponent in bases:
81447b5b 485 if not hasattr(cls, '_consume'):
61d96b89
FD
486 raise bt2.IncompleteUserClass(
487 "cannot create component class '{}': missing a _consume() method".format(
488 class_name
489 )
490 )
491
492 cc_ptr = native_bt.bt2_component_class_sink_create(
493 cls, comp_cls_name, comp_cls_descr, comp_cls_help
494 )
81447b5b 495 else:
61d96b89
FD
496 raise bt2.IncompleteUserClass(
497 "cannot find a known component class base in the bases of '{}'".format(
498 class_name
499 )
500 )
81447b5b
PP
501
502 if cc_ptr is None:
614743a5 503 raise bt2._MemoryError(
61d96b89
FD
504 "cannot create component class '{}'".format(class_name)
505 )
81447b5b 506
deec48a6 507 cls._bt_cc_ptr = cc_ptr
81447b5b 508
deec48a6 509 def _bt_init_from_native(cls, comp_ptr, params_ptr):
f6a5e476 510 # create instance, not user-initialized yet
81447b5b
PP
511 self = cls.__new__(cls)
512
bbb3650f 513 # pointer to native self component object (weak/borrowed)
deec48a6 514 self._bt_ptr = comp_ptr
81447b5b 515
f6a5e476
PP
516 # call user's __init__() method
517 if params_ptr is not None:
bbb3650f 518 params = bt2.value._create_from_ptr_and_get_ref(params_ptr)
f6a5e476
PP
519 else:
520 params = None
81447b5b 521
f6a5e476 522 self.__init__(params)
81447b5b
PP
523 return self
524
f6a5e476 525 def __call__(cls, *args, **kwargs):
3b2be708 526 raise RuntimeError(
61d96b89
FD
527 'cannot directly instantiate a user component from a Python module'
528 )
81447b5b
PP
529
530 @staticmethod
deec48a6 531 def _bt_set_iterator_class(cls, iter_cls):
81447b5b 532 if iter_cls is None:
61d96b89
FD
533 raise bt2.IncompleteUserClass(
534 "cannot create component class '{}': missing message iterator class".format(
535 cls.__name__
536 )
537 )
81447b5b 538
fa4c33e3 539 if not issubclass(iter_cls, bt2.message_iterator._UserMessageIterator):
61d96b89
FD
540 raise bt2.IncompleteUserClass(
541 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
542 cls.__name__
543 )
544 )
81447b5b 545
f6a5e476 546 if not hasattr(iter_cls, '__next__'):
61d96b89
FD
547 raise bt2.IncompleteUserClass(
548 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
549 cls.__name__
550 )
551 )
81447b5b
PP
552
553 cls._iter_cls = iter_cls
554
555 @property
556 def name(cls):
deec48a6 557 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 558 return native_bt.component_class_get_name(ptr)
81447b5b
PP
559
560 @property
561 def description(cls):
deec48a6 562 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 563 return native_bt.component_class_get_description(ptr)
81447b5b 564
40910fbb
PP
565 @property
566 def help(cls):
deec48a6 567 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 568 return native_bt.component_class_get_help(ptr)
40910fbb 569
81447b5b
PP
570 @property
571 def addr(cls):
deec48a6 572 return int(cls._bt_cc_ptr)
81447b5b 573
deec48a6 574 def _bt_query_from_native(cls, query_exec_ptr, obj, params_ptr, log_level):
911dec08 575 # this can raise, in which case the native call to
a67681c1 576 # bt_component_class_query() returns NULL
f6a5e476 577 if params_ptr is not None:
bbb3650f 578 params = bt2.value._create_from_ptr_and_get_ref(params_ptr)
f6a5e476
PP
579 else:
580 params = None
581
61d96b89 582 query_exec = bt2.QueryExecutor._create_from_ptr_and_get_ref(query_exec_ptr)
f6a5e476 583
1286dcbb 584 # this can raise, but the native side checks the exception
83da519a 585 results = cls._query(query_exec, obj, params, log_level)
f6a5e476 586
1286dcbb
PP
587 # this can raise, but the native side checks the exception
588 results = bt2.create_value(results)
911dec08
PP
589
590 if results is None:
70ad0c69 591 results_ptr = native_bt.value_null
911dec08 592 else:
f6a5e476 593 # return new reference
70ad0c69 594 results_ptr = results._ptr
911dec08 595
70ad0c69
SM
596 # We return a new reference.
597 bt2.value._Value._get_ref(results_ptr)
598
599 return int(results_ptr)
911dec08 600
c7fdd8d6 601 def _query(cls, query_executor, obj, params, log_level):
bbb3650f 602 raise NotImplementedError
f6a5e476 603
deec48a6
FD
604 def _bt_component_class_ptr(self):
605 return self._bt_as_component_class_ptr(self._bt_cc_ptr)
911dec08 606
81447b5b 607 def __del__(cls):
deec48a6
FD
608 if hasattr(cls, '_bt_cc_ptr'):
609 cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 610 native_bt.component_class_put_ref(cc_ptr)
81447b5b 611
61d96b89 612
e34fb94a
SM
613# Subclasses must provide these methods or property:
614#
deec48a6 615# - _bt_as_not_self_specific_component_ptr: static method, must return the passed
e34fb94a
SM
616# specialized self component pointer (e.g. 'bt_self_component_sink *') as a
617# specialized non-self pointer (e.g. 'bt_component_sink *').
deec48a6 618# - _bt_borrow_component_class_ptr: static method, must return a pointer to the
e34fb94a
SM
619# specialized component class (e.g. 'bt_component_class_sink *') of the
620# passed specialized component pointer (e.g. 'bt_component_sink *').
deec48a6 621# - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
e34fb94a 622# constants.
81447b5b 623
61d96b89 624
f6a5e476
PP
625class _UserComponent(metaclass=_UserComponentType):
626 @property
627 def name(self):
deec48a6
FD
628 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
629 ptr = self._bt_as_component_ptr(ptr)
e34fb94a
SM
630 name = native_bt.component_get_name(ptr)
631 assert name is not None
f6a5e476 632 return name
81447b5b 633
cc81b5ab
PP
634 @property
635 def logging_level(self):
deec48a6
FD
636 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
637 ptr = self._bt_as_component_ptr(ptr)
cc81b5ab
PP
638 return native_bt.component_get_logging_level(ptr)
639
f6a5e476 640 @property
c88be1c8 641 def cls(self):
deec48a6
FD
642 comp_ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
643 cc_ptr = self._bt_borrow_component_class_ptr(comp_ptr)
61d96b89
FD
644 return _create_component_class_from_ptr_and_get_ref(
645 cc_ptr, self._bt_comp_cls_type
646 )
81447b5b 647
81447b5b
PP
648 @property
649 def addr(self):
deec48a6 650 return int(self._bt_ptr)
81447b5b 651
f6a5e476 652 def __init__(self, params=None):
81447b5b
PP
653 pass
654
f6a5e476 655 def _finalize(self):
81447b5b
PP
656 pass
657
f6a5e476
PP
658 def _port_connected(self, port, other_port):
659 pass
81447b5b 660
61d96b89
FD
661 def _bt_port_connected_from_native(
662 self, self_port_ptr, self_port_type, other_port_ptr
663 ):
664 port = bt2.port._create_self_from_ptr_and_get_ref(self_port_ptr, self_port_type)
824ce8b6
SM
665
666 if self_port_type == native_bt.PORT_TYPE_OUTPUT:
667 other_port_type = native_bt.PORT_TYPE_INPUT
668 else:
669 other_port_type = native_bt.PORT_TYPE_OUTPUT
670
bc5c9924 671 other_port = bt2.port._create_from_ptr_and_get_ref(
61d96b89
FD
672 other_port_ptr, other_port_type
673 )
bc5c9924 674 self._port_connected(port, other_port)
81447b5b 675
cd03c43c 676 def _create_trace_class(self, assigns_automatic_stream_class_id=True):
deec48a6 677 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
0bee8ea9
SM
678 tc_ptr = native_bt.trace_class_create(ptr)
679
680 if tc_ptr is None:
614743a5 681 raise bt2._MemoryError('could not create trace class')
0bee8ea9 682
d016011d 683 tc = bt2.trace_class._TraceClass._create_from_ptr(tc_ptr)
0bee8ea9
SM
684 tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id
685
686 return tc
687
61d96b89
FD
688 def _create_clock_class(
689 self,
690 frequency=None,
691 name=None,
692 description=None,
693 precision=None,
694 offset=None,
695 origin_is_unix_epoch=True,
696 uuid=None,
697 ):
deec48a6 698 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
060aee52
SM
699 cc_ptr = native_bt.clock_class_create(ptr)
700
701 if cc_ptr is None:
614743a5 702 raise bt2._MemoryError('could not create clock class')
060aee52 703
4a5ca968 704 cc = bt2.clock_class._ClockClass._create_from_ptr(cc_ptr)
27d97a3f
SM
705
706 if frequency is not None:
707 cc._frequency = frequency
708
4a5ca968
SM
709 if name is not None:
710 cc._name = name
711
712 if description is not None:
713 cc._description = description
714
715 if precision is not None:
716 cc._precision = precision
717
718 if offset is not None:
719 cc._offset = offset
720
721 cc._origin_is_unix_epoch = origin_is_unix_epoch
722
723 if uuid is not None:
724 cc._uuid = uuid
725
27d97a3f 726 return cc
060aee52 727
81447b5b 728
f6a5e476 729class _UserSourceComponent(_UserComponent, _SourceComponent):
61d96b89
FD
730 _bt_as_not_self_specific_component_ptr = staticmethod(
731 native_bt.self_component_source_as_component_source
732 )
733 _bt_as_self_component_ptr = staticmethod(
734 native_bt.self_component_source_as_self_component
735 )
e34fb94a 736
81447b5b 737 @property
f6a5e476 738 def _output_ports(self):
bc5c9924 739 def get_output_port_count(self_ptr):
deec48a6 740 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
741 return native_bt.component_source_get_output_port_count(ptr)
742
61d96b89
FD
743 return _ComponentPorts(
744 self._bt_ptr,
745 native_bt.self_component_source_borrow_output_port_by_name,
746 native_bt.self_component_source_borrow_output_port_by_index,
747 get_output_port_count,
748 bt2.port._UserComponentOutputPort,
749 )
f6a5e476 750
03ec9ebd 751 def _add_output_port(self, name, user_data=None):
f6a5e476 752 utils._check_str(name)
bc5c9924 753 fn = native_bt.self_component_source_add_output_port
deec48a6 754 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
755 utils._handle_func_status(
756 comp_status, 'cannot add output port to source component object'
757 )
bc5c9924
SM
758 assert self_port_ptr is not None
759 return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr)
f6a5e476
PP
760
761
762class _UserFilterComponent(_UserComponent, _FilterComponent):
61d96b89
FD
763 _bt_as_not_self_specific_component_ptr = staticmethod(
764 native_bt.self_component_filter_as_component_filter
765 )
766 _bt_as_self_component_ptr = staticmethod(
767 native_bt.self_component_filter_as_self_component
768 )
e34fb94a 769
f6a5e476
PP
770 @property
771 def _output_ports(self):
bc5c9924 772 def get_output_port_count(self_ptr):
deec48a6 773 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
774 return native_bt.component_filter_get_output_port_count(ptr)
775
61d96b89
FD
776 return _ComponentPorts(
777 self._bt_ptr,
778 native_bt.self_component_filter_borrow_output_port_by_name,
779 native_bt.self_component_filter_borrow_output_port_by_index,
780 get_output_port_count,
781 bt2.port._UserComponentOutputPort,
782 )
81447b5b 783
f6a5e476
PP
784 @property
785 def _input_ports(self):
bc5c9924 786 def get_input_port_count(self_ptr):
deec48a6 787 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
788 return native_bt.component_filter_get_input_port_count(ptr)
789
61d96b89
FD
790 return _ComponentPorts(
791 self._bt_ptr,
792 native_bt.self_component_filter_borrow_input_port_by_name,
793 native_bt.self_component_filter_borrow_input_port_by_index,
794 get_input_port_count,
795 bt2.port._UserComponentInputPort,
796 )
f6a5e476 797
03ec9ebd 798 def _add_output_port(self, name, user_data=None):
f6a5e476 799 utils._check_str(name)
bc5c9924 800 fn = native_bt.self_component_filter_add_output_port
deec48a6 801 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
802 utils._handle_func_status(
803 comp_status, 'cannot add output port to filter component object'
804 )
bc5c9924
SM
805 assert self_port_ptr
806 return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr)
f6a5e476 807
03ec9ebd 808 def _add_input_port(self, name, user_data=None):
f6a5e476 809 utils._check_str(name)
bc5c9924 810 fn = native_bt.self_component_filter_add_input_port
deec48a6 811 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
812 utils._handle_func_status(
813 comp_status, 'cannot add input port to filter component object'
814 )
bc5c9924
SM
815 assert self_port_ptr
816 return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr)
f6a5e476
PP
817
818
819class _UserSinkComponent(_UserComponent, _SinkComponent):
61d96b89
FD
820 _bt_as_not_self_specific_component_ptr = staticmethod(
821 native_bt.self_component_sink_as_component_sink
822 )
823 _bt_as_self_component_ptr = staticmethod(
824 native_bt.self_component_sink_as_self_component
825 )
e34fb94a 826
f2849243
PP
827 def _bt_graph_is_configured_from_native(self):
828 self._graph_is_configured()
829
f0c6b5d5
PP
830 def _graph_is_configured(self):
831 pass
832
f6a5e476
PP
833 @property
834 def _input_ports(self):
bc5c9924 835 def get_input_port_count(self_ptr):
deec48a6 836 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
837 return native_bt.component_sink_get_input_port_count(ptr)
838
61d96b89
FD
839 return _ComponentPorts(
840 self._bt_ptr,
841 native_bt.self_component_sink_borrow_input_port_by_name,
842 native_bt.self_component_sink_borrow_input_port_by_index,
843 get_input_port_count,
844 bt2.port._UserComponentInputPort,
845 )
f6a5e476 846
03ec9ebd 847 def _add_input_port(self, name, user_data=None):
f6a5e476 848 utils._check_str(name)
bc5c9924 849 fn = native_bt.self_component_sink_add_input_port
deec48a6 850 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
851 utils._handle_func_status(
852 comp_status, 'cannot add input port to sink component object'
853 )
871a292a
SM
854 assert self_port_ptr
855 return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr)
692f1a01
PP
856
857 def _create_input_port_message_iterator(self, input_port):
858 utils._check_type(input_port, bt2.port._UserComponentInputPort)
859
860 msg_iter_ptr = native_bt.self_component_port_input_message_iterator_create_from_sink_component(
861 self._bt_ptr, input_port._ptr
862 )
863
864 if msg_iter_ptr is None:
865 raise bt2.CreationError('cannot create message iterator object')
866
867 return bt2.message_iterator._UserComponentInputPortMessageIterator(msg_iter_ptr)
d73bb381
PP
868
869 @property
870 def _is_interrupted(self):
871 return bool(native_bt.self_component_sink_is_interrupted(self._bt_ptr))
This page took 0.087003 seconds and 4 git commands to generate.