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 # 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
39 # Subclasses must implement some methods that this base class uses:
41 # - _bt_as_component_class_ptr: static method, convert the passed component class
42 # pointer to a 'bt_component_class *'.
45 class _GenericComponentClass(object._SharedObject
):
48 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
49 name
= native_bt
.component_class_get_name(ptr
)
50 assert name
is not None
54 def description(self
):
55 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
56 return native_bt
.component_class_get_description(ptr
)
60 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
61 return native_bt
.component_class_get_help(ptr
)
63 def _bt_component_class_ptr(self
):
64 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
66 def __eq__(self
, other
):
67 if not isinstance(other
, _GenericComponentClass
):
69 if not issubclass(other
, _UserComponent
):
74 return self
.addr
== other
.addr
77 class _GenericSourceComponentClass(_GenericComponentClass
):
78 _get_ref
= staticmethod(native_bt
.component_class_source_get_ref
)
79 _put_ref
= staticmethod(native_bt
.component_class_source_put_ref
)
80 _bt_as_component_class_ptr
= staticmethod(
81 native_bt
.component_class_source_as_component_class
85 class _GenericFilterComponentClass(_GenericComponentClass
):
86 _get_ref
= staticmethod(native_bt
.component_class_filter_get_ref
)
87 _put_ref
= staticmethod(native_bt
.component_class_filter_put_ref
)
88 _bt_as_component_class_ptr
= staticmethod(
89 native_bt
.component_class_filter_as_component_class
93 class _GenericSinkComponentClass(_GenericComponentClass
):
94 _get_ref
= staticmethod(native_bt
.component_class_sink_get_ref
)
95 _put_ref
= staticmethod(native_bt
.component_class_sink_put_ref
)
96 _bt_as_component_class_ptr
= staticmethod(
97 native_bt
.component_class_sink_as_component_class
101 class _PortIterator(collections
.abc
.Iterator
):
102 def __init__(self
, comp_ports
):
103 self
._comp
_ports
= comp_ports
107 if self
._at
== len(self
._comp
_ports
):
110 comp_ports
= self
._comp
_ports
111 comp_ptr
= comp_ports
._component
_ptr
113 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
114 assert port_ptr
is not None
116 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
117 assert name
is not None
123 class _ComponentPorts(collections
.abc
.Mapping
):
125 # component_ptr is a bt_component_source *, bt_component_filter * or
126 # bt_component_sink *. Its type must match the type expected by the
127 # functions passed as arguments.
132 borrow_port_ptr_by_name
,
133 borrow_port_ptr_at_index
,
137 self
._component
_ptr
= component_ptr
138 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
139 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
140 self
._get
_port
_count
= get_port_count
141 self
._port
_pycls
= port_pycls
143 def __getitem__(self
, key
):
144 utils
._check
_str
(key
)
145 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
150 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
153 count
= self
._get
_port
_count
(self
._component
_ptr
)
158 return _PortIterator(self
)
161 # This class holds the methods which are common to both generic
162 # component objects and Python user component objects.
164 # Subclasses must provide these methods or property:
166 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
167 # specialized component class (e.g. 'bt_component_class_sink *') of the
168 # passed specialized component pointer (e.g. 'bt_component_sink *').
169 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
171 # - _bt_as_component_ptr: static method, must return the passed specialized
172 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
178 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
179 name
= native_bt
.component_get_name(ptr
)
180 assert name
is not None
184 def logging_level(self
):
185 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
186 return native_bt
.component_get_logging_level(ptr
)
190 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
191 assert cc_ptr
is not None
192 return _create_component_class_from_ptr_and_get_ref(
193 cc_ptr
, self
._bt
_comp
_cls
_type
196 def __eq__(self
, other
):
197 if not hasattr(other
, 'addr'):
200 return self
.addr
== other
.addr
203 class _SourceComponent(_Component
):
204 _bt_borrow_component_class_ptr
= staticmethod(
205 native_bt
.component_source_borrow_class_const
207 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
208 _bt_as_component_class_ptr
= staticmethod(
209 native_bt
.component_class_source_as_component_class
211 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
214 class _FilterComponent(_Component
):
215 _bt_borrow_component_class_ptr
= staticmethod(
216 native_bt
.component_filter_borrow_class_const
218 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
219 _bt_as_component_class_ptr
= staticmethod(
220 native_bt
.component_class_filter_as_component_class
222 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
225 class _SinkComponent(_Component
):
226 _bt_borrow_component_class_ptr
= staticmethod(
227 native_bt
.component_sink_borrow_class_const
229 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
230 _bt_as_component_class_ptr
= staticmethod(
231 native_bt
.component_class_sink_as_component_class
233 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
236 # This is analogous to _GenericSourceComponentClass, but for source
238 class _GenericSourceComponent(object._SharedObject
, _SourceComponent
):
239 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
240 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
243 def output_ports(self
):
244 return _ComponentPorts(
246 native_bt
.component_source_borrow_output_port_by_name_const
,
247 native_bt
.component_source_borrow_output_port_by_index_const
,
248 native_bt
.component_source_get_output_port_count
,
249 bt2
.port
._OutputPort
,
253 # This is analogous to _GenericFilterComponentClass, but for filter
255 class _GenericFilterComponent(object._SharedObject
, _FilterComponent
):
256 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
257 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
260 def output_ports(self
):
261 return _ComponentPorts(
263 native_bt
.component_filter_borrow_output_port_by_name_const
,
264 native_bt
.component_filter_borrow_output_port_by_index_const
,
265 native_bt
.component_filter_get_output_port_count
,
266 bt2
.port
._OutputPort
,
270 def input_ports(self
):
271 return _ComponentPorts(
273 native_bt
.component_filter_borrow_input_port_by_name_const
,
274 native_bt
.component_filter_borrow_input_port_by_index_const
,
275 native_bt
.component_filter_get_input_port_count
,
280 # This is analogous to _GenericSinkComponentClass, but for sink
282 class _GenericSinkComponent(object._SharedObject
, _SinkComponent
):
283 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
284 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
287 def input_ports(self
):
288 return _ComponentPorts(
290 native_bt
.component_sink_borrow_input_port_by_name_const
,
291 native_bt
.component_sink_borrow_input_port_by_index_const
,
292 native_bt
.component_sink_get_input_port_count
,
297 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
298 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponent
,
299 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponent
,
300 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponent
,
304 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
305 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentClass
,
306 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentClass
,
307 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentClass
,
311 # Create a component Python object of type _GenericSourceComponent,
312 # _GenericFilterComponent or _GenericSinkComponent, depending on
315 # Steals the reference to ptr from the caller.
318 def _create_component_from_ptr(ptr
, comp_cls_type
):
319 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
322 # Same as the above, but acquire a new reference instead of stealing the
323 # reference from the caller.
326 def _create_component_from_ptr_and_get_ref(ptr
, comp_cls_type
):
327 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
329 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
332 # Create a component class Python object of type
333 # _GenericSourceComponentClass, _GenericFilterComponentClass or
334 # _GenericSinkComponentClass, depending on comp_cls_type.
336 # Acquires a new reference to ptr.
339 def _create_component_class_from_ptr_and_get_ref(ptr
, comp_cls_type
):
340 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
342 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
345 def _trim_docstring(docstring
):
346 lines
= docstring
.expandtabs().splitlines()
349 for line
in lines
[1:]:
350 stripped
= line
.lstrip()
353 indent
= min(indent
, len(line
) - len(stripped
))
355 trimmed
= [lines
[0].strip()]
357 if indent
< sys
.maxsize
:
358 for line
in lines
[1:]:
359 trimmed
.append(line
[indent
:].rstrip())
361 while trimmed
and not trimmed
[-1]:
364 while trimmed
and not trimmed
[0]:
367 return '\n'.join(trimmed
)
370 # Metaclass for component classes defined by Python code.
372 # The Python user can create a standard Python class which inherits one
373 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
374 # or _UserSinkComponent). Those base classes set this class
375 # (_UserComponentType) as their metaclass.
377 # Once the body of a user-defined component class is executed, this
378 # metaclass is used to create and initialize the class. The metaclass
379 # creates a native BT component class of the corresponding type and
380 # associates it with this user-defined class. The metaclass also defines
381 # class methods like the `name` and `description` properties to match
382 # the _GenericComponentClass interface.
384 # The component class name which is used is either:
386 # * The `name` parameter of the class:
388 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
391 # * If the `name` class parameter is not used: the name of the class
392 # itself (`MySink` in the example above).
394 # The component class description which is used is the user-defined
397 # class MySink(bt2.SinkComponent):
398 # 'Description goes here'
401 # A user-defined Python component class can have an __init__() method
402 # which must at least accept the `params` and `name` arguments:
404 # def __init__(self, params, name, something_else):
407 # The user-defined component class can also have a _finalize() method
408 # (do NOT use __del__()) to be notified when the component object is
411 # User-defined source and filter component classes must use the
412 # `message_iterator_class` class parameter to specify the
413 # message iterator class to use for this component class:
415 # class MyMessageIterator(bt2._UserMessageIterator):
418 # class MySource(bt2._UserSourceComponent,
419 # message_iterator_class=MyMessageIterator):
422 # This message iterator class must inherit
423 # bt2._UserMessageIterator, and it must define the _get() and
424 # _next() methods. The message iterator class can also define an
425 # __init__() method: this method has access to the original Python
426 # component object which was used to create it as the `component`
427 # property. The message iterator class can also define a
428 # _finalize() method (again, do NOT use __del__()): this is called when
429 # the message iterator is (really) destroyed.
431 # When the user-defined class is destroyed, this metaclass's __del__()
432 # method is called: the native BT component class pointer is put (not
433 # needed anymore, at least not by any Python code since all references
434 # are dropped for __del__() to be called).
435 class _UserComponentType(type):
436 # __new__() is used to catch custom class parameters
437 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
438 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
440 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
441 super().__init
__(class_name
, bases
, namespace
)
443 # skip our own bases; they are never directly instantiated by the user
446 '_UserFilterSinkComponent',
447 '_UserSourceComponent',
448 '_UserFilterComponent',
449 '_UserSinkComponent',
452 if class_name
in own_bases
:
455 comp_cls_name
= kwargs
.get('name', class_name
)
456 utils
._check
_str
(comp_cls_name
)
457 comp_cls_descr
= None
460 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
461 utils
._check
_str
(cls
.__doc
__)
462 docstring
= _trim_docstring(cls
.__doc
__)
463 lines
= docstring
.splitlines()
466 comp_cls_descr
= lines
[0]
469 comp_cls_help
= '\n'.join(lines
[2:])
471 iter_cls
= kwargs
.get('message_iterator_class')
473 if _UserSourceComponent
in bases
:
474 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
475 cc_ptr
= native_bt
.bt2_component_class_source_create(
476 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
478 elif _UserFilterComponent
in bases
:
479 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
480 cc_ptr
= native_bt
.bt2_component_class_filter_create(
481 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
483 elif _UserSinkComponent
in bases
:
484 if not hasattr(cls
, '_graph_is_configured'):
485 raise bt2
.IncompleteUserClass(
486 "cannot create component class '{}': missing a _graph_is_configured() method".format(
491 if not hasattr(cls
, '_consume'):
492 raise bt2
.IncompleteUserClass(
493 "cannot create component class '{}': missing a _consume() method".format(
498 cc_ptr
= native_bt
.bt2_component_class_sink_create(
499 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
502 raise bt2
.IncompleteUserClass(
503 "cannot find a known component class base in the bases of '{}'".format(
509 raise bt2
.CreationError(
510 "cannot create component class '{}'".format(class_name
)
513 cls
._bt
_cc
_ptr
= cc_ptr
515 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
):
516 # create instance, not user-initialized yet
517 self
= cls
.__new
__(cls
)
519 # pointer to native self component object (weak/borrowed)
520 self
._bt
_ptr
= comp_ptr
522 # call user's __init__() method
523 if params_ptr
is not None:
524 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
528 self
.__init
__(params
)
531 def __call__(cls
, *args
, **kwargs
):
533 'cannot directly instantiate a user component from a Python module'
537 def _bt_set_iterator_class(cls
, iter_cls
):
539 raise bt2
.IncompleteUserClass(
540 "cannot create component class '{}': missing message iterator class".format(
545 if not issubclass(iter_cls
, bt2
.message_iterator
._UserMessageIterator
):
546 raise bt2
.IncompleteUserClass(
547 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
552 if not hasattr(iter_cls
, '__next__'):
553 raise bt2
.IncompleteUserClass(
554 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
559 cls
._iter
_cls
= iter_cls
563 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
564 return native_bt
.component_class_get_name(ptr
)
567 def description(cls
):
568 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
569 return native_bt
.component_class_get_description(ptr
)
573 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
574 return native_bt
.component_class_get_help(ptr
)
578 return int(cls
._bt
_cc
_ptr
)
580 def _bt_query_from_native(cls
, query_exec_ptr
, obj
, params_ptr
, log_level
):
581 # this can raise, in which case the native call to
582 # bt_component_class_query() returns NULL
583 if params_ptr
is not None:
584 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
588 query_exec
= bt2
.QueryExecutor
._create
_from
_ptr
_and
_get
_ref
(query_exec_ptr
)
590 # this can raise, but the native side checks the exception
591 results
= cls
._query
(query_exec
, obj
, params
, log_level
)
593 # this can raise, but the native side checks the exception
594 results
= bt2
.create_value(results
)
597 results_ptr
= native_bt
.value_null
599 # return new reference
600 results_ptr
= results
._ptr
602 # We return a new reference.
603 bt2
.value
._Value
._get
_ref
(results_ptr
)
605 return int(results_ptr
)
607 def _query(cls
, query_executor
, obj
, params
, log_level
):
608 raise NotImplementedError
610 def _bt_component_class_ptr(self
):
611 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
614 if hasattr(cls
, '_bt_cc_ptr'):
615 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
616 native_bt
.component_class_put_ref(cc_ptr
)
619 # Subclasses must provide these methods or property:
621 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
622 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
623 # specialized non-self pointer (e.g. 'bt_component_sink *').
624 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
625 # specialized component class (e.g. 'bt_component_class_sink *') of the
626 # passed specialized component pointer (e.g. 'bt_component_sink *').
627 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
631 class _UserComponent(metaclass
=_UserComponentType
):
634 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
635 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
636 name
= native_bt
.component_get_name(ptr
)
637 assert name
is not None
641 def logging_level(self
):
642 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
643 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
644 return native_bt
.component_get_logging_level(ptr
)
648 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
649 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
650 return _create_component_class_from_ptr_and_get_ref(
651 cc_ptr
, self
._bt
_comp
_cls
_type
656 return int(self
._bt
_ptr
)
658 def __init__(self
, params
=None):
664 def _port_connected(self
, port
, other_port
):
667 def _bt_port_connected_from_native(
668 self
, self_port_ptr
, self_port_type
, other_port_ptr
670 port
= bt2
.port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
672 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
673 other_port_type
= native_bt
.PORT_TYPE_INPUT
675 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
677 other_port
= bt2
.port
._create
_from
_ptr
_and
_get
_ref
(
678 other_port_ptr
, other_port_type
680 self
._port
_connected
(port
, other_port
)
682 def _create_trace_class(self
, assigns_automatic_stream_class_id
=True):
683 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
684 tc_ptr
= native_bt
.trace_class_create(ptr
)
687 raise bt2
.CreationError('could not create trace class')
689 tc
= bt2
._TraceClass
._create
_from
_ptr
(tc_ptr
)
690 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
694 def _create_clock_class(
701 origin_is_unix_epoch
=True,
704 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
705 cc_ptr
= native_bt
.clock_class_create(ptr
)
708 raise bt2
.CreationError('could not create clock class')
710 cc
= bt2
.clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
712 if frequency
is not None:
713 cc
._frequency
= frequency
718 if description
is not None:
719 cc
._description
= description
721 if precision
is not None:
722 cc
._precision
= precision
724 if offset
is not None:
727 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
735 class _UserSourceComponent(_UserComponent
, _SourceComponent
):
736 _bt_as_not_self_specific_component_ptr
= staticmethod(
737 native_bt
.self_component_source_as_component_source
739 _bt_as_self_component_ptr
= staticmethod(
740 native_bt
.self_component_source_as_self_component
744 def _output_ports(self
):
745 def get_output_port_count(self_ptr
):
746 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
747 return native_bt
.component_source_get_output_port_count(ptr
)
749 return _ComponentPorts(
751 native_bt
.self_component_source_borrow_output_port_by_name
,
752 native_bt
.self_component_source_borrow_output_port_by_index
,
753 get_output_port_count
,
754 bt2
.port
._UserComponentOutputPort
,
757 def _add_output_port(self
, name
, user_data
=None):
758 utils
._check
_str
(name
)
759 fn
= native_bt
.self_component_source_add_output_port
760 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
761 utils
._handle
_func
_status
(
762 comp_status
, 'cannot add output port to source component object'
764 assert self_port_ptr
is not None
765 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
768 class _UserFilterComponent(_UserComponent
, _FilterComponent
):
769 _bt_as_not_self_specific_component_ptr
= staticmethod(
770 native_bt
.self_component_filter_as_component_filter
772 _bt_as_self_component_ptr
= staticmethod(
773 native_bt
.self_component_filter_as_self_component
777 def _output_ports(self
):
778 def get_output_port_count(self_ptr
):
779 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
780 return native_bt
.component_filter_get_output_port_count(ptr
)
782 return _ComponentPorts(
784 native_bt
.self_component_filter_borrow_output_port_by_name
,
785 native_bt
.self_component_filter_borrow_output_port_by_index
,
786 get_output_port_count
,
787 bt2
.port
._UserComponentOutputPort
,
791 def _input_ports(self
):
792 def get_input_port_count(self_ptr
):
793 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
794 return native_bt
.component_filter_get_input_port_count(ptr
)
796 return _ComponentPorts(
798 native_bt
.self_component_filter_borrow_input_port_by_name
,
799 native_bt
.self_component_filter_borrow_input_port_by_index
,
800 get_input_port_count
,
801 bt2
.port
._UserComponentInputPort
,
804 def _add_output_port(self
, name
, user_data
=None):
805 utils
._check
_str
(name
)
806 fn
= native_bt
.self_component_filter_add_output_port
807 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
808 utils
._handle
_func
_status
(
809 comp_status
, 'cannot add output port to filter component object'
812 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
814 def _add_input_port(self
, name
, user_data
=None):
815 utils
._check
_str
(name
)
816 fn
= native_bt
.self_component_filter_add_input_port
817 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
818 utils
._handle
_func
_status
(
819 comp_status
, 'cannot add input port to filter component object'
822 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
825 class _UserSinkComponent(_UserComponent
, _SinkComponent
):
826 _bt_as_not_self_specific_component_ptr
= staticmethod(
827 native_bt
.self_component_sink_as_component_sink
829 _bt_as_self_component_ptr
= staticmethod(
830 native_bt
.self_component_sink_as_self_component
833 def _bt_graph_is_configured_from_native(self
):
834 self
._graph
_is
_configured
()
837 def _input_ports(self
):
838 def get_input_port_count(self_ptr
):
839 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
840 return native_bt
.component_sink_get_input_port_count(ptr
)
842 return _ComponentPorts(
844 native_bt
.self_component_sink_borrow_input_port_by_name
,
845 native_bt
.self_component_sink_borrow_input_port_by_index
,
846 get_input_port_count
,
847 bt2
.port
._UserComponentInputPort
,
850 def _add_input_port(self
, name
, user_data
=None):
851 utils
._check
_str
(name
)
852 fn
= native_bt
.self_component_sink_add_input_port
853 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
854 utils
._handle
_func
_status
(
855 comp_status
, 'cannot add input port to sink component object'
858 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)