Explicitly mention `black` in CodingStyle guidelines
[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
5602ef81 24import bt2.message_iterator
81447b5b 25import collections.abc
c4239792 26import bt2.value
811644b8
PP
27import traceback
28import bt2.port
40910fbb 29import sys
81447b5b 30import bt2
811644b8
PP
31import os
32
33
81447b5b
PP
34# This class wraps a component class pointer. This component class could
35# have been created by Python code, but since we only have the pointer,
36# we can only wrap it in a generic way and lose the original Python
37# class.
601c0026
SM
38#
39# Subclasses must implement some methods that this base class uses:
40#
85906b6b 41# - _bt_as_component_class_ptr: static method, convert the passed component class
601c0026
SM
42# pointer to a 'bt_component_class *'.
43
78288f58 44class _GenericComponentClass(object._SharedObject):
81447b5b
PP
45 @property
46 def name(self):
85906b6b 47 ptr = self._bt_as_component_class_ptr(self._ptr)
601c0026
SM
48 name = native_bt.component_class_get_name(ptr)
49 assert name is not None
811644b8 50 return name
81447b5b
PP
51
52 @property
53 def description(self):
85906b6b 54 ptr = self._bt_as_component_class_ptr(self._ptr)
601c0026 55 return native_bt.component_class_get_description(ptr)
81447b5b 56
40910fbb
PP
57 @property
58 def help(self):
85906b6b 59 ptr = self._bt_as_component_class_ptr(self._ptr)
601c0026
SM
60 return native_bt.component_class_get_help(ptr)
61
85906b6b
FD
62 def _bt_component_class_ptr(self):
63 return self._bt_as_component_class_ptr(self._ptr)
40910fbb 64
811644b8
PP
65 def __eq__(self, other):
66 if not isinstance(other, _GenericComponentClass):
67 try:
68 if not issubclass(other, _UserComponent):
69 return False
70 except TypeError:
71 return False
81447b5b 72
811644b8 73 return self.addr == other.addr
81447b5b
PP
74
75
76class _GenericSourceComponentClass(_GenericComponentClass):
2f16a6a2
PP
77 _get_ref = staticmethod(native_bt.component_class_source_get_ref)
78 _put_ref = staticmethod(native_bt.component_class_source_put_ref)
85906b6b 79 _bt_as_component_class_ptr = staticmethod(native_bt.component_class_source_as_component_class)
81447b5b
PP
80
81
82class _GenericFilterComponentClass(_GenericComponentClass):
2f16a6a2
PP
83 _get_ref = staticmethod(native_bt.component_class_filter_get_ref)
84 _put_ref = staticmethod(native_bt.component_class_filter_put_ref)
85906b6b 85 _bt_as_component_class_ptr = staticmethod(native_bt.component_class_filter_as_component_class)
81447b5b
PP
86
87
88class _GenericSinkComponentClass(_GenericComponentClass):
2f16a6a2
PP
89 _get_ref = staticmethod(native_bt.component_class_sink_get_ref)
90 _put_ref = staticmethod(native_bt.component_class_sink_put_ref)
85906b6b 91 _bt_as_component_class_ptr = staticmethod(native_bt.component_class_sink_as_component_class)
81447b5b
PP
92
93
811644b8
PP
94class _PortIterator(collections.abc.Iterator):
95 def __init__(self, comp_ports):
96 self._comp_ports = comp_ports
97 self._at = 0
98
99 def __next__(self):
100 if self._at == len(self._comp_ports):
101 raise StopIteration
102
103 comp_ports = self._comp_ports
894a8df5
SM
104 comp_ptr = comp_ports._component_ptr
105
106 port_ptr = comp_ports._borrow_port_ptr_at_index(comp_ptr, self._at)
107 assert port_ptr is not None
108
109 name = native_bt.port_get_name(comp_ports._port_pycls._as_port_ptr(port_ptr))
110 assert name is not None
811644b8 111
811644b8
PP
112 self._at += 1
113 return name
114
115
116class _ComponentPorts(collections.abc.Mapping):
894a8df5
SM
117
118 # component_ptr is a bt_component_source *, bt_component_filter * or
119 # bt_component_sink *. Its type must match the type expected by the
120 # functions passed as arguments.
121
122 def __init__(self, component_ptr,
123 borrow_port_ptr_by_name,
124 borrow_port_ptr_at_index,
125 get_port_count,
126 port_pycls):
127 self._component_ptr = component_ptr
128 self._borrow_port_ptr_by_name = borrow_port_ptr_by_name
129 self._borrow_port_ptr_at_index = borrow_port_ptr_at_index
130 self._get_port_count = get_port_count
131 self._port_pycls = port_pycls
811644b8
PP
132
133 def __getitem__(self, key):
134 utils._check_str(key)
894a8df5 135 port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key)
811644b8
PP
136
137 if port_ptr is None:
138 raise KeyError(key)
139
894a8df5 140 return self._port_pycls._create_from_ptr_and_get_ref(port_ptr)
811644b8
PP
141
142 def __len__(self):
894a8df5
SM
143 count = self._get_port_count(self._component_ptr)
144 assert count >= 0
811644b8
PP
145 return count
146
147 def __iter__(self):
148 return _PortIterator(self)
149
150
81447b5b 151# This class holds the methods which are common to both generic
601c0026
SM
152# component objects and Python user component objects.
153#
154# Subclasses must provide these methods or property:
155#
85906b6b 156# - _bt_borrow_component_class_ptr: static method, must return a pointer to the
601c0026
SM
157# specialized component class (e.g. 'bt_component_class_sink *') of the
158# passed specialized component pointer (e.g. 'bt_component_sink *').
85906b6b 159# - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
601c0026 160# constants.
85906b6b 161# - _bt_as_component_ptr: static method, must return the passed specialized
1c9ed2ff 162# component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
601c0026 163
811644b8 164class _Component:
81447b5b
PP
165 @property
166 def name(self):
85906b6b 167 ptr = self._bt_as_component_ptr(self._ptr)
1c9ed2ff
SM
168 name = native_bt.component_get_name(ptr)
169 assert name is not None
811644b8
PP
170 return name
171
e874da19
PP
172 @property
173 def logging_level(self):
85906b6b 174 ptr = self._bt_as_component_ptr(self._ptr)
e874da19
PP
175 return native_bt.component_get_logging_level(ptr)
176
81447b5b 177 @property
e8ac1aae 178 def cls(self):
85906b6b 179 cc_ptr = self._bt_borrow_component_class_ptr(self._ptr)
601c0026 180 assert cc_ptr is not None
85906b6b 181 return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._bt_comp_cls_type)
81447b5b 182
811644b8
PP
183 def __eq__(self, other):
184 if not hasattr(other, 'addr'):
185 return False
81447b5b 186
811644b8 187 return self.addr == other.addr
81447b5b 188
81447b5b 189
811644b8 190class _SourceComponent(_Component):
85906b6b
FD
191 _bt_borrow_component_class_ptr = staticmethod(native_bt.component_source_borrow_class_const)
192 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
193 _bt_as_component_class_ptr = staticmethod(native_bt.component_class_source_as_component_class)
194 _bt_as_component_ptr = staticmethod(native_bt.component_source_as_component_const)
81447b5b 195
81447b5b 196
811644b8 197class _FilterComponent(_Component):
85906b6b
FD
198 _bt_borrow_component_class_ptr = staticmethod(native_bt.component_filter_borrow_class_const)
199 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
200 _bt_as_component_class_ptr = staticmethod(native_bt.component_class_filter_as_component_class)
201 _bt_as_component_ptr = staticmethod(native_bt.component_filter_as_component_const)
81447b5b 202
81447b5b 203
811644b8 204class _SinkComponent(_Component):
85906b6b
FD
205 _bt_borrow_component_class_ptr = staticmethod(native_bt.component_sink_borrow_class_const)
206 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
207 _bt_as_component_class_ptr = staticmethod(native_bt.component_class_sink_as_component_class)
208 _bt_as_component_ptr = staticmethod(native_bt.component_sink_as_component_const)
81447b5b
PP
209
210
211# This is analogous to _GenericSourceComponentClass, but for source
212# component objects.
78288f58 213class _GenericSourceComponent(object._SharedObject, _SourceComponent):
894a8df5
SM
214 _get_ref = staticmethod(native_bt.component_source_get_ref)
215 _put_ref = staticmethod(native_bt.component_source_put_ref)
216
811644b8
PP
217 @property
218 def output_ports(self):
894a8df5
SM
219 return _ComponentPorts(self._ptr,
220 native_bt.component_source_borrow_output_port_by_name_const,
221 native_bt.component_source_borrow_output_port_by_index_const,
222 native_bt.component_source_get_output_port_count,
223 bt2.port._OutputPort)
81447b5b
PP
224
225
226# This is analogous to _GenericFilterComponentClass, but for filter
227# component objects.
78288f58 228class _GenericFilterComponent(object._SharedObject, _FilterComponent):
894a8df5
SM
229 _get_ref = staticmethod(native_bt.component_filter_get_ref)
230 _put_ref = staticmethod(native_bt.component_filter_put_ref)
231
811644b8
PP
232 @property
233 def output_ports(self):
894a8df5
SM
234 return _ComponentPorts(self._ptr,
235 native_bt.component_filter_borrow_output_port_by_name_const,
236 native_bt.component_filter_borrow_output_port_by_index_const,
237 native_bt.component_filter_get_output_port_count,
238 bt2.port._OutputPort)
811644b8
PP
239
240 @property
241 def input_ports(self):
894a8df5
SM
242 return _ComponentPorts(self._ptr,
243 native_bt.component_filter_borrow_input_port_by_name_const,
244 native_bt.component_filter_borrow_input_port_by_index_const,
245 native_bt.component_filter_get_input_port_count,
246 bt2.port._InputPort)
81447b5b
PP
247
248
249# This is analogous to _GenericSinkComponentClass, but for sink
250# component objects.
78288f58 251class _GenericSinkComponent(object._SharedObject, _SinkComponent):
2f16a6a2
PP
252 _get_ref = staticmethod(native_bt.component_sink_get_ref)
253 _put_ref = staticmethod(native_bt.component_sink_put_ref)
601c0026 254
811644b8
PP
255 @property
256 def input_ports(self):
894a8df5
SM
257 return _ComponentPorts(self._ptr,
258 native_bt.component_sink_borrow_input_port_by_name_const,
259 native_bt.component_sink_borrow_input_port_by_index_const,
260 native_bt.component_sink_get_input_port_count,
261 bt2.port._InputPort)
81447b5b
PP
262
263
811644b8 264_COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = {
81447b5b
PP
265 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponent,
266 native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponent,
267 native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponent,
268}
269
270
811644b8 271_COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = {
81447b5b
PP
272 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponentClass,
273 native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponentClass,
274 native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponentClass,
275}
276
277
601c0026
SM
278# Create a component Python object of type _GenericSourceComponent,
279# _GenericFilterComponent or _GenericSinkComponent, depending on
280# comp_cls_type.
281#
282# Steals the reference to ptr from the caller.
283
284def _create_component_from_ptr(ptr, comp_cls_type):
811644b8 285 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr)
81447b5b 286
5f25509b
SM
287
288# Same as the above, but acquire a new reference instead of stealing the
289# reference from the caller.
290
291def _create_component_from_ptr_and_get_ref(ptr, comp_cls_type):
292 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr_and_get_ref(ptr)
293
294
601c0026
SM
295# Create a component class Python object of type
296# _GenericSourceComponentClass, _GenericFilterComponentClass or
297# _GenericSinkComponentClass, depending on comp_cls_type.
298#
299# Acquires a new reference to ptr.
81447b5b 300
601c0026
SM
301def _create_component_class_from_ptr_and_get_ref(ptr, comp_cls_type):
302 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type]._create_from_ptr_and_get_ref(ptr)
81447b5b
PP
303
304
40910fbb
PP
305def _trim_docstring(docstring):
306 lines = docstring.expandtabs().splitlines()
307 indent = sys.maxsize
308
309 for line in lines[1:]:
310 stripped = line.lstrip()
311
312 if stripped:
313 indent = min(indent, len(line) - len(stripped))
314
315 trimmed = [lines[0].strip()]
316
317 if indent < sys.maxsize:
318 for line in lines[1:]:
319 trimmed.append(line[indent:].rstrip())
320
321 while trimmed and not trimmed[-1]:
322 trimmed.pop()
323
324 while trimmed and not trimmed[0]:
325 trimmed.pop(0)
326
327 return '\n'.join(trimmed)
328
329
81447b5b
PP
330# Metaclass for component classes defined by Python code.
331#
332# The Python user can create a standard Python class which inherits one
811644b8
PP
333# of the three base classes (_UserSourceComponent, _UserFilterComponent,
334# or _UserSinkComponent). Those base classes set this class
81447b5b
PP
335# (_UserComponentType) as their metaclass.
336#
337# Once the body of a user-defined component class is executed, this
338# metaclass is used to create and initialize the class. The metaclass
339# creates a native BT component class of the corresponding type and
340# associates it with this user-defined class. The metaclass also defines
341# class methods like the `name` and `description` properties to match
342# the _GenericComponentClass interface.
343#
344# The component class name which is used is either:
345#
346# * The `name` parameter of the class:
347#
348# class MySink(bt2.SinkComponent, name='my-custom-sink'):
349# ...
350#
351# * If the `name` class parameter is not used: the name of the class
352# itself (`MySink` in the example above).
353#
354# The component class description which is used is the user-defined
355# class's docstring:
356#
357# class MySink(bt2.SinkComponent):
358# 'Description goes here'
359# ...
360#
361# A user-defined Python component class can have an __init__() method
362# which must at least accept the `params` and `name` arguments:
363#
364# def __init__(self, params, name, something_else):
365# ...
366#
811644b8 367# The user-defined component class can also have a _finalize() method
81447b5b 368# (do NOT use __del__()) to be notified when the component object is
811644b8 369# finalized.
81447b5b
PP
370#
371# User-defined source and filter component classes must use the
5602ef81
SM
372# `message_iterator_class` class parameter to specify the
373# message iterator class to use for this component class:
81447b5b 374#
5602ef81 375# class MyMessageIterator(bt2._UserMessageIterator):
81447b5b
PP
376# ...
377#
811644b8 378# class MySource(bt2._UserSourceComponent,
5602ef81 379# message_iterator_class=MyMessageIterator):
81447b5b
PP
380# ...
381#
5602ef81
SM
382# This message iterator class must inherit
383# bt2._UserMessageIterator, and it must define the _get() and
384# _next() methods. The message iterator class can also define an
81447b5b
PP
385# __init__() method: this method has access to the original Python
386# component object which was used to create it as the `component`
5602ef81 387# property. The message iterator class can also define a
811644b8 388# _finalize() method (again, do NOT use __del__()): this is called when
5602ef81 389# the message iterator is (really) destroyed.
81447b5b
PP
390#
391# When the user-defined class is destroyed, this metaclass's __del__()
392# method is called: the native BT component class pointer is put (not
393# needed anymore, at least not by any Python code since all references
394# are dropped for __del__() to be called).
395class _UserComponentType(type):
396 # __new__() is used to catch custom class parameters
397 def __new__(meta_cls, class_name, bases, attrs, **kwargs):
398 return super().__new__(meta_cls, class_name, bases, attrs)
399
400 def __init__(cls, class_name, bases, namespace, **kwargs):
401 super().__init__(class_name, bases, namespace)
402
403 # skip our own bases; they are never directly instantiated by the user
811644b8
PP
404 own_bases = (
405 '_UserComponent',
406 '_UserFilterSinkComponent',
407 '_UserSourceComponent',
408 '_UserFilterComponent',
409 '_UserSinkComponent',
410 )
411
412 if class_name in own_bases:
81447b5b
PP
413 return
414
415 comp_cls_name = kwargs.get('name', class_name)
811644b8 416 utils._check_str(comp_cls_name)
40910fbb
PP
417 comp_cls_descr = None
418 comp_cls_help = None
419
420 if hasattr(cls, '__doc__') and cls.__doc__ is not None:
811644b8 421 utils._check_str(cls.__doc__)
40910fbb
PP
422 docstring = _trim_docstring(cls.__doc__)
423 lines = docstring.splitlines()
424
425 if len(lines) >= 1:
426 comp_cls_descr = lines[0]
427
428 if len(lines) >= 3:
429 comp_cls_help = '\n'.join(lines[2:])
430
5602ef81 431 iter_cls = kwargs.get('message_iterator_class')
81447b5b 432
811644b8 433 if _UserSourceComponent in bases:
85906b6b 434 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
d24d5663 435 cc_ptr = native_bt.bt2_component_class_source_create(cls,
81447b5b
PP
436 comp_cls_name,
437 comp_cls_descr,
811644b8
PP
438 comp_cls_help)
439 elif _UserFilterComponent in bases:
85906b6b 440 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
d24d5663 441 cc_ptr = native_bt.bt2_component_class_filter_create(cls,
81447b5b
PP
442 comp_cls_name,
443 comp_cls_descr,
811644b8
PP
444 comp_cls_help)
445 elif _UserSinkComponent in bases:
a01b452b
SM
446 if not hasattr(cls, '_graph_is_configured'):
447 raise bt2.IncompleteUserClass("cannot create component class '{}': missing a _graph_is_configured() method".format(class_name))
448
81447b5b 449 if not hasattr(cls, '_consume'):
811644b8 450 raise bt2.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name))
81447b5b 451
d24d5663 452 cc_ptr = native_bt.bt2_component_class_sink_create(cls,
81447b5b 453 comp_cls_name,
40910fbb
PP
454 comp_cls_descr,
455 comp_cls_help)
81447b5b 456 else:
811644b8 457 raise bt2.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name))
81447b5b
PP
458
459 if cc_ptr is None:
460 raise bt2.CreationError("cannot create component class '{}'".format(class_name))
461
85906b6b 462 cls._bt_cc_ptr = cc_ptr
81447b5b 463
85906b6b 464 def _bt_init_from_native(cls, comp_ptr, params_ptr):
811644b8 465 # create instance, not user-initialized yet
81447b5b
PP
466 self = cls.__new__(cls)
467
601c0026 468 # pointer to native self component object (weak/borrowed)
85906b6b 469 self._bt_ptr = comp_ptr
81447b5b 470
811644b8
PP
471 # call user's __init__() method
472 if params_ptr is not None:
601c0026 473 params = bt2.value._create_from_ptr_and_get_ref(params_ptr)
811644b8
PP
474 else:
475 params = None
81447b5b 476
811644b8 477 self.__init__(params)
81447b5b
PP
478 return self
479
811644b8
PP
480 def __call__(cls, *args, **kwargs):
481 raise bt2.Error('cannot directly instantiate a user component from a Python module')
81447b5b
PP
482
483 @staticmethod
85906b6b 484 def _bt_set_iterator_class(cls, iter_cls):
81447b5b 485 if iter_cls is None:
5602ef81 486 raise bt2.IncompleteUserClass("cannot create component class '{}': missing message iterator class".format(cls.__name__))
81447b5b 487
5602ef81
SM
488 if not issubclass(iter_cls, bt2.message_iterator._UserMessageIterator):
489 raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(cls.__name__))
81447b5b 490
811644b8 491 if not hasattr(iter_cls, '__next__'):
5602ef81 492 raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class is missing a __next__() method".format(cls.__name__))
81447b5b
PP
493
494 cls._iter_cls = iter_cls
495
496 @property
497 def name(cls):
85906b6b 498 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
601c0026 499 return native_bt.component_class_get_name(ptr)
81447b5b
PP
500
501 @property
502 def description(cls):
85906b6b 503 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
601c0026 504 return native_bt.component_class_get_description(ptr)
81447b5b 505
40910fbb
PP
506 @property
507 def help(cls):
85906b6b 508 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
601c0026 509 return native_bt.component_class_get_help(ptr)
40910fbb 510
81447b5b
PP
511 @property
512 def addr(cls):
85906b6b 513 return int(cls._bt_cc_ptr)
81447b5b 514
85906b6b 515 def _bt_query_from_native(cls, query_exec_ptr, obj, params_ptr, log_level):
911dec08 516 # this can raise, in which case the native call to
a67681c1 517 # bt_component_class_query() returns NULL
811644b8 518 if params_ptr is not None:
601c0026 519 params = bt2.value._create_from_ptr_and_get_ref(params_ptr)
811644b8
PP
520 else:
521 params = None
522
601c0026
SM
523 query_exec = bt2.QueryExecutor._create_from_ptr_and_get_ref(
524 query_exec_ptr)
811644b8 525
c7eee084 526 # this can raise, but the native side checks the exception
f4e38e70 527 results = cls._query(query_exec, obj, params, log_level)
811644b8 528
c7eee084
PP
529 # this can raise, but the native side checks the exception
530 results = bt2.create_value(results)
911dec08
PP
531
532 if results is None:
b5947615 533 results_ptr = native_bt.value_null
911dec08 534 else:
811644b8 535 # return new reference
b5947615 536 results_ptr = results._ptr
911dec08 537
b5947615
SM
538 # We return a new reference.
539 bt2.value._Value._get_ref(results_ptr)
540
541 return int(results_ptr)
911dec08 542
9cf25bf2 543 def _query(cls, query_executor, obj, params, log_level):
601c0026 544 raise NotImplementedError
811644b8 545
85906b6b
FD
546 def _bt_component_class_ptr(self):
547 return self._bt_as_component_class_ptr(self._bt_cc_ptr)
911dec08 548
81447b5b 549 def __del__(cls):
85906b6b
FD
550 if hasattr(cls, '_bt_cc_ptr'):
551 cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
601c0026 552 native_bt.component_class_put_ref(cc_ptr)
81447b5b 553
1c9ed2ff
SM
554# Subclasses must provide these methods or property:
555#
85906b6b 556# - _bt_as_not_self_specific_component_ptr: static method, must return the passed
1c9ed2ff
SM
557# specialized self component pointer (e.g. 'bt_self_component_sink *') as a
558# specialized non-self pointer (e.g. 'bt_component_sink *').
85906b6b 559# - _bt_borrow_component_class_ptr: static method, must return a pointer to the
1c9ed2ff
SM
560# specialized component class (e.g. 'bt_component_class_sink *') of the
561# passed specialized component pointer (e.g. 'bt_component_sink *').
85906b6b 562# - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
1c9ed2ff 563# constants.
81447b5b 564
811644b8
PP
565class _UserComponent(metaclass=_UserComponentType):
566 @property
567 def name(self):
85906b6b
FD
568 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
569 ptr = self._bt_as_component_ptr(ptr)
1c9ed2ff
SM
570 name = native_bt.component_get_name(ptr)
571 assert name is not None
811644b8 572 return name
81447b5b 573
e874da19
PP
574 @property
575 def logging_level(self):
85906b6b
FD
576 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
577 ptr = self._bt_as_component_ptr(ptr)
e874da19
PP
578 return native_bt.component_get_logging_level(ptr)
579
811644b8 580 @property
e8ac1aae 581 def cls(self):
85906b6b
FD
582 comp_ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
583 cc_ptr = self._bt_borrow_component_class_ptr(comp_ptr)
584 return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._bt_comp_cls_type)
81447b5b 585
81447b5b
PP
586 @property
587 def addr(self):
85906b6b 588 return int(self._bt_ptr)
81447b5b 589
811644b8 590 def __init__(self, params=None):
81447b5b
PP
591 pass
592
811644b8 593 def _finalize(self):
81447b5b
PP
594 pass
595
811644b8
PP
596 def _port_connected(self, port, other_port):
597 pass
81447b5b 598
85906b6b 599 def _bt_port_connected_from_native(self, self_port_ptr, self_port_type, other_port_ptr):
894a8df5
SM
600 port = bt2.port._create_self_from_ptr_and_get_ref(
601 self_port_ptr, self_port_type)
2c67587a
SM
602
603 if self_port_type == native_bt.PORT_TYPE_OUTPUT:
604 other_port_type = native_bt.PORT_TYPE_INPUT
605 else:
606 other_port_type = native_bt.PORT_TYPE_OUTPUT
607
894a8df5
SM
608 other_port = bt2.port._create_from_ptr_and_get_ref(
609 other_port_ptr, other_port_type)
610 self._port_connected(port, other_port)
81447b5b 611
85906b6b 612 def _bt_graph_is_configured_from_native(self):
5f25509b
SM
613 self._graph_is_configured()
614
335a2da5 615 def _create_trace_class(self, assigns_automatic_stream_class_id=True):
85906b6b 616 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
fbbe9302
SM
617 tc_ptr = native_bt.trace_class_create(ptr)
618
619 if tc_ptr is None:
620 raise bt2.CreationError('could not create trace class')
621
2c6f8520 622 tc = bt2._TraceClass._create_from_ptr(tc_ptr)
fbbe9302
SM
623 tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id
624
625 return tc
626
be7bbff9
SM
627 def _create_clock_class(self, frequency=None, name=None, description=None,
628 precision=None, offset=None, origin_is_unix_epoch=True,
629 uuid=None):
85906b6b 630 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
3cdfbaea
SM
631 cc_ptr = native_bt.clock_class_create(ptr)
632
633 if cc_ptr is None:
634 raise bt2.CreationError('could not create clock class')
635
be7bbff9 636 cc = bt2.clock_class._ClockClass._create_from_ptr(cc_ptr)
2ae9f48c
SM
637
638 if frequency is not None:
639 cc._frequency = frequency
640
be7bbff9
SM
641 if name is not None:
642 cc._name = name
643
644 if description is not None:
645 cc._description = description
646
647 if precision is not None:
648 cc._precision = precision
649
650 if offset is not None:
651 cc._offset = offset
652
653 cc._origin_is_unix_epoch = origin_is_unix_epoch
654
655 if uuid is not None:
656 cc._uuid = uuid
657
2ae9f48c 658 return cc
3cdfbaea 659
81447b5b 660
811644b8 661class _UserSourceComponent(_UserComponent, _SourceComponent):
85906b6b
FD
662 _bt_as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_source_as_component_source)
663 _bt_as_self_component_ptr = staticmethod(native_bt.self_component_source_as_self_component)
1c9ed2ff 664
81447b5b 665 @property
811644b8 666 def _output_ports(self):
894a8df5 667 def get_output_port_count(self_ptr):
85906b6b 668 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
894a8df5
SM
669 return native_bt.component_source_get_output_port_count(ptr)
670
85906b6b 671 return _ComponentPorts(self._bt_ptr,
894a8df5
SM
672 native_bt.self_component_source_borrow_output_port_by_name,
673 native_bt.self_component_source_borrow_output_port_by_index,
674 get_output_port_count,
675 bt2.port._UserComponentOutputPort)
811644b8 676
2e00bc76 677 def _add_output_port(self, name, user_data=None):
811644b8 678 utils._check_str(name)
894a8df5 679 fn = native_bt.self_component_source_add_output_port
85906b6b 680 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
d24d5663
PP
681 utils._handle_func_status(comp_status,
682 'cannot add output port to source component object')
894a8df5
SM
683 assert self_port_ptr is not None
684 return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr)
811644b8
PP
685
686
687class _UserFilterComponent(_UserComponent, _FilterComponent):
85906b6b
FD
688 _bt_as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_filter_as_component_filter)
689 _bt_as_self_component_ptr = staticmethod(native_bt.self_component_filter_as_self_component)
1c9ed2ff 690
811644b8
PP
691 @property
692 def _output_ports(self):
894a8df5 693 def get_output_port_count(self_ptr):
85906b6b 694 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
894a8df5
SM
695 return native_bt.component_filter_get_output_port_count(ptr)
696
85906b6b 697 return _ComponentPorts(self._bt_ptr,
894a8df5
SM
698 native_bt.self_component_filter_borrow_output_port_by_name,
699 native_bt.self_component_filter_borrow_output_port_by_index,
700 get_output_port_count,
701 bt2.port._UserComponentOutputPort)
81447b5b 702
811644b8
PP
703 @property
704 def _input_ports(self):
894a8df5 705 def get_input_port_count(self_ptr):
85906b6b 706 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
894a8df5
SM
707 return native_bt.component_filter_get_input_port_count(ptr)
708
85906b6b 709 return _ComponentPorts(self._bt_ptr,
894a8df5
SM
710 native_bt.self_component_filter_borrow_input_port_by_name,
711 native_bt.self_component_filter_borrow_input_port_by_index,
712 get_input_port_count,
713 bt2.port._UserComponentInputPort)
811644b8 714
2e00bc76 715 def _add_output_port(self, name, user_data=None):
811644b8 716 utils._check_str(name)
894a8df5 717 fn = native_bt.self_component_filter_add_output_port
85906b6b 718 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
d24d5663
PP
719 utils._handle_func_status(comp_status,
720 'cannot add output port to filter component object')
894a8df5
SM
721 assert self_port_ptr
722 return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr)
811644b8 723
2e00bc76 724 def _add_input_port(self, name, user_data=None):
811644b8 725 utils._check_str(name)
894a8df5 726 fn = native_bt.self_component_filter_add_input_port
85906b6b 727 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
d24d5663
PP
728 utils._handle_func_status(comp_status,
729 'cannot add input port to filter component object')
894a8df5
SM
730 assert self_port_ptr
731 return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr)
811644b8
PP
732
733
734class _UserSinkComponent(_UserComponent, _SinkComponent):
85906b6b
FD
735 _bt_as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_sink_as_component_sink)
736 _bt_as_self_component_ptr = staticmethod(native_bt.self_component_sink_as_self_component)
1c9ed2ff 737
811644b8
PP
738 @property
739 def _input_ports(self):
894a8df5 740 def get_input_port_count(self_ptr):
85906b6b 741 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
894a8df5
SM
742 return native_bt.component_sink_get_input_port_count(ptr)
743
85906b6b 744 return _ComponentPorts(self._bt_ptr,
894a8df5
SM
745 native_bt.self_component_sink_borrow_input_port_by_name,
746 native_bt.self_component_sink_borrow_input_port_by_index,
747 get_input_port_count,
748 bt2.port._UserComponentInputPort)
811644b8 749
2e00bc76 750 def _add_input_port(self, name, user_data=None):
811644b8 751 utils._check_str(name)
894a8df5 752 fn = native_bt.self_component_sink_add_input_port
85906b6b 753 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
d24d5663
PP
754 utils._handle_func_status(comp_status,
755 'cannot add input port to sink component object')
5f25509b
SM
756 assert self_port_ptr
757 return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr)
This page took 0.080878 seconds and 4 git commands to generate.