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
:
104 raise bt2
.Error(gen_error_msg
)
107 class _PortIterator(collections
.abc
.Iterator
):
108 def __init__(self
, comp_ports
):
109 self
._comp
_ports
= comp_ports
113 if self
._at
== len(self
._comp
_ports
):
116 comp_ports
= self
._comp
_ports
117 comp_ptr
= comp_ports
._component
_ptr
119 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
120 assert port_ptr
is not None
122 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
123 assert name
is not None
129 class _ComponentPorts(collections
.abc
.Mapping
):
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.
135 def __init__(self
, component_ptr
,
136 borrow_port_ptr_by_name
,
137 borrow_port_ptr_at_index
,
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
146 def __getitem__(self
, key
):
147 utils
._check
_str
(key
)
148 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
153 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
156 count
= self
._get
_port
_count
(self
._component
_ptr
)
161 return _PortIterator(self
)
164 # This class holds the methods which are common to both generic
165 # component objects and Python user component objects.
167 # Subclasses must provide these methods or property:
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_*
174 # - _as_component_ptr: static method, must return the passed specialized
175 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
180 ptr
= self
._as
_component
_ptr
(self
._ptr
)
181 name
= native_bt
.component_get_name(ptr
)
182 assert name
is not None
186 def logging_level(self
):
187 ptr
= self
._as
_component
_ptr
(self
._ptr
)
188 return native_bt
.component_get_logging_level(ptr
)
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
)
196 def __eq__(self
, other
):
197 if not hasattr(other
, 'addr'):
200 return self
.addr
== other
.addr
203 class _SourceComponent(_Component
):
204 _borrow_component_class_ptr
= staticmethod(native_bt
.component_source_borrow_class_const
)
205 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
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
)
210 class _FilterComponent(_Component
):
211 _borrow_component_class_ptr
= staticmethod(native_bt
.component_filter_borrow_class_const
)
212 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
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
)
217 class _SinkComponent(_Component
):
218 _borrow_component_class_ptr
= staticmethod(native_bt
.component_sink_borrow_class_const
)
219 _comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
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
)
224 # This is analogous to _GenericSourceComponentClass, but for source
226 class _GenericSourceComponent(object._SharedObject
, _SourceComponent
):
227 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
228 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
231 def output_ports(self
):
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
)
239 # This is analogous to _GenericFilterComponentClass, but for filter
241 class _GenericFilterComponent(object._SharedObject
, _FilterComponent
):
242 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
243 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
246 def output_ports(self
):
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
)
254 def input_ports(self
):
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
,
262 # This is analogous to _GenericSinkComponentClass, but for sink
264 class _GenericSinkComponent(object._SharedObject
, _SinkComponent
):
265 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
266 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
269 def input_ports(self
):
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
,
277 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
278 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponent
,
279 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponent
,
280 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponent
,
284 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
285 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentClass
,
286 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentClass
,
287 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentClass
,
291 # Create a component Python object of type _GenericSourceComponent,
292 # _GenericFilterComponent or _GenericSinkComponent, depending on
295 # Steals the reference to ptr from the caller.
297 def _create_component_from_ptr(ptr
, comp_cls_type
):
298 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
301 # Same as the above, but acquire a new reference instead of stealing the
302 # reference from the caller.
304 def _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
)
308 # Create a component class Python object of type
309 # _GenericSourceComponentClass, _GenericFilterComponentClass or
310 # _GenericSinkComponentClass, depending on comp_cls_type.
312 # Acquires a new reference to ptr.
314 def _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
)
318 def _trim_docstring(docstring
):
319 lines
= docstring
.expandtabs().splitlines()
322 for line
in lines
[1:]:
323 stripped
= line
.lstrip()
326 indent
= min(indent
, len(line
) - len(stripped
))
328 trimmed
= [lines
[0].strip()]
330 if indent
< sys
.maxsize
:
331 for line
in lines
[1:]:
332 trimmed
.append(line
[indent
:].rstrip())
334 while trimmed
and not trimmed
[-1]:
337 while trimmed
and not trimmed
[0]:
340 return '\n'.join(trimmed
)
343 # Metaclass for component classes defined by Python code.
345 # The Python user can create a standard Python class which inherits one
346 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
347 # or _UserSinkComponent). Those base classes set this class
348 # (_UserComponentType) as their metaclass.
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.
357 # The component class name which is used is either:
359 # * The `name` parameter of the class:
361 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
364 # * If the `name` class parameter is not used: the name of the class
365 # itself (`MySink` in the example above).
367 # The component class description which is used is the user-defined
370 # class MySink(bt2.SinkComponent):
371 # 'Description goes here'
374 # A user-defined Python component class can have an __init__() method
375 # which must at least accept the `params` and `name` arguments:
377 # def __init__(self, params, name, something_else):
380 # The user-defined component class can also have a _finalize() method
381 # (do NOT use __del__()) to be notified when the component object is
384 # User-defined source and filter component classes must use the
385 # `message_iterator_class` class parameter to specify the
386 # message iterator class to use for this component class:
388 # class MyMessageIterator(bt2._UserMessageIterator):
391 # class MySource(bt2._UserSourceComponent,
392 # message_iterator_class=MyMessageIterator):
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
398 # __init__() method: this method has access to the original Python
399 # component object which was used to create it as the `component`
400 # property. The message iterator class can also define a
401 # _finalize() method (again, do NOT use __del__()): this is called when
402 # the message iterator is (really) destroyed.
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).
408 class _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
)
413 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
414 super().__init
__(class_name
, bases
, namespace
)
416 # skip our own bases; they are never directly instantiated by the user
419 '_UserFilterSinkComponent',
420 '_UserSourceComponent',
421 '_UserFilterComponent',
422 '_UserSinkComponent',
425 if class_name
in own_bases
:
428 comp_cls_name
= kwargs
.get('name', class_name
)
429 utils
._check
_str
(comp_cls_name
)
430 comp_cls_descr
= None
433 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
434 utils
._check
_str
(cls
.__doc
__)
435 docstring
= _trim_docstring(cls
.__doc
__)
436 lines
= docstring
.splitlines()
439 comp_cls_descr
= lines
[0]
442 comp_cls_help
= '\n'.join(lines
[2:])
444 iter_cls
= kwargs
.get('message_iterator_class')
446 if _UserSourceComponent
in bases
:
447 _UserComponentType
._set
_iterator
_class
(cls
, iter_cls
)
448 cc_ptr
= native_bt
.py3_component_class_source_create(cls
,
452 elif _UserFilterComponent
in bases
:
453 _UserComponentType
._set
_iterator
_class
(cls
, iter_cls
)
454 cc_ptr
= native_bt
.py3_component_class_filter_create(cls
,
458 elif _UserSinkComponent
in bases
:
459 if not hasattr(cls
, '_consume'):
460 raise bt2
.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name
))
462 cc_ptr
= native_bt
.py3_component_class_sink_create(cls
,
467 raise bt2
.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name
))
470 raise bt2
.CreationError("cannot create component class '{}'".format(class_name
))
474 def _init_from_native(cls
, comp_ptr
, params_ptr
):
475 # create instance, not user-initialized yet
476 self
= cls
.__new
__(cls
)
478 # pointer to native self component object (weak/borrowed)
481 # call user's __init__() method
482 if params_ptr
is not None:
483 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
487 self
.__init
__(params
)
490 def __call__(cls
, *args
, **kwargs
):
491 raise bt2
.Error('cannot directly instantiate a user component from a Python module')
494 def _set_iterator_class(cls
, iter_cls
):
496 raise bt2
.IncompleteUserClass("cannot create component class '{}': missing message iterator class".format(cls
.__name
__))
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
__))
501 if not hasattr(iter_cls
, '__next__'):
502 raise bt2
.IncompleteUserClass("cannot create component class '{}': message iterator class is missing a __next__() method".format(cls
.__name
__))
504 cls
._iter
_cls
= iter_cls
508 ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
509 return native_bt
.component_class_get_name(ptr
)
512 def description(cls
):
513 ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
514 return native_bt
.component_class_get_description(ptr
)
518 ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
519 return native_bt
.component_class_get_help(ptr
)
523 return int(cls
._cc
_ptr
)
525 def _query_from_native(cls
, query_exec_ptr
, obj
, params_ptr
, log_level
):
526 # this can raise, in which case the native call to
527 # bt_component_class_query() returns NULL
528 if params_ptr
is not None:
529 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
533 query_exec
= bt2
.QueryExecutor
._create
_from
_ptr
_and
_get
_ref
(
536 # this can raise, but the native side checks the exception
537 results
= cls
._query
(query_exec
, obj
, params
, log_level
)
539 # this can raise, but the native side checks the exception
540 results
= bt2
.create_value(results
)
543 results_addr
= int(native_bt
.value_null
)
545 # return new reference
546 results_addr
= int(results
._release
())
550 def _query(cls
, query_executor
, obj
, params
, log_level
):
551 raise NotImplementedError
553 def _component_class_ptr(self
):
554 return self
._as
_component
_class
_ptr
(self
._cc
_ptr
)
557 if hasattr(cls
, '_cc_ptr'):
558 cc_ptr
= cls
._as
_component
_class
_ptr
(cls
._cc
_ptr
)
559 native_bt
.component_class_put_ref(cc_ptr
)
561 # Subclasses must provide these methods or property:
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_*
572 class _UserComponent(metaclass
=_UserComponentType
):
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
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
)
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
)
595 return int(self
._ptr
)
597 def __init__(self
, params
=None):
603 def _port_connected(self
, port
, other_port
):
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
)
610 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
611 other_port_type
= native_bt
.PORT_TYPE_INPUT
613 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
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
)
619 def _graph_is_configured_from_native(self
):
620 self
._graph
_is
_configured
()
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
)
628 raise bt2
.CreationError('could not create trace class')
630 tc
= bt2
._TraceClass
._create
_from
_ptr
(tc_ptr
)
633 for key
, value
in env
.items():
639 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
643 def _create_clock_class(self
, frequency
=None, name
=None, description
=None,
644 precision
=None, offset
=None, origin_is_unix_epoch
=True,
646 ptr
= self
._as
_self
_component
_ptr
(self
._ptr
)
647 cc_ptr
= native_bt
.clock_class_create(ptr
)
650 raise bt2
.CreationError('could not create clock class')
652 cc
= bt2
.clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
654 if frequency
is not None:
655 cc
._frequency
= frequency
660 if description
is not None:
661 cc
._description
= description
663 if precision
is not None:
664 cc
._precision
= precision
666 if offset
is not None:
669 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
677 class _UserSourceComponent(_UserComponent
, _SourceComponent
):
678 _as_not_self_specific_component_ptr
= staticmethod(native_bt
.self_component_source_as_component_source
)
679 _as_self_component_ptr
= staticmethod(native_bt
.self_component_source_as_self_component
)
682 def _output_ports(self
):
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
)
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
)
693 def _add_output_port(self
, name
, user_data
=None):
694 utils
._check
_str
(name
)
695 fn
= native_bt
.self_component_source_add_output_port
696 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
697 _handle_component_status(comp_status
,
698 'cannot add output port to source component object')
699 assert self_port_ptr
is not None
700 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
703 class _UserFilterComponent(_UserComponent
, _FilterComponent
):
704 _as_not_self_specific_component_ptr
= staticmethod(native_bt
.self_component_filter_as_component_filter
)
705 _as_self_component_ptr
= staticmethod(native_bt
.self_component_filter_as_self_component
)
708 def _output_ports(self
):
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
)
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
)
720 def _input_ports(self
):
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
)
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
)
731 def _add_output_port(self
, name
, user_data
=None):
732 utils
._check
_str
(name
)
733 fn
= native_bt
.self_component_filter_add_output_port
734 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
735 _handle_component_status(comp_status
,
736 'cannot add output port to filter component object')
738 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
740 def _add_input_port(self
, name
, user_data
=None):
741 utils
._check
_str
(name
)
742 fn
= native_bt
.self_component_filter_add_input_port
743 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
744 _handle_component_status(comp_status
,
745 'cannot add input port to filter component object')
747 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
750 class _UserSinkComponent(_UserComponent
, _SinkComponent
):
751 _as_not_self_specific_component_ptr
= staticmethod(native_bt
.self_component_sink_as_component_sink
)
752 _as_self_component_ptr
= staticmethod(native_bt
.self_component_sink_as_self_component
)
755 def _input_ports(self
):
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
)
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
)
766 def _add_input_port(self
, name
, user_data
=None):
767 utils
._check
_str
(name
)
768 fn
= native_bt
.self_component_sink_add_input_port
769 comp_status
, self_port_ptr
= fn(self
._ptr
, name
, user_data
)
770 _handle_component_status(comp_status
,
771 'cannot add input port to sink component object')
773 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)