1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from bt2
import native_bt
, object, utils
24 from bt2
import message_iterator
as bt2_message_iterator
25 import collections
.abc
26 from bt2
import value
as bt2_value
27 from bt2
import trace_class
as bt2_trace_class
28 from bt2
import clock_class
as bt2_clock_class
29 from bt2
import query_executor
as bt2_query_executor
30 from bt2
import port
as bt2_port
35 # This class wraps a component class pointer. This component class could
36 # have been created by Python code, but since we only have the pointer,
37 # we can only wrap it in a generic way and lose the original Python
40 # Subclasses must implement some methods that this base class uses:
42 # - _bt_as_component_class_ptr: static method, convert the passed component class
43 # pointer to a 'bt_component_class *'.
46 class _ComponentClassConst(object._SharedObject
):
49 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
50 name
= native_bt
.component_class_get_name(ptr
)
51 assert name
is not None
55 def description(self
):
56 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
57 return native_bt
.component_class_get_description(ptr
)
61 ptr
= self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
62 return native_bt
.component_class_get_help(ptr
)
64 def _bt_component_class_ptr(self
):
65 return self
._bt
_as
_component
_class
_ptr
(self
._ptr
)
67 def __eq__(self
, other
):
68 if not isinstance(other
, _ComponentClassConst
):
70 if not issubclass(other
, _UserComponent
):
75 return self
.addr
== other
.addr
78 class _SourceComponentClassConst(_ComponentClassConst
):
79 _get_ref
= staticmethod(native_bt
.component_class_source_get_ref
)
80 _put_ref
= staticmethod(native_bt
.component_class_source_put_ref
)
81 _bt_as_component_class_ptr
= staticmethod(
82 native_bt
.component_class_source_as_component_class
86 class _FilterComponentClassConst(_ComponentClassConst
):
87 _get_ref
= staticmethod(native_bt
.component_class_filter_get_ref
)
88 _put_ref
= staticmethod(native_bt
.component_class_filter_put_ref
)
89 _bt_as_component_class_ptr
= staticmethod(
90 native_bt
.component_class_filter_as_component_class
94 class _SinkComponentClassConst(_ComponentClassConst
):
95 _get_ref
= staticmethod(native_bt
.component_class_sink_get_ref
)
96 _put_ref
= staticmethod(native_bt
.component_class_sink_put_ref
)
97 _bt_as_component_class_ptr
= staticmethod(
98 native_bt
.component_class_sink_as_component_class
102 class _PortIterator(collections
.abc
.Iterator
):
103 def __init__(self
, comp_ports
):
104 self
._comp
_ports
= comp_ports
108 if self
._at
== len(self
._comp
_ports
):
111 comp_ports
= self
._comp
_ports
112 comp_ptr
= comp_ports
._component
_ptr
114 port_ptr
= comp_ports
._borrow
_port
_ptr
_at
_index
(comp_ptr
, self
._at
)
115 assert port_ptr
is not None
117 name
= native_bt
.port_get_name(comp_ports
._port
_pycls
._as
_port
_ptr
(port_ptr
))
118 assert name
is not None
124 class _ComponentPorts(collections
.abc
.Mapping
):
126 # component_ptr is a bt_component_source *, bt_component_filter * or
127 # bt_component_sink *. Its type must match the type expected by the
128 # functions passed as arguments.
133 borrow_port_ptr_by_name
,
134 borrow_port_ptr_at_index
,
138 self
._component
_ptr
= component_ptr
139 self
._borrow
_port
_ptr
_by
_name
= borrow_port_ptr_by_name
140 self
._borrow
_port
_ptr
_at
_index
= borrow_port_ptr_at_index
141 self
._get
_port
_count
= get_port_count
142 self
._port
_pycls
= port_pycls
144 def __getitem__(self
, key
):
145 utils
._check
_str
(key
)
146 port_ptr
= self
._borrow
_port
_ptr
_by
_name
(self
._component
_ptr
, key
)
151 return self
._port
_pycls
._create
_from
_ptr
_and
_get
_ref
(port_ptr
)
154 count
= self
._get
_port
_count
(self
._component
_ptr
)
159 return _PortIterator(self
)
162 # This class holds the methods which are common to both generic
163 # component objects and Python user component objects.
165 # Subclasses must provide these methods or property:
167 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
168 # specialized component class (e.g. 'bt_component_class_sink *') of the
169 # passed specialized component pointer (e.g. 'bt_component_sink *').
170 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
172 # - _bt_as_component_ptr: static method, must return the passed specialized
173 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
176 class _ComponentConst
:
179 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
180 name
= native_bt
.component_get_name(ptr
)
181 assert name
is not None
185 def logging_level(self
):
186 ptr
= self
._bt
_as
_component
_ptr
(self
._ptr
)
187 return native_bt
.component_get_logging_level(ptr
)
191 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(self
._ptr
)
192 assert cc_ptr
is not None
193 return _create_component_class_from_const_ptr_and_get_ref(
194 cc_ptr
, self
._bt
_comp
_cls
_type
197 def __eq__(self
, other
):
198 if not hasattr(other
, 'addr'):
201 return self
.addr
== other
.addr
204 class _SourceComponentConst(_ComponentConst
):
205 _bt_borrow_component_class_ptr
= staticmethod(
206 native_bt
.component_source_borrow_class_const
208 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
209 _bt_as_component_class_ptr
= staticmethod(
210 native_bt
.component_class_source_as_component_class
212 _bt_as_component_ptr
= staticmethod(native_bt
.component_source_as_component_const
)
215 class _FilterComponentConst(_ComponentConst
):
216 _bt_borrow_component_class_ptr
= staticmethod(
217 native_bt
.component_filter_borrow_class_const
219 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
220 _bt_as_component_class_ptr
= staticmethod(
221 native_bt
.component_class_filter_as_component_class
223 _bt_as_component_ptr
= staticmethod(native_bt
.component_filter_as_component_const
)
226 class _SinkComponentConst(_ComponentConst
):
227 _bt_borrow_component_class_ptr
= staticmethod(
228 native_bt
.component_sink_borrow_class_const
230 _bt_comp_cls_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
231 _bt_as_component_class_ptr
= staticmethod(
232 native_bt
.component_class_sink_as_component_class
234 _bt_as_component_ptr
= staticmethod(native_bt
.component_sink_as_component_const
)
237 # This is analogous to _SourceComponentClassConst, but for source
239 class _GenericSourceComponentConst(object._SharedObject
, _SourceComponentConst
):
240 _get_ref
= staticmethod(native_bt
.component_source_get_ref
)
241 _put_ref
= staticmethod(native_bt
.component_source_put_ref
)
244 def output_ports(self
):
245 return _ComponentPorts(
247 native_bt
.component_source_borrow_output_port_by_name_const
,
248 native_bt
.component_source_borrow_output_port_by_index_const
,
249 native_bt
.component_source_get_output_port_count
,
250 bt2_port
._OutputPortConst
,
254 # This is analogous to _FilterComponentClassConst, but for filter
256 class _GenericFilterComponentConst(object._SharedObject
, _FilterComponentConst
):
257 _get_ref
= staticmethod(native_bt
.component_filter_get_ref
)
258 _put_ref
= staticmethod(native_bt
.component_filter_put_ref
)
261 def output_ports(self
):
262 return _ComponentPorts(
264 native_bt
.component_filter_borrow_output_port_by_name_const
,
265 native_bt
.component_filter_borrow_output_port_by_index_const
,
266 native_bt
.component_filter_get_output_port_count
,
267 bt2_port
._OutputPortConst
,
271 def input_ports(self
):
272 return _ComponentPorts(
274 native_bt
.component_filter_borrow_input_port_by_name_const
,
275 native_bt
.component_filter_borrow_input_port_by_index_const
,
276 native_bt
.component_filter_get_input_port_count
,
277 bt2_port
._InputPortConst
,
281 # This is analogous to _SinkComponentClassConst, but for sink
283 class _GenericSinkComponentConst(object._SharedObject
, _SinkComponentConst
):
284 _get_ref
= staticmethod(native_bt
.component_sink_get_ref
)
285 _put_ref
= staticmethod(native_bt
.component_sink_put_ref
)
288 def input_ports(self
):
289 return _ComponentPorts(
291 native_bt
.component_sink_borrow_input_port_by_name_const
,
292 native_bt
.component_sink_borrow_input_port_by_index_const
,
293 native_bt
.component_sink_get_input_port_count
,
294 bt2_port
._InputPortConst
,
298 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
299 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentConst
,
300 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentConst
,
301 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentConst
,
305 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
306 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _SourceComponentClassConst
,
307 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _FilterComponentClassConst
,
308 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _SinkComponentClassConst
,
312 # Create a component Python object of type _GenericSourceComponentConst,
313 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
316 # Steals the reference to ptr from the caller.
319 def _create_component_from_const_ptr(ptr
, comp_cls_type
):
320 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
323 # Same as the above, but acquire a new reference instead of stealing the
324 # reference from the caller.
327 def _create_component_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
328 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[
330 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
333 # Create a component class Python object of type
334 # _SourceComponentClassConst, _FilterComponentClassConst or
335 # _SinkComponentClassConst, depending on comp_cls_type.
337 # Acquires a new reference to ptr.
340 def _create_component_class_from_const_ptr_and_get_ref(ptr
, comp_cls_type
):
341 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[
343 ]._create
_from
_ptr
_and
_get
_ref
(ptr
)
346 def _trim_docstring(docstring
):
347 lines
= docstring
.expandtabs().splitlines()
355 for line
in lines
[1:]:
356 stripped
= line
.lstrip()
359 indent
= min(indent
, len(line
) - len(stripped
))
361 trimmed
= [lines
[0].strip()]
363 if indent
< sys
.maxsize
and len(lines
) > 1:
364 for line
in lines
[1:]:
365 trimmed
.append(line
[indent
:].rstrip())
367 while trimmed
and not trimmed
[-1]:
370 while trimmed
and not trimmed
[0]:
373 return '\n'.join(trimmed
)
376 # Metaclass for component classes defined by Python code.
378 # The Python user can create a standard Python class which inherits one
379 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
380 # or _UserSinkComponent). Those base classes set this class
381 # (_UserComponentType) as their metaclass.
383 # Once the body of a user-defined component class is executed, this
384 # metaclass is used to create and initialize the class. The metaclass
385 # creates a native BT component class of the corresponding type and
386 # associates it with this user-defined class. The metaclass also defines
387 # class methods like the `name` and `description` properties to match
388 # the _ComponentClassConst interface.
390 # The component class name which is used is either:
392 # * The `name` parameter of the class:
394 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
397 # * If the `name` class parameter is not used: the name of the class
398 # itself (`MySink` in the example above).
400 # The component class description which is used is the user-defined
403 # class MySink(bt2.SinkComponent):
404 # 'Description goes here'
407 # A user-defined Python component class can have an __init__() method
408 # which must at least accept the `params` and `name` arguments:
410 # def __init__(self, params, name, something_else):
413 # The user-defined component class can also have a _finalize() method
414 # (do NOT use __del__()) to be notified when the component object is
417 # User-defined source and filter component classes must use the
418 # `message_iterator_class` class parameter to specify the
419 # message iterator class to use for this component class:
421 # class MyMessageIterator(bt2._UserMessageIterator):
424 # class MySource(bt2._UserSourceComponent,
425 # message_iterator_class=MyMessageIterator):
428 # This message iterator class must inherit
429 # bt2._UserMessageIterator, and it must define the _get() and
430 # _next() methods. The message iterator class can also define an
431 # __init__() method: this method has access to the original Python
432 # component object which was used to create it as the `component`
433 # property. The message iterator class can also define a
434 # _finalize() method (again, do NOT use __del__()): this is called when
435 # the message iterator is (really) destroyed.
437 # When the user-defined class is destroyed, this metaclass's __del__()
438 # method is called: the native BT component class pointer is put (not
439 # needed anymore, at least not by any Python code since all references
440 # are dropped for __del__() to be called).
441 class _UserComponentType(type):
442 # __new__() is used to catch custom class parameters
443 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
444 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
446 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
447 super().__init
__(class_name
, bases
, namespace
)
449 # skip our own bases; they are never directly instantiated by the user
452 '_UserFilterSinkComponent',
453 '_UserSourceComponent',
454 '_UserFilterComponent',
455 '_UserSinkComponent',
458 if class_name
in own_bases
:
461 comp_cls_name
= kwargs
.get('name', class_name
)
462 utils
._check
_str
(comp_cls_name
)
463 comp_cls_descr
= None
466 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
467 utils
._check
_str
(cls
.__doc
__)
468 docstring
= _trim_docstring(cls
.__doc
__)
469 lines
= docstring
.splitlines()
472 comp_cls_descr
= lines
[0]
475 comp_cls_help
= '\n'.join(lines
[2:])
477 iter_cls
= kwargs
.get('message_iterator_class')
479 if _UserSourceComponent
in bases
:
480 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
481 cc_ptr
= native_bt
.bt2_component_class_source_create(
482 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
484 elif _UserFilterComponent
in bases
:
485 _UserComponentType
._bt
_set
_iterator
_class
(cls
, iter_cls
)
486 cc_ptr
= native_bt
.bt2_component_class_filter_create(
487 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
489 elif _UserSinkComponent
in bases
:
490 if not hasattr(cls
, '_user_consume'):
491 raise bt2
._IncompleteUserClass(
492 "cannot create component class '{}': missing a _user_consume() method".format(
497 cc_ptr
= native_bt
.bt2_component_class_sink_create(
498 cls
, comp_cls_name
, comp_cls_descr
, comp_cls_help
501 raise bt2
._IncompleteUserClass(
502 "cannot find a known component class base in the bases of '{}'".format(
508 raise bt2
._MemoryError(
509 "cannot create component class '{}'".format(class_name
)
512 cls
._bt
_cc
_ptr
= cc_ptr
514 def _bt_init_from_native(cls
, comp_ptr
, params_ptr
, obj
):
515 # create instance, not user-initialized yet
516 self
= cls
.__new
__(cls
)
519 config
= cls
._config
_pycls
()
521 # pointer to native self component object (weak/borrowed)
522 self
._bt
_ptr
= comp_ptr
524 # call user's __init__() method
525 if params_ptr
is not None:
526 params
= bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
(params_ptr
)
530 self
.__init
__(config
, params
, obj
)
533 def __call__(cls
, *args
, **kwargs
):
535 'cannot directly instantiate a user component from a Python module'
539 def _bt_set_iterator_class(cls
, iter_cls
):
541 raise bt2
._IncompleteUserClass(
542 "cannot create component class '{}': missing message iterator class".format(
547 if not issubclass(iter_cls
, bt2_message_iterator
._UserMessageIterator
):
548 raise bt2
._IncompleteUserClass(
549 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
554 if not hasattr(iter_cls
, '__next__'):
555 raise bt2
._IncompleteUserClass(
556 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
561 if hasattr(iter_cls
, '_user_can_seek_ns_from_origin') and not hasattr(
562 iter_cls
, '_user_seek_ns_from_origin'
564 raise bt2
._IncompleteUserClass(
565 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
570 if hasattr(iter_cls
, '_user_can_seek_beginning') and not hasattr(
571 iter_cls
, '_user_seek_beginning'
573 raise bt2
._IncompleteUserClass(
574 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
579 cls
._iter
_cls
= iter_cls
583 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
584 return native_bt
.component_class_get_name(ptr
)
587 def description(cls
):
588 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
589 return native_bt
.component_class_get_description(ptr
)
593 ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
594 return native_bt
.component_class_get_help(ptr
)
598 return int(cls
._bt
_cc
_ptr
)
600 def _bt_get_supported_mip_versions_from_native(cls
, params_ptr
, obj
, log_level
):
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 # this can raise, but the native side checks the exception
608 range_set
= cls
._user
_get
_supported
_mip
_versions
(params
, obj
, log_level
)
610 if type(range_set
) is not bt2
.UnsignedIntegerRangeSet
:
611 # this can raise, but the native side checks the exception
612 range_set
= bt2
.UnsignedIntegerRangeSet(range_set
)
614 # return new reference
615 range_set
._get
_ref
(range_set
._ptr
)
616 return int(range_set
._ptr
)
618 def _user_get_supported_mip_versions(cls
, params
, obj
, log_level
):
621 def _bt_query_from_native(
622 cls
, priv_query_exec_ptr
, object_name
, params_ptr
, method_obj
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 priv_query_exec
= bt2_query_executor
._PrivateQueryExecutor
(priv_query_exec_ptr
)
633 # this can raise, but the native side checks the exception
634 results
= cls
._user
_query
(priv_query_exec
, object_name
, params
, method_obj
)
636 # the private query executor is a private view on the query
637 # executor; it's not a shared object (the library does not
638 # offer an API to get/put a reference, just like "self"
639 # objects) from this query's point of view, so invalidate
640 # the object in case the user kept a reference and uses it
642 priv_query_exec
._invalidate
()
644 # this can raise, but the native side checks the exception
645 results
= bt2
.create_value(results
)
648 results_ptr
= native_bt
.value_null
650 results_ptr
= results
._ptr
652 # return new reference
653 bt2_value
._Value
._get
_ref
(results_ptr
)
654 return int(results_ptr
)
656 def _user_query(cls
, priv_query_executor
, object_name
, params
, method_obj
):
657 raise bt2
.UnknownObject
659 def _bt_component_class_ptr(self
):
660 return self
._bt
_as
_component
_class
_ptr
(self
._bt
_cc
_ptr
)
663 if hasattr(cls
, '_bt_cc_ptr'):
664 cc_ptr
= cls
._bt
_as
_component
_class
_ptr
(cls
._bt
_cc
_ptr
)
665 native_bt
.component_class_put_ref(cc_ptr
)
666 native_bt
.bt2_unregister_cc_ptr_to_py_cls(cc_ptr
)
669 # Configuration objects for components.
671 # These are passed in __init__ to allow components to change some configuration
672 # parameters during initialization and not after. As you can see, they are not
673 # used at the moment, but are there in case we want to add such parameters.
676 class _UserComponentConfiguration
:
680 class _UserSourceComponentConfiguration(_UserComponentConfiguration
):
684 class _UserFilterComponentConfiguration(_UserComponentConfiguration
):
688 class _UserSinkComponentConfiguration(_UserComponentConfiguration
):
692 # Subclasses must provide these methods or property:
694 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
695 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
696 # specialized non-self pointer (e.g. 'bt_component_sink *').
697 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
698 # specialized component class (e.g. 'bt_component_class_sink *') of the
699 # passed specialized component pointer (e.g. 'bt_component_sink *').
700 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
704 class _UserComponent(metaclass
=_UserComponentType
):
707 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
708 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
709 name
= native_bt
.component_get_name(ptr
)
710 assert name
is not None
714 def logging_level(self
):
715 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
716 ptr
= self
._bt
_as
_component
_ptr
(ptr
)
717 return native_bt
.component_get_logging_level(ptr
)
721 comp_ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self
._bt
_ptr
)
722 cc_ptr
= self
._bt
_borrow
_component
_class
_ptr
(comp_ptr
)
723 return _create_component_class_from_const_ptr_and_get_ref(
724 cc_ptr
, self
._bt
_comp
_cls
_type
729 return int(self
._bt
_ptr
)
732 def _graph_mip_version(self
):
733 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
734 return native_bt
.self_component_get_graph_mip_version(ptr
)
736 def __init__(self
, config
, params
, obj
):
739 def _user_finalize(self
):
742 def _user_port_connected(self
, port
, other_port
):
745 def _bt_port_connected_from_native(
746 self
, self_port_ptr
, self_port_type
, other_port_ptr
748 port
= bt2_port
._create
_self
_from
_ptr
_and
_get
_ref
(self_port_ptr
, self_port_type
)
750 if self_port_type
== native_bt
.PORT_TYPE_OUTPUT
:
751 other_port_type
= native_bt
.PORT_TYPE_INPUT
753 other_port_type
= native_bt
.PORT_TYPE_OUTPUT
755 other_port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(
756 other_port_ptr
, other_port_type
758 self
._user
_port
_connected
(port
, other_port
)
760 def _create_trace_class(
761 self
, user_attributes
=None, assigns_automatic_stream_class_id
=True
763 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
764 tc_ptr
= native_bt
.trace_class_create(ptr
)
767 raise bt2
._MemoryError('could not create trace class')
769 tc
= bt2_trace_class
._TraceClass
._create
_from
_ptr
(tc_ptr
)
770 tc
._assigns
_automatic
_stream
_class
_id
= assigns_automatic_stream_class_id
772 if user_attributes
is not None:
773 tc
._user
_attributes
= user_attributes
777 def _create_clock_class(
781 user_attributes
=None,
785 origin_is_unix_epoch
=True,
788 ptr
= self
._bt
_as
_self
_component
_ptr
(self
._bt
_ptr
)
789 cc_ptr
= native_bt
.clock_class_create(ptr
)
792 raise bt2
._MemoryError('could not create clock class')
794 cc
= bt2_clock_class
._ClockClass
._create
_from
_ptr
(cc_ptr
)
796 if frequency
is not None:
797 cc
._frequency
= frequency
802 if user_attributes
is not None:
803 cc
._user
_attributes
= user_attributes
805 if description
is not None:
806 cc
._description
= description
808 if precision
is not None:
809 cc
._precision
= precision
811 if offset
is not None:
814 cc
._origin
_is
_unix
_epoch
= origin_is_unix_epoch
822 class _UserSourceComponent(_UserComponent
, _SourceComponentConst
):
823 _bt_as_not_self_specific_component_ptr
= staticmethod(
824 native_bt
.self_component_source_as_component_source
826 _bt_as_self_component_ptr
= staticmethod(
827 native_bt
.self_component_source_as_self_component
829 _config_pycls
= _UserSourceComponentConfiguration
832 def _output_ports(self
):
833 def get_output_port_count(self_ptr
):
834 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
835 return native_bt
.component_source_get_output_port_count(ptr
)
837 return _ComponentPorts(
839 native_bt
.self_component_source_borrow_output_port_by_name
,
840 native_bt
.self_component_source_borrow_output_port_by_index
,
841 get_output_port_count
,
842 bt2_port
._UserComponentOutputPort
,
845 def _add_output_port(self
, name
, user_data
=None):
846 utils
._check
_str
(name
)
847 fn
= native_bt
.self_component_source_add_output_port
848 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
849 utils
._handle
_func
_status
(
850 comp_status
, 'cannot add output port to source component object'
852 assert self_port_ptr
is not None
853 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
858 class _UserFilterComponent(_UserComponent
, _FilterComponentConst
):
859 _bt_as_not_self_specific_component_ptr
= staticmethod(
860 native_bt
.self_component_filter_as_component_filter
862 _bt_as_self_component_ptr
= staticmethod(
863 native_bt
.self_component_filter_as_self_component
865 _config_pycls
= _UserFilterComponentConfiguration
868 def _output_ports(self
):
869 def get_output_port_count(self_ptr
):
870 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
871 return native_bt
.component_filter_get_output_port_count(ptr
)
873 return _ComponentPorts(
875 native_bt
.self_component_filter_borrow_output_port_by_name
,
876 native_bt
.self_component_filter_borrow_output_port_by_index
,
877 get_output_port_count
,
878 bt2_port
._UserComponentOutputPort
,
882 def _input_ports(self
):
883 def get_input_port_count(self_ptr
):
884 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
885 return native_bt
.component_filter_get_input_port_count(ptr
)
887 return _ComponentPorts(
889 native_bt
.self_component_filter_borrow_input_port_by_name
,
890 native_bt
.self_component_filter_borrow_input_port_by_index
,
891 get_input_port_count
,
892 bt2_port
._UserComponentInputPort
,
895 def _add_output_port(self
, name
, user_data
=None):
896 utils
._check
_str
(name
)
897 fn
= native_bt
.self_component_filter_add_output_port
898 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
899 utils
._handle
_func
_status
(
900 comp_status
, 'cannot add output port to filter component object'
903 return bt2_port
._UserComponentOutputPort
._create
_from
_ptr
_and
_get
_ref
(
907 def _add_input_port(self
, name
, user_data
=None):
908 utils
._check
_str
(name
)
909 fn
= native_bt
.self_component_filter_add_input_port
910 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
911 utils
._handle
_func
_status
(
912 comp_status
, 'cannot add input port to filter component object'
915 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
920 class _UserSinkComponent(_UserComponent
, _SinkComponentConst
):
921 _bt_as_not_self_specific_component_ptr
= staticmethod(
922 native_bt
.self_component_sink_as_component_sink
924 _bt_as_self_component_ptr
= staticmethod(
925 native_bt
.self_component_sink_as_self_component
927 _config_pycls
= _UserSinkComponentConfiguration
929 def _bt_graph_is_configured_from_native(self
):
930 self
._user
_graph
_is
_configured
()
932 def _user_graph_is_configured(self
):
936 def _input_ports(self
):
937 def get_input_port_count(self_ptr
):
938 ptr
= self
._bt
_as
_not
_self
_specific
_component
_ptr
(self_ptr
)
939 return native_bt
.component_sink_get_input_port_count(ptr
)
941 return _ComponentPorts(
943 native_bt
.self_component_sink_borrow_input_port_by_name
,
944 native_bt
.self_component_sink_borrow_input_port_by_index
,
945 get_input_port_count
,
946 bt2_port
._UserComponentInputPort
,
949 def _add_input_port(self
, name
, user_data
=None):
950 utils
._check
_str
(name
)
951 fn
= native_bt
.self_component_sink_add_input_port
952 comp_status
, self_port_ptr
= fn(self
._bt
_ptr
, name
, user_data
)
953 utils
._handle
_func
_status
(
954 comp_status
, 'cannot add input port to sink component object'
957 return bt2_port
._UserComponentInputPort
._create
_from
_ptr
_and
_get
_ref
(
961 def _create_message_iterator(self
, input_port
):
962 utils
._check
_type
(input_port
, bt2_port
._UserComponentInputPort
)
967 ) = native_bt
.bt2_message_iterator_create_from_sink_component(
968 self
._bt
_ptr
, input_port
._ptr
970 utils
._handle
_func
_status
(status
, 'cannot create message iterator object')
971 assert msg_iter_ptr
is not None
973 return bt2_message_iterator
._UserComponentInputPortMessageIterator
(msg_iter_ptr
)
976 def _is_interrupted(self
):
977 return bool(native_bt
.self_component_sink_is_interrupted(self
._bt
_ptr
))