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 import bt2
.notification_iterator
25 import collections
.abc
34 _env_var
= os
.environ
.get('BABELTRACE_PYTHON_BT2_NO_TRACEBACK')
35 _NO_PRINT_TRACEBACK
= _env_var
== '1'
38 # This class wraps a component class pointer. This component class could
39 # have been created by Python code, but since we only have the pointer,
40 # we can only wrap it in a generic way and lose the original Python
42 class _GenericComponentClass(object._Object
):
45 name
= native_bt
.component_class_get_name(self
._ptr
)
46 assert(name
is not None)
50 def description(self
):
51 return native_bt
.component_class_get_description(self
._ptr
)
55 return native_bt
.component_class_get_help(self
._ptr
)
57 def __eq__(self
, other
):
58 if not isinstance(other
, _GenericComponentClass
):
60 if not issubclass(other
, _UserComponent
):
65 return self
.addr
== other
.addr
68 class _GenericSourceComponentClass(_GenericComponentClass
):
72 class _GenericFilterComponentClass(_GenericComponentClass
):
76 class _GenericSinkComponentClass(_GenericComponentClass
):
80 def _handle_component_status(status
, gen_error_msg
):
81 if status
== native_bt
.COMPONENT_STATUS_END
:
83 elif status
== native_bt
.COMPONENT_STATUS_AGAIN
:
85 elif status
== native_bt
.COMPONENT_STATUS_UNSUPPORTED
:
86 raise bt2
.UnsupportedFeature
87 elif status
== native_bt
.COMPONENT_STATUS_REFUSE_PORT_CONNECTION
:
88 raise bt2
.PortConnectionRefused
89 elif status
== native_bt
.COMPONENT_STATUS_GRAPH_IS_CANCELED
:
90 raise bt2
.GraphCanceled
92 raise bt2
.Error(gen_error_msg
)
95 class _PortIterator(collections
.abc
.Iterator
):
96 def __init__(self
, comp_ports
):
97 self
._comp
_ports
= comp_ports
101 if self
._at
== len(self
._comp
_ports
):
104 comp_ports
= self
._comp
_ports
105 comp_ptr
= comp_ports
._component
._ptr
106 port_ptr
= comp_ports
._get
_port
_at
_index
_fn
(comp_ptr
, self
._at
)
109 if comp_ports
._is
_private
:
110 port_pub_ptr
= native_bt
.port_from_private_port(port_ptr
)
111 name
= native_bt
.port_get_name(port_pub_ptr
)
112 native_bt
.put(port_pub_ptr
)
114 name
= native_bt
.port_get_name(port_ptr
)
116 assert(name
is not None)
117 native_bt
.put(port_ptr
)
122 class _ComponentPorts(collections
.abc
.Mapping
):
123 def __init__(self
, is_private
, component
,
124 get_port_by_name_fn
, get_port_at_index_fn
,
126 self
._is
_private
= is_private
127 self
._component
= component
128 self
._get
_port
_by
_name
_fn
= get_port_by_name_fn
129 self
._get
_port
_at
_index
_fn
= get_port_at_index_fn
130 self
._get
_port
_count
_fn
= get_port_count_fn
132 def __getitem__(self
, key
):
133 utils
._check
_str
(key
)
134 port_ptr
= self
._get
_port
_by
_name
_fn
(self
._component
._ptr
, key
)
140 return bt2
.port
._create
_private
_from
_ptr
(port_ptr
)
142 return bt2
.port
._create
_from
_ptr
(port_ptr
)
146 pub_ptr
= native_bt
.component_from_private_component(self
._component
._ptr
)
147 count
= self
._get
_port
_count
_fn
(pub_ptr
)
148 native_bt
.put(pub_ptr
)
150 count
= self
._get
_port
_count
_fn
(self
._component
._ptr
)
156 return _PortIterator(self
)
159 # This class holds the methods which are common to both generic
160 # component objects and Python user component objects. They use the
161 # internal native _ptr, however it was set, to call native API
166 name
= native_bt
.component_get_name(self
._ptr
)
167 assert(name
is not None)
172 ptr
= native_bt
.component_get_graph(self
._ptr
)
174 return bt2
.Graph
._create
_from
_ptr
(ptr
)
177 def component_class(self
):
178 cc_ptr
= native_bt
.component_get_class(self
._ptr
)
180 return _create_generic_component_class_from_ptr(cc_ptr
)
182 def __eq__(self
, other
):
183 if not hasattr(other
, 'addr'):
186 return self
.addr
== other
.addr
189 class _SourceComponent(_Component
):
193 class _FilterComponent(_Component
):
197 class _SinkComponent(_Component
):
201 # This is analogous to _GenericSourceComponentClass, but for source
203 class _GenericSourceComponent(object._Object
, _SourceComponent
):
205 def output_ports(self
):
206 return _ComponentPorts(False, self
,
207 native_bt
.component_source_get_output_port_by_name
,
208 native_bt
.component_source_get_output_port_by_index
,
209 native_bt
.component_source_get_output_port_count
)
212 # This is analogous to _GenericFilterComponentClass, but for filter
214 class _GenericFilterComponent(object._Object
, _FilterComponent
):
216 def output_ports(self
):
217 return _ComponentPorts(False, self
,
218 native_bt
.component_filter_get_output_port_by_name
,
219 native_bt
.component_filter_get_output_port_by_index
,
220 native_bt
.component_filter_get_output_port_count
)
223 def input_ports(self
):
224 return _ComponentPorts(False, self
,
225 native_bt
.component_filter_get_input_port_by_name
,
226 native_bt
.component_filter_get_input_port_by_index
,
227 native_bt
.component_filter_get_input_port_count
)
230 # This is analogous to _GenericSinkComponentClass, but for sink
232 class _GenericSinkComponent(object._Object
, _SinkComponent
):
234 def input_ports(self
):
235 return _ComponentPorts(False, self
,
236 native_bt
.component_sink_get_input_port_by_name
,
237 native_bt
.component_sink_get_input_port_by_index
,
238 native_bt
.component_sink_get_input_port_count
)
241 _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
= {
242 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponent
,
243 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponent
,
244 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponent
,
248 _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
= {
249 native_bt
.COMPONENT_CLASS_TYPE_SOURCE
: _GenericSourceComponentClass
,
250 native_bt
.COMPONENT_CLASS_TYPE_FILTER
: _GenericFilterComponentClass
,
251 native_bt
.COMPONENT_CLASS_TYPE_SINK
: _GenericSinkComponentClass
,
255 def _create_generic_component_from_ptr(ptr
):
256 comp_cls_type
= native_bt
.component_get_class_type(ptr
)
257 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
260 def _create_generic_component_class_from_ptr(ptr
):
261 comp_cls_type
= native_bt
.component_class_get_type(ptr
)
262 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS
[comp_cls_type
]._create
_from
_ptr
(ptr
)
265 def _trim_docstring(docstring
):
266 lines
= docstring
.expandtabs().splitlines()
269 for line
in lines
[1:]:
270 stripped
= line
.lstrip()
273 indent
= min(indent
, len(line
) - len(stripped
))
275 trimmed
= [lines
[0].strip()]
277 if indent
< sys
.maxsize
:
278 for line
in lines
[1:]:
279 trimmed
.append(line
[indent
:].rstrip())
281 while trimmed
and not trimmed
[-1]:
284 while trimmed
and not trimmed
[0]:
287 return '\n'.join(trimmed
)
290 # Metaclass for component classes defined by Python code.
292 # The Python user can create a standard Python class which inherits one
293 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
294 # or _UserSinkComponent). Those base classes set this class
295 # (_UserComponentType) as their metaclass.
297 # Once the body of a user-defined component class is executed, this
298 # metaclass is used to create and initialize the class. The metaclass
299 # creates a native BT component class of the corresponding type and
300 # associates it with this user-defined class. The metaclass also defines
301 # class methods like the `name` and `description` properties to match
302 # the _GenericComponentClass interface.
304 # The component class name which is used is either:
306 # * The `name` parameter of the class:
308 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
311 # * If the `name` class parameter is not used: the name of the class
312 # itself (`MySink` in the example above).
314 # The component class description which is used is the user-defined
317 # class MySink(bt2.SinkComponent):
318 # 'Description goes here'
321 # A user-defined Python component class can have an __init__() method
322 # which must at least accept the `params` and `name` arguments:
324 # def __init__(self, params, name, something_else):
327 # The user-defined component class can also have a _finalize() method
328 # (do NOT use __del__()) to be notified when the component object is
331 # User-defined source and filter component classes must use the
332 # `notification_iterator_class` class parameter to specify the
333 # notification iterator class to use for this component class:
335 # class MyNotificationIterator(bt2._UserNotificationIterator):
338 # class MySource(bt2._UserSourceComponent,
339 # notification_iterator_class=MyNotificationIterator):
342 # This notification iterator class must inherit
343 # bt2._UserNotificationIterator, and it must define the _get() and
344 # _next() methods. The notification iterator class can also define an
345 # __init__() method: this method has access to the original Python
346 # component object which was used to create it as the `component`
347 # property. The notification iterator class can also define a
348 # _finalize() method (again, do NOT use __del__()): this is called when
349 # the notification iterator is (really) destroyed.
351 # When the user-defined class is destroyed, this metaclass's __del__()
352 # method is called: the native BT component class pointer is put (not
353 # needed anymore, at least not by any Python code since all references
354 # are dropped for __del__() to be called).
355 class _UserComponentType(type):
356 # __new__() is used to catch custom class parameters
357 def __new__(meta_cls
, class_name
, bases
, attrs
, **kwargs
):
358 return super().__new
__(meta_cls
, class_name
, bases
, attrs
)
360 def __init__(cls
, class_name
, bases
, namespace
, **kwargs
):
361 super().__init
__(class_name
, bases
, namespace
)
363 # skip our own bases; they are never directly instantiated by the user
366 '_UserFilterSinkComponent',
367 '_UserSourceComponent',
368 '_UserFilterComponent',
369 '_UserSinkComponent',
372 if class_name
in own_bases
:
375 comp_cls_name
= kwargs
.get('name', class_name
)
376 utils
._check
_str
(comp_cls_name
)
377 comp_cls_descr
= None
380 if hasattr(cls
, '__doc__') and cls
.__doc
__ is not None:
381 utils
._check
_str
(cls
.__doc
__)
382 docstring
= _trim_docstring(cls
.__doc
__)
383 lines
= docstring
.splitlines()
386 comp_cls_descr
= lines
[0]
389 comp_cls_help
= '\n'.join(lines
[2:])
391 iter_cls
= kwargs
.get('notification_iterator_class')
393 if _UserSourceComponent
in bases
:
394 _UserComponentType
._set
_iterator
_class
(cls
, iter_cls
)
395 cc_ptr
= native_bt
.py3_component_class_source_create(cls
,
399 elif _UserFilterComponent
in bases
:
400 _UserComponentType
._set
_iterator
_class
(cls
, iter_cls
)
401 cc_ptr
= native_bt
.py3_component_class_filter_create(cls
,
405 elif _UserSinkComponent
in bases
:
406 if not hasattr(cls
, '_consume'):
407 raise bt2
.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name
))
409 cc_ptr
= native_bt
.py3_component_class_sink_create(cls
,
414 raise bt2
.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name
))
417 raise bt2
.CreationError("cannot create component class '{}'".format(class_name
))
421 def _init_from_native(cls
, comp_ptr
, params_ptr
):
422 # create instance, not user-initialized yet
423 self
= cls
.__new
__(cls
)
425 # pointer to native private component object (weak/borrowed)
428 # call user's __init__() method
429 if params_ptr
is not None:
430 native_bt
.get(params_ptr
)
431 params
= bt2
.values
._create
_from
_ptr
(params_ptr
)
435 self
.__init
__(params
)
438 def __call__(cls
, *args
, **kwargs
):
439 raise bt2
.Error('cannot directly instantiate a user component from a Python module')
442 def _set_iterator_class(cls
, iter_cls
):
444 raise bt2
.IncompleteUserClass("cannot create component class '{}': missing notification iterator class".format(cls
.__name
__))
446 if not issubclass(iter_cls
, bt2
.notification_iterator
._UserNotificationIterator
):
447 raise bt2
.IncompleteUserClass("cannot create component class '{}': notification iterator class does not inherit bt2._UserNotificationIterator".format(cls
.__name
__))
449 if not hasattr(iter_cls
, '__next__'):
450 raise bt2
.IncompleteUserClass("cannot create component class '{}': notification iterator class is missing a __next__() method".format(cls
.__name
__))
452 cls
._iter
_cls
= iter_cls
456 return native_bt
.component_class_get_name(cls
._cc
_ptr
)
459 def description(cls
):
460 return native_bt
.component_class_get_description(cls
._cc
_ptr
)
464 return native_bt
.component_class_get_help(cls
._cc
_ptr
)
468 return int(cls
._cc
_ptr
)
470 def _query_from_native(cls
, query_exec_ptr
, obj
, params_ptr
):
471 # this can raise, in which case the native call to
472 # bt_component_class_query() returns NULL
473 if params_ptr
is not None:
474 native_bt
.get(params_ptr
)
475 params
= bt2
.values
._create
_from
_ptr
(params_ptr
)
479 native_bt
.get(query_exec_ptr
)
480 query_exec
= bt2
.QueryExecutor
._create
_from
_ptr
(query_exec_ptr
)
482 # this can raise, but the native side checks the exception
483 results
= cls
._query
(query_exec
, obj
, params
)
485 if results
is NotImplemented:
488 # this can raise, but the native side checks the exception
489 results
= bt2
.create_value(results
)
492 results_addr
= int(native_bt
.value_null
)
494 # return new reference
496 results_addr
= int(results
._ptr
)
500 def _query(cls
, query_executor
, obj
, params
):
501 # BT catches this and returns NULL to the user
502 return NotImplemented
504 def __eq__(self
, other
):
505 if not hasattr(other
, 'addr'):
508 return self
.addr
== other
.addr
511 if hasattr(cls
, '_cc_ptr'):
512 native_bt
.put(cls
._cc
_ptr
)
515 class _UserComponent(metaclass
=_UserComponentType
):
518 pub_ptr
= native_bt
.component_from_private_component(self
._ptr
)
519 name
= native_bt
.component_get_name(pub_ptr
)
520 native_bt
.put(pub_ptr
)
521 assert(name
is not None)
526 pub_ptr
= native_bt
.component_from_private_component(self
._ptr
)
527 ptr
= native_bt
.component_get_graph(pub_ptr
)
528 native_bt
.put(pub_ptr
)
530 return bt2
.Graph
._create
_from
_ptr
(ptr
)
533 def component_class(self
):
534 pub_ptr
= native_bt
.component_from_private_component(self
._ptr
)
535 cc_ptr
= native_bt
.component_get_class(pub_ptr
)
536 native_bt
.put(pub_ptr
)
538 return _create_generic_component_class_from_ptr(cc_ptr
)
542 return int(self
._ptr
)
544 def __init__(self
, params
=None):
550 def _accept_port_connection(self
, port
, other_port
):
553 def _accept_port_connection_from_native(self
, port_ptr
, other_port_ptr
):
554 native_bt
.get(port_ptr
)
555 native_bt
.get(other_port_ptr
)
556 port
= bt2
.port
._create
_private
_from
_ptr
(port_ptr
)
557 other_port
= bt2
.port
._create
_from
_ptr
(other_port_ptr
)
558 res
= self
._accept
_port
_connection
(port
, other_port_ptr
)
560 if type(res
) is not bool:
561 raise TypeError("'{}' is not a 'bool' object")
565 def _port_connected(self
, port
, other_port
):
568 def _port_connected_from_native(self
, port_ptr
, other_port_ptr
):
569 native_bt
.get(port_ptr
)
570 native_bt
.get(other_port_ptr
)
571 port
= bt2
.port
._create
_private
_from
_ptr
(port_ptr
)
572 other_port
= bt2
.port
._create
_from
_ptr
(other_port_ptr
)
575 self
._port
_connected
(port
, other_port_ptr
)
577 if not _NO_PRINT_TRACEBACK
:
578 traceback
.print_exc()
580 def _port_disconnected(self
, port
):
583 def _port_disconnected_from_native(self
, port_ptr
):
584 native_bt
.get(port_ptr
)
585 port
= bt2
.port
._create
_private
_from
_ptr
(port_ptr
)
588 self
._port
_disconnected
(port
)
590 if not _NO_PRINT_TRACEBACK
:
591 traceback
.print_exc()
594 class _UserSourceComponent(_UserComponent
, _SourceComponent
):
596 def _output_ports(self
):
597 return _ComponentPorts(True, self
,
598 native_bt
.private_component_source_get_output_private_port_by_name
,
599 native_bt
.private_component_source_get_output_private_port_by_index
,
600 native_bt
.component_source_get_output_port_count
)
602 def _add_output_port(self
, name
):
603 utils
._check
_str
(name
)
604 fn
= native_bt
.private_component_source_add_output_private_port
605 comp_status
, priv_port_ptr
= fn(self
._ptr
, name
, None)
606 _handle_component_status(comp_status
,
607 'cannot add output port to source component object')
608 assert(priv_port_ptr
)
609 return bt2
.port
._create
_private
_from
_ptr
(priv_port_ptr
)
612 class _UserFilterComponent(_UserComponent
, _FilterComponent
):
614 def _output_ports(self
):
615 return _ComponentPorts(True, self
,
616 native_bt
.private_component_filter_get_output_private_port_by_name
,
617 native_bt
.private_component_filter_get_output_private_port_by_index
,
618 native_bt
.component_filter_get_output_port_count
)
621 def _input_ports(self
):
622 return _ComponentPorts(True, self
,
623 native_bt
.private_component_filter_get_input_private_port_by_name
,
624 native_bt
.private_component_filter_get_input_private_port_by_index
,
625 native_bt
.component_filter_get_input_port_count
)
627 def _add_output_port(self
, name
):
628 utils
._check
_str
(name
)
629 fn
= native_bt
.private_component_filter_add_output_private_port
630 comp_status
, priv_port_ptr
= fn(self
._ptr
, name
, None)
631 _handle_component_status(comp_status
,
632 'cannot add output port to filter component object')
633 assert(priv_port_ptr
)
634 return bt2
.port
._create
_private
_from
_ptr
(priv_port_ptr
)
636 def _add_input_port(self
, name
):
637 utils
._check
_str
(name
)
638 fn
= native_bt
.private_component_filter_add_input_private_port
639 comp_status
, priv_port_ptr
= fn(self
._ptr
, name
, None)
640 _handle_component_status(comp_status
,
641 'cannot add input port to filter component object')
642 assert(priv_port_ptr
)
643 return bt2
.port
._create
_private
_from
_ptr
(priv_port_ptr
)
646 class _UserSinkComponent(_UserComponent
, _SinkComponent
):
648 def _input_ports(self
):
649 return _ComponentPorts(True, self
,
650 native_bt
.private_component_sink_get_input_private_port_by_name
,
651 native_bt
.private_component_sink_get_input_private_port_by_index
,
652 native_bt
.component_sink_get_input_port_count
)
654 def _add_input_port(self
, name
):
655 utils
._check
_str
(name
)
656 fn
= native_bt
.private_component_sink_add_input_private_port
657 comp_status
, priv_port_ptr
= fn(self
._ptr
, name
, None)
658 _handle_component_status(comp_status
,
659 'cannot add input port to sink component object')
660 assert(priv_port_ptr
)
661 return bt2
.port
._create
_private
_from
_ptr
(priv_port_ptr
)