1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
6 from bt2
import message_iterator
as bt2_message_iterator
8 from bt2
import value
as bt2_value
9 from bt2
import trace_class
as bt2_trace_class
10 from bt2
import clock_class
as bt2_clock_class
11 from bt2
import query_executor
as bt2_query_executor
12 from bt2
import port
as bt2_port
17 # This class wraps a component class pointer. This component class could
18 # have been created by Python code, but since we only have the pointer,
19 # we can only wrap it in a generic way and lose the original Python
22 # Subclasses must implement some methods that this base class uses:
24 # - _bt_as_component_class_ptr: static method, convert the passed component class
25 # pointer to a 'bt_component_class *'.
28 class _ComponentClassConst(object._SharedObject
):
31 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
32 name
= native_bt
.component_class_get_name(ptr
)
33 assert name
is not None
37 def description(self
):
38 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
39 return native_bt
.component_class_get_description(ptr
)
43 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
44 return native_bt
.component_class_get_help(ptr
)
46 def _bt_component_class_ptr(self
):
47 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
49 def __eq__(self
, other
):
50 if not isinstance(other
, _ComponentClassConst
):
52 if not issubclass(other
, _UserComponent
):
57 return self
.addr
== other
.addr
60 class _SourceComponentClassConst(_ComponentClassConst
):
61 _get_ref
= staticmethod(native_bt
.component_class_source_get_ref
)
62 _put_ref
= staticmethod(native_bt
.component_class_source_put_ref
)
63 _bt_as_component_class_ptr
= staticmethod(
64 native_bt
.component_class_source_as_component_class
68 class _FilterComponentClassConst(_ComponentClassConst
):
69 _get_ref
= staticmethod(native_bt
.component_class_filter_get_ref
)
70 _put_ref
= staticmethod(native_bt
.component_class_filter_put_ref
)
71 _bt_as_component_class_ptr
= staticmethod(
72 native_bt
.component_class_filter_as_component_class
76 class _SinkComponentClassConst(_ComponentClassConst
):
77 _get_ref
= staticmethod(native_bt
.component_class_sink_get_ref
)
78 _put_ref
= staticmethod(native_bt
.component_class_sink_put_ref
)
79 _bt_as_component_class_ptr
= staticmethod(
80 native_bt
.component_class_sink_as_component_class
84 class _PortIterator(collections
.abc
.Iterator
):
85 def __init__(self
, comp_ports
):
86 self
._comp
_ports
= comp_ports
90 if self
._at
== len(self
._comp
_ports
):
93 comp_ports
= self
._comp
_ports
94 comp_ptr
= comp_ports
._component
_ptr
96 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
97 assert port_ptr
is not None
99 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
100 assert name
is not None
106 class _ComponentPorts(collections
.abc
.Mapping
):
108 # component_ptr is a bt_component_source *, bt_component_filter * or
109 # bt_component_sink *. Its type must match the type expected by the
110 # functions passed as arguments.
115 borrow_port_ptr_by_name
,
116 borrow_port_ptr_at_index
,
120 self
._component
_ptr
= component_ptr
121 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
122 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
123 self
._get
_port
_count
= get_port_count
124 self
._port
_pycls
= port_pycls
126 def __getitem__(self
, key
):
127 utils
._check
_str
(key
)
128 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
133 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
136 count
= self
._get
_port
_count
(self
._component
_ptr
)
141 return _PortIterator(self
)
144 # This class holds the methods which are common to both generic
145 # component objects and Python user component objects.
147 # Subclasses must provide these methods or property:
149 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
150 # specialized component class (e.g. 'bt_component_class_sink *') of the
151 # passed specialized component pointer (e.g. 'bt_component_sink *').
152 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
154 # - _bt_as_component_ptr: static method, must return the passed specialized
155 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
158 class _ComponentConst
:
161 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
162 name
= native_bt
.component_get_name(ptr
)
163 assert name
is not None
167 def logging_level(self
):
168 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
169 return native_bt
.component_get_logging_level(ptr
)
173 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
174 assert cc_ptr
is not None
175 return _create_component_class_from_const_ptr_and_get_ref(
176 cc_ptr
, self
._bt
_comp
_cls
_type
179 def __eq__(self
, other
):
180 if not hasattr(other
, 'addr'):
183 return self
.addr
== other
.addr
186 class _SourceComponentConst(_ComponentConst
):
187 _bt_borrow_component_class_ptr
= staticmethod(
188 native_bt
.component_source_borrow_class_const
190 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
191 _bt_as_component_class_ptr
= staticmethod(
192 native_bt
.component_class_source_as_component_class
194 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
197 class _FilterComponentConst(_ComponentConst
):
198 _bt_borrow_component_class_ptr
= staticmethod(
199 native_bt
.component_filter_borrow_class_const
201 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
202 _bt_as_component_class_ptr
= staticmethod(
203 native_bt
.component_class_filter_as_component_class
205 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
208 class _SinkComponentConst(_ComponentConst
):
209 _bt_borrow_component_class_ptr
= staticmethod(
210 native_bt
.component_sink_borrow_class_const
212 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
213 _bt_as_component_class_ptr
= staticmethod(
214 native_bt
.component_class_sink_as_component_class
216 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
219 # This is analogous to _SourceComponentClassConst, but for source
221 class _GenericSourceComponentConst(object._SharedObject
, _SourceComponentConst
):
222 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
223 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
226 def output_ports(self
):
227 return _ComponentPorts(
229 native_bt
.component_source_borrow_output_port_by_name_const
,
230 native_bt
.component_source_borrow_output_port_by_index_const
,
231 native_bt
.component_source_get_output_port_count
,
232 bt2_port
._OutputPortConst
,
236 # This is analogous to _FilterComponentClassConst, but for filter
238 class _GenericFilterComponentConst(object._SharedObject
, _FilterComponentConst
):
239 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
240 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
243 def output_ports(self
):
244 return _ComponentPorts(
246 native_bt
.component_filter_borrow_output_port_by_name_const
,
247 native_bt
.component_filter_borrow_output_port_by_index_const
,
248 native_bt
.component_filter_get_output_port_count
,
249 bt2_port
._OutputPortConst
,
253 def input_ports(self
):
254 return _ComponentPorts(
256 native_bt
.component_filter_borrow_input_port_by_name_const
,
257 native_bt
.component_filter_borrow_input_port_by_index_const
,
258 native_bt
.component_filter_get_input_port_count
,
259 bt2_port
._InputPortConst
,
263 # This is analogous to _SinkComponentClassConst, but for sink
265 class _GenericSinkComponentConst(object._SharedObject
, _SinkComponentConst
):
266 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
267 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
270 def input_ports(self
):
271 return _ComponentPorts(
273 native_bt
.component_sink_borrow_input_port_by_name_const
,
274 native_bt
.component_sink_borrow_input_port_by_index_const
,
275 native_bt
.component_sink_get_input_port_count
,
276 bt2_port
._InputPortConst
,
280 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
281 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentConst
,
282 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentConst
,
283 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentConst
,
287 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
288 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClassConst
,
289 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClassConst
,
290 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClassConst
,
294 # Create a component Python object of type _GenericSourceComponentConst,
295 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
298 # Steals the reference to ptr from the caller.
301 def _create_component_from_const_ptr(ptr
, comp_cls_type
):
302 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
305 # Same as the above, but acquire a new reference instead of stealing the
306 # reference from the caller.
309 def _create_component_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
310 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
312 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
315 # Create a component class Python object of type
316 # _SourceComponentClassConst, _FilterComponentClassConst or
317 # _SinkComponentClassConst, depending on comp_cls_type.
319 # Acquires a new reference to ptr.
322 def _create_component_class_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
323 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
325 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
328 def _trim_docstring(docstring
):
329 lines
= docstring
.expandtabs().splitlines()
332 for line
in lines
[1:]:
333 stripped
= line
.lstrip()
336 indent
= min(indent
, len(line
) - len(stripped
))
338 trimmed
= [lines
[0].strip()]
340 if indent
< sys
.maxsize
:
341 for line
in lines
[1:]:
342 trimmed
.append(line
[indent
:].rstrip())
344 while trimmed
and not trimmed
[-1]:
347 while trimmed
and not trimmed
[0]:
350 return '\n'.join(trimmed
)
353 # Metaclass for component classes defined by Python code.
355 # The Python user can create a standard Python class which inherits one
356 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
357 # or _UserSinkComponent). Those base classes set this class
358 # (_UserComponentType) as their metaclass.
360 # Once the body of a user-defined component class is executed, this
361 # metaclass is used to create and initialize the class. The metaclass
362 # creates a native BT component class of the corresponding type and
363 # associates it with this user-defined class. The metaclass also defines
364 # class methods like the `name` and `description` properties to match
365 # the _ComponentClassConst interface.
367 # The component class name which is used is either:
369 # * The `name` parameter of the class:
371 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
374 # * If the `name` class parameter is not used: the name of the class
375 # itself (`MySink` in the example above).
377 # The component class description which is used is the user-defined
380 # class MySink(bt2.SinkComponent):
381 # 'Description goes here'
384 # A user-defined Python component class can have an __init__() method
385 # which must at least accept the `params` and `name` arguments:
387 # def __init__(self, params, name, something_else):
390 # The user-defined component class can also have a _finalize() method
391 # (do NOT use __del__()) to be notified when the component object is
394 # User-defined source and filter component classes must use the
395 # `message_iterator_class` class parameter to specify the
396 # message iterator class to use for this component class:
398 # class MyMessageIterator(bt2._UserMessageIterator):
401 # class MySource(bt2._UserSourceComponent,
402 # message_iterator_class=MyMessageIterator):
405 # This message iterator class must inherit
406 # bt2._UserMessageIterator, and it must define the _get() and
407 # _next() methods. The message iterator class can also define an
408 # __init__() method: this method has access to the original Python
409 # component object which was used to create it as the `component`
410 # property. The message iterator class can also define a
411 # _finalize() method (again, do NOT use __del__()): this is called when
412 # the message iterator is (really) destroyed.
414 # When the user-defined class is destroyed, this metaclass's __del__()
415 # method is called: the native BT component class pointer is put (not
416 # needed anymore, at least not by any Python code since all references
417 # are dropped for __del__() to be called).
418 class _UserComponentType(type):
419 # __new__() is used to catch custom class parameters
420 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
421 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
423 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
424 super().__init
__(class_name
, bases
, namespace
)
426 # skip our own bases; they are never directly instantiated by the user
429 '_UserFilterSinkComponent',
430 '_UserSourceComponent',
431 '_UserFilterComponent',
432 '_UserSinkComponent',
435 if class_name
in own_bases
:
438 comp_cls_name
= kwargs
.get('name', class_name
)
439 utils
._check
_str
(comp_cls_name
)
440 comp_cls_descr
= None
443 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
444 utils
._check
_str
(cls
.__doc
__)
445 docstring
= _trim_docstring(cls
.__doc
__)
446 lines
= docstring
.splitlines()
449 comp_cls_descr
= lines
[0]
452 comp_cls_help
= '\n'.join(lines
[2:])
454 iter_cls
= kwargs
.get('message_iterator_class')
456 if _UserSourceComponent
in bases
:
457 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
458 cc_ptr
= native_bt
.bt2_component_class_source_create(
459 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
461 elif _UserFilterComponent
in bases
:
462 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
463 cc_ptr
= native_bt
.bt2_component_class_filter_create(
464 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
466 elif _UserSinkComponent
in bases
:
467 if not hasattr(cls
, '_user_consume'):
468 raise bt2
._IncompleteUserClass(
469 "cannot create component class '{}': missing a _user_consume() method".format(
474 cc_ptr
= native_bt
.bt2_component_class_sink_create(
475 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
478 raise bt2
._IncompleteUserClass(
479 "cannot find a known component class base in the bases of '{}'".format(
485 raise bt2
._MemoryError(
486 "cannot create component class '{}'".format(class_name
)
489 cls
._bt
_cc
_ptr
= cc_ptr
491 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
, obj
):
492 # create instance, not user-initialized yet
493 self
= cls
.__new
__(cls
)
496 config
= cls
._config
_pycls
()
498 # pointer to native self component object (weak/borrowed)
499 self
._bt
_ptr
= comp_ptr
501 # call user's __init__() method
502 if params_ptr
is not None:
503 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
507 self
.__init
__(config
, params
, obj
)
510 def __call__(cls
, *args
, **kwargs
):
512 'cannot directly instantiate a user component from a Python module'
516 def _bt_set_iterator_class(cls
, iter_cls
):
518 raise bt2
._IncompleteUserClass(
519 "cannot create component class '{}': missing message iterator class".format(
524 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
525 raise bt2
._IncompleteUserClass(
526 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
531 if not hasattr(iter_cls
, '__next__'):
532 raise bt2
._IncompleteUserClass(
533 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
538 if hasattr(iter_cls
, '_user_can_seek_ns_from_origin') and not hasattr(
539 iter_cls
, '_user_seek_ns_from_origin'
541 raise bt2
._IncompleteUserClass(
542 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
547 if hasattr(iter_cls
, '_user_can_seek_beginning') and not hasattr(
548 iter_cls
, '_user_seek_beginning'
550 raise bt2
._IncompleteUserClass(
551 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
556 cls
._iter
_cls
= iter_cls
560 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
561 return native_bt
.component_class_get_name(ptr
)
564 def description(cls
):
565 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
566 return native_bt
.component_class_get_description(ptr
)
570 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
571 return native_bt
.component_class_get_help(ptr
)
575 return int(cls
._bt
_cc
_ptr
)
577 def _bt_get_supported_mip_versions_from_native(cls
, params_ptr
, obj
, log_level
):
578 # this can raise, but the native side checks the exception
579 if params_ptr
is not None:
580 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
584 # this can raise, but the native side checks the exception
585 range_set
= cls
._user
_get
_supported
_mip
_versions
(params
, obj
, log_level
)
587 if type(range_set
) is not bt2
.UnsignedIntegerRangeSet
:
588 # this can raise, but the native side checks the exception
589 range_set
= bt2
.UnsignedIntegerRangeSet(range_set
)
591 # return new reference
592 range_set
._get
_ref
(range_set
._ptr
)
593 return int(range_set
._ptr
)
595 def _user_get_supported_mip_versions(cls
, params
, obj
, log_level
):
598 def _bt_query_from_native(
599 cls
, priv_query_exec_ptr
, object_name
, params_ptr
, method_obj
601 # this can raise, but the native side checks the exception
602 if params_ptr
is not None:
603 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
607 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
610 # this can raise, but the native side checks the exception
611 results
= cls
._user
_query
(priv_query_exec
, object_name
, params
, method_obj
)
613 # the private query executor is a private view on the query
614 # executor; it's not a shared object (the library does not
615 # offer an API to get/put a reference, just like "self"
616 # objects) from this query's point of view, so invalidate
617 # the object in case the user kept a reference and uses it
619 priv_query_exec
._invalidate
()
621 # this can raise, but the native side checks the exception
622 results
= bt2
.create_value(results
)
625 results_ptr
= native_bt
.value_null
627 results_ptr
= results
._ptr
629 # return new reference
630 bt2_value
._Value
._get
_ref
(results_ptr
)
631 return int(results_ptr
)
633 def _user_query(cls
, priv_query_executor
, object_name
, params
, method_obj
):
634 raise bt2
.UnknownObject
636 def _bt_component_class_ptr(self
):
637 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
640 if hasattr(cls
, '_bt_cc_ptr'):
641 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
642 native_bt
.component_class_put_ref(cc_ptr
)
643 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
646 # Configuration objects for components.
648 # These are passed in __init__ to allow components to change some configuration
649 # parameters during initialization and not after. As you can see, they are not
650 # used at the moment, but are there in case we want to add such parameters.
653 class _UserComponentConfiguration
:
657 class _UserSourceComponentConfiguration(_UserComponentConfiguration
):
661 class _UserFilterComponentConfiguration(_UserComponentConfiguration
):
665 class _UserSinkComponentConfiguration(_UserComponentConfiguration
):
669 # Subclasses must provide these methods or property:
671 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
672 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
673 # specialized non-self pointer (e.g. 'bt_component_sink *').
674 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
675 # specialized component class (e.g. 'bt_component_class_sink *') of the
676 # passed specialized component pointer (e.g. 'bt_component_sink *').
677 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
681 class _UserComponent(metaclass
=_UserComponentType
):
684 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
685 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
686 name
= native_bt
.component_get_name(ptr
)
687 assert name
is not None
691 def logging_level(self
):
692 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
693 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
694 return native_bt
.component_get_logging_level(ptr
)
698 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
699 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
700 return _create_component_class_from_const_ptr_and_get_ref(
701 cc_ptr
, self
._bt
_comp
_cls
_type
706 return int(self
._bt
_ptr
)
709 def _graph_mip_version(self
):
710 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
711 return native_bt
.self_component_get_graph_mip_version(ptr
)
713 def __init__(self
, config
, params
, obj
):
716 def _user_finalize(self
):
719 def _user_port_connected(self
, port
, other_port
):
722 def _bt_port_connected_from_native(
723 self
, self_port_ptr
, self_port_type
, other_port_ptr
725 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
727 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
728 other_port_type
= native_bt
.PORT_TYPE_INPUT
730 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
732 other_port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(
733 other_port_ptr
, other_port_type
735 self
._user
_port
_connected
(port
, other_port
)
737 def _create_trace_class(
738 self
, user_attributes
=None, assigns_automatic_stream_class_id
=True
740 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
741 tc_ptr
= native_bt
.trace_class_create(ptr
)
744 raise bt2
._MemoryError('could not create trace class')
746 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
747 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
749 if user_attributes
is not None:
750 tc
._user
_attributes
= user_attributes
754 def _create_clock_class(
758 user_attributes
=None,
762 origin_is_unix_epoch
=True,
765 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
766 cc_ptr
= native_bt
.clock_class_create(ptr
)
769 raise bt2
._MemoryError('could not create clock class')
771 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
773 if frequency
is not None:
774 cc
._frequency
= frequency
779 if user_attributes
is not None:
780 cc
._user
_attributes
= user_attributes
782 if description
is not None:
783 cc
._description
= description
785 if precision
is not None:
786 cc
._precision
= precision
788 if offset
is not None:
791 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
799 class _UserSourceComponent(_UserComponent
, _SourceComponentConst
):
800 _bt_as_not_self_specific_component_ptr
= staticmethod(
801 native_bt
.self_component_source_as_component_source
803 _bt_as_self_component_ptr
= staticmethod(
804 native_bt
.self_component_source_as_self_component
806 _config_pycls
= _UserSourceComponentConfiguration
809 def _output_ports(self
):
810 def get_output_port_count(self_ptr
):
811 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
812 return native_bt
.component_source_get_output_port_count(ptr
)
814 return _ComponentPorts(
816 native_bt
.self_component_source_borrow_output_port_by_name
,
817 native_bt
.self_component_source_borrow_output_port_by_index
,
818 get_output_port_count
,
819 bt2_port
._UserComponentOutputPort
,
822 def _add_output_port(self
, name
, user_data
=None):
823 utils
._check
_str
(name
)
825 if name
in self
._output
_ports
:
827 'source component `{}` already contains an output port named `{}`'.format(
832 fn
= native_bt
.self_component_source_add_output_port
833 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
834 utils
._handle
_func
_status
(
835 comp_status
, 'cannot add output port to source component object'
837 assert self_port_ptr
is not None
838 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
843 class _UserFilterComponent(_UserComponent
, _FilterComponentConst
):
844 _bt_as_not_self_specific_component_ptr
= staticmethod(
845 native_bt
.self_component_filter_as_component_filter
847 _bt_as_self_component_ptr
= staticmethod(
848 native_bt
.self_component_filter_as_self_component
850 _config_pycls
= _UserFilterComponentConfiguration
853 def _output_ports(self
):
854 def get_output_port_count(self_ptr
):
855 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
856 return native_bt
.component_filter_get_output_port_count(ptr
)
858 return _ComponentPorts(
860 native_bt
.self_component_filter_borrow_output_port_by_name
,
861 native_bt
.self_component_filter_borrow_output_port_by_index
,
862 get_output_port_count
,
863 bt2_port
._UserComponentOutputPort
,
867 def _input_ports(self
):
868 def get_input_port_count(self_ptr
):
869 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
870 return native_bt
.component_filter_get_input_port_count(ptr
)
872 return _ComponentPorts(
874 native_bt
.self_component_filter_borrow_input_port_by_name
,
875 native_bt
.self_component_filter_borrow_input_port_by_index
,
876 get_input_port_count
,
877 bt2_port
._UserComponentInputPort
,
880 def _add_output_port(self
, name
, user_data
=None):
881 utils
._check
_str
(name
)
883 if name
in self
._output
_ports
:
885 'filter component `{}` already contains an output port named `{}`'.format(
890 fn
= native_bt
.self_component_filter_add_output_port
891 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
892 utils
._handle
_func
_status
(
893 comp_status
, 'cannot add output port to filter component object'
896 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
900 def _add_input_port(self
, name
, user_data
=None):
901 utils
._check
_str
(name
)
903 if name
in self
._input
_ports
:
905 'filter component `{}` already contains an input port named `{}`'.format(
910 fn
= native_bt
.self_component_filter_add_input_port
911 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
912 utils
._handle
_func
_status
(
913 comp_status
, 'cannot add input port to filter component object'
916 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
921 class _UserSinkComponent(_UserComponent
, _SinkComponentConst
):
922 _bt_as_not_self_specific_component_ptr
= staticmethod(
923 native_bt
.self_component_sink_as_component_sink
925 _bt_as_self_component_ptr
= staticmethod(
926 native_bt
.self_component_sink_as_self_component
928 _config_pycls
= _UserSinkComponentConfiguration
930 def _bt_graph_is_configured_from_native(self
):
931 self
._user
_graph
_is
_configured
()
933 def _user_graph_is_configured(self
):
937 def _input_ports(self
):
938 def get_input_port_count(self_ptr
):
939 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
940 return native_bt
.component_sink_get_input_port_count(ptr
)
942 return _ComponentPorts(
944 native_bt
.self_component_sink_borrow_input_port_by_name
,
945 native_bt
.self_component_sink_borrow_input_port_by_index
,
946 get_input_port_count
,
947 bt2_port
._UserComponentInputPort
,
950 def _add_input_port(self
, name
, user_data
=None):
951 utils
._check
_str
(name
)
953 if name
in self
._input
_ports
:
955 'sink component `{}` already contains an input port named `{}`'.format(
960 fn
= native_bt
.self_component_sink_add_input_port
961 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
962 utils
._handle
_func
_status
(
963 comp_status
, 'cannot add input port to sink component object'
966 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
970 def _create_message_iterator(self
, input_port
):
971 utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
973 if not input_port
.is_connected
:
974 raise ValueError('input port is not connected')
979 ) = native_bt
.bt2_message_iterator_create_from_sink_component(
980 self
._bt
_ptr
, input_port
._ptr
982 utils
._handle
_func
_status
(status
, 'cannot create message iterator object')
983 assert msg_iter_ptr
is not None
985 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
988 def _is_interrupted(self
):
989 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))