Fix: bt2: _trim_docstring(): docstring can have 0 or 1 line
[babeltrace.git] / src / bindings / python / bt2 / bt2 / component.py
CommitLineData
81447b5b
PP
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
23from bt2 import native_bt, object, utils
c946c9de 24from bt2 import message_iterator as bt2_message_iterator
81447b5b 25import collections.abc
c946c9de
PP
26from bt2 import value as bt2_value
27from bt2 import trace_class as bt2_trace_class
28from bt2 import clock_class as bt2_clock_class
29from bt2 import query_executor as bt2_query_executor
c946c9de 30from bt2 import port as bt2_port
40910fbb 31import sys
81447b5b 32import bt2
f6a5e476
PP
33
34
81447b5b
PP
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.
bbb3650f
SM
39#
40# Subclasses must implement some methods that this base class uses:
41#
deec48a6 42# - _bt_as_component_class_ptr: static method, convert the passed component class
bbb3650f
SM
43# pointer to a 'bt_component_class *'.
44
61d96b89 45
5265f5e3 46class _ComponentClassConst(object._SharedObject):
81447b5b
PP
47 @property
48 def name(self):
deec48a6 49 ptr = self._bt_as_component_class_ptr(self._ptr)
bbb3650f
SM
50 name = native_bt.component_class_get_name(ptr)
51 assert name is not None
f6a5e476 52 return name
81447b5b
PP
53
54 @property
55 def description(self):
deec48a6 56 ptr = self._bt_as_component_class_ptr(self._ptr)
bbb3650f 57 return native_bt.component_class_get_description(ptr)
81447b5b 58
40910fbb
PP
59 @property
60 def help(self):
deec48a6 61 ptr = self._bt_as_component_class_ptr(self._ptr)
bbb3650f
SM
62 return native_bt.component_class_get_help(ptr)
63
deec48a6
FD
64 def _bt_component_class_ptr(self):
65 return self._bt_as_component_class_ptr(self._ptr)
40910fbb 66
f6a5e476 67 def __eq__(self, other):
5265f5e3 68 if not isinstance(other, _ComponentClassConst):
f6a5e476
PP
69 try:
70 if not issubclass(other, _UserComponent):
71 return False
72 except TypeError:
73 return False
81447b5b 74
f6a5e476 75 return self.addr == other.addr
81447b5b
PP
76
77
5265f5e3 78class _SourceComponentClassConst(_ComponentClassConst):
a49e2cc3
PP
79 _get_ref = staticmethod(native_bt.component_class_source_get_ref)
80 _put_ref = staticmethod(native_bt.component_class_source_put_ref)
61d96b89
FD
81 _bt_as_component_class_ptr = staticmethod(
82 native_bt.component_class_source_as_component_class
83 )
81447b5b
PP
84
85
5265f5e3 86class _FilterComponentClassConst(_ComponentClassConst):
a49e2cc3
PP
87 _get_ref = staticmethod(native_bt.component_class_filter_get_ref)
88 _put_ref = staticmethod(native_bt.component_class_filter_put_ref)
61d96b89
FD
89 _bt_as_component_class_ptr = staticmethod(
90 native_bt.component_class_filter_as_component_class
91 )
81447b5b
PP
92
93
5265f5e3 94class _SinkComponentClassConst(_ComponentClassConst):
a49e2cc3
PP
95 _get_ref = staticmethod(native_bt.component_class_sink_get_ref)
96 _put_ref = staticmethod(native_bt.component_class_sink_put_ref)
61d96b89
FD
97 _bt_as_component_class_ptr = staticmethod(
98 native_bt.component_class_sink_as_component_class
99 )
81447b5b
PP
100
101
f6a5e476
PP
102class _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
bc5c9924
SM
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
f6a5e476 119
f6a5e476
PP
120 self._at += 1
121 return name
122
123
124class _ComponentPorts(collections.abc.Mapping):
bc5c9924
SM
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
61d96b89
FD
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 ):
bc5c9924
SM
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
f6a5e476
PP
143
144 def __getitem__(self, key):
145 utils._check_str(key)
bc5c9924 146 port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key)
f6a5e476
PP
147
148 if port_ptr is None:
149 raise KeyError(key)
150
bc5c9924 151 return self._port_pycls._create_from_ptr_and_get_ref(port_ptr)
f6a5e476
PP
152
153 def __len__(self):
bc5c9924
SM
154 count = self._get_port_count(self._component_ptr)
155 assert count >= 0
f6a5e476
PP
156 return count
157
158 def __iter__(self):
159 return _PortIterator(self)
160
161
81447b5b 162# This class holds the methods which are common to both generic
bbb3650f
SM
163# component objects and Python user component objects.
164#
165# Subclasses must provide these methods or property:
166#
deec48a6 167# - _bt_borrow_component_class_ptr: static method, must return a pointer to the
bbb3650f
SM
168# specialized component class (e.g. 'bt_component_class_sink *') of the
169# passed specialized component pointer (e.g. 'bt_component_sink *').
deec48a6 170# - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
bbb3650f 171# constants.
deec48a6 172# - _bt_as_component_ptr: static method, must return the passed specialized
e34fb94a 173# component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
bbb3650f 174
61d96b89 175
5265f5e3 176class _ComponentConst:
81447b5b
PP
177 @property
178 def name(self):
deec48a6 179 ptr = self._bt_as_component_ptr(self._ptr)
e34fb94a
SM
180 name = native_bt.component_get_name(ptr)
181 assert name is not None
f6a5e476
PP
182 return name
183
cc81b5ab
PP
184 @property
185 def logging_level(self):
deec48a6 186 ptr = self._bt_as_component_ptr(self._ptr)
cc81b5ab
PP
187 return native_bt.component_get_logging_level(ptr)
188
81447b5b 189 @property
c88be1c8 190 def cls(self):
deec48a6 191 cc_ptr = self._bt_borrow_component_class_ptr(self._ptr)
bbb3650f 192 assert cc_ptr is not None
5265f5e3 193 return _create_component_class_from_const_ptr_and_get_ref(
61d96b89
FD
194 cc_ptr, self._bt_comp_cls_type
195 )
81447b5b 196
f6a5e476
PP
197 def __eq__(self, other):
198 if not hasattr(other, 'addr'):
199 return False
81447b5b 200
f6a5e476 201 return self.addr == other.addr
81447b5b 202
81447b5b 203
5265f5e3 204class _SourceComponentConst(_ComponentConst):
61d96b89
FD
205 _bt_borrow_component_class_ptr = staticmethod(
206 native_bt.component_source_borrow_class_const
207 )
deec48a6 208 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
61d96b89
FD
209 _bt_as_component_class_ptr = staticmethod(
210 native_bt.component_class_source_as_component_class
211 )
deec48a6 212 _bt_as_component_ptr = staticmethod(native_bt.component_source_as_component_const)
81447b5b 213
81447b5b 214
5265f5e3 215class _FilterComponentConst(_ComponentConst):
61d96b89
FD
216 _bt_borrow_component_class_ptr = staticmethod(
217 native_bt.component_filter_borrow_class_const
218 )
deec48a6 219 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
61d96b89
FD
220 _bt_as_component_class_ptr = staticmethod(
221 native_bt.component_class_filter_as_component_class
222 )
deec48a6 223 _bt_as_component_ptr = staticmethod(native_bt.component_filter_as_component_const)
81447b5b 224
81447b5b 225
5265f5e3 226class _SinkComponentConst(_ComponentConst):
61d96b89
FD
227 _bt_borrow_component_class_ptr = staticmethod(
228 native_bt.component_sink_borrow_class_const
229 )
deec48a6 230 _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
61d96b89
FD
231 _bt_as_component_class_ptr = staticmethod(
232 native_bt.component_class_sink_as_component_class
233 )
deec48a6 234 _bt_as_component_ptr = staticmethod(native_bt.component_sink_as_component_const)
81447b5b
PP
235
236
5265f5e3 237# This is analogous to _SourceComponentClassConst, but for source
81447b5b 238# component objects.
5265f5e3 239class _GenericSourceComponentConst(object._SharedObject, _SourceComponentConst):
bc5c9924
SM
240 _get_ref = staticmethod(native_bt.component_source_get_ref)
241 _put_ref = staticmethod(native_bt.component_source_put_ref)
242
f6a5e476
PP
243 @property
244 def output_ports(self):
61d96b89
FD
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,
49e6b55c 250 bt2_port._OutputPortConst,
61d96b89 251 )
81447b5b
PP
252
253
5265f5e3 254# This is analogous to _FilterComponentClassConst, but for filter
81447b5b 255# component objects.
5265f5e3 256class _GenericFilterComponentConst(object._SharedObject, _FilterComponentConst):
bc5c9924
SM
257 _get_ref = staticmethod(native_bt.component_filter_get_ref)
258 _put_ref = staticmethod(native_bt.component_filter_put_ref)
259
f6a5e476
PP
260 @property
261 def output_ports(self):
61d96b89
FD
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,
49e6b55c 267 bt2_port._OutputPortConst,
61d96b89 268 )
f6a5e476
PP
269
270 @property
271 def input_ports(self):
61d96b89
FD
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,
49e6b55c 277 bt2_port._InputPortConst,
61d96b89 278 )
81447b5b
PP
279
280
5265f5e3 281# This is analogous to _SinkComponentClassConst, but for sink
81447b5b 282# component objects.
5265f5e3 283class _GenericSinkComponentConst(object._SharedObject, _SinkComponentConst):
a49e2cc3
PP
284 _get_ref = staticmethod(native_bt.component_sink_get_ref)
285 _put_ref = staticmethod(native_bt.component_sink_put_ref)
bbb3650f 286
f6a5e476
PP
287 @property
288 def input_ports(self):
61d96b89
FD
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,
49e6b55c 294 bt2_port._InputPortConst,
61d96b89 295 )
81447b5b
PP
296
297
f6a5e476 298_COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = {
5265f5e3
FD
299 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponentConst,
300 native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponentConst,
301 native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponentConst,
81447b5b
PP
302}
303
304
f6a5e476 305_COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = {
5265f5e3
FD
306 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _SourceComponentClassConst,
307 native_bt.COMPONENT_CLASS_TYPE_FILTER: _FilterComponentClassConst,
308 native_bt.COMPONENT_CLASS_TYPE_SINK: _SinkComponentClassConst,
81447b5b
PP
309}
310
311
5265f5e3
FD
312# Create a component Python object of type _GenericSourceComponentConst,
313# _GenericFilterComponentConst or _GenericSinkComponentConst, depending on
bbb3650f
SM
314# comp_cls_type.
315#
316# Steals the reference to ptr from the caller.
317
61d96b89 318
5265f5e3 319def _create_component_from_const_ptr(ptr, comp_cls_type):
f6a5e476 320 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr)
81447b5b 321
871a292a
SM
322
323# Same as the above, but acquire a new reference instead of stealing the
324# reference from the caller.
325
61d96b89 326
5265f5e3 327def _create_component_from_const_ptr_and_get_ref(ptr, comp_cls_type):
61d96b89
FD
328 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[
329 comp_cls_type
330 ]._create_from_ptr_and_get_ref(ptr)
871a292a
SM
331
332
bbb3650f 333# Create a component class Python object of type
5265f5e3
FD
334# _SourceComponentClassConst, _FilterComponentClassConst or
335# _SinkComponentClassConst, depending on comp_cls_type.
bbb3650f
SM
336#
337# Acquires a new reference to ptr.
81447b5b 338
61d96b89 339
5265f5e3 340def _create_component_class_from_const_ptr_and_get_ref(ptr, comp_cls_type):
61d96b89
FD
341 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[
342 comp_cls_type
343 ]._create_from_ptr_and_get_ref(ptr)
81447b5b
PP
344
345
40910fbb
PP
346def _trim_docstring(docstring):
347 lines = docstring.expandtabs().splitlines()
e30908cf
PP
348
349 if len(lines) == 0:
350 return ''
351
40910fbb
PP
352 indent = sys.maxsize
353
e30908cf
PP
354 if len(lines) > 1:
355 for line in lines[1:]:
356 stripped = line.lstrip()
40910fbb 357
e30908cf
PP
358 if stripped:
359 indent = min(indent, len(line) - len(stripped))
40910fbb
PP
360
361 trimmed = [lines[0].strip()]
362
e30908cf 363 if indent < sys.maxsize and len(lines) > 1:
40910fbb
PP
364 for line in lines[1:]:
365 trimmed.append(line[indent:].rstrip())
366
367 while trimmed and not trimmed[-1]:
368 trimmed.pop()
369
370 while trimmed and not trimmed[0]:
371 trimmed.pop(0)
372
373 return '\n'.join(trimmed)
374
375
81447b5b
PP
376# Metaclass for component classes defined by Python code.
377#
378# The Python user can create a standard Python class which inherits one
f6a5e476
PP
379# of the three base classes (_UserSourceComponent, _UserFilterComponent,
380# or _UserSinkComponent). Those base classes set this class
81447b5b
PP
381# (_UserComponentType) as their metaclass.
382#
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
5265f5e3 388# the _ComponentClassConst interface.
81447b5b
PP
389#
390# The component class name which is used is either:
391#
392# * The `name` parameter of the class:
393#
394# class MySink(bt2.SinkComponent, name='my-custom-sink'):
395# ...
396#
397# * If the `name` class parameter is not used: the name of the class
398# itself (`MySink` in the example above).
399#
400# The component class description which is used is the user-defined
401# class's docstring:
402#
403# class MySink(bt2.SinkComponent):
404# 'Description goes here'
405# ...
406#
407# A user-defined Python component class can have an __init__() method
408# which must at least accept the `params` and `name` arguments:
409#
410# def __init__(self, params, name, something_else):
411# ...
412#
f6a5e476 413# The user-defined component class can also have a _finalize() method
81447b5b 414# (do NOT use __del__()) to be notified when the component object is
f6a5e476 415# finalized.
81447b5b
PP
416#
417# User-defined source and filter component classes must use the
fa4c33e3
SM
418# `message_iterator_class` class parameter to specify the
419# message iterator class to use for this component class:
81447b5b 420#
fa4c33e3 421# class MyMessageIterator(bt2._UserMessageIterator):
81447b5b
PP
422# ...
423#
f6a5e476 424# class MySource(bt2._UserSourceComponent,
fa4c33e3 425# message_iterator_class=MyMessageIterator):
81447b5b
PP
426# ...
427#
fa4c33e3
SM
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
81447b5b
PP
431# __init__() method: this method has access to the original Python
432# component object which was used to create it as the `component`
fa4c33e3 433# property. The message iterator class can also define a
f6a5e476 434# _finalize() method (again, do NOT use __del__()): this is called when
fa4c33e3 435# the message iterator is (really) destroyed.
81447b5b
PP
436#
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).
441class _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)
445
446 def __init__(cls, class_name, bases, namespace, **kwargs):
447 super().__init__(class_name, bases, namespace)
448
449 # skip our own bases; they are never directly instantiated by the user
f6a5e476
PP
450 own_bases = (
451 '_UserComponent',
452 '_UserFilterSinkComponent',
453 '_UserSourceComponent',
454 '_UserFilterComponent',
455 '_UserSinkComponent',
456 )
457
458 if class_name in own_bases:
81447b5b
PP
459 return
460
461 comp_cls_name = kwargs.get('name', class_name)
f6a5e476 462 utils._check_str(comp_cls_name)
40910fbb
PP
463 comp_cls_descr = None
464 comp_cls_help = None
465
466 if hasattr(cls, '__doc__') and cls.__doc__ is not None:
f6a5e476 467 utils._check_str(cls.__doc__)
40910fbb
PP
468 docstring = _trim_docstring(cls.__doc__)
469 lines = docstring.splitlines()
470
471 if len(lines) >= 1:
472 comp_cls_descr = lines[0]
473
474 if len(lines) >= 3:
475 comp_cls_help = '\n'.join(lines[2:])
476
fa4c33e3 477 iter_cls = kwargs.get('message_iterator_class')
81447b5b 478
f6a5e476 479 if _UserSourceComponent in bases:
deec48a6 480 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
61d96b89
FD
481 cc_ptr = native_bt.bt2_component_class_source_create(
482 cls, comp_cls_name, comp_cls_descr, comp_cls_help
483 )
f6a5e476 484 elif _UserFilterComponent in bases:
deec48a6 485 _UserComponentType._bt_set_iterator_class(cls, iter_cls)
61d96b89
FD
486 cc_ptr = native_bt.bt2_component_class_filter_create(
487 cls, comp_cls_name, comp_cls_descr, comp_cls_help
488 )
f6a5e476 489 elif _UserSinkComponent in bases:
819d0ae7 490 if not hasattr(cls, '_user_consume'):
64e96b5d 491 raise bt2._IncompleteUserClass(
819d0ae7 492 "cannot create component class '{}': missing a _user_consume() method".format(
61d96b89
FD
493 class_name
494 )
495 )
496
497 cc_ptr = native_bt.bt2_component_class_sink_create(
498 cls, comp_cls_name, comp_cls_descr, comp_cls_help
499 )
81447b5b 500 else:
64e96b5d 501 raise bt2._IncompleteUserClass(
61d96b89
FD
502 "cannot find a known component class base in the bases of '{}'".format(
503 class_name
504 )
505 )
81447b5b
PP
506
507 if cc_ptr is None:
614743a5 508 raise bt2._MemoryError(
61d96b89
FD
509 "cannot create component class '{}'".format(class_name)
510 )
81447b5b 511
deec48a6 512 cls._bt_cc_ptr = cc_ptr
81447b5b 513
b20382e2 514 def _bt_init_from_native(cls, comp_ptr, params_ptr, obj):
f6a5e476 515 # create instance, not user-initialized yet
81447b5b
PP
516 self = cls.__new__(cls)
517
e3250e61
SM
518 # config object
519 config = cls._config_pycls()
520
bbb3650f 521 # pointer to native self component object (weak/borrowed)
deec48a6 522 self._bt_ptr = comp_ptr
81447b5b 523
f6a5e476
PP
524 # call user's __init__() method
525 if params_ptr is not None:
f19e40a1 526 params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr)
f6a5e476
PP
527 else:
528 params = None
81447b5b 529
e3250e61 530 self.__init__(config, params, obj)
81447b5b
PP
531 return self
532
f6a5e476 533 def __call__(cls, *args, **kwargs):
3b2be708 534 raise RuntimeError(
61d96b89
FD
535 'cannot directly instantiate a user component from a Python module'
536 )
81447b5b
PP
537
538 @staticmethod
deec48a6 539 def _bt_set_iterator_class(cls, iter_cls):
81447b5b 540 if iter_cls is None:
64e96b5d 541 raise bt2._IncompleteUserClass(
61d96b89
FD
542 "cannot create component class '{}': missing message iterator class".format(
543 cls.__name__
544 )
545 )
81447b5b 546
c946c9de 547 if not issubclass(iter_cls, bt2_message_iterator._UserMessageIterator):
64e96b5d 548 raise bt2._IncompleteUserClass(
61d96b89
FD
549 "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(
550 cls.__name__
551 )
552 )
81447b5b 553
f6a5e476 554 if not hasattr(iter_cls, '__next__'):
64e96b5d 555 raise bt2._IncompleteUserClass(
61d96b89
FD
556 "cannot create component class '{}': message iterator class is missing a __next__() method".format(
557 cls.__name__
558 )
559 )
81447b5b 560
7f8e969d
SM
561 if hasattr(iter_cls, '_user_can_seek_ns_from_origin') and not hasattr(
562 iter_cls, '_user_seek_ns_from_origin'
563 ):
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(
566 cls.__name__
567 )
568 )
569
570 if hasattr(iter_cls, '_user_can_seek_beginning') and not hasattr(
571 iter_cls, '_user_seek_beginning'
572 ):
573 raise bt2._IncompleteUserClass(
574 "cannot create component class '{}': message iterator class implements _user_can_seek_beginning but not _user_seek_beginning".format(
575 cls.__name__
576 )
577 )
578
81447b5b
PP
579 cls._iter_cls = iter_cls
580
581 @property
582 def name(cls):
deec48a6 583 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 584 return native_bt.component_class_get_name(ptr)
81447b5b
PP
585
586 @property
587 def description(cls):
deec48a6 588 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 589 return native_bt.component_class_get_description(ptr)
81447b5b 590
40910fbb
PP
591 @property
592 def help(cls):
deec48a6 593 ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 594 return native_bt.component_class_get_help(ptr)
40910fbb 595
81447b5b
PP
596 @property
597 def addr(cls):
deec48a6 598 return int(cls._bt_cc_ptr)
81447b5b 599
ed68d910
PP
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:
f19e40a1 603 params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr)
ed68d910
PP
604 else:
605 params = None
606
607 # this can raise, but the native side checks the exception
608 range_set = cls._user_get_supported_mip_versions(params, obj, log_level)
609
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)
613
614 # return new reference
615 range_set._get_ref(range_set._ptr)
616 return int(range_set._ptr)
617
618 def _user_get_supported_mip_versions(cls, params, obj, log_level):
619 return [0]
620
9bc48e22
PP
621 def _bt_query_from_native(
622 cls, priv_query_exec_ptr, object_name, params_ptr, method_obj
623 ):
ed68d910 624 # this can raise, but the native side checks the exception
f6a5e476 625 if params_ptr is not None:
f19e40a1 626 params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr)
f6a5e476
PP
627 else:
628 params = None
629
bf403eb2 630 priv_query_exec = bt2_query_executor._PrivateQueryExecutor(priv_query_exec_ptr)
f6a5e476 631
bf403eb2
PP
632 try:
633 # this can raise, but the native side checks the exception
9bc48e22 634 results = cls._user_query(priv_query_exec, object_name, params, method_obj)
bf403eb2
PP
635 finally:
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
641 # later
642 priv_query_exec._invalidate()
f6a5e476 643
1286dcbb
PP
644 # this can raise, but the native side checks the exception
645 results = bt2.create_value(results)
911dec08
PP
646
647 if results is None:
70ad0c69 648 results_ptr = native_bt.value_null
911dec08 649 else:
70ad0c69 650 results_ptr = results._ptr
911dec08 651
bf403eb2 652 # return new reference
c946c9de 653 bt2_value._Value._get_ref(results_ptr)
70ad0c69 654 return int(results_ptr)
911dec08 655
9bc48e22 656 def _user_query(cls, priv_query_executor, object_name, params, method_obj):
7dd3e712 657 raise bt2.UnknownObject
f6a5e476 658
deec48a6
FD
659 def _bt_component_class_ptr(self):
660 return self._bt_as_component_class_ptr(self._bt_cc_ptr)
911dec08 661
81447b5b 662 def __del__(cls):
deec48a6
FD
663 if hasattr(cls, '_bt_cc_ptr'):
664 cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr)
bbb3650f 665 native_bt.component_class_put_ref(cc_ptr)
774b7f28 666 native_bt.bt2_unregister_cc_ptr_to_py_cls(cc_ptr)
81447b5b 667
61d96b89 668
e3250e61
SM
669# Configuration objects for components.
670#
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.
674
675
676class _UserComponentConfiguration:
677 pass
678
679
680class _UserSourceComponentConfiguration(_UserComponentConfiguration):
681 pass
682
683
684class _UserFilterComponentConfiguration(_UserComponentConfiguration):
685 pass
686
687
688class _UserSinkComponentConfiguration(_UserComponentConfiguration):
689 pass
690
691
e34fb94a
SM
692# Subclasses must provide these methods or property:
693#
deec48a6 694# - _bt_as_not_self_specific_component_ptr: static method, must return the passed
e34fb94a
SM
695# specialized self component pointer (e.g. 'bt_self_component_sink *') as a
696# specialized non-self pointer (e.g. 'bt_component_sink *').
deec48a6 697# - _bt_borrow_component_class_ptr: static method, must return a pointer to the
e34fb94a
SM
698# specialized component class (e.g. 'bt_component_class_sink *') of the
699# passed specialized component pointer (e.g. 'bt_component_sink *').
deec48a6 700# - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
e34fb94a 701# constants.
81447b5b 702
61d96b89 703
f6a5e476
PP
704class _UserComponent(metaclass=_UserComponentType):
705 @property
706 def name(self):
deec48a6
FD
707 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
708 ptr = self._bt_as_component_ptr(ptr)
e34fb94a
SM
709 name = native_bt.component_get_name(ptr)
710 assert name is not None
f6a5e476 711 return name
81447b5b 712
cc81b5ab
PP
713 @property
714 def logging_level(self):
deec48a6
FD
715 ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr)
716 ptr = self._bt_as_component_ptr(ptr)
cc81b5ab
PP
717 return native_bt.component_get_logging_level(ptr)
718
f6a5e476 719 @property
c88be1c8 720 def cls(self):
deec48a6
FD
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)
5265f5e3 723 return _create_component_class_from_const_ptr_and_get_ref(
61d96b89
FD
724 cc_ptr, self._bt_comp_cls_type
725 )
81447b5b 726
81447b5b
PP
727 @property
728 def addr(self):
deec48a6 729 return int(self._bt_ptr)
81447b5b 730
253a9ecc
PP
731 @property
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)
735
e3250e61 736 def __init__(self, config, params, obj):
81447b5b
PP
737 pass
738
819d0ae7 739 def _user_finalize(self):
81447b5b
PP
740 pass
741
819d0ae7 742 def _user_port_connected(self, port, other_port):
f6a5e476 743 pass
81447b5b 744
61d96b89
FD
745 def _bt_port_connected_from_native(
746 self, self_port_ptr, self_port_type, other_port_ptr
747 ):
c946c9de 748 port = bt2_port._create_self_from_ptr_and_get_ref(self_port_ptr, self_port_type)
824ce8b6
SM
749
750 if self_port_type == native_bt.PORT_TYPE_OUTPUT:
751 other_port_type = native_bt.PORT_TYPE_INPUT
752 else:
753 other_port_type = native_bt.PORT_TYPE_OUTPUT
754
49e6b55c 755 other_port = bt2_port._create_from_const_ptr_and_get_ref(
61d96b89
FD
756 other_port_ptr, other_port_type
757 )
819d0ae7 758 self._user_port_connected(port, other_port)
81447b5b 759
b2df5780
PP
760 def _create_trace_class(
761 self, user_attributes=None, assigns_automatic_stream_class_id=True
762 ):
deec48a6 763 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
0bee8ea9
SM
764 tc_ptr = native_bt.trace_class_create(ptr)
765
766 if tc_ptr is None:
614743a5 767 raise bt2._MemoryError('could not create trace class')
0bee8ea9 768
c946c9de 769 tc = bt2_trace_class._TraceClass._create_from_ptr(tc_ptr)
0bee8ea9
SM
770 tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id
771
b2df5780
PP
772 if user_attributes is not None:
773 tc._user_attributes = user_attributes
774
0bee8ea9
SM
775 return tc
776
61d96b89
FD
777 def _create_clock_class(
778 self,
779 frequency=None,
780 name=None,
b2df5780 781 user_attributes=None,
61d96b89
FD
782 description=None,
783 precision=None,
784 offset=None,
785 origin_is_unix_epoch=True,
786 uuid=None,
787 ):
deec48a6 788 ptr = self._bt_as_self_component_ptr(self._bt_ptr)
060aee52
SM
789 cc_ptr = native_bt.clock_class_create(ptr)
790
791 if cc_ptr is None:
614743a5 792 raise bt2._MemoryError('could not create clock class')
060aee52 793
c946c9de 794 cc = bt2_clock_class._ClockClass._create_from_ptr(cc_ptr)
27d97a3f
SM
795
796 if frequency is not None:
797 cc._frequency = frequency
798
4a5ca968
SM
799 if name is not None:
800 cc._name = name
801
b2df5780
PP
802 if user_attributes is not None:
803 cc._user_attributes = user_attributes
804
4a5ca968
SM
805 if description is not None:
806 cc._description = description
807
808 if precision is not None:
809 cc._precision = precision
810
811 if offset is not None:
812 cc._offset = offset
813
814 cc._origin_is_unix_epoch = origin_is_unix_epoch
815
816 if uuid is not None:
817 cc._uuid = uuid
818
27d97a3f 819 return cc
060aee52 820
81447b5b 821
5265f5e3 822class _UserSourceComponent(_UserComponent, _SourceComponentConst):
61d96b89
FD
823 _bt_as_not_self_specific_component_ptr = staticmethod(
824 native_bt.self_component_source_as_component_source
825 )
826 _bt_as_self_component_ptr = staticmethod(
827 native_bt.self_component_source_as_self_component
828 )
e3250e61 829 _config_pycls = _UserSourceComponentConfiguration
e34fb94a 830
81447b5b 831 @property
f6a5e476 832 def _output_ports(self):
bc5c9924 833 def get_output_port_count(self_ptr):
deec48a6 834 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
835 return native_bt.component_source_get_output_port_count(ptr)
836
61d96b89
FD
837 return _ComponentPorts(
838 self._bt_ptr,
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,
c946c9de 842 bt2_port._UserComponentOutputPort,
61d96b89 843 )
f6a5e476 844
03ec9ebd 845 def _add_output_port(self, name, user_data=None):
f6a5e476 846 utils._check_str(name)
bc5c9924 847 fn = native_bt.self_component_source_add_output_port
deec48a6 848 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
849 utils._handle_func_status(
850 comp_status, 'cannot add output port to source component object'
851 )
bc5c9924 852 assert self_port_ptr is not None
246d5223
PP
853 return bt2_port._UserComponentOutputPort._create_from_ptr_and_get_ref(
854 self_port_ptr
855 )
f6a5e476
PP
856
857
5265f5e3 858class _UserFilterComponent(_UserComponent, _FilterComponentConst):
61d96b89
FD
859 _bt_as_not_self_specific_component_ptr = staticmethod(
860 native_bt.self_component_filter_as_component_filter
861 )
862 _bt_as_self_component_ptr = staticmethod(
863 native_bt.self_component_filter_as_self_component
864 )
e3250e61 865 _config_pycls = _UserFilterComponentConfiguration
e34fb94a 866
f6a5e476
PP
867 @property
868 def _output_ports(self):
bc5c9924 869 def get_output_port_count(self_ptr):
deec48a6 870 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
871 return native_bt.component_filter_get_output_port_count(ptr)
872
61d96b89
FD
873 return _ComponentPorts(
874 self._bt_ptr,
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,
c946c9de 878 bt2_port._UserComponentOutputPort,
61d96b89 879 )
81447b5b 880
f6a5e476
PP
881 @property
882 def _input_ports(self):
bc5c9924 883 def get_input_port_count(self_ptr):
deec48a6 884 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
885 return native_bt.component_filter_get_input_port_count(ptr)
886
61d96b89
FD
887 return _ComponentPorts(
888 self._bt_ptr,
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,
c946c9de 892 bt2_port._UserComponentInputPort,
61d96b89 893 )
f6a5e476 894
03ec9ebd 895 def _add_output_port(self, name, user_data=None):
f6a5e476 896 utils._check_str(name)
bc5c9924 897 fn = native_bt.self_component_filter_add_output_port
deec48a6 898 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
899 utils._handle_func_status(
900 comp_status, 'cannot add output port to filter component object'
901 )
bc5c9924 902 assert self_port_ptr
246d5223
PP
903 return bt2_port._UserComponentOutputPort._create_from_ptr_and_get_ref(
904 self_port_ptr
905 )
f6a5e476 906
03ec9ebd 907 def _add_input_port(self, name, user_data=None):
f6a5e476 908 utils._check_str(name)
bc5c9924 909 fn = native_bt.self_component_filter_add_input_port
deec48a6 910 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
911 utils._handle_func_status(
912 comp_status, 'cannot add input port to filter component object'
913 )
bc5c9924 914 assert self_port_ptr
246d5223
PP
915 return bt2_port._UserComponentInputPort._create_from_ptr_and_get_ref(
916 self_port_ptr
917 )
f6a5e476
PP
918
919
5265f5e3 920class _UserSinkComponent(_UserComponent, _SinkComponentConst):
61d96b89
FD
921 _bt_as_not_self_specific_component_ptr = staticmethod(
922 native_bt.self_component_sink_as_component_sink
923 )
924 _bt_as_self_component_ptr = staticmethod(
925 native_bt.self_component_sink_as_self_component
926 )
e3250e61 927 _config_pycls = _UserSinkComponentConfiguration
e34fb94a 928
f2849243 929 def _bt_graph_is_configured_from_native(self):
819d0ae7 930 self._user_graph_is_configured()
f2849243 931
819d0ae7 932 def _user_graph_is_configured(self):
f0c6b5d5
PP
933 pass
934
f6a5e476
PP
935 @property
936 def _input_ports(self):
bc5c9924 937 def get_input_port_count(self_ptr):
deec48a6 938 ptr = self._bt_as_not_self_specific_component_ptr(self_ptr)
bc5c9924
SM
939 return native_bt.component_sink_get_input_port_count(ptr)
940
61d96b89
FD
941 return _ComponentPorts(
942 self._bt_ptr,
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,
c946c9de 946 bt2_port._UserComponentInputPort,
61d96b89 947 )
f6a5e476 948
03ec9ebd 949 def _add_input_port(self, name, user_data=None):
f6a5e476 950 utils._check_str(name)
bc5c9924 951 fn = native_bt.self_component_sink_add_input_port
deec48a6 952 comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data)
61d96b89
FD
953 utils._handle_func_status(
954 comp_status, 'cannot add input port to sink component object'
955 )
871a292a 956 assert self_port_ptr
246d5223
PP
957 return bt2_port._UserComponentInputPort._create_from_ptr_and_get_ref(
958 self_port_ptr
959 )
692f1a01 960
fbd8a4e0 961 def _create_message_iterator(self, input_port):
c946c9de 962 utils._check_type(input_port, bt2_port._UserComponentInputPort)
692f1a01 963
c1491996
FD
964 (
965 status,
966 msg_iter_ptr,
fbd8a4e0 967 ) = native_bt.bt2_message_iterator_create_from_sink_component(
692f1a01
PP
968 self._bt_ptr, input_port._ptr
969 )
ab8b2b1b
SM
970 utils._handle_func_status(status, 'cannot create message iterator object')
971 assert msg_iter_ptr is not None
692f1a01 972
c946c9de 973 return bt2_message_iterator._UserComponentInputPortMessageIterator(msg_iter_ptr)
d73bb381
PP
974
975 @property
976 def _is_interrupted(self):
977 return bool(native_bt.self_component_sink_is_interrupted(self._bt_ptr))
This page took 0.113144 seconds and 4 git commands to generate.