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 from bt2
import message_iterator
as bt2_message_iterator
25 import collections
.abc
26 from bt2
import value
as bt2_value
27 from bt2
import trace_class
as bt2_trace_class
28 from bt2
import clock_class
as bt2_clock_class
29 from bt2
import query_executor
as bt2_query_executor
31 from bt2
import port
as bt2_port
37 # This class wraps a component class pointer. This component class could
38 # have been created by Python code, but since we only have the pointer,
39 # we can only wrap it in a generic way and lose the original Python
42 # Subclasses must implement some methods that this base class uses:
44 # - _bt_as_component_class_ptr: static method, convert the passed component class
45 # pointer to a 'bt_component_class *'.
48 class _ComponentClass(object._SharedObject
):
51 ptr
= self
._bt
_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
._bt
_as
_component
_class
_ptr
(self
._ptr
)
59 return native_bt
.component_class_get_description(ptr
)
63 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
64 return native_bt
.component_class_get_help(ptr
)
66 def _bt_component_class_ptr(self
):
67 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
69 def __eq__(self
, other
):
70 if not isinstance(other
, _ComponentClass
):
72 if not issubclass(other
, _UserComponent
):
77 return self
.addr
== other
.addr
80 class _SourceComponentClass(_ComponentClass
):
81 _get_ref
= staticmethod(native_bt
.component_class_source_get_ref
)
82 _put_ref
= staticmethod(native_bt
.component_class_source_put_ref
)
83 _bt_as_component_class_ptr
= staticmethod(
84 native_bt
.component_class_source_as_component_class
88 class _FilterComponentClass(_ComponentClass
):
89 _get_ref
= staticmethod(native_bt
.component_class_filter_get_ref
)
90 _put_ref
= staticmethod(native_bt
.component_class_filter_put_ref
)
91 _bt_as_component_class_ptr
= staticmethod(
92 native_bt
.component_class_filter_as_component_class
96 class _SinkComponentClass(_ComponentClass
):
97 _get_ref
= staticmethod(native_bt
.component_class_sink_get_ref
)
98 _put_ref
= staticmethod(native_bt
.component_class_sink_put_ref
)
99 _bt_as_component_class_ptr
= staticmethod(
100 native_bt
.component_class_sink_as_component_class
104 class _PortIterator(collections
.abc
.Iterator
):
105 def __init__(self
, comp_ports
):
106 self
._comp
_ports
= comp_ports
110 if self
._at
== len(self
._comp
_ports
):
113 comp_ports
= self
._comp
_ports
114 comp_ptr
= comp_ports
._component
_ptr
116 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
117 assert port_ptr
is not None
119 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
120 assert name
is not None
126 class _ComponentPorts(collections
.abc
.Mapping
):
128 # component_ptr is a bt_component_source *, bt_component_filter * or
129 # bt_component_sink *. Its type must match the type expected by the
130 # functions passed as arguments.
135 borrow_port_ptr_by_name
,
136 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 # - _bt_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 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
174 # - _bt_as_component_ptr: static method, must return the passed specialized
175 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
181 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
182 name
= native_bt
.component_get_name(ptr
)
183 assert name
is not None
187 def logging_level(self
):
188 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
189 return native_bt
.component_get_logging_level(ptr
)
193 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
194 assert cc_ptr
is not None
195 return _create_component_class_from_ptr_and_get_ref(
196 cc_ptr
, self
._bt
_comp
_cls
_type
199 def __eq__(self
, other
):
200 if not hasattr(other
, 'addr'):
203 return self
.addr
== other
.addr
206 class _SourceComponent(_Component
):
207 _bt_borrow_component_class_ptr
= staticmethod(
208 native_bt
.component_source_borrow_class_const
210 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
211 _bt_as_component_class_ptr
= staticmethod(
212 native_bt
.component_class_source_as_component_class
214 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
217 class _FilterComponent(_Component
):
218 _bt_borrow_component_class_ptr
= staticmethod(
219 native_bt
.component_filter_borrow_class_const
221 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
222 _bt_as_component_class_ptr
= staticmethod(
223 native_bt
.component_class_filter_as_component_class
225 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
228 class _SinkComponent(_Component
):
229 _bt_borrow_component_class_ptr
= staticmethod(
230 native_bt
.component_sink_borrow_class_const
232 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
233 _bt_as_component_class_ptr
= staticmethod(
234 native_bt
.component_class_sink_as_component_class
236 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
239 # This is analogous to _SourceComponentClass, but for source
241 class _GenericSourceComponent(object._SharedObject
, _SourceComponent
):
242 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
243 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
246 def output_ports(self
):
247 return _ComponentPorts(
249 native_bt
.component_source_borrow_output_port_by_name_const
,
250 native_bt
.component_source_borrow_output_port_by_index_const
,
251 native_bt
.component_source_get_output_port_count
,
252 bt2_port
._OutputPort
,
256 # This is analogous to _FilterComponentClass, but for filter
258 class _GenericFilterComponent(object._SharedObject
, _FilterComponent
):
259 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
260 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
263 def output_ports(self
):
264 return _ComponentPorts(
266 native_bt
.component_filter_borrow_output_port_by_name_const
,
267 native_bt
.component_filter_borrow_output_port_by_index_const
,
268 native_bt
.component_filter_get_output_port_count
,
269 bt2_port
._OutputPort
,
273 def input_ports(self
):
274 return _ComponentPorts(
276 native_bt
.component_filter_borrow_input_port_by_name_const
,
277 native_bt
.component_filter_borrow_input_port_by_index_const
,
278 native_bt
.component_filter_get_input_port_count
,
283 # This is analogous to _SinkComponentClass, but for sink
285 class _GenericSinkComponent(object._SharedObject
, _SinkComponent
):
286 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
287 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
290 def input_ports(self
):
291 return _ComponentPorts(
293 native_bt
.component_sink_borrow_input_port_by_name_const
,
294 native_bt
.component_sink_borrow_input_port_by_index_const
,
295 native_bt
.component_sink_get_input_port_count
,
300 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
301 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponent
,
302 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponent
,
303 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponent
,
307 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
308 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClass
,
309 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClass
,
310 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClass
,
314 # Create a component Python object of type _GenericSourceComponent,
315 # _GenericFilterComponent or _GenericSinkComponent, depending on
318 # Steals the reference to ptr from the caller.
321 def _create_component_from_ptr(ptr
, comp_cls_type
):
322 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
325 # Same as the above, but acquire a new reference instead of stealing the
326 # reference from the caller.
329 def _create_component_from_ptr_and_get_ref(ptr
, comp_cls_type
):
330 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
332 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
335 # Create a component class Python object of type
336 # _SourceComponentClass, _FilterComponentClass or
337 # _SinkComponentClass, depending on comp_cls_type.
339 # Acquires a new reference to ptr.
342 def _create_component_class_from_ptr_and_get_ref(ptr
, comp_cls_type
):
343 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
345 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
348 def _trim_docstring(docstring
):
349 lines
= docstring
.expandtabs().splitlines()
352 for line
in lines
[1:]:
353 stripped
= line
.lstrip()
356 indent
= min(indent
, len(line
) - len(stripped
))
358 trimmed
= [lines
[0].strip()]
360 if indent
< sys
.maxsize
:
361 for line
in lines
[1:]:
362 trimmed
.append(line
[indent
:].rstrip())
364 while trimmed
and not trimmed
[-1]:
367 while trimmed
and not trimmed
[0]:
370 return '\n'.join(trimmed
)
373 # Metaclass for component classes defined by Python code.
375 # The Python user can create a standard Python class which inherits one
376 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
377 # or _UserSinkComponent). Those base classes set this class
378 # (_UserComponentType) as their metaclass.
380 # Once the body of a user-defined component class is executed, this
381 # metaclass is used to create and initialize the class. The metaclass
382 # creates a native BT component class of the corresponding type and
383 # associates it with this user-defined class. The metaclass also defines
384 # class methods like the `name` and `description` properties to match
385 # the _ComponentClass interface.
387 # The component class name which is used is either:
389 # * The `name` parameter of the class:
391 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
394 # * If the `name` class parameter is not used: the name of the class
395 # itself (`MySink` in the example above).
397 # The component class description which is used is the user-defined
400 # class MySink(bt2.SinkComponent):
401 # 'Description goes here'
404 # A user-defined Python component class can have an __init__() method
405 # which must at least accept the `params` and `name` arguments:
407 # def __init__(self, params, name, something_else):
410 # The user-defined component class can also have a _finalize() method
411 # (do NOT use __del__()) to be notified when the component object is
414 # User-defined source and filter component classes must use the
415 # `message_iterator_class` class parameter to specify the
416 # message iterator class to use for this component class:
418 # class MyMessageIterator(bt2._UserMessageIterator):
421 # class MySource(bt2._UserSourceComponent,
422 # message_iterator_class=MyMessageIterator):
425 # This message iterator class must inherit
426 # bt2._UserMessageIterator, and it must define the _get() and
427 # _next() methods. The message iterator class can also define an
428 # __init__() method: this method has access to the original Python
429 # component object which was used to create it as the `component`
430 # property. The message iterator class can also define a
431 # _finalize() method (again, do NOT use __del__()): this is called when
432 # the message iterator is (really) destroyed.
434 # When the user-defined class is destroyed, this metaclass's __del__()
435 # method is called: the native BT component class pointer is put (not
436 # needed anymore, at least not by any Python code since all references
437 # are dropped for __del__() to be called).
438 class _UserComponentType(type):
439 # __new__() is used to catch custom class parameters
440 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
441 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
443 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
444 super().__init
__(class_name
, bases
, namespace
)
446 # skip our own bases; they are never directly instantiated by the user
449 '_UserFilterSinkComponent',
450 '_UserSourceComponent',
451 '_UserFilterComponent',
452 '_UserSinkComponent',
455 if class_name
in own_bases
:
458 comp_cls_name
= kwargs
.get('name', class_name
)
459 utils
._check
_str
(comp_cls_name
)
460 comp_cls_descr
= None
463 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
464 utils
._check
_str
(cls
.__doc
__)
465 docstring
= _trim_docstring(cls
.__doc
__)
466 lines
= docstring
.splitlines()
469 comp_cls_descr
= lines
[0]
472 comp_cls_help
= '\n'.join(lines
[2:])
474 iter_cls
= kwargs
.get('message_iterator_class')
476 if _UserSourceComponent
in bases
:
477 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
478 cc_ptr
= native_bt
.bt2_component_class_source_create(
479 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
481 elif _UserFilterComponent
in bases
:
482 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
483 cc_ptr
= native_bt
.bt2_component_class_filter_create(
484 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
486 elif _UserSinkComponent
in bases
:
487 if not hasattr(cls
, '_user_consume'):
488 raise bt2
._IncompleteUserClass(
489 "cannot create component class '{}': missing a _user_consume() method".format(
494 cc_ptr
= native_bt
.bt2_component_class_sink_create(
495 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
498 raise bt2
._IncompleteUserClass(
499 "cannot find a known component class base in the bases of '{}'".format(
505 raise bt2
._MemoryError(
506 "cannot create component class '{}'".format(class_name
)
509 cls
._bt
_cc
_ptr
= cc_ptr
511 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
):
512 # create instance, not user-initialized yet
513 self
= cls
.__new
__(cls
)
515 # pointer to native self component object (weak/borrowed)
516 self
._bt
_ptr
= comp_ptr
518 # call user's __init__() method
519 if params_ptr
is not None:
520 params
= bt2_value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
524 self
.__init
__(params
)
527 def __call__(cls
, *args
, **kwargs
):
529 'cannot directly instantiate a user component from a Python module'
533 def _bt_set_iterator_class(cls
, iter_cls
):
535 raise bt2
._IncompleteUserClass(
536 "cannot create component class '{}': missing message iterator class".format(
541 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
542 raise bt2
._IncompleteUserClass(
543 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
548 if not hasattr(iter_cls
, '__next__'):
549 raise bt2
._IncompleteUserClass(
550 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
555 cls
._iter
_cls
= iter_cls
559 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
560 return native_bt
.component_class_get_name(ptr
)
563 def description(cls
):
564 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
565 return native_bt
.component_class_get_description(ptr
)
569 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
570 return native_bt
.component_class_get_help(ptr
)
574 return int(cls
._bt
_cc
_ptr
)
576 def _bt_query_from_native(cls
, priv_query_exec_ptr
, obj
, params_ptr
):
577 # this can raise, in which case the native call to
578 # bt_component_class_query() returns NULL
579 if params_ptr
is not None:
580 params
= bt2_value
._create
_from
_ptr
_and
_get
_ref
(params_ptr
)
584 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
587 # this can raise, but the native side checks the exception
588 results
= cls
._user
_query
(priv_query_exec
, obj
, params
)
590 # the private query executor is a private view on the query
591 # executor; it's not a shared object (the library does not
592 # offer an API to get/put a reference, just like "self"
593 # objects) from this query's point of view, so invalidate
594 # the object in case the user kept a reference and uses it
596 priv_query_exec
._invalidate
()
598 # this can raise, but the native side checks the exception
599 results
= bt2
.create_value(results
)
602 results_ptr
= native_bt
.value_null
604 results_ptr
= results
._ptr
606 # return new reference
607 bt2_value
._Value
._get
_ref
(results_ptr
)
608 return int(results_ptr
)
610 def _user_query(cls
, priv_query_executor
, obj
, params
):
611 raise bt2
.UnknownObject
613 def _bt_component_class_ptr(self
):
614 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
617 if hasattr(cls
, '_bt_cc_ptr'):
618 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
619 native_bt
.component_class_put_ref(cc_ptr
)
620 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
623 # Subclasses must provide these methods or property:
625 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
626 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
627 # specialized non-self pointer (e.g. 'bt_component_sink *').
628 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
629 # specialized component class (e.g. 'bt_component_class_sink *') of the
630 # passed specialized component pointer (e.g. 'bt_component_sink *').
631 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
635 class _UserComponent(metaclass
=_UserComponentType
):
638 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
639 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
640 name
= native_bt
.component_get_name(ptr
)
641 assert name
is not None
645 def logging_level(self
):
646 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
647 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
648 return native_bt
.component_get_logging_level(ptr
)
652 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
653 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
654 return _create_component_class_from_ptr_and_get_ref(
655 cc_ptr
, self
._bt
_comp
_cls
_type
660 return int(self
._bt
_ptr
)
662 def __init__(self
, params
=None):
665 def _user_finalize(self
):
668 def _user_port_connected(self
, port
, other_port
):
671 def _bt_port_connected_from_native(
672 self
, self_port_ptr
, self_port_type
, other_port_ptr
674 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
676 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
677 other_port_type
= native_bt
.PORT_TYPE_INPUT
679 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
681 other_port
= bt2_port
._create
_from
_ptr
_and
_get
_ref
(
682 other_port_ptr
, other_port_type
684 self
._user
_port
_connected
(port
, other_port
)
686 def _create_trace_class(self
, assigns_automatic_stream_class_id
=True):
687 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
688 tc_ptr
= native_bt
.trace_class_create(ptr
)
691 raise bt2
._MemoryError('could not create trace class')
693 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
694 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
698 def _create_clock_class(
705 origin_is_unix_epoch
=True,
708 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
709 cc_ptr
= native_bt
.clock_class_create(ptr
)
712 raise bt2
._MemoryError('could not create clock class')
714 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
716 if frequency
is not None:
717 cc
._frequency
= frequency
722 if description
is not None:
723 cc
._description
= description
725 if precision
is not None:
726 cc
._precision
= precision
728 if offset
is not None:
731 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
739 class _UserSourceComponent(_UserComponent
, _SourceComponent
):
740 _bt_as_not_self_specific_component_ptr
= staticmethod(
741 native_bt
.self_component_source_as_component_source
743 _bt_as_self_component_ptr
= staticmethod(
744 native_bt
.self_component_source_as_self_component
748 def _output_ports(self
):
749 def get_output_port_count(self_ptr
):
750 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
751 return native_bt
.component_source_get_output_port_count(ptr
)
753 return _ComponentPorts(
755 native_bt
.self_component_source_borrow_output_port_by_name
,
756 native_bt
.self_component_source_borrow_output_port_by_index
,
757 get_output_port_count
,
758 bt2_port
._UserComponentOutputPort
,
761 def _add_output_port(self
, name
, user_data
=None):
762 utils
._check
_str
(name
)
763 fn
= native_bt
.self_component_source_add_output_port
764 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
765 utils
._handle
_func
_status
(
766 comp_status
, 'cannot add output port to source component object'
768 assert self_port_ptr
is not None
769 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
772 class _UserFilterComponent(_UserComponent
, _FilterComponent
):
773 _bt_as_not_self_specific_component_ptr
= staticmethod(
774 native_bt
.self_component_filter_as_component_filter
776 _bt_as_self_component_ptr
= staticmethod(
777 native_bt
.self_component_filter_as_self_component
781 def _output_ports(self
):
782 def get_output_port_count(self_ptr
):
783 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
784 return native_bt
.component_filter_get_output_port_count(ptr
)
786 return _ComponentPorts(
788 native_bt
.self_component_filter_borrow_output_port_by_name
,
789 native_bt
.self_component_filter_borrow_output_port_by_index
,
790 get_output_port_count
,
791 bt2_port
._UserComponentOutputPort
,
795 def _input_ports(self
):
796 def get_input_port_count(self_ptr
):
797 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
798 return native_bt
.component_filter_get_input_port_count(ptr
)
800 return _ComponentPorts(
802 native_bt
.self_component_filter_borrow_input_port_by_name
,
803 native_bt
.self_component_filter_borrow_input_port_by_index
,
804 get_input_port_count
,
805 bt2_port
._UserComponentInputPort
,
808 def _add_output_port(self
, name
, user_data
=None):
809 utils
._check
_str
(name
)
810 fn
= native_bt
.self_component_filter_add_output_port
811 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
812 utils
._handle
_func
_status
(
813 comp_status
, 'cannot add output port to filter component object'
816 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
(self_port_ptr
)
818 def _add_input_port(self
, name
, user_data
=None):
819 utils
._check
_str
(name
)
820 fn
= native_bt
.self_component_filter_add_input_port
821 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
822 utils
._handle
_func
_status
(
823 comp_status
, 'cannot add input port to filter component object'
826 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
829 class _UserSinkComponent(_UserComponent
, _SinkComponent
):
830 _bt_as_not_self_specific_component_ptr
= staticmethod(
831 native_bt
.self_component_sink_as_component_sink
833 _bt_as_self_component_ptr
= staticmethod(
834 native_bt
.self_component_sink_as_self_component
837 def _bt_graph_is_configured_from_native(self
):
838 self
._user
_graph
_is
_configured
()
840 def _user_graph_is_configured(self
):
844 def _input_ports(self
):
845 def get_input_port_count(self_ptr
):
846 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
847 return native_bt
.component_sink_get_input_port_count(ptr
)
849 return _ComponentPorts(
851 native_bt
.self_component_sink_borrow_input_port_by_name
,
852 native_bt
.self_component_sink_borrow_input_port_by_index
,
853 get_input_port_count
,
854 bt2_port
._UserComponentInputPort
,
857 def _add_input_port(self
, name
, user_data
=None):
858 utils
._check
_str
(name
)
859 fn
= native_bt
.self_component_sink_add_input_port
860 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
861 utils
._handle
_func
_status
(
862 comp_status
, 'cannot add input port to sink component object'
865 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
(self_port_ptr
)
867 def _create_input_port_message_iterator(self
, input_port
):
868 utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
870 msg_iter_ptr
= native_bt
.self_component_port_input_message_iterator_create_from_sink_component(
871 self
._bt
_ptr
, input_port
._ptr
874 if msg_iter_ptr
is None:
875 raise bt2
.CreationError('cannot create message iterator object')
877 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
880 def _is_interrupted(self
):
881 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))