black: run `black` version 19.10b0 on entire project
[babeltrace.git] / src / bindings / python / bt2 / bt2 / component.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4 #
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:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
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
21 # THE SOFTWARE.
22
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
31 import sys
32 import bt2
33
34
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
38 # class.
39 #
40 # Subclasses must implement some methods that this base class uses:
41 #
42 # - _bt_as_component_class_ptr: static method, convert the passed component class
43 # pointer to a 'bt_component_class *'.
44
45
46 class _ComponentClassConst(object._SharedObject):
47 @property
48 def name(self):
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
52 return name
53
54 @property
55 def description(self):
56 ptr = self._bt_as_component_class_ptr(self._ptr)
57 return native_bt.component_class_get_description(ptr)
58
59 @property
60 def help(self):
61 ptr = self._bt_as_component_class_ptr(self._ptr)
62 return native_bt.component_class_get_help(ptr)
63
64 def _bt_component_class_ptr(self):
65 return self._bt_as_component_class_ptr(self._ptr)
66
67 def __eq__(self, other):
68 if not isinstance(other, _ComponentClassConst):
69 try:
70 if not issubclass(other, _UserComponent):
71 return False
72 except TypeError:
73 return False
74
75 return self.addr == other.addr
76
77
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
83 )
84
85
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
91 )
92
93
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
99 )
100
101
102 class _PortIterator(collections.abc.Iterator):
103 def __init__(self, comp_ports):
104 self._comp_ports = comp_ports
105 self._at = 0
106
107 def __next__(self):
108 if self._at == len(self._comp_ports):
109 raise StopIteration
110
111 comp_ports = self._comp_ports
112 comp_ptr = comp_ports._component_ptr
113
114 port_ptr = comp_ports._borrow_port_ptr_at_index(comp_ptr, self._at)
115 assert port_ptr is not None
116
117 name = native_bt.port_get_name(comp_ports._port_pycls._as_port_ptr(port_ptr))
118 assert name is not None
119
120 self._at += 1
121 return name
122
123
124 class _ComponentPorts(collections.abc.Mapping):
125
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.
129
130 def __init__(
131 self,
132 component_ptr,
133 borrow_port_ptr_by_name,
134 borrow_port_ptr_at_index,
135 get_port_count,
136 port_pycls,
137 ):
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
143
144 def __getitem__(self, key):
145 utils._check_str(key)
146 port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key)
147
148 if port_ptr is None:
149 raise KeyError(key)
150
151 return self._port_pycls._create_from_ptr_and_get_ref(port_ptr)
152
153 def __len__(self):
154 count = self._get_port_count(self._component_ptr)
155 assert count >= 0
156 return count
157
158 def __iter__(self):
159 return _PortIterator(self)
160
161
162 # This class holds the methods which are common to both generic
163 # component objects and Python user component objects.
164 #
165 # Subclasses must provide these methods or property:
166 #
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_*
171 # constants.
172 # - _bt_as_component_ptr: static method, must return the passed specialized
173 # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
174
175
176 class _ComponentConst:
177 @property
178 def name(self):
179 ptr = self._bt_as_component_ptr(self._ptr)
180 name = native_bt.component_get_name(ptr)
181 assert name is not None
182 return name
183
184 @property
185 def logging_level(self):
186 ptr = self._bt_as_component_ptr(self._ptr)
187 return native_bt.component_get_logging_level(ptr)
188
189 @property
190 def cls(self):
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
195 )
196
197 def __eq__(self, other):
198 if not hasattr(other, 'addr'):
199 return False
200
201 return self.addr == other.addr
202
203
204 class _SourceComponentConst(_ComponentConst):
205 _bt_borrow_component_class_ptr = staticmethod(
206 native_bt.component_source_borrow_class_const
207 )
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
211 )
212 _bt_as_component_ptr = staticmethod(native_bt.component_source_as_component_const)
213
214
215 class _FilterComponentConst(_ComponentConst):
216 _bt_borrow_component_class_ptr = staticmethod(
217 native_bt.component_filter_borrow_class_const
218 )
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
222 )
223 _bt_as_component_ptr = staticmethod(native_bt.component_filter_as_component_const)
224
225
226 class _SinkComponentConst(_ComponentConst):
227 _bt_borrow_component_class_ptr = staticmethod(
228 native_bt.component_sink_borrow_class_const
229 )
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
233 )
234 _bt_as_component_ptr = staticmethod(native_bt.component_sink_as_component_const)
235
236
237 # This is analogous to _SourceComponentClassConst, but for source
238 # component objects.
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)
242
243 @property
244 def output_ports(self):
245 return _ComponentPorts(
246 self._ptr,
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,
251 )
252
253
254 # This is analogous to _FilterComponentClassConst, but for filter
255 # component objects.
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)
259
260 @property
261 def output_ports(self):
262 return _ComponentPorts(
263 self._ptr,
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,
268 )
269
270 @property
271 def input_ports(self):
272 return _ComponentPorts(
273 self._ptr,
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,
278 )
279
280
281 # This is analogous to _SinkComponentClassConst, but for sink
282 # component objects.
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)
286
287 @property
288 def input_ports(self):
289 return _ComponentPorts(
290 self._ptr,
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,
295 )
296
297
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,
302 }
303
304
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,
309 }
310
311
312 # Create a component Python object of type _GenericSourceComponentConst,
313 # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
314 # comp_cls_type.
315 #
316 # Steals the reference to ptr from the caller.
317
318
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)
321
322
323 # Same as the above, but acquire a new reference instead of stealing the
324 # reference from the caller.
325
326
327 def _create_component_from_const_ptr_and_get_ref(ptr, comp_cls_type):
328 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[
329 comp_cls_type
330 ]._create_from_ptr_and_get_ref(ptr)
331
332
333 # Create a component class Python object of type
334 # _SourceComponentClassConst, _FilterComponentClassConst or
335 # _SinkComponentClassConst, depending on comp_cls_type.
336 #
337 # Acquires a new reference to ptr.
338
339
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[
342 comp_cls_type
343 ]._create_from_ptr_and_get_ref(ptr)
344
345
346 def _trim_docstring(docstring):
347 lines = docstring.expandtabs().splitlines()
348 indent = sys.maxsize
349
350 for line in lines[1:]:
351 stripped = line.lstrip()
352
353 if stripped:
354 indent = min(indent, len(line) - len(stripped))
355
356 trimmed = [lines[0].strip()]
357
358 if indent < sys.maxsize:
359 for line in lines[1:]:
360 trimmed.append(line[indent:].rstrip())
361
362 while trimmed and not trimmed[-1]:
363 trimmed.pop()
364
365 while trimmed and not trimmed[0]:
366 trimmed.pop(0)
367
368 return '\n'.join(trimmed)
369
370
371 # Metaclass for component classes defined by Python code.
372 #
373 # The Python user can create a standard Python class which inherits one
374 # of the three base classes (_UserSourceComponent, _UserFilterComponent,
375 # or _UserSinkComponent). Those base classes set this class
376 # (_UserComponentType) as their metaclass.
377 #
378 # Once the body of a user-defined component class is executed, this
379 # metaclass is used to create and initialize the class. The metaclass
380 # creates a native BT component class of the corresponding type and
381 # associates it with this user-defined class. The metaclass also defines
382 # class methods like the `name` and `description` properties to match
383 # the _ComponentClassConst interface.
384 #
385 # The component class name which is used is either:
386 #
387 # * The `name` parameter of the class:
388 #
389 # class MySink(bt2.SinkComponent, name='my-custom-sink'):
390 # ...
391 #
392 # * If the `name` class parameter is not used: the name of the class
393 # itself (`MySink` in the example above).
394 #
395 # The component class description which is used is the user-defined
396 # class's docstring:
397 #
398 # class MySink(bt2.SinkComponent):
399 # 'Description goes here'
400 # ...
401 #
402 # A user-defined Python component class can have an __init__() method
403 # which must at least accept the `params` and `name` arguments:
404 #
405 # def __init__(self, params, name, something_else):
406 # ...
407 #
408 # The user-defined component class can also have a _finalize() method
409 # (do NOT use __del__()) to be notified when the component object is
410 # finalized.
411 #
412 # User-defined source and filter component classes must use the
413 # `message_iterator_class` class parameter to specify the
414 # message iterator class to use for this component class:
415 #
416 # class MyMessageIterator(bt2._UserMessageIterator):
417 # ...
418 #
419 # class MySource(bt2._UserSourceComponent,
420 # message_iterator_class=MyMessageIterator):
421 # ...
422 #
423 # This message iterator class must inherit
424 # bt2._UserMessageIterator, and it must define the _get() and
425 # _next() methods. The message iterator class can also define an
426 # __init__() method: this method has access to the original Python
427 # component object which was used to create it as the `component`
428 # property. The message iterator class can also define a
429 # _finalize() method (again, do NOT use __del__()): this is called when
430 # the message iterator is (really) destroyed.
431 #
432 # When the user-defined class is destroyed, this metaclass's __del__()
433 # method is called: the native BT component class pointer is put (not
434 # needed anymore, at least not by any Python code since all references
435 # are dropped for __del__() to be called).
436 class _UserComponentType(type):
437 # __new__() is used to catch custom class parameters
438 def __new__(meta_cls, class_name, bases, attrs, **kwargs):
439 return super().__new__(meta_cls, class_name, bases, attrs)
440
441 def __init__(cls, class_name, bases, namespace, **kwargs):
442 super().__init__(class_name, bases, namespace)
443
444 # skip our own bases; they are never directly instantiated by the user
445 own_bases = (
446 '_UserComponent',
447 '_UserFilterSinkComponent',
448 '_UserSourceComponent',
449 '_UserFilterComponent',
450 '_UserSinkComponent',
451 )
452
453 if class_name in own_bases:
454 return
455
456 comp_cls_name = kwargs.get('name', class_name)
457 utils._check_str(comp_cls_name)
458 comp_cls_descr = None
459 comp_cls_help = None
460
461 if hasattr(cls, '__doc__') and cls.__doc__ is not None:
462 utils._check_str(cls.__doc__)
463 docstring = _trim_docstring(cls.__doc__)
464 lines = docstring.splitlines()
465
466 if len(lines) >= 1:
467 comp_cls_descr = lines[0]
468
469 if len(lines) >= 3:
470 comp_cls_help = '\n'.join(lines[2:])
471
472 iter_cls = kwargs.get('message_iterator_class')
473
474 if _UserSourceComponent in bases:
475 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
476 cc_ptr = native_bt.bt2_component_class_source_create(
477 cls, comp_cls_name, comp_cls_descr, comp_cls_help
478 )
479 elif _UserFilterComponent in bases:
480 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
481 cc_ptr = native_bt.bt2_component_class_filter_create(
482 cls, comp_cls_name, comp_cls_descr, comp_cls_help
483 )
484 elif _UserSinkComponent in bases:
485 if not hasattr(cls, '_user_consume'):
486 raise bt2._IncompleteUserClass(
487 "cannot create component class '{}': missing a _user_consume() method".format(
488 class_name
489 )
490 )
491
492 cc_ptr = native_bt.bt2_component_class_sink_create(
493 cls, comp_cls_name, comp_cls_descr, comp_cls_help
494 )
495 else:
496 raise bt2._IncompleteUserClass(
497 "cannot find a known component class base in the bases of '{}'".format(
498 class_name
499 )
500 )
501
502 if cc_ptr is None:
503 raise bt2._MemoryError(
504 "cannot create component class '{}'".format(class_name)
505 )
506
507 cls._bt_cc_ptr = cc_ptr
508
509 def _bt_init_from_native(cls, comp_ptr, params_ptr, obj):
510 # create instance, not user-initialized yet
511 self = cls.__new__(cls)
512
513 # config object
514 config = cls._config_pycls()
515
516 # pointer to native self component object (weak/borrowed)
517 self._bt_ptr = comp_ptr
518
519 # call user's __init__() method
520 if params_ptr is not None:
521 params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr)
522 else:
523 params = None
524
525 self.__init__(config, params, obj)
526 return self
527
528 def __call__(cls, *args, **kwargs):
529 raise RuntimeError(
530 'cannot directly instantiate a user component from a Python module'
531 )
532
533 @staticmethod
534 def _bt_set_iterator_class(cls, iter_cls):
535 if iter_cls is None:
536 raise bt2._IncompleteUserClass(
537 "cannot create component class '{}': missing message iterator class".format(
538 cls.__name__
539 )
540 )
541
542 if not issubclass(iter_cls, bt2_message_iterator._UserMessageIterator):
543 raise bt2._IncompleteUserClass(
544 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
545 cls.__name__
546 )
547 )
548
549 if not hasattr(iter_cls, '__next__'):
550 raise bt2._IncompleteUserClass(
551 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
552 cls.__name__
553 )
554 )
555
556 if hasattr(iter_cls, '_user_can_seek_ns_from_origin') and not hasattr(
557 iter_cls, '_user_seek_ns_from_origin'
558 ):
559 raise bt2._IncompleteUserClass(
560 "cannot create component class '{}': message iterator class implements _user_can_seek_ns_from_origin but not _user_seek_ns_from_origin".format(
561 cls.__name__
562 )
563 )
564
565 if hasattr(iter_cls, '_user_can_seek_beginning') and not hasattr(
566 iter_cls, '_user_seek_beginning'
567 ):
568 raise bt2._IncompleteUserClass(
569 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
570 cls.__name__
571 )
572 )
573
574 cls._iter_cls = iter_cls
575
576 @property
577 def name(cls):
578 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
579 return native_bt.component_class_get_name(ptr)
580
581 @property
582 def description(cls):
583 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
584 return native_bt.component_class_get_description(ptr)
585
586 @property
587 def help(cls):
588 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
589 return native_bt.component_class_get_help(ptr)
590
591 @property
592 def addr(cls):
593 return int(cls._bt_cc_ptr)
594
595 def _bt_get_supported_mip_versions_from_native(cls, params_ptr, obj, log_level):
596 # this can raise, but the native side checks the exception
597 if params_ptr is not None:
598 params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr)
599 else:
600 params = None
601
602 # this can raise, but the native side checks the exception
603 range_set = cls._user_get_supported_mip_versions(params, obj, log_level)
604
605 if type(range_set) is not bt2.UnsignedIntegerRangeSet:
606 # this can raise, but the native side checks the exception
607 range_set = bt2.UnsignedIntegerRangeSet(range_set)
608
609 # return new reference
610 range_set._get_ref(range_set._ptr)
611 return int(range_set._ptr)
612
613 def _user_get_supported_mip_versions(cls, params, obj, log_level):
614 return [0]
615
616 def _bt_query_from_native(cls, priv_query_exec_ptr, object, params_ptr, method_obj):
617 # this can raise, but the native side checks the exception
618 if params_ptr is not None:
619 params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr)
620 else:
621 params = None
622
623 priv_query_exec = bt2_query_executor._PrivateQueryExecutor(priv_query_exec_ptr)
624
625 try:
626 # this can raise, but the native side checks the exception
627 results = cls._user_query(priv_query_exec, object, params, method_obj)
628 finally:
629 # the private query executor is a private view on the query
630 # executor; it's not a shared object (the library does not
631 # offer an API to get/put a reference, just like "self"
632 # objects) from this query's point of view, so invalidate
633 # the object in case the user kept a reference and uses it
634 # later
635 priv_query_exec._invalidate()
636
637 # this can raise, but the native side checks the exception
638 results = bt2.create_value(results)
639
640 if results is None:
641 results_ptr = native_bt.value_null
642 else:
643 results_ptr = results._ptr
644
645 # return new reference
646 bt2_value._Value._get_ref(results_ptr)
647 return int(results_ptr)
648
649 def _user_query(cls, priv_query_executor, object, params, method_obj):
650 raise bt2.UnknownObject
651
652 def _bt_component_class_ptr(self):
653 return self._bt_as_component_class_ptr(self._bt_cc_ptr)
654
655 def __del__(cls):
656 if hasattr(cls, '_bt_cc_ptr'):
657 cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
658 native_bt.component_class_put_ref(cc_ptr)
659 native_bt.bt2_unregister_cc_ptr_to_py_cls(cc_ptr)
660
661
662 # Configuration objects for components.
663 #
664 # These are passed in __init__ to allow components to change some configuration
665 # parameters during initialization and not after. As you can see, they are not
666 # used at the moment, but are there in case we want to add such parameters.
667
668
669 class _UserComponentConfiguration:
670 pass
671
672
673 class _UserSourceComponentConfiguration(_UserComponentConfiguration):
674 pass
675
676
677 class _UserFilterComponentConfiguration(_UserComponentConfiguration):
678 pass
679
680
681 class _UserSinkComponentConfiguration(_UserComponentConfiguration):
682 pass
683
684
685 # Subclasses must provide these methods or property:
686 #
687 # - _bt_as_not_self_specific_component_ptr: static method, must return the passed
688 # specialized self component pointer (e.g. 'bt_self_component_sink *') as a
689 # specialized non-self pointer (e.g. 'bt_component_sink *').
690 # - _bt_borrow_component_class_ptr: static method, must return a pointer to the
691 # specialized component class (e.g. 'bt_component_class_sink *') of the
692 # passed specialized component pointer (e.g. 'bt_component_sink *').
693 # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
694 # constants.
695
696
697 class _UserComponent(metaclass=_UserComponentType):
698 @property
699 def name(self):
700 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
701 ptr = self._bt_as_component_ptr(ptr)
702 name = native_bt.component_get_name(ptr)
703 assert name is not None
704 return name
705
706 @property
707 def logging_level(self):
708 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
709 ptr = self._bt_as_component_ptr(ptr)
710 return native_bt.component_get_logging_level(ptr)
711
712 @property
713 def cls(self):
714 comp_ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
715 cc_ptr = self._bt_borrow_component_class_ptr(comp_ptr)
716 return _create_component_class_from_const_ptr_and_get_ref(
717 cc_ptr, self._bt_comp_cls_type
718 )
719
720 @property
721 def addr(self):
722 return int(self._bt_ptr)
723
724 @property
725 def _graph_mip_version(self):
726 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
727 return native_bt.self_component_get_graph_mip_version(ptr)
728
729 def __init__(self, config, params, obj):
730 pass
731
732 def _user_finalize(self):
733 pass
734
735 def _user_port_connected(self, port, other_port):
736 pass
737
738 def _bt_port_connected_from_native(
739 self, self_port_ptr, self_port_type, other_port_ptr
740 ):
741 port = bt2_port._create_self_from_ptr_and_get_ref(self_port_ptr, self_port_type)
742
743 if self_port_type == native_bt.PORT_TYPE_OUTPUT:
744 other_port_type = native_bt.PORT_TYPE_INPUT
745 else:
746 other_port_type = native_bt.PORT_TYPE_OUTPUT
747
748 other_port = bt2_port._create_from_const_ptr_and_get_ref(
749 other_port_ptr, other_port_type
750 )
751 self._user_port_connected(port, other_port)
752
753 def _create_trace_class(
754 self, user_attributes=None, assigns_automatic_stream_class_id=True
755 ):
756 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
757 tc_ptr = native_bt.trace_class_create(ptr)
758
759 if tc_ptr is None:
760 raise bt2._MemoryError('could not create trace class')
761
762 tc = bt2_trace_class._TraceClass._create_from_ptr(tc_ptr)
763 tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id
764
765 if user_attributes is not None:
766 tc._user_attributes = user_attributes
767
768 return tc
769
770 def _create_clock_class(
771 self,
772 frequency=None,
773 name=None,
774 user_attributes=None,
775 description=None,
776 precision=None,
777 offset=None,
778 origin_is_unix_epoch=True,
779 uuid=None,
780 ):
781 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
782 cc_ptr = native_bt.clock_class_create(ptr)
783
784 if cc_ptr is None:
785 raise bt2._MemoryError('could not create clock class')
786
787 cc = bt2_clock_class._ClockClass._create_from_ptr(cc_ptr)
788
789 if frequency is not None:
790 cc._frequency = frequency
791
792 if name is not None:
793 cc._name = name
794
795 if user_attributes is not None:
796 cc._user_attributes = user_attributes
797
798 if description is not None:
799 cc._description = description
800
801 if precision is not None:
802 cc._precision = precision
803
804 if offset is not None:
805 cc._offset = offset
806
807 cc._origin_is_unix_epoch = origin_is_unix_epoch
808
809 if uuid is not None:
810 cc._uuid = uuid
811
812 return cc
813
814
815 class _UserSourceComponent(_UserComponent, _SourceComponentConst):
816 _bt_as_not_self_specific_component_ptr = staticmethod(
817 native_bt.self_component_source_as_component_source
818 )
819 _bt_as_self_component_ptr = staticmethod(
820 native_bt.self_component_source_as_self_component
821 )
822 _config_pycls = _UserSourceComponentConfiguration
823
824 @property
825 def _output_ports(self):
826 def get_output_port_count(self_ptr):
827 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
828 return native_bt.component_source_get_output_port_count(ptr)
829
830 return _ComponentPorts(
831 self._bt_ptr,
832 native_bt.self_component_source_borrow_output_port_by_name,
833 native_bt.self_component_source_borrow_output_port_by_index,
834 get_output_port_count,
835 bt2_port._UserComponentOutputPort,
836 )
837
838 def _add_output_port(self, name, user_data=None):
839 utils._check_str(name)
840 fn = native_bt.self_component_source_add_output_port
841 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
842 utils._handle_func_status(
843 comp_status, 'cannot add output port to source component object'
844 )
845 assert self_port_ptr is not None
846 return bt2_port._UserComponentOutputPort._create_from_ptr(self_port_ptr)
847
848
849 class _UserFilterComponent(_UserComponent, _FilterComponentConst):
850 _bt_as_not_self_specific_component_ptr = staticmethod(
851 native_bt.self_component_filter_as_component_filter
852 )
853 _bt_as_self_component_ptr = staticmethod(
854 native_bt.self_component_filter_as_self_component
855 )
856 _config_pycls = _UserFilterComponentConfiguration
857
858 @property
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_filter_get_output_port_count(ptr)
863
864 return _ComponentPorts(
865 self._bt_ptr,
866 native_bt.self_component_filter_borrow_output_port_by_name,
867 native_bt.self_component_filter_borrow_output_port_by_index,
868 get_output_port_count,
869 bt2_port._UserComponentOutputPort,
870 )
871
872 @property
873 def _input_ports(self):
874 def get_input_port_count(self_ptr):
875 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
876 return native_bt.component_filter_get_input_port_count(ptr)
877
878 return _ComponentPorts(
879 self._bt_ptr,
880 native_bt.self_component_filter_borrow_input_port_by_name,
881 native_bt.self_component_filter_borrow_input_port_by_index,
882 get_input_port_count,
883 bt2_port._UserComponentInputPort,
884 )
885
886 def _add_output_port(self, name, user_data=None):
887 utils._check_str(name)
888 fn = native_bt.self_component_filter_add_output_port
889 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
890 utils._handle_func_status(
891 comp_status, 'cannot add output port to filter component object'
892 )
893 assert self_port_ptr
894 return bt2_port._UserComponentOutputPort._create_from_ptr(self_port_ptr)
895
896 def _add_input_port(self, name, user_data=None):
897 utils._check_str(name)
898 fn = native_bt.self_component_filter_add_input_port
899 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
900 utils._handle_func_status(
901 comp_status, 'cannot add input port to filter component object'
902 )
903 assert self_port_ptr
904 return bt2_port._UserComponentInputPort._create_from_ptr(self_port_ptr)
905
906
907 class _UserSinkComponent(_UserComponent, _SinkComponentConst):
908 _bt_as_not_self_specific_component_ptr = staticmethod(
909 native_bt.self_component_sink_as_component_sink
910 )
911 _bt_as_self_component_ptr = staticmethod(
912 native_bt.self_component_sink_as_self_component
913 )
914 _config_pycls = _UserSinkComponentConfiguration
915
916 def _bt_graph_is_configured_from_native(self):
917 self._user_graph_is_configured()
918
919 def _user_graph_is_configured(self):
920 pass
921
922 @property
923 def _input_ports(self):
924 def get_input_port_count(self_ptr):
925 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
926 return native_bt.component_sink_get_input_port_count(ptr)
927
928 return _ComponentPorts(
929 self._bt_ptr,
930 native_bt.self_component_sink_borrow_input_port_by_name,
931 native_bt.self_component_sink_borrow_input_port_by_index,
932 get_input_port_count,
933 bt2_port._UserComponentInputPort,
934 )
935
936 def _add_input_port(self, name, user_data=None):
937 utils._check_str(name)
938 fn = native_bt.self_component_sink_add_input_port
939 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
940 utils._handle_func_status(
941 comp_status, 'cannot add input port to sink component object'
942 )
943 assert self_port_ptr
944 return bt2_port._UserComponentInputPort._create_from_ptr(self_port_ptr)
945
946 def _create_input_port_message_iterator(self, input_port):
947 utils._check_type(input_port, bt2_port._UserComponentInputPort)
948
949 (
950 status,
951 msg_iter_ptr,
952 ) = native_bt.bt2_self_component_port_input_message_iterator_create_from_sink_component(
953 self._bt_ptr, input_port._ptr
954 )
955 utils._handle_func_status(status, 'cannot create message iterator object')
956 assert msg_iter_ptr is not None
957
958 return bt2_message_iterator._UserComponentInputPortMessageIterator(msg_iter_ptr)
959
960 @property
961 def _is_interrupted(self):
962 return bool(native_bt.self_component_sink_is_interrupted(self._bt_ptr))
This page took 0.078502 seconds and 4 git commands to generate.