1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
8 from bt2
import port
as bt2_port
9 from bt2
import error
as bt2_error
10 from bt2
import utils
as bt2_utils
11 from bt2
import value
as bt2_value
12 from bt2
import object as bt2_object
13 from bt2
import native_bt
14 from bt2
import clock_class
as bt2_clock_class
15 from bt2
import trace_class
as bt2_trace_class
16 from bt2
import query_executor
as bt2_query_executor
17 from bt2
import message_iterator
as bt2_message_iterator
18 from bt2
import integer_range_set
as bt2_integer_range_set
21 class _IncompleteUserClass(Exception):
25 # This class wraps a component class pointer. This component class could
26 # have been created by Python code, but since we only have the pointer,
27 # we can only wrap it in a generic way and lose the original Python
30 # Subclasses must implement some methods that this base class uses:
32 # - _bt_as_component_class_ptr: static method, convert the passed component class
33 # pointer to a 'bt_component_class *'.
36 class _ComponentClassConst(bt2_object
._SharedObject
):
39 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
40 name
= native_bt
.component_class_get_name(ptr
)
41 assert name
is not None
45 def description(self
):
46 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
47 return native_bt
.component_class_get_description(ptr
)
51 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
52 return native_bt
.component_class_get_help(ptr
)
54 def _bt_component_class_ptr(self
):
55 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
57 def __eq__(self
, other
):
58 if not isinstance(other
, _ComponentClassConst
):
60 if not issubclass(other
, _UserComponent
):
65 return self
.addr
== other
.addr
68 class _SourceComponentClassConst(_ComponentClassConst
):
71 native_bt
.component_class_source_get_ref(ptr
)
75 native_bt
.component_class_source_put_ref(ptr
)
77 _bt_as_component_class_ptr
= staticmethod(
78 native_bt
.component_class_source_as_component_class
82 class _FilterComponentClassConst(_ComponentClassConst
):
85 native_bt
.component_class_filter_get_ref(ptr
)
89 native_bt
.component_class_filter_put_ref(ptr
)
91 _bt_as_component_class_ptr
= staticmethod(
92 native_bt
.component_class_filter_as_component_class
96 class _SinkComponentClassConst(_ComponentClassConst
):
99 native_bt
.component_class_sink_get_ref(ptr
)
103 native_bt
.component_class_sink_put_ref(ptr
)
105 _bt_as_component_class_ptr
= staticmethod(
106 native_bt
.component_class_sink_as_component_class
110 class _PortIterator(collections
.abc
.Iterator
):
111 def __init__(self
, comp_ports
):
112 self
._comp
_ports
= comp_ports
116 if self
._at
== len(self
._comp
_ports
):
119 comp_ports
= self
._comp
_ports
120 comp_ptr
= comp_ports
._component
_ptr
122 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
123 assert port_ptr
is not None
125 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
126 assert name
is not None
132 class _ComponentPorts(collections
.abc
.Mapping
):
133 # component_ptr is a bt_component_source *, bt_component_filter * or
134 # bt_component_sink *. Its type must match the type expected by the
135 # functions passed as arguments.
140 borrow_port_ptr_by_name
,
141 borrow_port_ptr_at_index
,
145 self
._component
_ptr
= component_ptr
146 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
147 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
148 self
._get
_port
_count
= get_port_count
149 self
._port
_pycls
= port_pycls
151 def __getitem__(self
, key
):
152 bt2_utils
._check
_str
(key
)
153 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
158 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
161 count
= self
._get
_port
_count
(self
._component
_ptr
)
166 return _PortIterator(self
)
169 # This class holds the methods which are common to both generic
170 # component objects and Python user component objects.
172 # Subclasses must provide these methods or property:
174 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
175 # specialized component class (e.g. 'bt_component_class_sink *') of the
176 # passed specialized component pointer (e.g. 'bt_component_sink *').
177 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
179 # - _bt_as_component_ptr: static method, must return the passed specialized
180 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
183 class _ComponentConst
:
186 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
187 name
= native_bt
.component_get_name(ptr
)
188 assert name
is not None
192 def logging_level(self
):
193 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
194 return native_bt
.component_get_logging_level(ptr
)
198 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
199 assert cc_ptr
is not None
200 return _create_component_class_from_const_ptr_and_get_ref(
201 cc_ptr
, self
._bt
_comp
_cls
_type
204 def __eq__(self
, other
):
205 if not hasattr(other
, "addr"):
208 return self
.addr
== other
.addr
211 class _SourceComponentConst(_ComponentConst
):
212 _bt_borrow_component_class_ptr
= staticmethod(
213 native_bt
.component_source_borrow_class_const
215 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
216 _bt_as_component_class_ptr
= staticmethod(
217 native_bt
.component_class_source_as_component_class
219 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
222 class _FilterComponentConst(_ComponentConst
):
223 _bt_borrow_component_class_ptr
= staticmethod(
224 native_bt
.component_filter_borrow_class_const
226 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
227 _bt_as_component_class_ptr
= staticmethod(
228 native_bt
.component_class_filter_as_component_class
230 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
233 class _SinkComponentConst(_ComponentConst
):
234 _bt_borrow_component_class_ptr
= staticmethod(
235 native_bt
.component_sink_borrow_class_const
237 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
238 _bt_as_component_class_ptr
= staticmethod(
239 native_bt
.component_class_sink_as_component_class
241 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
244 # This is analogous to _SourceComponentClassConst, but for source
246 class _GenericSourceComponentConst(bt2_object
._SharedObject
, _SourceComponentConst
):
249 native_bt
.component_source_get_ref(ptr
)
253 native_bt
.component_source_put_ref(ptr
)
256 def output_ports(self
):
257 return _ComponentPorts(
259 native_bt
.component_source_borrow_output_port_by_name_const
,
260 native_bt
.component_source_borrow_output_port_by_index_const
,
261 native_bt
.component_source_get_output_port_count
,
262 bt2_port
._OutputPortConst
,
266 # This is analogous to _FilterComponentClassConst, but for filter
268 class _GenericFilterComponentConst(bt2_object
._SharedObject
, _FilterComponentConst
):
271 native_bt
.component_filter_get_ref(ptr
)
275 native_bt
.component_filter_put_ref(ptr
)
278 def output_ports(self
):
279 return _ComponentPorts(
281 native_bt
.component_filter_borrow_output_port_by_name_const
,
282 native_bt
.component_filter_borrow_output_port_by_index_const
,
283 native_bt
.component_filter_get_output_port_count
,
284 bt2_port
._OutputPortConst
,
288 def input_ports(self
):
289 return _ComponentPorts(
291 native_bt
.component_filter_borrow_input_port_by_name_const
,
292 native_bt
.component_filter_borrow_input_port_by_index_const
,
293 native_bt
.component_filter_get_input_port_count
,
294 bt2_port
._InputPortConst
,
298 # This is analogous to _SinkComponentClassConst, but for sink
300 class _GenericSinkComponentConst(bt2_object
._SharedObject
, _SinkComponentConst
):
303 native_bt
.component_sink_get_ref(ptr
)
307 native_bt
.component_sink_put_ref(ptr
)
310 def input_ports(self
):
311 return _ComponentPorts(
313 native_bt
.component_sink_borrow_input_port_by_name_const
,
314 native_bt
.component_sink_borrow_input_port_by_index_const
,
315 native_bt
.component_sink_get_input_port_count
,
316 bt2_port
._InputPortConst
,
320 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
321 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentConst
,
322 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentConst
,
323 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentConst
,
327 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
328 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClassConst
,
329 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClassConst
,
330 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClassConst
,
334 # Create a component Python object of type _GenericSourceComponentConst,
335 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
338 # Steals the reference to ptr from the caller.
341 def _create_component_from_const_ptr(ptr
, comp_cls_type
):
342 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
345 # Same as the above, but acquire a new reference instead of stealing the
346 # reference from the caller.
349 def _create_component_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
350 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
352 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
355 # Create a component class Python object of type
356 # _SourceComponentClassConst, _FilterComponentClassConst or
357 # _SinkComponentClassConst, depending on comp_cls_type.
359 # Acquires a new reference to ptr.
362 def _create_component_class_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
363 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
365 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
368 def _trim_docstring(docstring
):
369 lines
= docstring
.expandtabs().splitlines()
377 for line
in lines
[1:]:
378 stripped
= line
.lstrip()
381 indent
= min(indent
, len(line
) - len(stripped
))
383 trimmed
= [lines
[0].strip()]
385 if indent
< sys
.maxsize
and len(lines
) > 1:
386 for line
in lines
[1:]:
387 trimmed
.append(line
[indent
:].rstrip())
389 while trimmed
and not trimmed
[-1]:
392 while trimmed
and not trimmed
[0]:
395 return "\n".join(trimmed
)
398 # Metaclass for component classes defined by Python code.
400 # The Python user can create a standard Python class which inherits one
401 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
402 # or _UserSinkComponent). Those base classes set this class
403 # (_UserComponentType) as their metaclass.
405 # Once the body of a user-defined component class is executed, this
406 # metaclass is used to create and initialize the class. The metaclass
407 # creates a native BT component class of the corresponding type and
408 # associates it with this user-defined class. The metaclass also defines
409 # class methods like the `name` and `description` properties to match
410 # the _ComponentClassConst interface.
412 # The component class name which is used is either:
414 # * The `name` parameter of the class:
416 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
419 # * If the `name` class parameter is not used: the name of the class
420 # itself (`MySink` in the example above).
422 # The component class description which is used is the user-defined
425 # class MySink(bt2.SinkComponent):
426 # 'Description goes here'
429 # A user-defined Python component class can have an __init__() method
430 # which must accept the following parameters:
432 # def __init__(self, config, params, obj):
435 # The value of the `obj` parameter is what was passed as the `obj`
436 # parameter if the component was instantiated from Python with
437 # Graph.add_component(). If the component was not instantiated from
438 # Python, is is always `None`.
440 # The user-defined component class can also have a _user_finalize()
441 # method (do NOT use __del__()) to be notified when the component object
444 # User-defined source and filter component classes must use the
445 # `message_iterator_class` class parameter to specify the
446 # message iterator class to use for this component class:
448 # class MyMessageIterator(bt2._UserMessageIterator):
451 # class MySource(bt2._UserSourceComponent,
452 # message_iterator_class=MyMessageIterator):
455 # This message iterator class must inherit bt2._UserMessageIterator.
456 # It can implement the __init__() method, which must accept the
457 # following parameters:
459 # def __init__(self, config, port):
462 # It can also implement the __next__() and _user_finalize() methods
463 # (again, do NOT use __del__()), which don't accept any parameters
466 # When the user-defined class is destroyed, this metaclass's __del__()
467 # method is called: the native BT component class pointer is put (not
468 # needed anymore, at least not by any Python code since all references
469 # are dropped for __del__() to be called).
470 class _UserComponentType(type):
471 # __new__() is used to catch custom class parameters
472 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
473 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
475 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
476 super().__init
__(class_name
, bases
, namespace
)
478 # skip our own bases; they are never directly instantiated by the user
481 "_UserFilterSinkComponent",
482 "_UserSourceComponent",
483 "_UserFilterComponent",
484 "_UserSinkComponent",
487 if class_name
in own_bases
:
490 comp_cls_name
= kwargs
.get("name", class_name
)
491 bt2_utils
._check
_str
(comp_cls_name
)
492 comp_cls_descr
= None
495 if hasattr(cls
, "__doc__") and cls
.__doc
__ is not None:
496 bt2_utils
._check
_str
(cls
.__doc
__)
497 docstring
= _trim_docstring(cls
.__doc
__)
498 lines
= docstring
.splitlines()
501 comp_cls_descr
= lines
[0]
504 comp_cls_help
= "\n".join(lines
[2:])
506 iter_cls
= kwargs
.get("message_iterator_class")
508 if _UserSourceComponent
in bases
:
509 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
510 cc_ptr
= native_bt
.bt2_component_class_source_create(
511 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
513 elif _UserFilterComponent
in bases
:
514 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
515 cc_ptr
= native_bt
.bt2_component_class_filter_create(
516 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
518 elif _UserSinkComponent
in bases
:
519 if not hasattr(cls
, "_user_consume"):
520 raise _IncompleteUserClass(
521 "cannot create component class '{}': missing a _user_consume() method".format(
526 cc_ptr
= native_bt
.bt2_component_class_sink_create(
527 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
530 raise _IncompleteUserClass(
531 "cannot find a known component class base in the bases of '{}'".format(
537 raise bt2_error
._MemoryError
(
538 "cannot create component class '{}'".format(class_name
)
541 cls
._bt
_cc
_ptr
= cc_ptr
543 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
, obj
):
544 # create instance, not user-initialized yet
545 self
= cls
.__new
__(cls
)
548 config
= cls
._config
_pycls
()
550 # pointer to native self component object (weak/borrowed)
551 self
._bt
_ptr
= comp_ptr
553 # call user's __init__() method
554 if params_ptr
is not None:
555 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
559 self
.__init
__(config
, params
, obj
)
562 def __call__(cls
, *args
, **kwargs
):
564 "cannot directly instantiate a user component from a Python module"
568 def _bt_set_iterator_class(cls
, iter_cls
):
570 raise _IncompleteUserClass(
571 "cannot create component class '{}': missing message iterator class".format(
576 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
577 raise _IncompleteUserClass(
578 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
583 if not hasattr(iter_cls
, "__next__"):
584 raise _IncompleteUserClass(
585 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
590 if hasattr(iter_cls
, "_user_can_seek_ns_from_origin") and not hasattr(
591 iter_cls
, "_user_seek_ns_from_origin"
593 raise _IncompleteUserClass(
594 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
599 if hasattr(iter_cls
, "_user_can_seek_beginning") and not hasattr(
600 iter_cls
, "_user_seek_beginning"
602 raise _IncompleteUserClass(
603 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
608 cls
._iter
_cls
= iter_cls
612 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
613 return native_bt
.component_class_get_name(ptr
)
616 def description(cls
):
617 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
618 return native_bt
.component_class_get_description(ptr
)
622 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
623 return native_bt
.component_class_get_help(ptr
)
627 return int(cls
._bt
_cc
_ptr
)
629 def _bt_get_supported_mip_versions_from_native(cls
, params_ptr
, obj
, log_level
):
630 # this can raise, but the native side checks the exception
631 if params_ptr
is not None:
632 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
636 # this can raise, but the native side checks the exception
637 range_set
= cls
._user
_get
_supported
_mip
_versions
(params
, obj
, log_level
)
639 if type(range_set
) is not bt2_integer_range_set
.UnsignedIntegerRangeSet
:
640 # this can raise, but the native side checks the exception
641 range_set
= bt2_integer_range_set
.UnsignedIntegerRangeSet(range_set
)
643 # return new reference
644 range_set
._get
_ref
(range_set
._ptr
)
645 return int(range_set
._ptr
)
647 def _user_get_supported_mip_versions(cls
, params
, obj
, log_level
):
650 def _bt_query_from_native(
651 cls
, priv_query_exec_ptr
, object_name
, params_ptr
, method_obj
653 # this can raise, but the native side checks the exception
654 if params_ptr
is not None:
655 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
659 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
662 # this can raise, but the native side checks the exception
663 results
= cls
._user
_query
(priv_query_exec
, object_name
, params
, method_obj
)
665 # the private query executor is a private view on the query
666 # executor; it's not a shared object (the library does not
667 # offer an API to get/put a reference, just like "self"
668 # objects) from this query's point of view, so invalidate
669 # the object in case the user kept a reference and uses it
671 priv_query_exec
._invalidate
()
673 # this can raise, but the native side checks the exception
674 results
= bt2_value
.create_value(results
)
677 results_ptr
= native_bt
.value_null
679 results_ptr
= results
._ptr
681 # return new reference
682 bt2_value
._Value
._get
_ref
(results_ptr
)
683 return int(results_ptr
)
685 def _user_query(cls
, priv_query_executor
, object_name
, params
, method_obj
):
686 raise bt2_utils
.UnknownObject
688 def _bt_component_class_ptr(self
):
689 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
692 if hasattr(cls
, "_bt_cc_ptr"):
693 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
694 native_bt
.component_class_put_ref(cc_ptr
)
695 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
698 # Configuration objects for components.
700 # These are passed in __init__ to allow components to change some configuration
701 # parameters during initialization and not after. As you can see, they are not
702 # used at the moment, but are there in case we want to add such parameters.
705 class _UserComponentConfiguration
:
709 class _UserSourceComponentConfiguration(_UserComponentConfiguration
):
713 class _UserFilterComponentConfiguration(_UserComponentConfiguration
):
717 class _UserSinkComponentConfiguration(_UserComponentConfiguration
):
721 # Subclasses must provide these methods or property:
723 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
724 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
725 # specialized non-self pointer (e.g. 'bt_component_sink *').
726 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
727 # specialized component class (e.g. 'bt_component_class_sink *') of the
728 # passed specialized component pointer (e.g. 'bt_component_sink *').
729 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
733 class _UserComponent(metaclass
=_UserComponentType
):
736 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
737 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
738 name
= native_bt
.component_get_name(ptr
)
739 assert name
is not None
743 def logging_level(self
):
744 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
745 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
746 return native_bt
.component_get_logging_level(ptr
)
750 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
751 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
752 return _create_component_class_from_const_ptr_and_get_ref(
753 cc_ptr
, self
._bt
_comp
_cls
_type
758 return int(self
._bt
_ptr
)
761 def _graph_mip_version(self
):
762 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
763 return native_bt
.self_component_get_graph_mip_version(ptr
)
765 def __init__(self
, config
, params
, obj
):
768 def _user_finalize(self
):
771 def _user_port_connected(self
, port
, other_port
):
774 def _bt_port_connected_from_native(
775 self
, self_port_ptr
, self_port_type
, other_port_ptr
777 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
779 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
780 other_port_type
= native_bt
.PORT_TYPE_INPUT
782 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
784 other_port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(
785 other_port_ptr
, other_port_type
787 self
._user
_port
_connected
(port
, other_port
)
789 def _create_trace_class(
790 self
, user_attributes
=None, assigns_automatic_stream_class_id
=True
792 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
793 tc_ptr
= native_bt
.trace_class_create(ptr
)
796 raise bt2_error
._MemoryError
("could not create trace class")
798 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
799 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
801 if user_attributes
is not None:
802 tc
._user
_attributes
= user_attributes
806 def _create_clock_class(
810 user_attributes
=None,
814 origin_is_unix_epoch
=True,
817 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
818 cc_ptr
= native_bt
.clock_class_create(ptr
)
821 raise bt2_error
._MemoryError
("could not create clock class")
823 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
825 if frequency
is not None:
826 cc
._frequency
= frequency
831 if user_attributes
is not None:
832 cc
._user
_attributes
= user_attributes
834 if description
is not None:
835 cc
._description
= description
837 if precision
is not None:
838 cc
._precision
= precision
840 if offset
is not None:
843 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
851 class _UserSourceComponent(_UserComponent
, _SourceComponentConst
):
852 _bt_as_not_self_specific_component_ptr
= staticmethod(
853 native_bt
.self_component_source_as_component_source
855 _bt_as_self_component_ptr
= staticmethod(
856 native_bt
.self_component_source_as_self_component
858 _config_pycls
= _UserSourceComponentConfiguration
861 def _output_ports(self
):
862 def get_output_port_count(self_ptr
):
863 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
864 return native_bt
.component_source_get_output_port_count(ptr
)
866 return _ComponentPorts(
868 native_bt
.self_component_source_borrow_output_port_by_name
,
869 native_bt
.self_component_source_borrow_output_port_by_index
,
870 get_output_port_count
,
871 bt2_port
._UserComponentOutputPort
,
874 def _add_output_port(self
, name
, user_data
=None):
875 bt2_utils
._check
_str
(name
)
877 if name
in self
._output
_ports
:
879 "source component `{}` already contains an output port named `{}`".format(
884 fn
= native_bt
.self_component_source_add_output_port
885 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
886 bt2_utils
._handle
_func
_status
(
887 comp_status
, "cannot add output port to source component object"
889 assert self_port_ptr
is not None
890 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
895 class _UserFilterComponent(_UserComponent
, _FilterComponentConst
):
896 _bt_as_not_self_specific_component_ptr
= staticmethod(
897 native_bt
.self_component_filter_as_component_filter
899 _bt_as_self_component_ptr
= staticmethod(
900 native_bt
.self_component_filter_as_self_component
902 _config_pycls
= _UserFilterComponentConfiguration
905 def _output_ports(self
):
906 def get_output_port_count(self_ptr
):
907 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
908 return native_bt
.component_filter_get_output_port_count(ptr
)
910 return _ComponentPorts(
912 native_bt
.self_component_filter_borrow_output_port_by_name
,
913 native_bt
.self_component_filter_borrow_output_port_by_index
,
914 get_output_port_count
,
915 bt2_port
._UserComponentOutputPort
,
919 def _input_ports(self
):
920 def get_input_port_count(self_ptr
):
921 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
922 return native_bt
.component_filter_get_input_port_count(ptr
)
924 return _ComponentPorts(
926 native_bt
.self_component_filter_borrow_input_port_by_name
,
927 native_bt
.self_component_filter_borrow_input_port_by_index
,
928 get_input_port_count
,
929 bt2_port
._UserComponentInputPort
,
932 def _add_output_port(self
, name
, user_data
=None):
933 bt2_utils
._check
_str
(name
)
935 if name
in self
._output
_ports
:
937 "filter component `{}` already contains an output port named `{}`".format(
942 fn
= native_bt
.self_component_filter_add_output_port
943 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
944 bt2_utils
._handle
_func
_status
(
945 comp_status
, "cannot add output port to filter component object"
948 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
952 def _add_input_port(self
, name
, user_data
=None):
953 bt2_utils
._check
_str
(name
)
955 if name
in self
._input
_ports
:
957 "filter component `{}` already contains an input port named `{}`".format(
962 fn
= native_bt
.self_component_filter_add_input_port
963 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
964 bt2_utils
._handle
_func
_status
(
965 comp_status
, "cannot add input port to filter component object"
968 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
973 class _UserSinkComponent(_UserComponent
, _SinkComponentConst
):
974 _bt_as_not_self_specific_component_ptr
= staticmethod(
975 native_bt
.self_component_sink_as_component_sink
977 _bt_as_self_component_ptr
= staticmethod(
978 native_bt
.self_component_sink_as_self_component
980 _config_pycls
= _UserSinkComponentConfiguration
982 def _bt_graph_is_configured_from_native(self
):
983 self
._user
_graph
_is
_configured
()
985 def _user_graph_is_configured(self
):
989 def _input_ports(self
):
990 def get_input_port_count(self_ptr
):
991 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
992 return native_bt
.component_sink_get_input_port_count(ptr
)
994 return _ComponentPorts(
996 native_bt
.self_component_sink_borrow_input_port_by_name
,
997 native_bt
.self_component_sink_borrow_input_port_by_index
,
998 get_input_port_count
,
999 bt2_port
._UserComponentInputPort
,
1002 def _add_input_port(self
, name
, user_data
=None):
1003 bt2_utils
._check
_str
(name
)
1005 if name
in self
._input
_ports
:
1007 "sink component `{}` already contains an input port named `{}`".format(
1012 fn
= native_bt
.self_component_sink_add_input_port
1013 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
1014 bt2_utils
._handle
_func
_status
(
1015 comp_status
, "cannot add input port to sink component object"
1017 assert self_port_ptr
1018 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
1022 def _create_message_iterator(self
, input_port
):
1023 bt2_utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
1025 if not input_port
.is_connected
:
1026 raise ValueError("input port is not connected")
1031 ) = native_bt
.bt2_message_iterator_create_from_sink_component(
1032 self
._bt
_ptr
, input_port
._ptr
1034 bt2_utils
._handle
_func
_status
(status
, "cannot create message iterator object")
1035 assert msg_iter_ptr
is not None
1037 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
1040 def _is_interrupted(self
):
1041 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))