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
15 from bt2
import error
as bt2_error
16 from bt2
import integer_range_set
as bt2_integer_range_set
20 class _IncompleteUserClass(Exception):
24 # This class wraps a component class pointer. This component class could
25 # have been created by Python code, but since we only have the pointer,
26 # we can only wrap it in a generic way and lose the original Python
29 # Subclasses must implement some methods that this base class uses:
31 # - _bt_as_component_class_ptr: static method, convert the passed component class
32 # pointer to a 'bt_component_class *'.
35 class _ComponentClassConst(bt2_object
._SharedObject
):
38 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
39 name
= native_bt
.component_class_get_name(ptr
)
40 assert name
is not None
44 def description(self
):
45 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
46 return native_bt
.component_class_get_description(ptr
)
50 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
51 return native_bt
.component_class_get_help(ptr
)
53 def _bt_component_class_ptr(self
):
54 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
56 def __eq__(self
, other
):
57 if not isinstance(other
, _ComponentClassConst
):
59 if not issubclass(other
, _UserComponent
):
64 return self
.addr
== other
.addr
67 class _SourceComponentClassConst(_ComponentClassConst
):
70 native_bt
.component_class_source_get_ref(ptr
)
74 native_bt
.component_class_source_put_ref(ptr
)
76 _bt_as_component_class_ptr
= staticmethod(
77 native_bt
.component_class_source_as_component_class
81 class _FilterComponentClassConst(_ComponentClassConst
):
84 native_bt
.component_class_filter_get_ref(ptr
)
88 native_bt
.component_class_filter_put_ref(ptr
)
90 _bt_as_component_class_ptr
= staticmethod(
91 native_bt
.component_class_filter_as_component_class
95 class _SinkComponentClassConst(_ComponentClassConst
):
98 native_bt
.component_class_sink_get_ref(ptr
)
102 native_bt
.component_class_sink_put_ref(ptr
)
104 _bt_as_component_class_ptr
= staticmethod(
105 native_bt
.component_class_sink_as_component_class
109 class _PortIterator(collections
.abc
.Iterator
):
110 def __init__(self
, comp_ports
):
111 self
._comp
_ports
= comp_ports
115 if self
._at
== len(self
._comp
_ports
):
118 comp_ports
= self
._comp
_ports
119 comp_ptr
= comp_ports
._component
_ptr
121 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
122 assert port_ptr
is not None
124 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
125 assert name
is not None
131 class _ComponentPorts(collections
.abc
.Mapping
):
132 # component_ptr is a bt_component_source *, bt_component_filter * or
133 # bt_component_sink *. Its type must match the type expected by the
134 # functions passed as arguments.
139 borrow_port_ptr_by_name
,
140 borrow_port_ptr_at_index
,
144 self
._component
_ptr
= component_ptr
145 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
146 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
147 self
._get
_port
_count
= get_port_count
148 self
._port
_pycls
= port_pycls
150 def __getitem__(self
, key
):
151 bt2_utils
._check
_str
(key
)
152 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
157 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
160 count
= self
._get
_port
_count
(self
._component
_ptr
)
165 return _PortIterator(self
)
168 # This class holds the methods which are common to both generic
169 # component objects and Python user component objects.
171 # Subclasses must provide these methods or property:
173 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
174 # specialized component class (e.g. 'bt_component_class_sink *') of the
175 # passed specialized component pointer (e.g. 'bt_component_sink *').
176 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
178 # - _bt_as_component_ptr: static method, must return the passed specialized
179 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
182 class _ComponentConst
:
185 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
186 name
= native_bt
.component_get_name(ptr
)
187 assert name
is not None
191 def logging_level(self
):
192 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
193 return native_bt
.component_get_logging_level(ptr
)
197 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
198 assert cc_ptr
is not None
199 return _create_component_class_from_const_ptr_and_get_ref(
200 cc_ptr
, self
._bt
_comp
_cls
_type
203 def __eq__(self
, other
):
204 if not hasattr(other
, "addr"):
207 return self
.addr
== other
.addr
210 class _SourceComponentConst(_ComponentConst
):
211 _bt_borrow_component_class_ptr
= staticmethod(
212 native_bt
.component_source_borrow_class_const
214 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
215 _bt_as_component_class_ptr
= staticmethod(
216 native_bt
.component_class_source_as_component_class
218 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
221 class _FilterComponentConst(_ComponentConst
):
222 _bt_borrow_component_class_ptr
= staticmethod(
223 native_bt
.component_filter_borrow_class_const
225 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
226 _bt_as_component_class_ptr
= staticmethod(
227 native_bt
.component_class_filter_as_component_class
229 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
232 class _SinkComponentConst(_ComponentConst
):
233 _bt_borrow_component_class_ptr
= staticmethod(
234 native_bt
.component_sink_borrow_class_const
236 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
237 _bt_as_component_class_ptr
= staticmethod(
238 native_bt
.component_class_sink_as_component_class
240 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
243 # This is analogous to _SourceComponentClassConst, but for source
245 class _GenericSourceComponentConst(bt2_object
._SharedObject
, _SourceComponentConst
):
248 native_bt
.component_source_get_ref(ptr
)
252 native_bt
.component_source_put_ref(ptr
)
255 def output_ports(self
):
256 return _ComponentPorts(
258 native_bt
.component_source_borrow_output_port_by_name_const
,
259 native_bt
.component_source_borrow_output_port_by_index_const
,
260 native_bt
.component_source_get_output_port_count
,
261 bt2_port
._OutputPortConst
,
265 # This is analogous to _FilterComponentClassConst, but for filter
267 class _GenericFilterComponentConst(bt2_object
._SharedObject
, _FilterComponentConst
):
270 native_bt
.component_filter_get_ref(ptr
)
274 native_bt
.component_filter_put_ref(ptr
)
277 def output_ports(self
):
278 return _ComponentPorts(
280 native_bt
.component_filter_borrow_output_port_by_name_const
,
281 native_bt
.component_filter_borrow_output_port_by_index_const
,
282 native_bt
.component_filter_get_output_port_count
,
283 bt2_port
._OutputPortConst
,
287 def input_ports(self
):
288 return _ComponentPorts(
290 native_bt
.component_filter_borrow_input_port_by_name_const
,
291 native_bt
.component_filter_borrow_input_port_by_index_const
,
292 native_bt
.component_filter_get_input_port_count
,
293 bt2_port
._InputPortConst
,
297 # This is analogous to _SinkComponentClassConst, but for sink
299 class _GenericSinkComponentConst(bt2_object
._SharedObject
, _SinkComponentConst
):
302 native_bt
.component_sink_get_ref(ptr
)
306 native_bt
.component_sink_put_ref(ptr
)
309 def input_ports(self
):
310 return _ComponentPorts(
312 native_bt
.component_sink_borrow_input_port_by_name_const
,
313 native_bt
.component_sink_borrow_input_port_by_index_const
,
314 native_bt
.component_sink_get_input_port_count
,
315 bt2_port
._InputPortConst
,
319 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
320 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentConst
,
321 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentConst
,
322 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentConst
,
326 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
327 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClassConst
,
328 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClassConst
,
329 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClassConst
,
333 # Create a component Python object of type _GenericSourceComponentConst,
334 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
337 # Steals the reference to ptr from the caller.
340 def _create_component_from_const_ptr(ptr
, comp_cls_type
):
341 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
344 # Same as the above, but acquire a new reference instead of stealing the
345 # reference from the caller.
348 def _create_component_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
349 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
351 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
354 # Create a component class Python object of type
355 # _SourceComponentClassConst, _FilterComponentClassConst or
356 # _SinkComponentClassConst, depending on comp_cls_type.
358 # Acquires a new reference to ptr.
361 def _create_component_class_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
362 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
364 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
367 def _trim_docstring(docstring
):
368 lines
= docstring
.expandtabs().splitlines()
376 for line
in lines
[1:]:
377 stripped
= line
.lstrip()
380 indent
= min(indent
, len(line
) - len(stripped
))
382 trimmed
= [lines
[0].strip()]
384 if indent
< sys
.maxsize
and len(lines
) > 1:
385 for line
in lines
[1:]:
386 trimmed
.append(line
[indent
:].rstrip())
388 while trimmed
and not trimmed
[-1]:
391 while trimmed
and not trimmed
[0]:
394 return "\n".join(trimmed
)
397 # Metaclass for component classes defined by Python code.
399 # The Python user can create a standard Python class which inherits one
400 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
401 # or _UserSinkComponent). Those base classes set this class
402 # (_UserComponentType) as their metaclass.
404 # Once the body of a user-defined component class is executed, this
405 # metaclass is used to create and initialize the class. The metaclass
406 # creates a native BT component class of the corresponding type and
407 # associates it with this user-defined class. The metaclass also defines
408 # class methods like the `name` and `description` properties to match
409 # the _ComponentClassConst interface.
411 # The component class name which is used is either:
413 # * The `name` parameter of the class:
415 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
418 # * If the `name` class parameter is not used: the name of the class
419 # itself (`MySink` in the example above).
421 # The component class description which is used is the user-defined
424 # class MySink(bt2.SinkComponent):
425 # 'Description goes here'
428 # A user-defined Python component class can have an __init__() method
429 # which must accept the following parameters:
431 # def __init__(self, config, params, obj):
434 # The value of the `obj` parameter is what was passed as the `obj`
435 # parameter if the component was instantiated from Python with
436 # Graph.add_component(). If the component was not instantiated from
437 # Python, is is always `None`.
439 # The user-defined component class can also have a _user_finalize()
440 # method (do NOT use __del__()) to be notified when the component object
443 # User-defined source and filter component classes must use the
444 # `message_iterator_class` class parameter to specify the
445 # message iterator class to use for this component class:
447 # class MyMessageIterator(bt2._UserMessageIterator):
450 # class MySource(bt2._UserSourceComponent,
451 # message_iterator_class=MyMessageIterator):
454 # This message iterator class must inherit bt2._UserMessageIterator.
455 # It can implement the __init__() method, which must accept the
456 # following parameters:
458 # def __init__(self, config, port):
461 # It can also implement the __next__() and _user_finalize() methods
462 # (again, do NOT use __del__()), which don't accept any parameters
465 # When the user-defined class is destroyed, this metaclass's __del__()
466 # method is called: the native BT component class pointer is put (not
467 # needed anymore, at least not by any Python code since all references
468 # are dropped for __del__() to be called).
469 class _UserComponentType(type):
470 # __new__() is used to catch custom class parameters
471 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
472 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
474 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
475 super().__init
__(class_name
, bases
, namespace
)
477 # skip our own bases; they are never directly instantiated by the user
480 "_UserFilterSinkComponent",
481 "_UserSourceComponent",
482 "_UserFilterComponent",
483 "_UserSinkComponent",
486 if class_name
in own_bases
:
489 comp_cls_name
= kwargs
.get("name", class_name
)
490 bt2_utils
._check
_str
(comp_cls_name
)
491 comp_cls_descr
= None
494 if hasattr(cls
, "__doc__") and cls
.__doc
__ is not None:
495 bt2_utils
._check
_str
(cls
.__doc
__)
496 docstring
= _trim_docstring(cls
.__doc
__)
497 lines
= docstring
.splitlines()
500 comp_cls_descr
= lines
[0]
503 comp_cls_help
= "\n".join(lines
[2:])
505 iter_cls
= kwargs
.get("message_iterator_class")
507 if _UserSourceComponent
in bases
:
508 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
509 cc_ptr
= native_bt
.bt2_component_class_source_create(
510 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
512 elif _UserFilterComponent
in bases
:
513 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
514 cc_ptr
= native_bt
.bt2_component_class_filter_create(
515 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
517 elif _UserSinkComponent
in bases
:
518 if not hasattr(cls
, "_user_consume"):
519 raise _IncompleteUserClass(
520 "cannot create component class '{}': missing a _user_consume() method".format(
525 cc_ptr
= native_bt
.bt2_component_class_sink_create(
526 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
529 raise _IncompleteUserClass(
530 "cannot find a known component class base in the bases of '{}'".format(
536 raise bt2_error
._MemoryError
(
537 "cannot create component class '{}'".format(class_name
)
540 cls
._bt
_cc
_ptr
= cc_ptr
542 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
, obj
):
543 # create instance, not user-initialized yet
544 self
= cls
.__new
__(cls
)
547 config
= cls
._config
_pycls
()
549 # pointer to native self component object (weak/borrowed)
550 self
._bt
_ptr
= comp_ptr
552 # call user's __init__() method
553 if params_ptr
is not None:
554 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
558 self
.__init
__(config
, params
, obj
)
561 def __call__(cls
, *args
, **kwargs
):
563 "cannot directly instantiate a user component from a Python module"
567 def _bt_set_iterator_class(cls
, iter_cls
):
569 raise _IncompleteUserClass(
570 "cannot create component class '{}': missing message iterator class".format(
575 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
576 raise _IncompleteUserClass(
577 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
582 if not hasattr(iter_cls
, "__next__"):
583 raise _IncompleteUserClass(
584 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
589 if hasattr(iter_cls
, "_user_can_seek_ns_from_origin") and not hasattr(
590 iter_cls
, "_user_seek_ns_from_origin"
592 raise _IncompleteUserClass(
593 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
598 if hasattr(iter_cls
, "_user_can_seek_beginning") and not hasattr(
599 iter_cls
, "_user_seek_beginning"
601 raise _IncompleteUserClass(
602 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
607 cls
._iter
_cls
= iter_cls
611 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
612 return native_bt
.component_class_get_name(ptr
)
615 def description(cls
):
616 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
617 return native_bt
.component_class_get_description(ptr
)
621 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
622 return native_bt
.component_class_get_help(ptr
)
626 return int(cls
._bt
_cc
_ptr
)
628 def _bt_get_supported_mip_versions_from_native(cls
, params_ptr
, obj
, log_level
):
629 # this can raise, but the native side checks the exception
630 if params_ptr
is not None:
631 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
635 # this can raise, but the native side checks the exception
636 range_set
= cls
._user
_get
_supported
_mip
_versions
(params
, obj
, log_level
)
638 if type(range_set
) is not bt2_integer_range_set
.UnsignedIntegerRangeSet
:
639 # this can raise, but the native side checks the exception
640 range_set
= bt2_integer_range_set
.UnsignedIntegerRangeSet(range_set
)
642 # return new reference
643 range_set
._get
_ref
(range_set
._ptr
)
644 return int(range_set
._ptr
)
646 def _user_get_supported_mip_versions(cls
, params
, obj
, log_level
):
649 def _bt_query_from_native(
650 cls
, priv_query_exec_ptr
, object_name
, params_ptr
, method_obj
652 # this can raise, but the native side checks the exception
653 if params_ptr
is not None:
654 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
658 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
661 # this can raise, but the native side checks the exception
662 results
= cls
._user
_query
(priv_query_exec
, object_name
, params
, method_obj
)
664 # the private query executor is a private view on the query
665 # executor; it's not a shared object (the library does not
666 # offer an API to get/put a reference, just like "self"
667 # objects) from this query's point of view, so invalidate
668 # the object in case the user kept a reference and uses it
670 priv_query_exec
._invalidate
()
672 # this can raise, but the native side checks the exception
673 results
= bt2_value
.create_value(results
)
676 results_ptr
= native_bt
.value_null
678 results_ptr
= results
._ptr
680 # return new reference
681 bt2_value
._Value
._get
_ref
(results_ptr
)
682 return int(results_ptr
)
684 def _user_query(cls
, priv_query_executor
, object_name
, params
, method_obj
):
685 raise bt2_utils
.UnknownObject
687 def _bt_component_class_ptr(self
):
688 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
691 if hasattr(cls
, "_bt_cc_ptr"):
692 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
693 native_bt
.component_class_put_ref(cc_ptr
)
694 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
697 # Configuration objects for components.
699 # These are passed in __init__ to allow components to change some configuration
700 # parameters during initialization and not after. As you can see, they are not
701 # used at the moment, but are there in case we want to add such parameters.
704 class _UserComponentConfiguration
:
708 class _UserSourceComponentConfiguration(_UserComponentConfiguration
):
712 class _UserFilterComponentConfiguration(_UserComponentConfiguration
):
716 class _UserSinkComponentConfiguration(_UserComponentConfiguration
):
720 # Subclasses must provide these methods or property:
722 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
723 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
724 # specialized non-self pointer (e.g. 'bt_component_sink *').
725 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
726 # specialized component class (e.g. 'bt_component_class_sink *') of the
727 # passed specialized component pointer (e.g. 'bt_component_sink *').
728 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
732 class _UserComponent(metaclass
=_UserComponentType
):
735 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
736 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
737 name
= native_bt
.component_get_name(ptr
)
738 assert name
is not None
742 def logging_level(self
):
743 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
744 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
745 return native_bt
.component_get_logging_level(ptr
)
749 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
750 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
751 return _create_component_class_from_const_ptr_and_get_ref(
752 cc_ptr
, self
._bt
_comp
_cls
_type
757 return int(self
._bt
_ptr
)
760 def _graph_mip_version(self
):
761 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
762 return native_bt
.self_component_get_graph_mip_version(ptr
)
764 def __init__(self
, config
, params
, obj
):
767 def _user_finalize(self
):
770 def _user_port_connected(self
, port
, other_port
):
773 def _bt_port_connected_from_native(
774 self
, self_port_ptr
, self_port_type
, other_port_ptr
776 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
778 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
779 other_port_type
= native_bt
.PORT_TYPE_INPUT
781 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
783 other_port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(
784 other_port_ptr
, other_port_type
786 self
._user
_port
_connected
(port
, other_port
)
788 def _create_trace_class(
789 self
, user_attributes
=None, assigns_automatic_stream_class_id
=True
791 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
792 tc_ptr
= native_bt
.trace_class_create(ptr
)
795 raise bt2_error
._MemoryError
("could not create trace class")
797 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
798 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
800 if user_attributes
is not None:
801 tc
._user
_attributes
= user_attributes
805 def _create_clock_class(
809 user_attributes
=None,
813 origin_is_unix_epoch
=True,
816 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
817 cc_ptr
= native_bt
.clock_class_create(ptr
)
820 raise bt2_error
._MemoryError
("could not create clock class")
822 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
824 if frequency
is not None:
825 cc
._frequency
= frequency
830 if user_attributes
is not None:
831 cc
._user
_attributes
= user_attributes
833 if description
is not None:
834 cc
._description
= description
836 if precision
is not None:
837 cc
._precision
= precision
839 if offset
is not None:
842 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
850 class _UserSourceComponent(_UserComponent
, _SourceComponentConst
):
851 _bt_as_not_self_specific_component_ptr
= staticmethod(
852 native_bt
.self_component_source_as_component_source
854 _bt_as_self_component_ptr
= staticmethod(
855 native_bt
.self_component_source_as_self_component
857 _config_pycls
= _UserSourceComponentConfiguration
860 def _output_ports(self
):
861 def get_output_port_count(self_ptr
):
862 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
863 return native_bt
.component_source_get_output_port_count(ptr
)
865 return _ComponentPorts(
867 native_bt
.self_component_source_borrow_output_port_by_name
,
868 native_bt
.self_component_source_borrow_output_port_by_index
,
869 get_output_port_count
,
870 bt2_port
._UserComponentOutputPort
,
873 def _add_output_port(self
, name
, user_data
=None):
874 bt2_utils
._check
_str
(name
)
876 if name
in self
._output
_ports
:
878 "source component `{}` already contains an output port named `{}`".format(
883 fn
= native_bt
.self_component_source_add_output_port
884 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
885 bt2_utils
._handle
_func
_status
(
886 comp_status
, "cannot add output port to source component object"
888 assert self_port_ptr
is not None
889 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
894 class _UserFilterComponent(_UserComponent
, _FilterComponentConst
):
895 _bt_as_not_self_specific_component_ptr
= staticmethod(
896 native_bt
.self_component_filter_as_component_filter
898 _bt_as_self_component_ptr
= staticmethod(
899 native_bt
.self_component_filter_as_self_component
901 _config_pycls
= _UserFilterComponentConfiguration
904 def _output_ports(self
):
905 def get_output_port_count(self_ptr
):
906 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
907 return native_bt
.component_filter_get_output_port_count(ptr
)
909 return _ComponentPorts(
911 native_bt
.self_component_filter_borrow_output_port_by_name
,
912 native_bt
.self_component_filter_borrow_output_port_by_index
,
913 get_output_port_count
,
914 bt2_port
._UserComponentOutputPort
,
918 def _input_ports(self
):
919 def get_input_port_count(self_ptr
):
920 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
921 return native_bt
.component_filter_get_input_port_count(ptr
)
923 return _ComponentPorts(
925 native_bt
.self_component_filter_borrow_input_port_by_name
,
926 native_bt
.self_component_filter_borrow_input_port_by_index
,
927 get_input_port_count
,
928 bt2_port
._UserComponentInputPort
,
931 def _add_output_port(self
, name
, user_data
=None):
932 bt2_utils
._check
_str
(name
)
934 if name
in self
._output
_ports
:
936 "filter component `{}` already contains an output port named `{}`".format(
941 fn
= native_bt
.self_component_filter_add_output_port
942 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
943 bt2_utils
._handle
_func
_status
(
944 comp_status
, "cannot add output port to filter component object"
947 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
951 def _add_input_port(self
, name
, user_data
=None):
952 bt2_utils
._check
_str
(name
)
954 if name
in self
._input
_ports
:
956 "filter component `{}` already contains an input port named `{}`".format(
961 fn
= native_bt
.self_component_filter_add_input_port
962 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
963 bt2_utils
._handle
_func
_status
(
964 comp_status
, "cannot add input port to filter component object"
967 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
972 class _UserSinkComponent(_UserComponent
, _SinkComponentConst
):
973 _bt_as_not_self_specific_component_ptr
= staticmethod(
974 native_bt
.self_component_sink_as_component_sink
976 _bt_as_self_component_ptr
= staticmethod(
977 native_bt
.self_component_sink_as_self_component
979 _config_pycls
= _UserSinkComponentConfiguration
981 def _bt_graph_is_configured_from_native(self
):
982 self
._user
_graph
_is
_configured
()
984 def _user_graph_is_configured(self
):
988 def _input_ports(self
):
989 def get_input_port_count(self_ptr
):
990 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
991 return native_bt
.component_sink_get_input_port_count(ptr
)
993 return _ComponentPorts(
995 native_bt
.self_component_sink_borrow_input_port_by_name
,
996 native_bt
.self_component_sink_borrow_input_port_by_index
,
997 get_input_port_count
,
998 bt2_port
._UserComponentInputPort
,
1001 def _add_input_port(self
, name
, user_data
=None):
1002 bt2_utils
._check
_str
(name
)
1004 if name
in self
._input
_ports
:
1006 "sink component `{}` already contains an input port named `{}`".format(
1011 fn
= native_bt
.self_component_sink_add_input_port
1012 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
1013 bt2_utils
._handle
_func
_status
(
1014 comp_status
, "cannot add input port to sink component object"
1016 assert self_port_ptr
1017 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
1021 def _create_message_iterator(self
, input_port
):
1022 bt2_utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
1024 if not input_port
.is_connected
:
1025 raise ValueError("input port is not connected")
1030 ) = native_bt
.bt2_message_iterator_create_from_sink_component(
1031 self
._bt
_ptr
, input_port
._ptr
1033 bt2_utils
._handle
_func
_status
(status
, "cannot create message iterator object")
1034 assert msg_iter_ptr
is not None
1036 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
1039 def _is_interrupted(self
):
1040 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))