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
27 import bt2
.trace_class
35 # This class wraps a component class pointer. This component class could
36 # have been created by Python code, but since we only have the pointer,
37 # we can only wrap it in a generic way and lose the original Python
40 # Subclasses must implement some methods that this base class uses:
42 # - _bt_as_component_class_ptr: static method, convert the passed component class
43 # pointer to a 'bt_component_class *'.
46 class _ComponentClass(object._SharedObject
):
49 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
50 name
= native_bt
.component_class_get_name(ptr
)
51 assert name
is not None
55 def description(self
):
56 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
57 return native_bt
.component_class_get_description(ptr
)
61 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
62 return native_bt
.component_class_get_help(ptr
)
64 def _bt_component_class_ptr(self
):
65 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
67 def __eq__(self
, other
):
68 if not isinstance(other
, _ComponentClass
):
70 if not issubclass(other
, _UserComponent
):
75 return self
.addr
== other
.addr
78 class _SourceComponentClass(_ComponentClass
):
79 _get_ref
= staticmethod(native_bt
.component_class_source_get_ref
)
80 _put_ref
= staticmethod(native_bt
.component_class_source_put_ref
)
81 _bt_as_component_class_ptr
= staticmethod(
82 native_bt
.component_class_source_as_component_class
86 class _FilterComponentClass(_ComponentClass
):
87 _get_ref
= staticmethod(native_bt
.component_class_filter_get_ref
)
88 _put_ref
= staticmethod(native_bt
.component_class_filter_put_ref
)
89 _bt_as_component_class_ptr
= staticmethod(
90 native_bt
.component_class_filter_as_component_class
94 class _SinkComponentClass(_ComponentClass
):
95 _get_ref
= staticmethod(native_bt
.component_class_sink_get_ref
)
96 _put_ref
= staticmethod(native_bt
.component_class_sink_put_ref
)
97 _bt_as_component_class_ptr
= staticmethod(
98 native_bt
.component_class_sink_as_component_class
102 class _PortIterator(collections
.abc
.Iterator
):
103 def __init__(self
, comp_ports
):
104 self
._comp
_ports
= comp_ports
108 if self
._at
== len(self
._comp
_ports
):
111 comp_ports
= self
._comp
_ports
112 comp_ptr
= comp_ports
._component
_ptr
114 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
115 assert port_ptr
is not None
117 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
118 assert name
is not None
124 class _ComponentPorts(collections
.abc
.Mapping
):
126 # component_ptr is a bt_component_source *, bt_component_filter * or
127 # bt_component_sink *. Its type must match the type expected by the
128 # functions passed as arguments.
133 borrow_port_ptr_by_name
,
134 borrow_port_ptr_at_index
,
138 self
._component
_ptr
= component_ptr
139 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
140 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
141 self
._get
_port
_count
= get_port_count
142 self
._port
_pycls
= port_pycls
144 def __getitem__(self
, key
):
145 utils
._check
_str
(key
)
146 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
151 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
154 count
= self
._get
_port
_count
(self
._component
_ptr
)
159 return _PortIterator(self
)
162 # This class holds the methods which are common to both generic
163 # component objects and Python user component objects.
165 # Subclasses must provide these methods or property:
167 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
168 # specialized component class (e.g. 'bt_component_class_sink *') of the
169 # passed specialized component pointer (e.g. 'bt_component_sink *').
170 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
172 # - _bt_as_component_ptr: static method, must return the passed specialized
173 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
179 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
180 name
= native_bt
.component_get_name(ptr
)
181 assert name
is not None
185 def logging_level(self
):
186 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
187 return native_bt
.component_get_logging_level(ptr
)
191 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
192 assert cc_ptr
is not None
193 return _create_component_class_from_ptr_and_get_ref(
194 cc_ptr
, self
._bt
_comp
_cls
_type
197 def __eq__(self
, other
):
198 if not hasattr(other
, 'addr'):
201 return self
.addr
== other
.addr
204 class _SourceComponent(_Component
):
205 _bt_borrow_component_class_ptr
= staticmethod(
206 native_bt
.component_source_borrow_class_const
208 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
209 _bt_as_component_class_ptr
= staticmethod(
210 native_bt
.component_class_source_as_component_class
212 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
215 class _FilterComponent(_Component
):
216 _bt_borrow_component_class_ptr
= staticmethod(
217 native_bt
.component_filter_borrow_class_const
219 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
220 _bt_as_component_class_ptr
= staticmethod(
221 native_bt
.component_class_filter_as_component_class
223 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
226 class _SinkComponent(_Component
):
227 _bt_borrow_component_class_ptr
= staticmethod(
228 native_bt
.component_sink_borrow_class_const
230 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
231 _bt_as_component_class_ptr
= staticmethod(
232 native_bt
.component_class_sink_as_component_class
234 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
237 # This is analogous to _SourceComponentClass, but for source
239 class _GenericSourceComponent(object._SharedObject
, _SourceComponent
):
240 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
241 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
244 def output_ports(self
):
245 return _ComponentPorts(
247 native_bt
.component_source_borrow_output_port_by_name_const
,
248 native_bt
.component_source_borrow_output_port_by_index_const
,
249 native_bt
.component_source_get_output_port_count
,
250 bt2
.port
._OutputPort
,
254 # This is analogous to _FilterComponentClass, but for filter
256 class _GenericFilterComponent(object._SharedObject
, _FilterComponent
):
257 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
258 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
261 def output_ports(self
):
262 return _ComponentPorts(
264 native_bt
.component_filter_borrow_output_port_by_name_const
,
265 native_bt
.component_filter_borrow_output_port_by_index_const
,
266 native_bt
.component_filter_get_output_port_count
,
267 bt2
.port
._OutputPort
,
271 def input_ports(self
):
272 return _ComponentPorts(
274 native_bt
.component_filter_borrow_input_port_by_name_const
,
275 native_bt
.component_filter_borrow_input_port_by_index_const
,
276 native_bt
.component_filter_get_input_port_count
,
281 # This is analogous to _SinkComponentClass, but for sink
283 class _GenericSinkComponent(object._SharedObject
, _SinkComponent
):
284 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
285 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
288 def input_ports(self
):
289 return _ComponentPorts(
291 native_bt
.component_sink_borrow_input_port_by_name_const
,
292 native_bt
.component_sink_borrow_input_port_by_index_const
,
293 native_bt
.component_sink_get_input_port_count
,
298 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
299 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponent
,
300 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponent
,
301 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponent
,
305 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
306 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClass
,
307 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClass
,
308 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClass
,
312 # Create a component Python object of type _GenericSourceComponent,
313 # _GenericFilterComponent or _GenericSinkComponent, depending on
316 # Steals the reference to ptr from the caller.
319 def _create_component_from_ptr(ptr
, comp_cls_type
):
320 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
323 # Same as the above, but acquire a new reference instead of stealing the
324 # reference from the caller.
327 def _create_component_from_ptr_and_get_ref(ptr
, comp_cls_type
):
328 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
330 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
333 # Create a component class Python object of type
334 # _SourceComponentClass, _FilterComponentClass or
335 # _SinkComponentClass, depending on comp_cls_type.
337 # Acquires a new reference to ptr.
340 def _create_component_class_from_ptr_and_get_ref(ptr
, comp_cls_type
):
341 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
343 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
346 def _trim_docstring(docstring
):
347 lines
= docstring
.expandtabs().splitlines()
350 for line
in lines
[1:]:
351 stripped
= line
.lstrip()
354 indent
= min(indent
, len(line
) - len(stripped
))
356 trimmed
= [lines
[0].strip()]
358 if indent
< sys
.maxsize
:
359 for line
in lines
[1:]:
360 trimmed
.append(line
[indent
:].rstrip())
362 while trimmed
and not trimmed
[-1]:
365 while trimmed
and not trimmed
[0]:
368 return '\n'.join(trimmed
)
371 # Metaclass for component classes defined by Python code.
373 # The Python user can create a standard Python class which inherits one
374 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
375 # or _UserSinkComponent). Those base classes set this class
376 # (_UserComponentType) as their metaclass.
378 # Once the body of a user-defined component class is executed, this
379 # metaclass is used to create and initialize the class. The metaclass
380 # creates a native BT component class of the corresponding type and
381 # associates it with this user-defined class. The metaclass also defines
382 # class methods like the `name` and `description` properties to match
383 # the _ComponentClass interface.
385 # The component class name which is used is either:
387 # * The `name` parameter of the class:
389 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
392 # * If the `name` class parameter is not used: the name of the class
393 # itself (`MySink` in the example above).
395 # The component class description which is used is the user-defined
398 # class MySink(bt2.SinkComponent):
399 # 'Description goes here'
402 # A user-defined Python component class can have an __init__() method
403 # which must at least accept the `params` and `name` arguments:
405 # def __init__(self, params, name, something_else):
408 # The user-defined component class can also have a _finalize() method
409 # (do NOT use __del__()) to be notified when the component object is
412 # User-defined source and filter component classes must use the
413 # `message_iterator_class` class parameter to specify the
414 # message iterator class to use for this component class:
416 # class MyMessageIterator(bt2._UserMessageIterator):
419 # class MySource(bt2._UserSourceComponent,
420 # message_iterator_class=MyMessageIterator):
423 # This message iterator class must inherit
424 # bt2._UserMessageIterator, and it must define the _get() and
425 # _next() methods. The message iterator class can also define an
426 # __init__() method: this method has access to the original Python
427 # component object which was used to create it as the `component`
428 # property. The message iterator class can also define a
429 # _finalize() method (again, do NOT use __del__()): this is called when
430 # the message iterator is (really) destroyed.
432 # When the user-defined class is destroyed, this metaclass's __del__()
433 # method is called: the native BT component class pointer is put (not
434 # needed anymore, at least not by any Python code since all references
435 # are dropped for __del__() to be called).
436 class _UserComponentType(type):
437 # __new__() is used to catch custom class parameters
438 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
439 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
441 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
442 super().__init
__(class_name
, bases
, namespace
)
444 # skip our own bases; they are never directly instantiated by the user
447 '_UserFilterSinkComponent',
448 '_UserSourceComponent',
449 '_UserFilterComponent',
450 '_UserSinkComponent',
453 if class_name
in own_bases
:
456 comp_cls_name
= kwargs
.get('name', class_name
)
457 utils
._check
_str
(comp_cls_name
)
458 comp_cls_descr
= None
461 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
462 utils
._check
_str
(cls
.__doc
__)
463 docstring
= _trim_docstring(cls
.__doc
__)
464 lines
= docstring
.splitlines()
467 comp_cls_descr
= lines
[0]
470 comp_cls_help
= '\n'.join(lines
[2:])
472 iter_cls
= kwargs
.get('message_iterator_class')
474 if _UserSourceComponent
in bases
:
475 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
476 cc_ptr
= native_bt
.bt2_component_class_source_create(
477 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
479 elif _UserFilterComponent
in bases
:
480 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
481 cc_ptr
= native_bt
.bt2_component_class_filter_create(
482 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
484 elif _UserSinkComponent
in bases
:
485 if not hasattr(cls
, '_consume'):
486 raise bt2
._IncompleteUserClass(
487 "cannot create component class '{}': missing a _consume() method".format(
492 cc_ptr
= native_bt
.bt2_component_class_sink_create(
493 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
496 raise bt2
._IncompleteUserClass(
497 "cannot find a known component class base in the bases of '{}'".format(
503 raise bt2
._MemoryError(
504 "cannot create component class '{}'".format(class_name
)
507 cls
._bt
_cc
_ptr
= cc_ptr
509 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
):
510 # create instance, not user-initialized yet
511 self
= cls
.__new
__(cls
)
513 # pointer to native self component object (weak/borrowed)
514 self
._bt
_ptr
= comp_ptr
516 # call user's __init__() method
517 if params_ptr
is not None:
518 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
522 self
.__init
__(params
)
525 def __call__(cls
, *args
, **kwargs
):
527 'cannot directly instantiate a user component from a Python module'
531 def _bt_set_iterator_class(cls
, iter_cls
):
533 raise bt2
._IncompleteUserClass(
534 "cannot create component class '{}': missing message iterator class".format(
539 if not issubclass(iter_cls
, bt2
.message_iterator
._UserMessageIterator
):
540 raise bt2
._IncompleteUserClass(
541 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
546 if not hasattr(iter_cls
, '__next__'):
547 raise bt2
._IncompleteUserClass(
548 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
553 cls
._iter
_cls
= iter_cls
557 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
558 return native_bt
.component_class_get_name(ptr
)
561 def description(cls
):
562 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
563 return native_bt
.component_class_get_description(ptr
)
567 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
568 return native_bt
.component_class_get_help(ptr
)
572 return int(cls
._bt
_cc
_ptr
)
574 def _bt_query_from_native(cls
, query_exec_ptr
, obj
, params_ptr
, log_level
):
575 # this can raise, in which case the native call to
576 # bt_component_class_query() returns NULL
577 if params_ptr
is not None:
578 params
= bt2
.value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
582 query_exec
= bt2
.QueryExecutor
._create
_from
_ptr
_and
_get
_ref
(query_exec_ptr
)
584 # this can raise, but the native side checks the exception
585 results
= cls
._query
(query_exec
, obj
, params
, log_level
)
587 # this can raise, but the native side checks the exception
588 results
= bt2
.create_value(results
)
591 results_ptr
= native_bt
.value_null
593 # return new reference
594 results_ptr
= results
._ptr
596 # We return a new reference.
597 bt2
.value
._Value
._get
_ref
(results_ptr
)
599 return int(results_ptr
)
601 def _query(cls
, query_executor
, obj
, params
, log_level
):
602 raise NotImplementedError
604 def _bt_component_class_ptr(self
):
605 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
608 if hasattr(cls
, '_bt_cc_ptr'):
609 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
610 native_bt
.component_class_put_ref(cc_ptr
)
613 # Subclasses must provide these methods or property:
615 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
616 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
617 # specialized non-self pointer (e.g. 'bt_component_sink *').
618 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
619 # specialized component class (e.g. 'bt_component_class_sink *') of the
620 # passed specialized component pointer (e.g. 'bt_component_sink *').
621 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
625 class _UserComponent(metaclass
=_UserComponentType
):
628 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
629 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
630 name
= native_bt
.component_get_name(ptr
)
631 assert name
is not None
635 def logging_level(self
):
636 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
637 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
638 return native_bt
.component_get_logging_level(ptr
)
642 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
643 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
644 return _create_component_class_from_ptr_and_get_ref(
645 cc_ptr
, self
._bt
_comp
_cls
_type
650 return int(self
._bt
_ptr
)
652 def __init__(self
, params
=None):
658 def _port_connected(self
, port
, other_port
):
661 def _bt_port_connected_from_native(
662 self
, self_port_ptr
, self_port_type
, other_port_ptr
664 port
= bt2
.port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
666 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
667 other_port_type
= native_bt
.PORT_TYPE_INPUT
669 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
671 other_port
= bt2
.port
._create
_from
_ptr
_and
_get
_ref
(
672 other_port_ptr
, other_port_type
674 self
._port
_connected
(port
, other_port
)
676 def _create_trace_class(self
, assigns_automatic_stream_class_id
=True):
677 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
678 tc_ptr
= native_bt
.trace_class_create(ptr
)
681 raise bt2
._MemoryError('could not create trace class')
683 tc
= bt2
.trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
684 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
688 def _create_clock_class(
695 origin_is_unix_epoch
=True,
698 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
699 cc_ptr
= native_bt
.clock_class_create(ptr
)
702 raise bt2
._MemoryError('could not create clock class')
704 cc
= bt2
.clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
706 if frequency
is not None:
707 cc
._frequency
= frequency
712 if description
is not None:
713 cc
._description
= description
715 if precision
is not None:
716 cc
._precision
= precision
718 if offset
is not None:
721 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
729 class _UserSourceComponent(_UserComponent
, _SourceComponent
):
730 _bt_as_not_self_specific_component_ptr
= staticmethod(
731 native_bt
.self_component_source_as_component_source
733 _bt_as_self_component_ptr
= staticmethod(
734 native_bt
.self_component_source_as_self_component
738 def _output_ports(self
):
739 def get_output_port_count(self_ptr
):
740 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
741 return native_bt
.component_source_get_output_port_count(ptr
)
743 return _ComponentPorts(
745 native_bt
.self_component_source_borrow_output_port_by_name
,
746 native_bt
.self_component_source_borrow_output_port_by_index
,
747 get_output_port_count
,
748 bt2
.port
._UserComponentOutputPort
,
751 def _add_output_port(self
, name
, user_data
=None):
752 utils
._check
_str
(name
)
753 fn
= native_bt
.self_component_source_add_output_port
754 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
755 utils
._handle
_func
_status
(
756 comp_status
, 'cannot add output port to source component object'
758 assert self_port_ptr
is not None
759 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
762 class _UserFilterComponent(_UserComponent
, _FilterComponent
):
763 _bt_as_not_self_specific_component_ptr
= staticmethod(
764 native_bt
.self_component_filter_as_component_filter
766 _bt_as_self_component_ptr
= staticmethod(
767 native_bt
.self_component_filter_as_self_component
771 def _output_ports(self
):
772 def get_output_port_count(self_ptr
):
773 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
774 return native_bt
.component_filter_get_output_port_count(ptr
)
776 return _ComponentPorts(
778 native_bt
.self_component_filter_borrow_output_port_by_name
,
779 native_bt
.self_component_filter_borrow_output_port_by_index
,
780 get_output_port_count
,
781 bt2
.port
._UserComponentOutputPort
,
785 def _input_ports(self
):
786 def get_input_port_count(self_ptr
):
787 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
788 return native_bt
.component_filter_get_input_port_count(ptr
)
790 return _ComponentPorts(
792 native_bt
.self_component_filter_borrow_input_port_by_name
,
793 native_bt
.self_component_filter_borrow_input_port_by_index
,
794 get_input_port_count
,
795 bt2
.port
._UserComponentInputPort
,
798 def _add_output_port(self
, name
, user_data
=None):
799 utils
._check
_str
(name
)
800 fn
= native_bt
.self_component_filter_add_output_port
801 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
802 utils
._handle
_func
_status
(
803 comp_status
, 'cannot add output port to filter component object'
806 return bt2
.port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
808 def _add_input_port(self
, name
, user_data
=None):
809 utils
._check
_str
(name
)
810 fn
= native_bt
.self_component_filter_add_input_port
811 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
812 utils
._handle
_func
_status
(
813 comp_status
, 'cannot add input port to filter component object'
816 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
819 class _UserSinkComponent(_UserComponent
, _SinkComponent
):
820 _bt_as_not_self_specific_component_ptr
= staticmethod(
821 native_bt
.self_component_sink_as_component_sink
823 _bt_as_self_component_ptr
= staticmethod(
824 native_bt
.self_component_sink_as_self_component
827 def _bt_graph_is_configured_from_native(self
):
828 self
._graph
_is
_configured
()
830 def _graph_is_configured(self
):
834 def _input_ports(self
):
835 def get_input_port_count(self_ptr
):
836 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
837 return native_bt
.component_sink_get_input_port_count(ptr
)
839 return _ComponentPorts(
841 native_bt
.self_component_sink_borrow_input_port_by_name
,
842 native_bt
.self_component_sink_borrow_input_port_by_index
,
843 get_input_port_count
,
844 bt2
.port
._UserComponentInputPort
,
847 def _add_input_port(self
, name
, user_data
=None):
848 utils
._check
_str
(name
)
849 fn
= native_bt
.self_component_sink_add_input_port
850 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
851 utils
._handle
_func
_status
(
852 comp_status
, 'cannot add input port to sink component object'
855 return bt2
.port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
857 def _create_input_port_message_iterator(self
, input_port
):
858 utils
._check
_type
(input_port
, bt2
.port
._UserComponentInputPort
)
860 msg_iter_ptr
= native_bt
.self_component_port_input_message_iterator_create_from_sink_component(
861 self
._bt
_ptr
, input_port
._ptr
864 if msg_iter_ptr
is None:
865 raise bt2
.CreationError('cannot create message iterator object')
867 return bt2
.message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
870 def _is_interrupted(self
):
871 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))