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