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 class _IncompleteUserClass(Exception):
23 # This class wraps a component class pointer. This component class could
24 # have been created by Python code, but since we only have the pointer,
25 # we can only wrap it in a generic way and lose the original Python
28 # Subclasses must implement some methods that this base class uses:
30 # - _bt_as_component_class_ptr: static method, convert the passed component class
31 # pointer to a 'bt_component_class *'.
34 class _ComponentClassConst(bt2_object
._SharedObject
):
37 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
38 name
= native_bt
.component_class_get_name(ptr
)
39 assert name
is not None
43 def description(self
):
44 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
45 return native_bt
.component_class_get_description(ptr
)
49 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
50 return native_bt
.component_class_get_help(ptr
)
52 def _bt_component_class_ptr(self
):
53 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
55 def __eq__(self
, other
):
56 if not isinstance(other
, _ComponentClassConst
):
58 if not issubclass(other
, _UserComponent
):
63 return self
.addr
== other
.addr
66 class _SourceComponentClassConst(_ComponentClassConst
):
69 native_bt
.component_class_source_get_ref(ptr
)
73 native_bt
.component_class_source_put_ref(ptr
)
75 _bt_as_component_class_ptr
= staticmethod(
76 native_bt
.component_class_source_as_component_class
80 class _FilterComponentClassConst(_ComponentClassConst
):
83 native_bt
.component_class_filter_get_ref(ptr
)
87 native_bt
.component_class_filter_put_ref(ptr
)
89 _bt_as_component_class_ptr
= staticmethod(
90 native_bt
.component_class_filter_as_component_class
94 class _SinkComponentClassConst(_ComponentClassConst
):
97 native_bt
.component_class_sink_get_ref(ptr
)
101 native_bt
.component_class_sink_put_ref(ptr
)
103 _bt_as_component_class_ptr
= staticmethod(
104 native_bt
.component_class_sink_as_component_class
108 class _PortIterator(collections
.abc
.Iterator
):
109 def __init__(self
, comp_ports
):
110 self
._comp
_ports
= comp_ports
114 if self
._at
== len(self
._comp
_ports
):
117 comp_ports
= self
._comp
_ports
118 comp_ptr
= comp_ports
._component
_ptr
120 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
121 assert port_ptr
is not None
123 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
124 assert name
is not None
130 class _ComponentPorts(collections
.abc
.Mapping
):
131 # component_ptr is a bt_component_source *, bt_component_filter * or
132 # bt_component_sink *. Its type must match the type expected by the
133 # functions passed as arguments.
138 borrow_port_ptr_by_name
,
139 borrow_port_ptr_at_index
,
143 self
._component
_ptr
= component_ptr
144 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
145 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
146 self
._get
_port
_count
= get_port_count
147 self
._port
_pycls
= port_pycls
149 def __getitem__(self
, key
):
150 bt2_utils
._check
_str
(key
)
151 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
156 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
159 count
= self
._get
_port
_count
(self
._component
_ptr
)
164 return _PortIterator(self
)
167 # This class holds the methods which are common to both generic
168 # component objects and Python user component objects.
170 # Subclasses must provide these methods or property:
172 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
173 # specialized component class (e.g. 'bt_component_class_sink *') of the
174 # passed specialized component pointer (e.g. 'bt_component_sink *').
175 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
177 # - _bt_as_component_ptr: static method, must return the passed specialized
178 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
181 class _ComponentConst
:
184 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
185 name
= native_bt
.component_get_name(ptr
)
186 assert name
is not None
190 def logging_level(self
):
191 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
192 return native_bt
.component_get_logging_level(ptr
)
196 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
197 assert cc_ptr
is not None
198 return _create_component_class_from_const_ptr_and_get_ref(
199 cc_ptr
, self
._bt
_comp
_cls
_type
202 def __eq__(self
, other
):
203 if not hasattr(other
, "addr"):
206 return self
.addr
== other
.addr
209 class _SourceComponentConst(_ComponentConst
):
210 _bt_borrow_component_class_ptr
= staticmethod(
211 native_bt
.component_source_borrow_class_const
213 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
214 _bt_as_component_class_ptr
= staticmethod(
215 native_bt
.component_class_source_as_component_class
217 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
220 class _FilterComponentConst(_ComponentConst
):
221 _bt_borrow_component_class_ptr
= staticmethod(
222 native_bt
.component_filter_borrow_class_const
224 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
225 _bt_as_component_class_ptr
= staticmethod(
226 native_bt
.component_class_filter_as_component_class
228 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
231 class _SinkComponentConst(_ComponentConst
):
232 _bt_borrow_component_class_ptr
= staticmethod(
233 native_bt
.component_sink_borrow_class_const
235 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
236 _bt_as_component_class_ptr
= staticmethod(
237 native_bt
.component_class_sink_as_component_class
239 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
242 # This is analogous to _SourceComponentClassConst, but for source
244 class _GenericSourceComponentConst(bt2_object
._SharedObject
, _SourceComponentConst
):
247 native_bt
.component_source_get_ref(ptr
)
251 native_bt
.component_source_put_ref(ptr
)
254 def output_ports(self
):
255 return _ComponentPorts(
257 native_bt
.component_source_borrow_output_port_by_name_const
,
258 native_bt
.component_source_borrow_output_port_by_index_const
,
259 native_bt
.component_source_get_output_port_count
,
260 bt2_port
._OutputPortConst
,
264 # This is analogous to _FilterComponentClassConst, but for filter
266 class _GenericFilterComponentConst(bt2_object
._SharedObject
, _FilterComponentConst
):
269 native_bt
.component_filter_get_ref(ptr
)
273 native_bt
.component_filter_put_ref(ptr
)
276 def output_ports(self
):
277 return _ComponentPorts(
279 native_bt
.component_filter_borrow_output_port_by_name_const
,
280 native_bt
.component_filter_borrow_output_port_by_index_const
,
281 native_bt
.component_filter_get_output_port_count
,
282 bt2_port
._OutputPortConst
,
286 def input_ports(self
):
287 return _ComponentPorts(
289 native_bt
.component_filter_borrow_input_port_by_name_const
,
290 native_bt
.component_filter_borrow_input_port_by_index_const
,
291 native_bt
.component_filter_get_input_port_count
,
292 bt2_port
._InputPortConst
,
296 # This is analogous to _SinkComponentClassConst, but for sink
298 class _GenericSinkComponentConst(bt2_object
._SharedObject
, _SinkComponentConst
):
301 native_bt
.component_sink_get_ref(ptr
)
305 native_bt
.component_sink_put_ref(ptr
)
308 def input_ports(self
):
309 return _ComponentPorts(
311 native_bt
.component_sink_borrow_input_port_by_name_const
,
312 native_bt
.component_sink_borrow_input_port_by_index_const
,
313 native_bt
.component_sink_get_input_port_count
,
314 bt2_port
._InputPortConst
,
318 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
319 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentConst
,
320 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentConst
,
321 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentConst
,
325 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
326 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClassConst
,
327 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClassConst
,
328 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClassConst
,
332 # Create a component Python object of type _GenericSourceComponentConst,
333 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
336 # Steals the reference to ptr from the caller.
339 def _create_component_from_const_ptr(ptr
, comp_cls_type
):
340 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
343 # Same as the above, but acquire a new reference instead of stealing the
344 # reference from the caller.
347 def _create_component_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
348 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
350 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
353 # Create a component class Python object of type
354 # _SourceComponentClassConst, _FilterComponentClassConst or
355 # _SinkComponentClassConst, depending on comp_cls_type.
357 # Acquires a new reference to ptr.
360 def _create_component_class_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
361 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
363 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
366 def _trim_docstring(docstring
):
367 lines
= docstring
.expandtabs().splitlines()
375 for line
in lines
[1:]:
376 stripped
= line
.lstrip()
379 indent
= min(indent
, len(line
) - len(stripped
))
381 trimmed
= [lines
[0].strip()]
383 if indent
< sys
.maxsize
and len(lines
) > 1:
384 for line
in lines
[1:]:
385 trimmed
.append(line
[indent
:].rstrip())
387 while trimmed
and not trimmed
[-1]:
390 while trimmed
and not trimmed
[0]:
393 return "\n".join(trimmed
)
396 # Metaclass for component classes defined by Python code.
398 # The Python user can create a standard Python class which inherits one
399 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
400 # or _UserSinkComponent). Those base classes set this class
401 # (_UserComponentType) as their metaclass.
403 # Once the body of a user-defined component class is executed, this
404 # metaclass is used to create and initialize the class. The metaclass
405 # creates a native BT component class of the corresponding type and
406 # associates it with this user-defined class. The metaclass also defines
407 # class methods like the `name` and `description` properties to match
408 # the _ComponentClassConst interface.
410 # The component class name which is used is either:
412 # * The `name` parameter of the class:
414 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
417 # * If the `name` class parameter is not used: the name of the class
418 # itself (`MySink` in the example above).
420 # The component class description which is used is the user-defined
423 # class MySink(bt2.SinkComponent):
424 # 'Description goes here'
427 # A user-defined Python component class can have an __init__() method
428 # which must accept the following parameters:
430 # def __init__(self, config, params, obj):
433 # The value of the `obj` parameter is what was passed as the `obj`
434 # parameter if the component was instantiated from Python with
435 # Graph.add_component(). If the component was not instantiated from
436 # Python, is is always `None`.
438 # The user-defined component class can also have a _user_finalize()
439 # method (do NOT use __del__()) to be notified when the component object
442 # User-defined source and filter component classes must use the
443 # `message_iterator_class` class parameter to specify the
444 # message iterator class to use for this component class:
446 # class MyMessageIterator(bt2._UserMessageIterator):
449 # class MySource(bt2._UserSourceComponent,
450 # message_iterator_class=MyMessageIterator):
453 # This message iterator class must inherit bt2._UserMessageIterator.
454 # It can implement the __init__() method, which must accept the
455 # following parameters:
457 # def __init__(self, config, port):
460 # It can also implement the __next__() and _user_finalize() methods
461 # (again, do NOT use __del__()), which don't accept any parameters
464 # When the user-defined class is destroyed, this metaclass's __del__()
465 # method is called: the native BT component class pointer is put (not
466 # needed anymore, at least not by any Python code since all references
467 # are dropped for __del__() to be called).
468 class _UserComponentType(type):
469 # __new__() is used to catch custom class parameters
470 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
471 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
473 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
474 super().__init
__(class_name
, bases
, namespace
)
476 # skip our own bases; they are never directly instantiated by the user
479 "_UserFilterSinkComponent",
480 "_UserSourceComponent",
481 "_UserFilterComponent",
482 "_UserSinkComponent",
485 if class_name
in own_bases
:
488 comp_cls_name
= kwargs
.get("name", class_name
)
489 bt2_utils
._check
_str
(comp_cls_name
)
490 comp_cls_descr
= None
493 if hasattr(cls
, "__doc__") and cls
.__doc
__ is not None:
494 bt2_utils
._check
_str
(cls
.__doc
__)
495 docstring
= _trim_docstring(cls
.__doc
__)
496 lines
= docstring
.splitlines()
499 comp_cls_descr
= lines
[0]
502 comp_cls_help
= "\n".join(lines
[2:])
504 iter_cls
= kwargs
.get("message_iterator_class")
506 if _UserSourceComponent
in bases
:
507 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
508 cc_ptr
= native_bt
.bt2_component_class_source_create(
509 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
511 elif _UserFilterComponent
in bases
:
512 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
513 cc_ptr
= native_bt
.bt2_component_class_filter_create(
514 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
516 elif _UserSinkComponent
in bases
:
517 if not hasattr(cls
, "_user_consume"):
518 raise bt2
._IncompleteUserClass(
519 "cannot create component class '{}': missing a _user_consume() method".format(
524 cc_ptr
= native_bt
.bt2_component_class_sink_create(
525 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
528 raise bt2
._IncompleteUserClass(
529 "cannot find a known component class base in the bases of '{}'".format(
535 raise bt2
._MemoryError(
536 "cannot create component class '{}'".format(class_name
)
539 cls
._bt
_cc
_ptr
= cc_ptr
541 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
, obj
):
542 # create instance, not user-initialized yet
543 self
= cls
.__new
__(cls
)
546 config
= cls
._config
_pycls
()
548 # pointer to native self component object (weak/borrowed)
549 self
._bt
_ptr
= comp_ptr
551 # call user's __init__() method
552 if params_ptr
is not None:
553 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
557 self
.__init
__(config
, params
, obj
)
560 def __call__(cls
, *args
, **kwargs
):
562 "cannot directly instantiate a user component from a Python module"
566 def _bt_set_iterator_class(cls
, iter_cls
):
568 raise bt2
._IncompleteUserClass(
569 "cannot create component class '{}': missing message iterator class".format(
574 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
575 raise bt2
._IncompleteUserClass(
576 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
581 if not hasattr(iter_cls
, "__next__"):
582 raise bt2
._IncompleteUserClass(
583 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
588 if hasattr(iter_cls
, "_user_can_seek_ns_from_origin") and not hasattr(
589 iter_cls
, "_user_seek_ns_from_origin"
591 raise bt2
._IncompleteUserClass(
592 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
597 if hasattr(iter_cls
, "_user_can_seek_beginning") and not hasattr(
598 iter_cls
, "_user_seek_beginning"
600 raise bt2
._IncompleteUserClass(
601 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
606 cls
._iter
_cls
= iter_cls
610 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
611 return native_bt
.component_class_get_name(ptr
)
614 def description(cls
):
615 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
616 return native_bt
.component_class_get_description(ptr
)
620 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
621 return native_bt
.component_class_get_help(ptr
)
625 return int(cls
._bt
_cc
_ptr
)
627 def _bt_get_supported_mip_versions_from_native(cls
, params_ptr
, obj
, log_level
):
628 # this can raise, but the native side checks the exception
629 if params_ptr
is not None:
630 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
634 # this can raise, but the native side checks the exception
635 range_set
= cls
._user
_get
_supported
_mip
_versions
(params
, obj
, log_level
)
637 if type(range_set
) is not bt2
.UnsignedIntegerRangeSet
:
638 # this can raise, but the native side checks the exception
639 range_set
= bt2
.UnsignedIntegerRangeSet(range_set
)
641 # return new reference
642 range_set
._get
_ref
(range_set
._ptr
)
643 return int(range_set
._ptr
)
645 def _user_get_supported_mip_versions(cls
, params
, obj
, log_level
):
648 def _bt_query_from_native(
649 cls
, priv_query_exec_ptr
, object_name
, params_ptr
, method_obj
651 # this can raise, but the native side checks the exception
652 if params_ptr
is not None:
653 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
657 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
660 # this can raise, but the native side checks the exception
661 results
= cls
._user
_query
(priv_query_exec
, object_name
, params
, method_obj
)
663 # the private query executor is a private view on the query
664 # executor; it's not a shared object (the library does not
665 # offer an API to get/put a reference, just like "self"
666 # objects) from this query's point of view, so invalidate
667 # the object in case the user kept a reference and uses it
669 priv_query_exec
._invalidate
()
671 # this can raise, but the native side checks the exception
672 results
= bt2
.create_value(results
)
675 results_ptr
= native_bt
.value_null
677 results_ptr
= results
._ptr
679 # return new reference
680 bt2_value
._Value
._get
_ref
(results_ptr
)
681 return int(results_ptr
)
683 def _user_query(cls
, priv_query_executor
, object_name
, params
, method_obj
):
684 raise bt2
.UnknownObject
686 def _bt_component_class_ptr(self
):
687 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
690 if hasattr(cls
, "_bt_cc_ptr"):
691 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
692 native_bt
.component_class_put_ref(cc_ptr
)
693 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
696 # Configuration objects for components.
698 # These are passed in __init__ to allow components to change some configuration
699 # parameters during initialization and not after. As you can see, they are not
700 # used at the moment, but are there in case we want to add such parameters.
703 class _UserComponentConfiguration
:
707 class _UserSourceComponentConfiguration(_UserComponentConfiguration
):
711 class _UserFilterComponentConfiguration(_UserComponentConfiguration
):
715 class _UserSinkComponentConfiguration(_UserComponentConfiguration
):
719 # Subclasses must provide these methods or property:
721 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
722 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
723 # specialized non-self pointer (e.g. 'bt_component_sink *').
724 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
725 # specialized component class (e.g. 'bt_component_class_sink *') of the
726 # passed specialized component pointer (e.g. 'bt_component_sink *').
727 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
731 class _UserComponent(metaclass
=_UserComponentType
):
734 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
735 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
736 name
= native_bt
.component_get_name(ptr
)
737 assert name
is not None
741 def logging_level(self
):
742 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
743 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
744 return native_bt
.component_get_logging_level(ptr
)
748 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
749 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
750 return _create_component_class_from_const_ptr_and_get_ref(
751 cc_ptr
, self
._bt
_comp
_cls
_type
756 return int(self
._bt
_ptr
)
759 def _graph_mip_version(self
):
760 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
761 return native_bt
.self_component_get_graph_mip_version(ptr
)
763 def __init__(self
, config
, params
, obj
):
766 def _user_finalize(self
):
769 def _user_port_connected(self
, port
, other_port
):
772 def _bt_port_connected_from_native(
773 self
, self_port_ptr
, self_port_type
, other_port_ptr
775 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
777 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
778 other_port_type
= native_bt
.PORT_TYPE_INPUT
780 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
782 other_port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(
783 other_port_ptr
, other_port_type
785 self
._user
_port
_connected
(port
, other_port
)
787 def _create_trace_class(
788 self
, user_attributes
=None, assigns_automatic_stream_class_id
=True
790 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
791 tc_ptr
= native_bt
.trace_class_create(ptr
)
794 raise bt2
._MemoryError("could not create trace class")
796 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
797 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
799 if user_attributes
is not None:
800 tc
._user
_attributes
= user_attributes
804 def _create_clock_class(
808 user_attributes
=None,
812 origin_is_unix_epoch
=True,
815 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
816 cc_ptr
= native_bt
.clock_class_create(ptr
)
819 raise bt2
._MemoryError("could not create clock class")
821 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
823 if frequency
is not None:
824 cc
._frequency
= frequency
829 if user_attributes
is not None:
830 cc
._user
_attributes
= user_attributes
832 if description
is not None:
833 cc
._description
= description
835 if precision
is not None:
836 cc
._precision
= precision
838 if offset
is not None:
841 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
849 class _UserSourceComponent(_UserComponent
, _SourceComponentConst
):
850 _bt_as_not_self_specific_component_ptr
= staticmethod(
851 native_bt
.self_component_source_as_component_source
853 _bt_as_self_component_ptr
= staticmethod(
854 native_bt
.self_component_source_as_self_component
856 _config_pycls
= _UserSourceComponentConfiguration
859 def _output_ports(self
):
860 def get_output_port_count(self_ptr
):
861 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
862 return native_bt
.component_source_get_output_port_count(ptr
)
864 return _ComponentPorts(
866 native_bt
.self_component_source_borrow_output_port_by_name
,
867 native_bt
.self_component_source_borrow_output_port_by_index
,
868 get_output_port_count
,
869 bt2_port
._UserComponentOutputPort
,
872 def _add_output_port(self
, name
, user_data
=None):
873 bt2_utils
._check
_str
(name
)
875 if name
in self
._output
_ports
:
877 "source component `{}` already contains an output port named `{}`".format(
882 fn
= native_bt
.self_component_source_add_output_port
883 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
884 bt2_utils
._handle
_func
_status
(
885 comp_status
, "cannot add output port to source component object"
887 assert self_port_ptr
is not None
888 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
893 class _UserFilterComponent(_UserComponent
, _FilterComponentConst
):
894 _bt_as_not_self_specific_component_ptr
= staticmethod(
895 native_bt
.self_component_filter_as_component_filter
897 _bt_as_self_component_ptr
= staticmethod(
898 native_bt
.self_component_filter_as_self_component
900 _config_pycls
= _UserFilterComponentConfiguration
903 def _output_ports(self
):
904 def get_output_port_count(self_ptr
):
905 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
906 return native_bt
.component_filter_get_output_port_count(ptr
)
908 return _ComponentPorts(
910 native_bt
.self_component_filter_borrow_output_port_by_name
,
911 native_bt
.self_component_filter_borrow_output_port_by_index
,
912 get_output_port_count
,
913 bt2_port
._UserComponentOutputPort
,
917 def _input_ports(self
):
918 def get_input_port_count(self_ptr
):
919 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
920 return native_bt
.component_filter_get_input_port_count(ptr
)
922 return _ComponentPorts(
924 native_bt
.self_component_filter_borrow_input_port_by_name
,
925 native_bt
.self_component_filter_borrow_input_port_by_index
,
926 get_input_port_count
,
927 bt2_port
._UserComponentInputPort
,
930 def _add_output_port(self
, name
, user_data
=None):
931 bt2_utils
._check
_str
(name
)
933 if name
in self
._output
_ports
:
935 "filter component `{}` already contains an output port named `{}`".format(
940 fn
= native_bt
.self_component_filter_add_output_port
941 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
942 bt2_utils
._handle
_func
_status
(
943 comp_status
, "cannot add output port to filter component object"
946 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
950 def _add_input_port(self
, name
, user_data
=None):
951 bt2_utils
._check
_str
(name
)
953 if name
in self
._input
_ports
:
955 "filter component `{}` already contains an input port named `{}`".format(
960 fn
= native_bt
.self_component_filter_add_input_port
961 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
962 bt2_utils
._handle
_func
_status
(
963 comp_status
, "cannot add input port to filter component object"
966 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
971 class _UserSinkComponent(_UserComponent
, _SinkComponentConst
):
972 _bt_as_not_self_specific_component_ptr
= staticmethod(
973 native_bt
.self_component_sink_as_component_sink
975 _bt_as_self_component_ptr
= staticmethod(
976 native_bt
.self_component_sink_as_self_component
978 _config_pycls
= _UserSinkComponentConfiguration
980 def _bt_graph_is_configured_from_native(self
):
981 self
._user
_graph
_is
_configured
()
983 def _user_graph_is_configured(self
):
987 def _input_ports(self
):
988 def get_input_port_count(self_ptr
):
989 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
990 return native_bt
.component_sink_get_input_port_count(ptr
)
992 return _ComponentPorts(
994 native_bt
.self_component_sink_borrow_input_port_by_name
,
995 native_bt
.self_component_sink_borrow_input_port_by_index
,
996 get_input_port_count
,
997 bt2_port
._UserComponentInputPort
,
1000 def _add_input_port(self
, name
, user_data
=None):
1001 bt2_utils
._check
_str
(name
)
1003 if name
in self
._input
_ports
:
1005 "sink component `{}` already contains an input port named `{}`".format(
1010 fn
= native_bt
.self_component_sink_add_input_port
1011 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
1012 bt2_utils
._handle
_func
_status
(
1013 comp_status
, "cannot add input port to sink component object"
1015 assert self_port_ptr
1016 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
1020 def _create_message_iterator(self
, input_port
):
1021 bt2_utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
1023 if not input_port
.is_connected
:
1024 raise ValueError("input port is not connected")
1029 ) = native_bt
.bt2_message_iterator_create_from_sink_component(
1030 self
._bt
_ptr
, input_port
._ptr
1032 bt2_utils
._handle
_func
_status
(status
, "cannot create message iterator object")
1033 assert msg_iter_ptr
is not None
1035 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
1038 def _is_interrupted(self
):
1039 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))