1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import bt2
.message_iterator
25 import collections
.abc
34 _env_var
= os
.environ
.get('BABELTRACE_PYTHON_BT2_NO_TRACEBACK')
35 _NO_PRINT_TRACEBACK
= _env_var
== '1'
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
43 # Subclasses must implement some methods that this base class uses:
45 # - _as_component_class_ptr: static method, convert the passed component class
46 # pointer to a 'bt_component_class *'.
48 class _GenericComponentClass(object._SharedObject
):
51 ptr
= self
._as
_component
_class
_ptr
(self
._ptr
)
52 name
= native_bt
.component_class_get_name(ptr
)
53 assert name
is not None
57 def description(self
):
58 ptr
= self
._as
_component
_class
_ptr
(self
._ptr
)
59 return native_bt
.component_class_get_description(ptr
)
63 ptr
= self
._as
_component
_class
_ptr
(self
._ptr
)
64 return native_bt
.component_class_get_help(ptr
)
66 def _component_class_ptr(self
):
67 return self
._as
_component
_class
_ptr
(self
._ptr
)
69 def __eq__(self
, other
):
70 if not isinstance(other
, _GenericComponentClass
):
72 if not issubclass(other
, _UserComponent
):
77 return self
.addr
== other
.addr
80 class _GenericSourceComponentClass(_GenericComponentClass
):
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
)
86 class _GenericFilterComponentClass(_GenericComponentClass
):
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
)
92 class _GenericSinkComponentClass(_GenericComponentClass
):
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
)
98 def _handle_component_status(status
, gen_error_msg
):
99 if status
== native_bt
.SELF_COMPONENT_STATUS_END
:
101 elif status
== native_bt
.SELF_COMPONENT_STATUS_AGAIN
:
103 elif status
== native_bt
.SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
:
104 raise bt2
.PortConnectionRefused
106 raise bt2
.Error(gen_error_msg
)
109 class _PortIterator(collections
.abc
.Iterator
):
110 def __init__(self
, comp_ports
):
111 self
._comp
_ports
= comp_ports
115 if self
._at
== len(self
._comp
_ports
):
118 comp_ports
= self
._comp
_ports
119 comp_ptr
= comp_ports
._component
_ptr
121 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
122 assert port_ptr
is not None
124 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
125 assert name
is not None
131 class _ComponentPorts(collections
.abc
.Mapping
):
133 # component_ptr is a bt_component_source *, bt_component_filter * or
134 # bt_component_sink *. Its type must match the type expected by the
135 # functions passed as arguments.
137 def __init__(self
, component_ptr
,
138 borrow_port_ptr_by_name
,
139 borrow_port_ptr_at_index
,
142 self
._component
_ptr
= component_ptr
143 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
144 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
145 self
._get
_port
_count
= get_port_count
146 self
._port
_pycls
= port_pycls
148 def __getitem__(self
, key
):
149 utils
._check
_str
(key
)
150 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
155 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
158 count
= self
._get
_port
_count
(self
._component
_ptr
)
163 return _PortIterator(self
)
166 # This class holds the methods which are common to both generic
167 # component objects and Python user component objects.
169 # Subclasses must provide these methods or property:
171 # - _borrow_component_class_ptr: static method, must return a pointer to the
172 # specialized component class (e.g. 'bt_component_class_sink *') of the
173 # passed specialized component pointer (e.g. 'bt_component_sink *').
174 # - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
176 # - _as_component_ptr: static method, must return the passed specialized
177 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
182 ptr
= self
._as
_component
_ptr
(self
._ptr
)
183 name
= native_bt
.component_get_name(ptr
)
184 assert name
is not None
188 def logging_level(self
):
189 ptr
= self
._as
_component
_ptr
(self
._ptr
)
190 return native_bt
.component_get_logging_level(ptr
)
194 cc_ptr
= self
._borrow
_component
_class
_ptr
(self
._ptr
)
195 assert cc_ptr
is not None
196 return _create_component_class_from_ptr_and_get_ref(cc_ptr
, self
._comp
_cls
_type
)
198 def __eq__(self
, other
):
199 if not hasattr(other
, 'addr'):
202 return self
.addr
== other
.addr
205 class _SourceComponent(_Component
):
206 _borrow_component_class_ptr
= staticmethod(native_bt
.component_source_borrow_class_const
)
207 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
208 _as_component_class_ptr
= staticmethod(native_bt
.component_class_source_as_component_class
)
209 _as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
212 class _FilterComponent(_Component
):
213 _borrow_component_class_ptr
= staticmethod(native_bt
.component_filter_borrow_class_const
)
214 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
215 _as_component_class_ptr
= staticmethod(native_bt
.component_class_filter_as_component_class
)
216 _as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
219 class _SinkComponent(_Component
):
220 _borrow_component_class_ptr
= staticmethod(native_bt
.component_sink_borrow_class_const
)
221 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
222 _as_component_class_ptr
= staticmethod(native_bt
.component_class_sink_as_component_class
)
223 _as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
226 # This is analogous to _GenericSourceComponentClass, but for source
228 class _GenericSourceComponent(object._SharedObject
, _SourceComponent
):
229 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
230 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
233 def output_ports(self
):
234 return _ComponentPorts(self
._ptr
,
235 native_bt
.component_source_borrow_output_port_by_name_const
,
236 native_bt
.component_source_borrow_output_port_by_index_const
,
237 native_bt
.component_source_get_output_port_count
,
238 bt2
.port
._OutputPort
)
241 # This is analogous to _GenericFilterComponentClass, but for filter
243 class _GenericFilterComponent(object._SharedObject
, _FilterComponent
):
244 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
245 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
248 def output_ports(self
):
249 return _ComponentPorts(self
._ptr
,
250 native_bt
.component_filter_borrow_output_port_by_name_const
,
251 native_bt
.component_filter_borrow_output_port_by_index_const
,
252 native_bt
.component_filter_get_output_port_count
,
253 bt2
.port
._OutputPort
)
256 def input_ports(self
):
257 return _ComponentPorts(self
._ptr
,
258 native_bt
.component_filter_borrow_input_port_by_name_const
,
259 native_bt
.component_filter_borrow_input_port_by_index_const
,
260 native_bt
.component_filter_get_input_port_count
,
264 # This is analogous to _GenericSinkComponentClass, but for sink
266 class _GenericSinkComponent(object._SharedObject
, _SinkComponent
):
267 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
268 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
271 def input_ports(self
):
272 return _ComponentPorts(self
._ptr
,
273 native_bt
.component_sink_borrow_input_port_by_name_const
,
274 native_bt
.component_sink_borrow_input_port_by_index_const
,
275 native_bt
.component_sink_get_input_port_count
,
279 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
280 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponent
,
281 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponent
,
282 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponent
,
286 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
287 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentClass
,
288 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentClass
,
289 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentClass
,
293 # Create a component Python object of type _GenericSourceComponent,
294 # _GenericFilterComponent or _GenericSinkComponent, depending on
297 # Steals the reference to ptr from the caller.
299 def _create_component_from_ptr(ptr
, comp_cls_type
):
300 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
303 # Same as the above, but acquire a new reference instead of stealing the
304 # reference from the caller.
306 def _create_component_from_ptr_and_get_ref(ptr
, comp_cls_type
):
307 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
310 # Create a component class Python object of type
311 # _GenericSourceComponentClass, _GenericFilterComponentClass or
312 # _GenericSinkComponentClass, depending on comp_cls_type.
314 # Acquires a new reference to ptr.
316 def _create_component_class_from_ptr_and_get_ref(ptr
, comp_cls_type
):
317 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[comp_cls_type
]._create
_from
_ptr
_and
_get
_ref
(ptr
)
320 def _trim_docstring(docstring
):
321 lines
= docstring
.expandtabs().splitlines()
324 for line
in lines
[1:]:
325 stripped
= line
.lstrip()
328 indent
= min(indent
, len(line
) - len(stripped
))
330 trimmed
= [lines
[0].strip()]
332 if indent
< sys
.maxsize
:
333 for line
in lines
[1:]:
334 trimmed
.append(line
[indent
:].rstrip())
336 while trimmed
and not trimmed
[-1]:
339 while trimmed
and not trimmed
[0]:
342 return '\n'.join(trimmed
)
345 # Metaclass for component classes defined by Python code.
347 # The Python user can create a standard Python class which inherits one
348 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
349 # or _UserSinkComponent). Those base classes set this class
350 # (_UserComponentType) as their metaclass.
352 # Once the body of a user-defined component class is executed, this
353 # metaclass is used to create and initialize the class. The metaclass
354 # creates a native BT component class of the corresponding type and
355 # associates it with this user-defined class. The metaclass also defines
356 # class methods like the `name` and `description` properties to match
357 # the _GenericComponentClass interface.
359 # The component class name which is used is either:
361 # * The `name` parameter of the class:
363 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
366 # * If the `name` class parameter is not used: the name of the class
367 # itself (`MySink` in the example above).
369 # The component class description which is used is the user-defined
372 # class MySink(bt2.SinkComponent):
373 # 'Description goes here'
376 # A user-defined Python component class can have an __init__() method
377 # which must at least accept the `params` and `name` arguments:
379 # def __init__(self, params, name, something_else):
382 # The user-defined component class can also have a _finalize() method
383 # (do NOT use __del__()) to be notified when the component object is
386 # User-defined source and filter component classes must use the
387 # `message_iterator_class` class parameter to specify the
388 # message iterator class to use for this component class:
390 # class MyMessageIterator(bt2._UserMessageIterator):
393 # class MySource(bt2._UserSourceComponent,
394 # message_iterator_class=MyMessageIterator):
397 # This message iterator class must inherit
398 # bt2._UserMessageIterator, and it must define the _get() and
399 # _next() methods. The message iterator class can also define an
400 # __init__() method: this method has access to the original Python
401 # component object which was used to create it as the `component`
402 # property. The message iterator class can also define a
403 # _finalize() method (again, do NOT use __del__()): this is called when
404 # the message iterator is (really) destroyed.
406 # When the user-defined class is destroyed, this metaclass's __del__()
407 # method is called: the native BT component class pointer is put (not
408 # needed anymore, at least not by any Python code since all references
409 # are dropped for __del__() to be called).
410 class _UserComponentType(type):
411 # __new__() is used to catch custom class parameters
412 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
413 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
415 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
416 super().__init
__(class_name
, bases
, namespace
)
418 # skip our own bases; they are never directly instantiated by the user
421 '_UserFilterSinkComponent',
422 '_UserSourceComponent',
423 '_UserFilterComponent',
424 '_UserSinkComponent',
427 if class_name
in own_bases
:
430 comp_cls_name
= kwargs
.get('name', class_name
)
431 utils
._check
_str
(comp_cls_name
)
432 comp_cls_descr
= None
435 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
436 utils
._check
_str
(cls
.__doc
__)
437 docstring
= _trim_docstring(cls
.__doc
__)
438 lines
= docstring
.splitlines()
441 comp_cls_descr
= lines
[0]
444 comp_cls_help
= '\n'.join(lines
[2:])
446 iter_cls
= kwargs
.get('message_iterator_class')
448 if _UserSourceComponent
in bases
:
449 _UserComponentType
._set
_iterator
_class
(cls
, iter_cls
)
450 cc_ptr
= native_bt
.py3_component_class_source_create(cls
,
454 elif _UserFilterComponent
in bases
:
455 _UserComponentType
._set
_iterator
_class
(cls
, iter_cls
)
456 cc_ptr
= native_bt
.py3_component_class_filter_create(cls
,
460 elif _UserSinkComponent
in bases
:
461 if not hasattr(cls
, '_consume'):
462 raise bt2
.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name
))
464 cc_ptr
= native_bt
.py3_component_class_sink_create(cls
,
469 raise bt2
.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name
))
472 raise bt2
.CreationError("cannot create component class '{}'".format(class_name
))
476 def _init_from_native(cls
, comp_ptr
, params_ptr
):
477 # create instance, not user-initialized yet
478 self
= cls
.__new
__(cls
)
480 # pointer to native self component object (weak/borrowed)
483 # call user's __init__() method
484 if params_ptr
is not None:
485 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
489 self
.__init
__(params
)
492 def __call__(cls
, *args
, **kwargs
):
493 raise bt2
.Error('cannot directly instantiate a user component from a Python module')
496 def _set_iterator_class(cls
, iter_cls
):
498 raise bt2
.IncompleteUserClass("cannot create component class '{}': missing message iterator class".format(cls
.__name
__))
500 if not issubclass(iter_cls
, bt2
.message_iterator
._UserMessageIterator
):
501 raise bt2
.IncompleteUserClass("cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(cls
.__name
__))
503 if not hasattr(iter_cls
, '__next__'):
504 raise bt2
.IncompleteUserClass("cannot create component class '{}': message iterator class is missing a __next__() method".format(cls
.__name
__))
506 cls
._iter
_cls
= iter_cls
510 ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
511 return native_bt
.component_class_get_name(ptr
)
514 def description(cls
):
515 ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
516 return native_bt
.component_class_get_description(ptr
)
520 ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
521 return native_bt
.component_class_get_help(ptr
)
525 return int(cls
._cc
_ptr
)
527 def _query_from_native(cls
, query_exec_ptr
, obj
, params_ptr
):
528 # this can raise, in which case the native call to
529 # bt_component_class_query() returns NULL
530 if params_ptr
is not None:
531 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
535 query_exec
= bt2
.QueryExecutor
._create
_from
_ptr
_and
_get
_ref
(
538 # this can raise, but the native side checks the exception
539 results
= cls
._query
(query_exec
, obj
, params
)
541 # this can raise, but the native side checks the exception
542 results
= bt2
.create_value(results
)
545 results_addr
= int(native_bt
.value_null
)
547 # return new reference
548 results_addr
= int(results
._release
())
552 def _query(cls
, query_executor
, obj
, params
):
553 raise NotImplementedError
555 def _component_class_ptr(self
):
556 return self
._as
_component
_class
_ptr
(self
._cc
_ptr
)
559 if hasattr(cls
, '_cc_ptr'):
560 cc_ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
561 native_bt
.component_class_put_ref(cc_ptr
)
563 # Subclasses must provide these methods or property:
565 # - _as_not_self_specific_component_ptr: static method, must return the passed
566 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
567 # specialized non-self pointer (e.g. 'bt_component_sink *').
568 # - _borrow_component_class_ptr: static method, must return a pointer to the
569 # specialized component class (e.g. 'bt_component_class_sink *') of the
570 # passed specialized component pointer (e.g. 'bt_component_sink *').
571 # - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
574 class _UserComponent(metaclass
=_UserComponentType
):
577 ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self
._ptr
)
578 ptr
= self
._as
_component
_ptr
(ptr
)
579 name
= native_bt
.component_get_name(ptr
)
580 assert name
is not None
584 def logging_level(self
):
585 ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self
._ptr
)
586 ptr
= self
._as
_component
_ptr
(ptr
)
587 return native_bt
.component_get_logging_level(ptr
)
591 comp_ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self
._ptr
)
592 cc_ptr
= self
._borrow
_component
_class
_ptr
(comp_ptr
)
593 return _create_component_class_from_ptr_and_get_ref(cc_ptr
, self
._comp
_cls
_type
)
597 return int(self
._ptr
)
599 def __init__(self
, params
=None):
605 def _accept_port_connection(self
, port
, other_port
):
608 def _accept_port_connection_from_native(self
, self_port_ptr
, self_port_type
, other_port_ptr
):
609 port
= bt2
.port
._create
_self
_from
_ptr
_and
_get
_ref
(
610 self_port_ptr
, self_port_type
)
612 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
613 other_port_type
= native_bt
.PORT_TYPE_INPUT
615 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
617 other_port
= bt2
.port
._create
_from
_ptr
_and
_get
_ref
(
618 other_port_ptr
, other_port_type
)
619 res
= self
._accept
_port
_connection
(port
, other_port_ptr
)
621 if type(res
) is not bool:
622 raise TypeError("'{}' is not a 'bool' object")
626 def _port_connected(self
, port
, other_port
):
629 def _port_connected_from_native(self
, self_port_ptr
, self_port_type
, other_port_ptr
):
630 port
= bt2
.port
._create
_self
_from
_ptr
_and
_get
_ref
(
631 self_port_ptr
, self_port_type
)
633 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
634 other_port_type
= native_bt
.PORT_TYPE_INPUT
636 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
638 other_port
= bt2
.port
._create
_from
_ptr
_and
_get
_ref
(
639 other_port_ptr
, other_port_type
)
640 self
._port
_connected
(port
, other_port
)
642 def _graph_is_configured_from_native(self
):
643 self
._graph
_is
_configured
()
645 def _create_trace_class(self
, env
=None, uuid
=None,
646 assigns_automatic_stream_class_id
=True):
647 ptr
= self
._as
_self
_component
_ptr
(self
._ptr
)
648 tc_ptr
= native_bt
.trace_class_create(ptr
)
651 raise bt2
.CreationError('could not create trace class')
653 tc
= bt2
._TraceClass
._create
_from
_ptr
(tc_ptr
)
656 for key
, value
in env
.items():
662 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
666 def _create_clock_class(self
, frequency
=None, name
=None, description
=None,
667 precision
=None, offset
=None, origin_is_unix_epoch
=True,
669 ptr
= self
._as
_self
_component
_ptr
(self
._ptr
)
670 cc_ptr
= native_bt
.clock_class_create(ptr
)
673 raise bt2
.CreationError('could not create clock class')
675 cc
= bt2
.clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
677 if frequency
is not None:
678 cc
._frequency
= frequency
683 if description
is not None:
684 cc
._description
= description
686 if precision
is not None:
687 cc
._precision
= precision
689 if offset
is not None:
692 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
700 class _UserSourceComponent(_UserComponent
, _SourceComponent
):
701 _as_not_self_specific_component_ptr
= staticmethod(native_bt
.self_component_source_as_component_source
)
702 _as_self_component_ptr
= staticmethod(native_bt
.self_component_source_as_self_component
)
705 def _output_ports(self
):
706 def get_output_port_count(self_ptr
):
707 ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self_ptr
)
708 return native_bt
.component_source_get_output_port_count(ptr
)
710 return _ComponentPorts(self
._ptr
,
711 native_bt
.self_component_source_borrow_output_port_by_name
,
712 native_bt
.self_component_source_borrow_output_port_by_index
,
713 get_output_port_count
,
714 bt2
.port
._UserComponentOutputPort
)
716 def _add_output_port(self
, name
, user_data
=None):
717 utils
._check
_str
(name
)
718 fn
= native_bt
.self_component_source_add_output_port
719 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
720 _handle_component_status(comp_status
,
721 'cannot add output port to source component object')
722 assert self_port_ptr
is not None
723 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
726 class _UserFilterComponent(_UserComponent
, _FilterComponent
):
727 _as_not_self_specific_component_ptr
= staticmethod(native_bt
.self_component_filter_as_component_filter
)
728 _as_self_component_ptr
= staticmethod(native_bt
.self_component_filter_as_self_component
)
731 def _output_ports(self
):
732 def get_output_port_count(self_ptr
):
733 ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self_ptr
)
734 return native_bt
.component_filter_get_output_port_count(ptr
)
736 return _ComponentPorts(self
._ptr
,
737 native_bt
.self_component_filter_borrow_output_port_by_name
,
738 native_bt
.self_component_filter_borrow_output_port_by_index
,
739 get_output_port_count
,
740 bt2
.port
._UserComponentOutputPort
)
743 def _input_ports(self
):
744 def get_input_port_count(self_ptr
):
745 ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self_ptr
)
746 return native_bt
.component_filter_get_input_port_count(ptr
)
748 return _ComponentPorts(self
._ptr
,
749 native_bt
.self_component_filter_borrow_input_port_by_name
,
750 native_bt
.self_component_filter_borrow_input_port_by_index
,
751 get_input_port_count
,
752 bt2
.port
._UserComponentInputPort
)
754 def _add_output_port(self
, name
, user_data
=None):
755 utils
._check
_str
(name
)
756 fn
= native_bt
.self_component_filter_add_output_port
757 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
758 _handle_component_status(comp_status
,
759 'cannot add output port to filter component object')
761 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
763 def _add_input_port(self
, name
, user_data
=None):
764 utils
._check
_str
(name
)
765 fn
= native_bt
.self_component_filter_add_input_port
766 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
767 _handle_component_status(comp_status
,
768 'cannot add input port to filter component object')
770 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
773 class _UserSinkComponent(_UserComponent
, _SinkComponent
):
774 _as_not_self_specific_component_ptr
= staticmethod(native_bt
.self_component_sink_as_component_sink
)
775 _as_self_component_ptr
= staticmethod(native_bt
.self_component_sink_as_self_component
)
778 def _input_ports(self
):
779 def get_input_port_count(self_ptr
):
780 ptr
= self
._as
_not
_self
_specific
_component
_ptr
(self_ptr
)
781 return native_bt
.component_sink_get_input_port_count(ptr
)
783 return _ComponentPorts(self
._ptr
,
784 native_bt
.self_component_sink_borrow_input_port_by_name
,
785 native_bt
.self_component_sink_borrow_input_port_by_index
,
786 get_input_port_count
,
787 bt2
.port
._UserComponentInputPort
)
789 def _add_input_port(self
, name
, user_data
=None):
790 utils
._check
_str
(name
)
791 fn
= native_bt
.self_component_sink_add_input_port
792 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
793 _handle_component_status(comp_status
,
794 'cannot add input port to sink component object')
796 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)