Commit | Line | Data |
---|---|---|
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 | ||
23 | from bt2 import native_bt, object, utils | |
3fb99a22 | 24 | from bt2 import message_iterator as bt2_message_iterator |
81447b5b | 25 | import collections.abc |
3fb99a22 PP |
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 | |
3fb99a22 | 30 | from bt2 import port as bt2_port |
40910fbb | 31 | import sys |
81447b5b | 32 | import bt2 |
811644b8 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. | |
601c0026 SM |
39 | # |
40 | # Subclasses must implement some methods that this base class uses: | |
41 | # | |
85906b6b | 42 | # - _bt_as_component_class_ptr: static method, convert the passed component class |
601c0026 SM |
43 | # pointer to a 'bt_component_class *'. |
44 | ||
cfbd7cf3 | 45 | |
615238be | 46 | class _ComponentClassConst(object._SharedObject): |
81447b5b PP |
47 | @property |
48 | def name(self): | |
85906b6b | 49 | ptr = self._bt_as_component_class_ptr(self._ptr) |
601c0026 SM |
50 | name = native_bt.component_class_get_name(ptr) |
51 | assert name is not None | |
811644b8 | 52 | return name |
81447b5b PP |
53 | |
54 | @property | |
55 | def description(self): | |
85906b6b | 56 | ptr = self._bt_as_component_class_ptr(self._ptr) |
601c0026 | 57 | return native_bt.component_class_get_description(ptr) |
81447b5b | 58 | |
40910fbb PP |
59 | @property |
60 | def help(self): | |
85906b6b | 61 | ptr = self._bt_as_component_class_ptr(self._ptr) |
601c0026 SM |
62 | return native_bt.component_class_get_help(ptr) |
63 | ||
85906b6b FD |
64 | def _bt_component_class_ptr(self): |
65 | return self._bt_as_component_class_ptr(self._ptr) | |
40910fbb | 66 | |
811644b8 | 67 | def __eq__(self, other): |
615238be | 68 | if not isinstance(other, _ComponentClassConst): |
811644b8 PP |
69 | try: |
70 | if not issubclass(other, _UserComponent): | |
71 | return False | |
72 | except TypeError: | |
73 | return False | |
81447b5b | 74 | |
811644b8 | 75 | return self.addr == other.addr |
81447b5b PP |
76 | |
77 | ||
615238be | 78 | class _SourceComponentClassConst(_ComponentClassConst): |
2f16a6a2 PP |
79 | _get_ref = staticmethod(native_bt.component_class_source_get_ref) |
80 | _put_ref = staticmethod(native_bt.component_class_source_put_ref) | |
cfbd7cf3 FD |
81 | _bt_as_component_class_ptr = staticmethod( |
82 | native_bt.component_class_source_as_component_class | |
83 | ) | |
81447b5b PP |
84 | |
85 | ||
615238be | 86 | class _FilterComponentClassConst(_ComponentClassConst): |
2f16a6a2 PP |
87 | _get_ref = staticmethod(native_bt.component_class_filter_get_ref) |
88 | _put_ref = staticmethod(native_bt.component_class_filter_put_ref) | |
cfbd7cf3 FD |
89 | _bt_as_component_class_ptr = staticmethod( |
90 | native_bt.component_class_filter_as_component_class | |
91 | ) | |
81447b5b PP |
92 | |
93 | ||
615238be | 94 | class _SinkComponentClassConst(_ComponentClassConst): |
2f16a6a2 PP |
95 | _get_ref = staticmethod(native_bt.component_class_sink_get_ref) |
96 | _put_ref = staticmethod(native_bt.component_class_sink_put_ref) | |
cfbd7cf3 FD |
97 | _bt_as_component_class_ptr = staticmethod( |
98 | native_bt.component_class_sink_as_component_class | |
99 | ) | |
81447b5b PP |
100 | |
101 | ||
811644b8 PP |
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 | |
894a8df5 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 | |
811644b8 | 119 | |
811644b8 PP |
120 | self._at += 1 |
121 | return name | |
122 | ||
123 | ||
124 | class _ComponentPorts(collections.abc.Mapping): | |
894a8df5 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 | ||
cfbd7cf3 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 | ): | |
894a8df5 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 | |
811644b8 PP |
143 | |
144 | def __getitem__(self, key): | |
145 | utils._check_str(key) | |
894a8df5 | 146 | port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key) |
811644b8 PP |
147 | |
148 | if port_ptr is None: | |
149 | raise KeyError(key) | |
150 | ||
894a8df5 | 151 | return self._port_pycls._create_from_ptr_and_get_ref(port_ptr) |
811644b8 PP |
152 | |
153 | def __len__(self): | |
894a8df5 SM |
154 | count = self._get_port_count(self._component_ptr) |
155 | assert count >= 0 | |
811644b8 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 |
601c0026 SM |
163 | # component objects and Python user component objects. |
164 | # | |
165 | # Subclasses must provide these methods or property: | |
166 | # | |
85906b6b | 167 | # - _bt_borrow_component_class_ptr: static method, must return a pointer to the |
601c0026 SM |
168 | # specialized component class (e.g. 'bt_component_class_sink *') of the |
169 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
85906b6b | 170 | # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* |
601c0026 | 171 | # constants. |
85906b6b | 172 | # - _bt_as_component_ptr: static method, must return the passed specialized |
1c9ed2ff | 173 | # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'. |
601c0026 | 174 | |
cfbd7cf3 | 175 | |
615238be | 176 | class _ComponentConst: |
81447b5b PP |
177 | @property |
178 | def name(self): | |
85906b6b | 179 | ptr = self._bt_as_component_ptr(self._ptr) |
1c9ed2ff SM |
180 | name = native_bt.component_get_name(ptr) |
181 | assert name is not None | |
811644b8 PP |
182 | return name |
183 | ||
e874da19 PP |
184 | @property |
185 | def logging_level(self): | |
85906b6b | 186 | ptr = self._bt_as_component_ptr(self._ptr) |
e874da19 PP |
187 | return native_bt.component_get_logging_level(ptr) |
188 | ||
81447b5b | 189 | @property |
e8ac1aae | 190 | def cls(self): |
85906b6b | 191 | cc_ptr = self._bt_borrow_component_class_ptr(self._ptr) |
601c0026 | 192 | assert cc_ptr is not None |
615238be | 193 | return _create_component_class_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
194 | cc_ptr, self._bt_comp_cls_type |
195 | ) | |
81447b5b | 196 | |
811644b8 PP |
197 | def __eq__(self, other): |
198 | if not hasattr(other, 'addr'): | |
199 | return False | |
81447b5b | 200 | |
811644b8 | 201 | return self.addr == other.addr |
81447b5b | 202 | |
81447b5b | 203 | |
615238be | 204 | class _SourceComponentConst(_ComponentConst): |
cfbd7cf3 FD |
205 | _bt_borrow_component_class_ptr = staticmethod( |
206 | native_bt.component_source_borrow_class_const | |
207 | ) | |
85906b6b | 208 | _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
cfbd7cf3 FD |
209 | _bt_as_component_class_ptr = staticmethod( |
210 | native_bt.component_class_source_as_component_class | |
211 | ) | |
85906b6b | 212 | _bt_as_component_ptr = staticmethod(native_bt.component_source_as_component_const) |
81447b5b | 213 | |
81447b5b | 214 | |
615238be | 215 | class _FilterComponentConst(_ComponentConst): |
cfbd7cf3 FD |
216 | _bt_borrow_component_class_ptr = staticmethod( |
217 | native_bt.component_filter_borrow_class_const | |
218 | ) | |
85906b6b | 219 | _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
cfbd7cf3 FD |
220 | _bt_as_component_class_ptr = staticmethod( |
221 | native_bt.component_class_filter_as_component_class | |
222 | ) | |
85906b6b | 223 | _bt_as_component_ptr = staticmethod(native_bt.component_filter_as_component_const) |
81447b5b | 224 | |
81447b5b | 225 | |
615238be | 226 | class _SinkComponentConst(_ComponentConst): |
cfbd7cf3 FD |
227 | _bt_borrow_component_class_ptr = staticmethod( |
228 | native_bt.component_sink_borrow_class_const | |
229 | ) | |
85906b6b | 230 | _bt_comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
cfbd7cf3 FD |
231 | _bt_as_component_class_ptr = staticmethod( |
232 | native_bt.component_class_sink_as_component_class | |
233 | ) | |
85906b6b | 234 | _bt_as_component_ptr = staticmethod(native_bt.component_sink_as_component_const) |
81447b5b PP |
235 | |
236 | ||
615238be | 237 | # This is analogous to _SourceComponentClassConst, but for source |
81447b5b | 238 | # component objects. |
615238be | 239 | class _GenericSourceComponentConst(object._SharedObject, _SourceComponentConst): |
894a8df5 SM |
240 | _get_ref = staticmethod(native_bt.component_source_get_ref) |
241 | _put_ref = staticmethod(native_bt.component_source_put_ref) | |
242 | ||
811644b8 PP |
243 | @property |
244 | def output_ports(self): | |
cfbd7cf3 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, | |
5813b3a3 | 250 | bt2_port._OutputPortConst, |
cfbd7cf3 | 251 | ) |
81447b5b PP |
252 | |
253 | ||
615238be | 254 | # This is analogous to _FilterComponentClassConst, but for filter |
81447b5b | 255 | # component objects. |
615238be | 256 | class _GenericFilterComponentConst(object._SharedObject, _FilterComponentConst): |
894a8df5 SM |
257 | _get_ref = staticmethod(native_bt.component_filter_get_ref) |
258 | _put_ref = staticmethod(native_bt.component_filter_put_ref) | |
259 | ||
811644b8 PP |
260 | @property |
261 | def output_ports(self): | |
cfbd7cf3 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, | |
5813b3a3 | 267 | bt2_port._OutputPortConst, |
cfbd7cf3 | 268 | ) |
811644b8 PP |
269 | |
270 | @property | |
271 | def input_ports(self): | |
cfbd7cf3 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, | |
5813b3a3 | 277 | bt2_port._InputPortConst, |
cfbd7cf3 | 278 | ) |
81447b5b PP |
279 | |
280 | ||
615238be | 281 | # This is analogous to _SinkComponentClassConst, but for sink |
81447b5b | 282 | # component objects. |
615238be | 283 | class _GenericSinkComponentConst(object._SharedObject, _SinkComponentConst): |
2f16a6a2 PP |
284 | _get_ref = staticmethod(native_bt.component_sink_get_ref) |
285 | _put_ref = staticmethod(native_bt.component_sink_put_ref) | |
601c0026 | 286 | |
811644b8 PP |
287 | @property |
288 | def input_ports(self): | |
cfbd7cf3 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, | |
5813b3a3 | 294 | bt2_port._InputPortConst, |
cfbd7cf3 | 295 | ) |
81447b5b PP |
296 | |
297 | ||
811644b8 | 298 | _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = { |
615238be 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 | ||
811644b8 | 305 | _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = { |
615238be 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 | ||
615238be FD |
312 | # Create a component Python object of type _GenericSourceComponentConst, |
313 | # _GenericFilterComponentConst or _GenericSinkComponentConst, depending on | |
601c0026 SM |
314 | # comp_cls_type. |
315 | # | |
316 | # Steals the reference to ptr from the caller. | |
317 | ||
cfbd7cf3 | 318 | |
615238be | 319 | def _create_component_from_const_ptr(ptr, comp_cls_type): |
811644b8 | 320 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr) |
81447b5b | 321 | |
5f25509b SM |
322 | |
323 | # Same as the above, but acquire a new reference instead of stealing the | |
324 | # reference from the caller. | |
325 | ||
cfbd7cf3 | 326 | |
615238be | 327 | def _create_component_from_const_ptr_and_get_ref(ptr, comp_cls_type): |
cfbd7cf3 FD |
328 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[ |
329 | comp_cls_type | |
330 | ]._create_from_ptr_and_get_ref(ptr) | |
5f25509b SM |
331 | |
332 | ||
601c0026 | 333 | # Create a component class Python object of type |
615238be FD |
334 | # _SourceComponentClassConst, _FilterComponentClassConst or |
335 | # _SinkComponentClassConst, depending on comp_cls_type. | |
601c0026 SM |
336 | # |
337 | # Acquires a new reference to ptr. | |
81447b5b | 338 | |
cfbd7cf3 | 339 | |
615238be | 340 | def _create_component_class_from_const_ptr_and_get_ref(ptr, comp_cls_type): |
cfbd7cf3 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 |
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 | ||
81447b5b PP |
371 | # Metaclass for component classes defined by Python code. |
372 | # | |
373 | # The Python user can create a standard Python class which inherits one | |
811644b8 PP |
374 | # of the three base classes (_UserSourceComponent, _UserFilterComponent, |
375 | # or _UserSinkComponent). Those base classes set this class | |
81447b5b PP |
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 | |
615238be | 383 | # the _ComponentClassConst interface. |
81447b5b PP |
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 | # | |
811644b8 | 408 | # The user-defined component class can also have a _finalize() method |
81447b5b | 409 | # (do NOT use __del__()) to be notified when the component object is |
811644b8 | 410 | # finalized. |
81447b5b PP |
411 | # |
412 | # User-defined source and filter component classes must use the | |
5602ef81 SM |
413 | # `message_iterator_class` class parameter to specify the |
414 | # message iterator class to use for this component class: | |
81447b5b | 415 | # |
5602ef81 | 416 | # class MyMessageIterator(bt2._UserMessageIterator): |
81447b5b PP |
417 | # ... |
418 | # | |
811644b8 | 419 | # class MySource(bt2._UserSourceComponent, |
5602ef81 | 420 | # message_iterator_class=MyMessageIterator): |
81447b5b PP |
421 | # ... |
422 | # | |
5602ef81 SM |
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 | |
81447b5b PP |
426 | # __init__() method: this method has access to the original Python |
427 | # component object which was used to create it as the `component` | |
5602ef81 | 428 | # property. The message iterator class can also define a |
811644b8 | 429 | # _finalize() method (again, do NOT use __del__()): this is called when |
5602ef81 | 430 | # the message iterator is (really) destroyed. |
81447b5b PP |
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 | |
811644b8 PP |
445 | own_bases = ( |
446 | '_UserComponent', | |
447 | '_UserFilterSinkComponent', | |
448 | '_UserSourceComponent', | |
449 | '_UserFilterComponent', | |
450 | '_UserSinkComponent', | |
451 | ) | |
452 | ||
453 | if class_name in own_bases: | |
81447b5b PP |
454 | return |
455 | ||
456 | comp_cls_name = kwargs.get('name', class_name) | |
811644b8 | 457 | utils._check_str(comp_cls_name) |
40910fbb PP |
458 | comp_cls_descr = None |
459 | comp_cls_help = None | |
460 | ||
461 | if hasattr(cls, '__doc__') and cls.__doc__ is not None: | |
811644b8 | 462 | utils._check_str(cls.__doc__) |
40910fbb PP |
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 | ||
5602ef81 | 472 | iter_cls = kwargs.get('message_iterator_class') |
81447b5b | 473 | |
811644b8 | 474 | if _UserSourceComponent in bases: |
85906b6b | 475 | _UserComponentType._bt_set_iterator_class(cls, iter_cls) |
cfbd7cf3 FD |
476 | cc_ptr = native_bt.bt2_component_class_source_create( |
477 | cls, comp_cls_name, comp_cls_descr, comp_cls_help | |
478 | ) | |
811644b8 | 479 | elif _UserFilterComponent in bases: |
85906b6b | 480 | _UserComponentType._bt_set_iterator_class(cls, iter_cls) |
cfbd7cf3 FD |
481 | cc_ptr = native_bt.bt2_component_class_filter_create( |
482 | cls, comp_cls_name, comp_cls_descr, comp_cls_help | |
483 | ) | |
811644b8 | 484 | elif _UserSinkComponent in bases: |
6a91742b | 485 | if not hasattr(cls, '_user_consume'): |
cb06aa27 | 486 | raise bt2._IncompleteUserClass( |
6a91742b | 487 | "cannot create component class '{}': missing a _user_consume() method".format( |
cfbd7cf3 FD |
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 | ) | |
81447b5b | 495 | else: |
cb06aa27 | 496 | raise bt2._IncompleteUserClass( |
cfbd7cf3 FD |
497 | "cannot find a known component class base in the bases of '{}'".format( |
498 | class_name | |
499 | ) | |
500 | ) | |
81447b5b PP |
501 | |
502 | if cc_ptr is None: | |
694c792b | 503 | raise bt2._MemoryError( |
cfbd7cf3 FD |
504 | "cannot create component class '{}'".format(class_name) |
505 | ) | |
81447b5b | 506 | |
85906b6b | 507 | cls._bt_cc_ptr = cc_ptr |
81447b5b | 508 | |
66964f3f | 509 | def _bt_init_from_native(cls, comp_ptr, params_ptr, obj): |
811644b8 | 510 | # create instance, not user-initialized yet |
81447b5b PP |
511 | self = cls.__new__(cls) |
512 | ||
601c0026 | 513 | # pointer to native self component object (weak/borrowed) |
85906b6b | 514 | self._bt_ptr = comp_ptr |
81447b5b | 515 | |
811644b8 PP |
516 | # call user's __init__() method |
517 | if params_ptr is not None: | |
e42e1587 | 518 | params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr) |
811644b8 PP |
519 | else: |
520 | params = None | |
81447b5b | 521 | |
66964f3f | 522 | self.__init__(params, obj) |
81447b5b PP |
523 | return self |
524 | ||
811644b8 | 525 | def __call__(cls, *args, **kwargs): |
ce4923b0 | 526 | raise RuntimeError( |
cfbd7cf3 FD |
527 | 'cannot directly instantiate a user component from a Python module' |
528 | ) | |
81447b5b PP |
529 | |
530 | @staticmethod | |
85906b6b | 531 | def _bt_set_iterator_class(cls, iter_cls): |
81447b5b | 532 | if iter_cls is None: |
cb06aa27 | 533 | raise bt2._IncompleteUserClass( |
cfbd7cf3 FD |
534 | "cannot create component class '{}': missing message iterator class".format( |
535 | cls.__name__ | |
536 | ) | |
537 | ) | |
81447b5b | 538 | |
3fb99a22 | 539 | if not issubclass(iter_cls, bt2_message_iterator._UserMessageIterator): |
cb06aa27 | 540 | raise bt2._IncompleteUserClass( |
cfbd7cf3 FD |
541 | "cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format( |
542 | cls.__name__ | |
543 | ) | |
544 | ) | |
81447b5b | 545 | |
811644b8 | 546 | if not hasattr(iter_cls, '__next__'): |
cb06aa27 | 547 | raise bt2._IncompleteUserClass( |
cfbd7cf3 FD |
548 | "cannot create component class '{}': message iterator class is missing a __next__() method".format( |
549 | cls.__name__ | |
550 | ) | |
551 | ) | |
81447b5b PP |
552 | |
553 | cls._iter_cls = iter_cls | |
554 | ||
555 | @property | |
556 | def name(cls): | |
85906b6b | 557 | ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 558 | return native_bt.component_class_get_name(ptr) |
81447b5b PP |
559 | |
560 | @property | |
561 | def description(cls): | |
85906b6b | 562 | ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 563 | return native_bt.component_class_get_description(ptr) |
81447b5b | 564 | |
40910fbb PP |
565 | @property |
566 | def help(cls): | |
85906b6b | 567 | ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) |
601c0026 | 568 | return native_bt.component_class_get_help(ptr) |
40910fbb | 569 | |
81447b5b PP |
570 | @property |
571 | def addr(cls): | |
85906b6b | 572 | return int(cls._bt_cc_ptr) |
81447b5b | 573 | |
fd57054b PP |
574 | def _bt_get_supported_mip_versions_from_native(cls, params_ptr, obj, log_level): |
575 | # this can raise, but the native side checks the exception | |
576 | if params_ptr is not None: | |
e42e1587 | 577 | params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr) |
fd57054b PP |
578 | else: |
579 | params = None | |
580 | ||
581 | # this can raise, but the native side checks the exception | |
582 | range_set = cls._user_get_supported_mip_versions(params, obj, log_level) | |
583 | ||
584 | if type(range_set) is not bt2.UnsignedIntegerRangeSet: | |
585 | # this can raise, but the native side checks the exception | |
586 | range_set = bt2.UnsignedIntegerRangeSet(range_set) | |
587 | ||
588 | # return new reference | |
589 | range_set._get_ref(range_set._ptr) | |
590 | return int(range_set._ptr) | |
591 | ||
592 | def _user_get_supported_mip_versions(cls, params, obj, log_level): | |
593 | return [0] | |
594 | ||
7c14d641 | 595 | def _bt_query_from_native(cls, priv_query_exec_ptr, object, params_ptr, method_obj): |
fd57054b | 596 | # this can raise, but the native side checks the exception |
811644b8 | 597 | if params_ptr is not None: |
e42e1587 | 598 | params = bt2_value._create_from_const_ptr_and_get_ref(params_ptr) |
811644b8 PP |
599 | else: |
600 | params = None | |
601 | ||
3c729b9a | 602 | priv_query_exec = bt2_query_executor._PrivateQueryExecutor(priv_query_exec_ptr) |
811644b8 | 603 | |
3c729b9a PP |
604 | try: |
605 | # this can raise, but the native side checks the exception | |
7c14d641 | 606 | results = cls._user_query(priv_query_exec, object, params, method_obj) |
3c729b9a PP |
607 | finally: |
608 | # the private query executor is a private view on the query | |
609 | # executor; it's not a shared object (the library does not | |
610 | # offer an API to get/put a reference, just like "self" | |
611 | # objects) from this query's point of view, so invalidate | |
612 | # the object in case the user kept a reference and uses it | |
613 | # later | |
614 | priv_query_exec._invalidate() | |
811644b8 | 615 | |
c7eee084 PP |
616 | # this can raise, but the native side checks the exception |
617 | results = bt2.create_value(results) | |
911dec08 PP |
618 | |
619 | if results is None: | |
b5947615 | 620 | results_ptr = native_bt.value_null |
911dec08 | 621 | else: |
b5947615 | 622 | results_ptr = results._ptr |
911dec08 | 623 | |
3c729b9a | 624 | # return new reference |
3fb99a22 | 625 | bt2_value._Value._get_ref(results_ptr) |
b5947615 | 626 | return int(results_ptr) |
911dec08 | 627 | |
7c14d641 | 628 | def _user_query(cls, priv_query_executor, object, params, method_obj): |
76b6c2f7 | 629 | raise bt2.UnknownObject |
811644b8 | 630 | |
85906b6b FD |
631 | def _bt_component_class_ptr(self): |
632 | return self._bt_as_component_class_ptr(self._bt_cc_ptr) | |
911dec08 | 633 | |
81447b5b | 634 | def __del__(cls): |
85906b6b FD |
635 | if hasattr(cls, '_bt_cc_ptr'): |
636 | cc_ptr = cls._bt_as_component_class_ptr(cls._bt_cc_ptr) | |
601c0026 | 637 | native_bt.component_class_put_ref(cc_ptr) |
ab1cea3f | 638 | native_bt.bt2_unregister_cc_ptr_to_py_cls(cc_ptr) |
81447b5b | 639 | |
cfbd7cf3 | 640 | |
1c9ed2ff SM |
641 | # Subclasses must provide these methods or property: |
642 | # | |
85906b6b | 643 | # - _bt_as_not_self_specific_component_ptr: static method, must return the passed |
1c9ed2ff SM |
644 | # specialized self component pointer (e.g. 'bt_self_component_sink *') as a |
645 | # specialized non-self pointer (e.g. 'bt_component_sink *'). | |
85906b6b | 646 | # - _bt_borrow_component_class_ptr: static method, must return a pointer to the |
1c9ed2ff SM |
647 | # specialized component class (e.g. 'bt_component_class_sink *') of the |
648 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
85906b6b | 649 | # - _bt_comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* |
1c9ed2ff | 650 | # constants. |
81447b5b | 651 | |
cfbd7cf3 | 652 | |
811644b8 PP |
653 | class _UserComponent(metaclass=_UserComponentType): |
654 | @property | |
655 | def name(self): | |
85906b6b FD |
656 | ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr) |
657 | ptr = self._bt_as_component_ptr(ptr) | |
1c9ed2ff SM |
658 | name = native_bt.component_get_name(ptr) |
659 | assert name is not None | |
811644b8 | 660 | return name |
81447b5b | 661 | |
e874da19 PP |
662 | @property |
663 | def logging_level(self): | |
85906b6b FD |
664 | ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr) |
665 | ptr = self._bt_as_component_ptr(ptr) | |
e874da19 PP |
666 | return native_bt.component_get_logging_level(ptr) |
667 | ||
811644b8 | 668 | @property |
e8ac1aae | 669 | def cls(self): |
85906b6b FD |
670 | comp_ptr = self._bt_as_not_self_specific_component_ptr(self._bt_ptr) |
671 | cc_ptr = self._bt_borrow_component_class_ptr(comp_ptr) | |
615238be | 672 | return _create_component_class_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
673 | cc_ptr, self._bt_comp_cls_type |
674 | ) | |
81447b5b | 675 | |
81447b5b PP |
676 | @property |
677 | def addr(self): | |
85906b6b | 678 | return int(self._bt_ptr) |
81447b5b | 679 | |
056deb59 PP |
680 | @property |
681 | def _graph_mip_version(self): | |
682 | ptr = self._bt_as_self_component_ptr(self._bt_ptr) | |
683 | return native_bt.self_component_get_graph_mip_version(ptr) | |
684 | ||
66964f3f | 685 | def __init__(self, params=None, obj=None): |
81447b5b PP |
686 | pass |
687 | ||
6a91742b | 688 | def _user_finalize(self): |
81447b5b PP |
689 | pass |
690 | ||
6a91742b | 691 | def _user_port_connected(self, port, other_port): |
811644b8 | 692 | pass |
81447b5b | 693 | |
cfbd7cf3 FD |
694 | def _bt_port_connected_from_native( |
695 | self, self_port_ptr, self_port_type, other_port_ptr | |
696 | ): | |
3fb99a22 | 697 | port = bt2_port._create_self_from_ptr_and_get_ref(self_port_ptr, self_port_type) |
2c67587a SM |
698 | |
699 | if self_port_type == native_bt.PORT_TYPE_OUTPUT: | |
700 | other_port_type = native_bt.PORT_TYPE_INPUT | |
701 | else: | |
702 | other_port_type = native_bt.PORT_TYPE_OUTPUT | |
703 | ||
5813b3a3 | 704 | other_port = bt2_port._create_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
705 | other_port_ptr, other_port_type |
706 | ) | |
6a91742b | 707 | self._user_port_connected(port, other_port) |
81447b5b | 708 | |
5783664e PP |
709 | def _create_trace_class( |
710 | self, user_attributes=None, assigns_automatic_stream_class_id=True | |
711 | ): | |
85906b6b | 712 | ptr = self._bt_as_self_component_ptr(self._bt_ptr) |
fbbe9302 SM |
713 | tc_ptr = native_bt.trace_class_create(ptr) |
714 | ||
715 | if tc_ptr is None: | |
694c792b | 716 | raise bt2._MemoryError('could not create trace class') |
fbbe9302 | 717 | |
3fb99a22 | 718 | tc = bt2_trace_class._TraceClass._create_from_ptr(tc_ptr) |
fbbe9302 SM |
719 | tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id |
720 | ||
5783664e PP |
721 | if user_attributes is not None: |
722 | tc._user_attributes = user_attributes | |
723 | ||
fbbe9302 SM |
724 | return tc |
725 | ||
cfbd7cf3 FD |
726 | def _create_clock_class( |
727 | self, | |
728 | frequency=None, | |
729 | name=None, | |
5783664e | 730 | user_attributes=None, |
cfbd7cf3 FD |
731 | description=None, |
732 | precision=None, | |
733 | offset=None, | |
734 | origin_is_unix_epoch=True, | |
735 | uuid=None, | |
736 | ): | |
85906b6b | 737 | ptr = self._bt_as_self_component_ptr(self._bt_ptr) |
3cdfbaea SM |
738 | cc_ptr = native_bt.clock_class_create(ptr) |
739 | ||
740 | if cc_ptr is None: | |
694c792b | 741 | raise bt2._MemoryError('could not create clock class') |
3cdfbaea | 742 | |
3fb99a22 | 743 | cc = bt2_clock_class._ClockClass._create_from_ptr(cc_ptr) |
2ae9f48c SM |
744 | |
745 | if frequency is not None: | |
746 | cc._frequency = frequency | |
747 | ||
be7bbff9 SM |
748 | if name is not None: |
749 | cc._name = name | |
750 | ||
5783664e PP |
751 | if user_attributes is not None: |
752 | cc._user_attributes = user_attributes | |
753 | ||
be7bbff9 SM |
754 | if description is not None: |
755 | cc._description = description | |
756 | ||
757 | if precision is not None: | |
758 | cc._precision = precision | |
759 | ||
760 | if offset is not None: | |
761 | cc._offset = offset | |
762 | ||
763 | cc._origin_is_unix_epoch = origin_is_unix_epoch | |
764 | ||
765 | if uuid is not None: | |
766 | cc._uuid = uuid | |
767 | ||
2ae9f48c | 768 | return cc |
3cdfbaea | 769 | |
81447b5b | 770 | |
615238be | 771 | class _UserSourceComponent(_UserComponent, _SourceComponentConst): |
cfbd7cf3 FD |
772 | _bt_as_not_self_specific_component_ptr = staticmethod( |
773 | native_bt.self_component_source_as_component_source | |
774 | ) | |
775 | _bt_as_self_component_ptr = staticmethod( | |
776 | native_bt.self_component_source_as_self_component | |
777 | ) | |
1c9ed2ff | 778 | |
81447b5b | 779 | @property |
811644b8 | 780 | def _output_ports(self): |
894a8df5 | 781 | def get_output_port_count(self_ptr): |
85906b6b | 782 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
783 | return native_bt.component_source_get_output_port_count(ptr) |
784 | ||
cfbd7cf3 FD |
785 | return _ComponentPorts( |
786 | self._bt_ptr, | |
787 | native_bt.self_component_source_borrow_output_port_by_name, | |
788 | native_bt.self_component_source_borrow_output_port_by_index, | |
789 | get_output_port_count, | |
3fb99a22 | 790 | bt2_port._UserComponentOutputPort, |
cfbd7cf3 | 791 | ) |
811644b8 | 792 | |
2e00bc76 | 793 | def _add_output_port(self, name, user_data=None): |
811644b8 | 794 | utils._check_str(name) |
894a8df5 | 795 | fn = native_bt.self_component_source_add_output_port |
85906b6b | 796 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
cfbd7cf3 FD |
797 | utils._handle_func_status( |
798 | comp_status, 'cannot add output port to source component object' | |
799 | ) | |
894a8df5 | 800 | assert self_port_ptr is not None |
3fb99a22 | 801 | return bt2_port._UserComponentOutputPort._create_from_ptr(self_port_ptr) |
811644b8 PP |
802 | |
803 | ||
615238be | 804 | class _UserFilterComponent(_UserComponent, _FilterComponentConst): |
cfbd7cf3 FD |
805 | _bt_as_not_self_specific_component_ptr = staticmethod( |
806 | native_bt.self_component_filter_as_component_filter | |
807 | ) | |
808 | _bt_as_self_component_ptr = staticmethod( | |
809 | native_bt.self_component_filter_as_self_component | |
810 | ) | |
1c9ed2ff | 811 | |
811644b8 PP |
812 | @property |
813 | def _output_ports(self): | |
894a8df5 | 814 | def get_output_port_count(self_ptr): |
85906b6b | 815 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
816 | return native_bt.component_filter_get_output_port_count(ptr) |
817 | ||
cfbd7cf3 FD |
818 | return _ComponentPorts( |
819 | self._bt_ptr, | |
820 | native_bt.self_component_filter_borrow_output_port_by_name, | |
821 | native_bt.self_component_filter_borrow_output_port_by_index, | |
822 | get_output_port_count, | |
3fb99a22 | 823 | bt2_port._UserComponentOutputPort, |
cfbd7cf3 | 824 | ) |
81447b5b | 825 | |
811644b8 PP |
826 | @property |
827 | def _input_ports(self): | |
894a8df5 | 828 | def get_input_port_count(self_ptr): |
85906b6b | 829 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
830 | return native_bt.component_filter_get_input_port_count(ptr) |
831 | ||
cfbd7cf3 FD |
832 | return _ComponentPorts( |
833 | self._bt_ptr, | |
834 | native_bt.self_component_filter_borrow_input_port_by_name, | |
835 | native_bt.self_component_filter_borrow_input_port_by_index, | |
836 | get_input_port_count, | |
3fb99a22 | 837 | bt2_port._UserComponentInputPort, |
cfbd7cf3 | 838 | ) |
811644b8 | 839 | |
2e00bc76 | 840 | def _add_output_port(self, name, user_data=None): |
811644b8 | 841 | utils._check_str(name) |
894a8df5 | 842 | fn = native_bt.self_component_filter_add_output_port |
85906b6b | 843 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
cfbd7cf3 FD |
844 | utils._handle_func_status( |
845 | comp_status, 'cannot add output port to filter component object' | |
846 | ) | |
894a8df5 | 847 | assert self_port_ptr |
3fb99a22 | 848 | return bt2_port._UserComponentOutputPort._create_from_ptr(self_port_ptr) |
811644b8 | 849 | |
2e00bc76 | 850 | def _add_input_port(self, name, user_data=None): |
811644b8 | 851 | utils._check_str(name) |
894a8df5 | 852 | fn = native_bt.self_component_filter_add_input_port |
85906b6b | 853 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
cfbd7cf3 FD |
854 | utils._handle_func_status( |
855 | comp_status, 'cannot add input port to filter component object' | |
856 | ) | |
894a8df5 | 857 | assert self_port_ptr |
3fb99a22 | 858 | return bt2_port._UserComponentInputPort._create_from_ptr(self_port_ptr) |
811644b8 PP |
859 | |
860 | ||
615238be | 861 | class _UserSinkComponent(_UserComponent, _SinkComponentConst): |
cfbd7cf3 FD |
862 | _bt_as_not_self_specific_component_ptr = staticmethod( |
863 | native_bt.self_component_sink_as_component_sink | |
864 | ) | |
865 | _bt_as_self_component_ptr = staticmethod( | |
866 | native_bt.self_component_sink_as_self_component | |
867 | ) | |
1c9ed2ff | 868 | |
d14a864a | 869 | def _bt_graph_is_configured_from_native(self): |
6a91742b | 870 | self._user_graph_is_configured() |
d14a864a | 871 | |
6a91742b | 872 | def _user_graph_is_configured(self): |
cd1ef6f2 PP |
873 | pass |
874 | ||
811644b8 PP |
875 | @property |
876 | def _input_ports(self): | |
894a8df5 | 877 | def get_input_port_count(self_ptr): |
85906b6b | 878 | ptr = self._bt_as_not_self_specific_component_ptr(self_ptr) |
894a8df5 SM |
879 | return native_bt.component_sink_get_input_port_count(ptr) |
880 | ||
cfbd7cf3 FD |
881 | return _ComponentPorts( |
882 | self._bt_ptr, | |
883 | native_bt.self_component_sink_borrow_input_port_by_name, | |
884 | native_bt.self_component_sink_borrow_input_port_by_index, | |
885 | get_input_port_count, | |
3fb99a22 | 886 | bt2_port._UserComponentInputPort, |
cfbd7cf3 | 887 | ) |
811644b8 | 888 | |
2e00bc76 | 889 | def _add_input_port(self, name, user_data=None): |
811644b8 | 890 | utils._check_str(name) |
894a8df5 | 891 | fn = native_bt.self_component_sink_add_input_port |
85906b6b | 892 | comp_status, self_port_ptr = fn(self._bt_ptr, name, user_data) |
cfbd7cf3 FD |
893 | utils._handle_func_status( |
894 | comp_status, 'cannot add input port to sink component object' | |
895 | ) | |
5f25509b | 896 | assert self_port_ptr |
3fb99a22 | 897 | return bt2_port._UserComponentInputPort._create_from_ptr(self_port_ptr) |
ca02df0a PP |
898 | |
899 | def _create_input_port_message_iterator(self, input_port): | |
3fb99a22 | 900 | utils._check_type(input_port, bt2_port._UserComponentInputPort) |
ca02df0a | 901 | |
e803df70 | 902 | status, msg_iter_ptr = native_bt.bt2_self_component_port_input_message_iterator_create_from_sink_component( |
ca02df0a PP |
903 | self._bt_ptr, input_port._ptr |
904 | ) | |
e803df70 SM |
905 | utils._handle_func_status(status, 'cannot create message iterator object') |
906 | assert msg_iter_ptr is not None | |
ca02df0a | 907 | |
3fb99a22 | 908 | return bt2_message_iterator._UserComponentInputPortMessageIterator(msg_iter_ptr) |
9b4f9b42 PP |
909 | |
910 | @property | |
911 | def _is_interrupted(self): | |
912 | return bool(native_bt.self_component_sink_is_interrupted(self._bt_ptr)) |