1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
6 from bt2
import object as bt2_object
7 from bt2
import utils
as bt2_utils
8 from bt2
import message_iterator
as bt2_message_iterator
10 from bt2
import value
as bt2_value
11 from bt2
import trace_class
as bt2_trace_class
12 from bt2
import clock_class
as bt2_clock_class
13 from bt2
import query_executor
as bt2_query_executor
14 from bt2
import port
as bt2_port
19 # This class wraps a component class pointer. This component class could
20 # have been created by Python code, but since we only have the pointer,
21 # we can only wrap it in a generic way and lose the original Python
24 # Subclasses must implement some methods that this base class uses:
26 # - _bt_as_component_class_ptr: static method, convert the passed component class
27 # pointer to a 'bt_component_class *'.
30 class _ComponentClassConst(bt2_object
._SharedObject
):
33 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
34 name
= native_bt
.component_class_get_name(ptr
)
35 assert name
is not None
39 def description(self
):
40 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
41 return native_bt
.component_class_get_description(ptr
)
45 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
46 return native_bt
.component_class_get_help(ptr
)
48 def _bt_component_class_ptr(self
):
49 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
51 def __eq__(self
, other
):
52 if not isinstance(other
, _ComponentClassConst
):
54 if not issubclass(other
, _UserComponent
):
59 return self
.addr
== other
.addr
62 class _SourceComponentClassConst(_ComponentClassConst
):
65 native_bt
.component_class_source_get_ref(ptr
)
69 native_bt
.component_class_source_put_ref(ptr
)
71 _bt_as_component_class_ptr
= staticmethod(
72 native_bt
.component_class_source_as_component_class
76 class _FilterComponentClassConst(_ComponentClassConst
):
79 native_bt
.component_class_filter_get_ref(ptr
)
83 native_bt
.component_class_filter_put_ref(ptr
)
85 _bt_as_component_class_ptr
= staticmethod(
86 native_bt
.component_class_filter_as_component_class
90 class _SinkComponentClassConst(_ComponentClassConst
):
93 native_bt
.component_class_sink_get_ref(ptr
)
97 native_bt
.component_class_sink_put_ref(ptr
)
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
):
127 # component_ptr is a bt_component_source *, bt_component_filter * or
128 # bt_component_sink *. Its type must match the type expected by the
129 # functions passed as arguments.
134 borrow_port_ptr_by_name
,
135 borrow_port_ptr_at_index
,
139 self
._component
_ptr
= component_ptr
140 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
141 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
142 self
._get
_port
_count
= get_port_count
143 self
._port
_pycls
= port_pycls
145 def __getitem__(self
, key
):
146 bt2_utils
._check
_str
(key
)
147 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
152 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
155 count
= self
._get
_port
_count
(self
._component
_ptr
)
160 return _PortIterator(self
)
163 # This class holds the methods which are common to both generic
164 # component objects and Python user component objects.
166 # Subclasses must provide these methods or property:
168 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
169 # specialized component class (e.g. 'bt_component_class_sink *') of the
170 # passed specialized component pointer (e.g. 'bt_component_sink *').
171 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
173 # - _bt_as_component_ptr: static method, must return the passed specialized
174 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
177 class _ComponentConst
:
180 ptr
= self
._bt
_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
._bt
_as
_component
_ptr
(self
._ptr
)
188 return native_bt
.component_get_logging_level(ptr
)
192 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
193 assert cc_ptr
is not None
194 return _create_component_class_from_const_ptr_and_get_ref(
195 cc_ptr
, self
._bt
_comp
_cls
_type
198 def __eq__(self
, other
):
199 if not hasattr(other
, "addr"):
202 return self
.addr
== other
.addr
205 class _SourceComponentConst(_ComponentConst
):
206 _bt_borrow_component_class_ptr
= staticmethod(
207 native_bt
.component_source_borrow_class_const
209 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
210 _bt_as_component_class_ptr
= staticmethod(
211 native_bt
.component_class_source_as_component_class
213 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
216 class _FilterComponentConst(_ComponentConst
):
217 _bt_borrow_component_class_ptr
= staticmethod(
218 native_bt
.component_filter_borrow_class_const
220 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
221 _bt_as_component_class_ptr
= staticmethod(
222 native_bt
.component_class_filter_as_component_class
224 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
227 class _SinkComponentConst(_ComponentConst
):
228 _bt_borrow_component_class_ptr
= staticmethod(
229 native_bt
.component_sink_borrow_class_const
231 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
232 _bt_as_component_class_ptr
= staticmethod(
233 native_bt
.component_class_sink_as_component_class
235 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
238 # This is analogous to _SourceComponentClassConst, but for source
240 class _GenericSourceComponentConst(bt2_object
._SharedObject
, _SourceComponentConst
):
243 native_bt
.component_source_get_ref(ptr
)
247 native_bt
.component_source_put_ref(ptr
)
250 def output_ports(self
):
251 return _ComponentPorts(
253 native_bt
.component_source_borrow_output_port_by_name_const
,
254 native_bt
.component_source_borrow_output_port_by_index_const
,
255 native_bt
.component_source_get_output_port_count
,
256 bt2_port
._OutputPortConst
,
260 # This is analogous to _FilterComponentClassConst, but for filter
262 class _GenericFilterComponentConst(bt2_object
._SharedObject
, _FilterComponentConst
):
265 native_bt
.component_filter_get_ref(ptr
)
269 native_bt
.component_filter_put_ref(ptr
)
272 def output_ports(self
):
273 return _ComponentPorts(
275 native_bt
.component_filter_borrow_output_port_by_name_const
,
276 native_bt
.component_filter_borrow_output_port_by_index_const
,
277 native_bt
.component_filter_get_output_port_count
,
278 bt2_port
._OutputPortConst
,
282 def input_ports(self
):
283 return _ComponentPorts(
285 native_bt
.component_filter_borrow_input_port_by_name_const
,
286 native_bt
.component_filter_borrow_input_port_by_index_const
,
287 native_bt
.component_filter_get_input_port_count
,
288 bt2_port
._InputPortConst
,
292 # This is analogous to _SinkComponentClassConst, but for sink
294 class _GenericSinkComponentConst(bt2_object
._SharedObject
, _SinkComponentConst
):
297 native_bt
.component_sink_get_ref(ptr
)
301 native_bt
.component_sink_put_ref(ptr
)
304 def input_ports(self
):
305 return _ComponentPorts(
307 native_bt
.component_sink_borrow_input_port_by_name_const
,
308 native_bt
.component_sink_borrow_input_port_by_index_const
,
309 native_bt
.component_sink_get_input_port_count
,
310 bt2_port
._InputPortConst
,
314 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
315 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentConst
,
316 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentConst
,
317 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentConst
,
321 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
322 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClassConst
,
323 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClassConst
,
324 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClassConst
,
328 # Create a component Python object of type _GenericSourceComponentConst,
329 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
332 # Steals the reference to ptr from the caller.
335 def _create_component_from_const_ptr(ptr
, comp_cls_type
):
336 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
339 # Same as the above, but acquire a new reference instead of stealing the
340 # reference from the caller.
343 def _create_component_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
344 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
346 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
349 # Create a component class Python object of type
350 # _SourceComponentClassConst, _FilterComponentClassConst or
351 # _SinkComponentClassConst, depending on comp_cls_type.
353 # Acquires a new reference to ptr.
356 def _create_component_class_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
357 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
359 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
362 def _trim_docstring(docstring
):
363 lines
= docstring
.expandtabs().splitlines()
371 for line
in lines
[1:]:
372 stripped
= line
.lstrip()
375 indent
= min(indent
, len(line
) - len(stripped
))
377 trimmed
= [lines
[0].strip()]
379 if indent
< sys
.maxsize
and len(lines
) > 1:
380 for line
in lines
[1:]:
381 trimmed
.append(line
[indent
:].rstrip())
383 while trimmed
and not trimmed
[-1]:
386 while trimmed
and not trimmed
[0]:
389 return "\n".join(trimmed
)
392 # Metaclass for component classes defined by Python code.
394 # The Python user can create a standard Python class which inherits one
395 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
396 # or _UserSinkComponent). Those base classes set this class
397 # (_UserComponentType) as their metaclass.
399 # Once the body of a user-defined component class is executed, this
400 # metaclass is used to create and initialize the class. The metaclass
401 # creates a native BT component class of the corresponding type and
402 # associates it with this user-defined class. The metaclass also defines
403 # class methods like the `name` and `description` properties to match
404 # the _ComponentClassConst interface.
406 # The component class name which is used is either:
408 # * The `name` parameter of the class:
410 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
413 # * If the `name` class parameter is not used: the name of the class
414 # itself (`MySink` in the example above).
416 # The component class description which is used is the user-defined
419 # class MySink(bt2.SinkComponent):
420 # 'Description goes here'
423 # A user-defined Python component class can have an __init__() method
424 # which must accept the following parameters:
426 # def __init__(self, config, params, obj):
429 # The value of the `obj` parameter is what was passed as the `obj`
430 # parameter if the component was instantiated from Python with
431 # Graph.add_component(). If the component was not instantiated from
432 # Python, is is always `None`.
434 # The user-defined component class can also have a _user_finalize()
435 # method (do NOT use __del__()) to be notified when the component object
438 # User-defined source and filter component classes must use the
439 # `message_iterator_class` class parameter to specify the
440 # message iterator class to use for this component class:
442 # class MyMessageIterator(bt2._UserMessageIterator):
445 # class MySource(bt2._UserSourceComponent,
446 # message_iterator_class=MyMessageIterator):
449 # This message iterator class must inherit bt2._UserMessageIterator.
450 # It can implement the __init__() method, which must accept the
451 # following parameters:
453 # def __init__(self, config, port):
456 # It can also implement the __next__() and _user_finalize() methods
457 # (again, do NOT use __del__()), which don't accept any parameters
460 # When the user-defined class is destroyed, this metaclass's __del__()
461 # method is called: the native BT component class pointer is put (not
462 # needed anymore, at least not by any Python code since all references
463 # are dropped for __del__() to be called).
464 class _UserComponentType(type):
465 # __new__() is used to catch custom class parameters
466 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
467 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
469 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
470 super().__init
__(class_name
, bases
, namespace
)
472 # skip our own bases; they are never directly instantiated by the user
475 "_UserFilterSinkComponent",
476 "_UserSourceComponent",
477 "_UserFilterComponent",
478 "_UserSinkComponent",
481 if class_name
in own_bases
:
484 comp_cls_name
= kwargs
.get("name", class_name
)
485 bt2_utils
._check
_str
(comp_cls_name
)
486 comp_cls_descr
= None
489 if hasattr(cls
, "__doc__") and cls
.__doc
__ is not None:
490 bt2_utils
._check
_str
(cls
.__doc
__)
491 docstring
= _trim_docstring(cls
.__doc
__)
492 lines
= docstring
.splitlines()
495 comp_cls_descr
= lines
[0]
498 comp_cls_help
= "\n".join(lines
[2:])
500 iter_cls
= kwargs
.get("message_iterator_class")
502 if _UserSourceComponent
in bases
:
503 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
504 cc_ptr
= native_bt
.bt2_component_class_source_create(
505 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
507 elif _UserFilterComponent
in bases
:
508 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
509 cc_ptr
= native_bt
.bt2_component_class_filter_create(
510 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
512 elif _UserSinkComponent
in bases
:
513 if not hasattr(cls
, "_user_consume"):
514 raise bt2
._IncompleteUserClass(
515 "cannot create component class '{}': missing a _user_consume() method".format(
520 cc_ptr
= native_bt
.bt2_component_class_sink_create(
521 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
524 raise bt2
._IncompleteUserClass(
525 "cannot find a known component class base in the bases of '{}'".format(
531 raise bt2
._MemoryError(
532 "cannot create component class '{}'".format(class_name
)
535 cls
._bt
_cc
_ptr
= cc_ptr
537 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
, obj
):
538 # create instance, not user-initialized yet
539 self
= cls
.__new
__(cls
)
542 config
= cls
._config
_pycls
()
544 # pointer to native self component object (weak/borrowed)
545 self
._bt
_ptr
= comp_ptr
547 # call user's __init__() method
548 if params_ptr
is not None:
549 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
553 self
.__init
__(config
, params
, obj
)
556 def __call__(cls
, *args
, **kwargs
):
558 "cannot directly instantiate a user component from a Python module"
562 def _bt_set_iterator_class(cls
, iter_cls
):
564 raise bt2
._IncompleteUserClass(
565 "cannot create component class '{}': missing message iterator class".format(
570 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
571 raise bt2
._IncompleteUserClass(
572 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
577 if not hasattr(iter_cls
, "__next__"):
578 raise bt2
._IncompleteUserClass(
579 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
584 if hasattr(iter_cls
, "_user_can_seek_ns_from_origin") and not hasattr(
585 iter_cls
, "_user_seek_ns_from_origin"
587 raise bt2
._IncompleteUserClass(
588 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
593 if hasattr(iter_cls
, "_user_can_seek_beginning") and not hasattr(
594 iter_cls
, "_user_seek_beginning"
596 raise bt2
._IncompleteUserClass(
597 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
602 cls
._iter
_cls
= iter_cls
606 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
607 return native_bt
.component_class_get_name(ptr
)
610 def description(cls
):
611 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
612 return native_bt
.component_class_get_description(ptr
)
616 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
617 return native_bt
.component_class_get_help(ptr
)
621 return int(cls
._bt
_cc
_ptr
)
623 def _bt_get_supported_mip_versions_from_native(cls
, params_ptr
, obj
, log_level
):
624 # this can raise, but the native side checks the exception
625 if params_ptr
is not None:
626 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
630 # this can raise, but the native side checks the exception
631 range_set
= cls
._user
_get
_supported
_mip
_versions
(params
, obj
, log_level
)
633 if type(range_set
) is not bt2
.UnsignedIntegerRangeSet
:
634 # this can raise, but the native side checks the exception
635 range_set
= bt2
.UnsignedIntegerRangeSet(range_set
)
637 # return new reference
638 range_set
._get
_ref
(range_set
._ptr
)
639 return int(range_set
._ptr
)
641 def _user_get_supported_mip_versions(cls
, params
, obj
, log_level
):
644 def _bt_query_from_native(
645 cls
, priv_query_exec_ptr
, object_name
, params_ptr
, method_obj
647 # this can raise, but the native side checks the exception
648 if params_ptr
is not None:
649 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
653 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
656 # this can raise, but the native side checks the exception
657 results
= cls
._user
_query
(priv_query_exec
, object_name
, params
, method_obj
)
659 # the private query executor is a private view on the query
660 # executor; it's not a shared object (the library does not
661 # offer an API to get/put a reference, just like "self"
662 # objects) from this query's point of view, so invalidate
663 # the object in case the user kept a reference and uses it
665 priv_query_exec
._invalidate
()
667 # this can raise, but the native side checks the exception
668 results
= bt2
.create_value(results
)
671 results_ptr
= native_bt
.value_null
673 results_ptr
= results
._ptr
675 # return new reference
676 bt2_value
._Value
._get
_ref
(results_ptr
)
677 return int(results_ptr
)
679 def _user_query(cls
, priv_query_executor
, object_name
, params
, method_obj
):
680 raise bt2
.UnknownObject
682 def _bt_component_class_ptr(self
):
683 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
686 if hasattr(cls
, "_bt_cc_ptr"):
687 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
688 native_bt
.component_class_put_ref(cc_ptr
)
689 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
692 # Configuration objects for components.
694 # These are passed in __init__ to allow components to change some configuration
695 # parameters during initialization and not after. As you can see, they are not
696 # used at the moment, but are there in case we want to add such parameters.
699 class _UserComponentConfiguration
:
703 class _UserSourceComponentConfiguration(_UserComponentConfiguration
):
707 class _UserFilterComponentConfiguration(_UserComponentConfiguration
):
711 class _UserSinkComponentConfiguration(_UserComponentConfiguration
):
715 # Subclasses must provide these methods or property:
717 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
718 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
719 # specialized non-self pointer (e.g. 'bt_component_sink *').
720 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
721 # specialized component class (e.g. 'bt_component_class_sink *') of the
722 # passed specialized component pointer (e.g. 'bt_component_sink *').
723 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
727 class _UserComponent(metaclass
=_UserComponentType
):
730 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
731 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
732 name
= native_bt
.component_get_name(ptr
)
733 assert name
is not None
737 def logging_level(self
):
738 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
739 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
740 return native_bt
.component_get_logging_level(ptr
)
744 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
745 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
746 return _create_component_class_from_const_ptr_and_get_ref(
747 cc_ptr
, self
._bt
_comp
_cls
_type
752 return int(self
._bt
_ptr
)
755 def _graph_mip_version(self
):
756 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
757 return native_bt
.self_component_get_graph_mip_version(ptr
)
759 def __init__(self
, config
, params
, obj
):
762 def _user_finalize(self
):
765 def _user_port_connected(self
, port
, other_port
):
768 def _bt_port_connected_from_native(
769 self
, self_port_ptr
, self_port_type
, other_port_ptr
771 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
773 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
774 other_port_type
= native_bt
.PORT_TYPE_INPUT
776 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
778 other_port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(
779 other_port_ptr
, other_port_type
781 self
._user
_port
_connected
(port
, other_port
)
783 def _create_trace_class(
784 self
, user_attributes
=None, assigns_automatic_stream_class_id
=True
786 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
787 tc_ptr
= native_bt
.trace_class_create(ptr
)
790 raise bt2
._MemoryError("could not create trace class")
792 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
793 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
795 if user_attributes
is not None:
796 tc
._user
_attributes
= user_attributes
800 def _create_clock_class(
804 user_attributes
=None,
808 origin_is_unix_epoch
=True,
811 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
812 cc_ptr
= native_bt
.clock_class_create(ptr
)
815 raise bt2
._MemoryError("could not create clock class")
817 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
819 if frequency
is not None:
820 cc
._frequency
= frequency
825 if user_attributes
is not None:
826 cc
._user
_attributes
= user_attributes
828 if description
is not None:
829 cc
._description
= description
831 if precision
is not None:
832 cc
._precision
= precision
834 if offset
is not None:
837 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
845 class _UserSourceComponent(_UserComponent
, _SourceComponentConst
):
846 _bt_as_not_self_specific_component_ptr
= staticmethod(
847 native_bt
.self_component_source_as_component_source
849 _bt_as_self_component_ptr
= staticmethod(
850 native_bt
.self_component_source_as_self_component
852 _config_pycls
= _UserSourceComponentConfiguration
855 def _output_ports(self
):
856 def get_output_port_count(self_ptr
):
857 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
858 return native_bt
.component_source_get_output_port_count(ptr
)
860 return _ComponentPorts(
862 native_bt
.self_component_source_borrow_output_port_by_name
,
863 native_bt
.self_component_source_borrow_output_port_by_index
,
864 get_output_port_count
,
865 bt2_port
._UserComponentOutputPort
,
868 def _add_output_port(self
, name
, user_data
=None):
869 bt2_utils
._check
_str
(name
)
871 if name
in self
._output
_ports
:
873 "source component `{}` already contains an output port named `{}`".format(
878 fn
= native_bt
.self_component_source_add_output_port
879 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
880 bt2_utils
._handle
_func
_status
(
881 comp_status
, "cannot add output port to source component object"
883 assert self_port_ptr
is not None
884 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
889 class _UserFilterComponent(_UserComponent
, _FilterComponentConst
):
890 _bt_as_not_self_specific_component_ptr
= staticmethod(
891 native_bt
.self_component_filter_as_component_filter
893 _bt_as_self_component_ptr
= staticmethod(
894 native_bt
.self_component_filter_as_self_component
896 _config_pycls
= _UserFilterComponentConfiguration
899 def _output_ports(self
):
900 def get_output_port_count(self_ptr
):
901 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
902 return native_bt
.component_filter_get_output_port_count(ptr
)
904 return _ComponentPorts(
906 native_bt
.self_component_filter_borrow_output_port_by_name
,
907 native_bt
.self_component_filter_borrow_output_port_by_index
,
908 get_output_port_count
,
909 bt2_port
._UserComponentOutputPort
,
913 def _input_ports(self
):
914 def get_input_port_count(self_ptr
):
915 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
916 return native_bt
.component_filter_get_input_port_count(ptr
)
918 return _ComponentPorts(
920 native_bt
.self_component_filter_borrow_input_port_by_name
,
921 native_bt
.self_component_filter_borrow_input_port_by_index
,
922 get_input_port_count
,
923 bt2_port
._UserComponentInputPort
,
926 def _add_output_port(self
, name
, user_data
=None):
927 bt2_utils
._check
_str
(name
)
929 if name
in self
._output
_ports
:
931 "filter component `{}` already contains an output port named `{}`".format(
936 fn
= native_bt
.self_component_filter_add_output_port
937 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
938 bt2_utils
._handle
_func
_status
(
939 comp_status
, "cannot add output port to filter component object"
942 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
946 def _add_input_port(self
, name
, user_data
=None):
947 bt2_utils
._check
_str
(name
)
949 if name
in self
._input
_ports
:
951 "filter component `{}` already contains an input port named `{}`".format(
956 fn
= native_bt
.self_component_filter_add_input_port
957 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
958 bt2_utils
._handle
_func
_status
(
959 comp_status
, "cannot add input port to filter component object"
962 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
967 class _UserSinkComponent(_UserComponent
, _SinkComponentConst
):
968 _bt_as_not_self_specific_component_ptr
= staticmethod(
969 native_bt
.self_component_sink_as_component_sink
971 _bt_as_self_component_ptr
= staticmethod(
972 native_bt
.self_component_sink_as_self_component
974 _config_pycls
= _UserSinkComponentConfiguration
976 def _bt_graph_is_configured_from_native(self
):
977 self
._user
_graph
_is
_configured
()
979 def _user_graph_is_configured(self
):
983 def _input_ports(self
):
984 def get_input_port_count(self_ptr
):
985 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
986 return native_bt
.component_sink_get_input_port_count(ptr
)
988 return _ComponentPorts(
990 native_bt
.self_component_sink_borrow_input_port_by_name
,
991 native_bt
.self_component_sink_borrow_input_port_by_index
,
992 get_input_port_count
,
993 bt2_port
._UserComponentInputPort
,
996 def _add_input_port(self
, name
, user_data
=None):
997 bt2_utils
._check
_str
(name
)
999 if name
in self
._input
_ports
:
1001 "sink component `{}` already contains an input port named `{}`".format(
1006 fn
= native_bt
.self_component_sink_add_input_port
1007 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
1008 bt2_utils
._handle
_func
_status
(
1009 comp_status
, "cannot add input port to sink component object"
1011 assert self_port_ptr
1012 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
1016 def _create_message_iterator(self
, input_port
):
1017 bt2_utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
1019 if not input_port
.is_connected
:
1020 raise ValueError("input port is not connected")
1025 ) = native_bt
.bt2_message_iterator_create_from_sink_component(
1026 self
._bt
_ptr
, input_port
._ptr
1028 bt2_utils
._handle
_func
_status
(status
, "cannot create message iterator object")
1029 assert msg_iter_ptr
is not None
1031 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
1034 def _is_interrupted(self
):
1035 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))