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 | |
5602ef81 | 24 | import bt2.message_iterator |
81447b5b | 25 | import collections.abc |
c4239792 | 26 | import bt2.value |
811644b8 PP |
27 | import traceback |
28 | import bt2.port | |
40910fbb | 29 | import sys |
81447b5b | 30 | import bt2 |
811644b8 PP |
31 | import os |
32 | ||
33 | ||
34 | _env_var = os.environ.get('BABELTRACE_PYTHON_BT2_NO_TRACEBACK') | |
35 | _NO_PRINT_TRACEBACK = _env_var == '1' | |
81447b5b PP |
36 | |
37 | ||
38 | # This class wraps a component class pointer. This component class could | |
39 | # have been created by Python code, but since we only have the pointer, | |
40 | # we can only wrap it in a generic way and lose the original Python | |
41 | # class. | |
601c0026 SM |
42 | # |
43 | # Subclasses must implement some methods that this base class uses: | |
44 | # | |
45 | # - _as_component_class_ptr: static method, convert the passed component class | |
46 | # pointer to a 'bt_component_class *'. | |
47 | ||
78288f58 | 48 | class _GenericComponentClass(object._SharedObject): |
81447b5b PP |
49 | @property |
50 | def name(self): | |
601c0026 SM |
51 | ptr = self._as_component_class_ptr(self._ptr) |
52 | name = native_bt.component_class_get_name(ptr) | |
53 | assert name is not None | |
811644b8 | 54 | return name |
81447b5b PP |
55 | |
56 | @property | |
57 | def description(self): | |
601c0026 SM |
58 | ptr = self._as_component_class_ptr(self._ptr) |
59 | return native_bt.component_class_get_description(ptr) | |
81447b5b | 60 | |
40910fbb PP |
61 | @property |
62 | def help(self): | |
601c0026 SM |
63 | ptr = self._as_component_class_ptr(self._ptr) |
64 | return native_bt.component_class_get_help(ptr) | |
65 | ||
66 | def _component_class_ptr(self): | |
67 | return self._as_component_class_ptr(self._ptr) | |
40910fbb | 68 | |
811644b8 PP |
69 | def __eq__(self, other): |
70 | if not isinstance(other, _GenericComponentClass): | |
71 | try: | |
72 | if not issubclass(other, _UserComponent): | |
73 | return False | |
74 | except TypeError: | |
75 | return False | |
81447b5b | 76 | |
811644b8 | 77 | return self.addr == other.addr |
81447b5b PP |
78 | |
79 | ||
80 | class _GenericSourceComponentClass(_GenericComponentClass): | |
2f16a6a2 PP |
81 | _get_ref = staticmethod(native_bt.component_class_source_get_ref) |
82 | _put_ref = staticmethod(native_bt.component_class_source_put_ref) | |
83 | _as_component_class_ptr = staticmethod(native_bt.component_class_source_as_component_class) | |
81447b5b PP |
84 | |
85 | ||
86 | class _GenericFilterComponentClass(_GenericComponentClass): | |
2f16a6a2 PP |
87 | _get_ref = staticmethod(native_bt.component_class_filter_get_ref) |
88 | _put_ref = staticmethod(native_bt.component_class_filter_put_ref) | |
89 | _as_component_class_ptr = staticmethod(native_bt.component_class_filter_as_component_class) | |
81447b5b PP |
90 | |
91 | ||
92 | class _GenericSinkComponentClass(_GenericComponentClass): | |
2f16a6a2 PP |
93 | _get_ref = staticmethod(native_bt.component_class_sink_get_ref) |
94 | _put_ref = staticmethod(native_bt.component_class_sink_put_ref) | |
95 | _as_component_class_ptr = staticmethod(native_bt.component_class_sink_as_component_class) | |
81447b5b PP |
96 | |
97 | ||
811644b8 PP |
98 | class _PortIterator(collections.abc.Iterator): |
99 | def __init__(self, comp_ports): | |
100 | self._comp_ports = comp_ports | |
101 | self._at = 0 | |
102 | ||
103 | def __next__(self): | |
104 | if self._at == len(self._comp_ports): | |
105 | raise StopIteration | |
106 | ||
107 | comp_ports = self._comp_ports | |
894a8df5 SM |
108 | comp_ptr = comp_ports._component_ptr |
109 | ||
110 | port_ptr = comp_ports._borrow_port_ptr_at_index(comp_ptr, self._at) | |
111 | assert port_ptr is not None | |
112 | ||
113 | name = native_bt.port_get_name(comp_ports._port_pycls._as_port_ptr(port_ptr)) | |
114 | assert name is not None | |
811644b8 | 115 | |
811644b8 PP |
116 | self._at += 1 |
117 | return name | |
118 | ||
119 | ||
120 | class _ComponentPorts(collections.abc.Mapping): | |
894a8df5 SM |
121 | |
122 | # component_ptr is a bt_component_source *, bt_component_filter * or | |
123 | # bt_component_sink *. Its type must match the type expected by the | |
124 | # functions passed as arguments. | |
125 | ||
126 | def __init__(self, component_ptr, | |
127 | borrow_port_ptr_by_name, | |
128 | borrow_port_ptr_at_index, | |
129 | get_port_count, | |
130 | port_pycls): | |
131 | self._component_ptr = component_ptr | |
132 | self._borrow_port_ptr_by_name = borrow_port_ptr_by_name | |
133 | self._borrow_port_ptr_at_index = borrow_port_ptr_at_index | |
134 | self._get_port_count = get_port_count | |
135 | self._port_pycls = port_pycls | |
811644b8 PP |
136 | |
137 | def __getitem__(self, key): | |
138 | utils._check_str(key) | |
894a8df5 | 139 | port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key) |
811644b8 PP |
140 | |
141 | if port_ptr is None: | |
142 | raise KeyError(key) | |
143 | ||
894a8df5 | 144 | return self._port_pycls._create_from_ptr_and_get_ref(port_ptr) |
811644b8 PP |
145 | |
146 | def __len__(self): | |
894a8df5 SM |
147 | count = self._get_port_count(self._component_ptr) |
148 | assert count >= 0 | |
811644b8 PP |
149 | return count |
150 | ||
151 | def __iter__(self): | |
152 | return _PortIterator(self) | |
153 | ||
154 | ||
81447b5b | 155 | # This class holds the methods which are common to both generic |
601c0026 SM |
156 | # component objects and Python user component objects. |
157 | # | |
158 | # Subclasses must provide these methods or property: | |
159 | # | |
160 | # - _borrow_component_class_ptr: static method, must return a pointer to the | |
161 | # specialized component class (e.g. 'bt_component_class_sink *') of the | |
162 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
163 | # - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* | |
164 | # constants. | |
1c9ed2ff SM |
165 | # - _as_component_ptr: static method, must return the passed specialized |
166 | # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'. | |
601c0026 | 167 | |
811644b8 | 168 | class _Component: |
81447b5b PP |
169 | @property |
170 | def name(self): | |
1c9ed2ff SM |
171 | ptr = self._as_component_ptr(self._ptr) |
172 | name = native_bt.component_get_name(ptr) | |
173 | assert name is not None | |
811644b8 PP |
174 | return name |
175 | ||
e874da19 PP |
176 | @property |
177 | def logging_level(self): | |
178 | ptr = self._as_component_ptr(self._ptr) | |
179 | return native_bt.component_get_logging_level(ptr) | |
180 | ||
81447b5b | 181 | @property |
e8ac1aae | 182 | def cls(self): |
601c0026 SM |
183 | cc_ptr = self._borrow_component_class_ptr(self._ptr) |
184 | assert cc_ptr is not None | |
185 | return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type) | |
81447b5b | 186 | |
811644b8 PP |
187 | def __eq__(self, other): |
188 | if not hasattr(other, 'addr'): | |
189 | return False | |
81447b5b | 190 | |
811644b8 | 191 | return self.addr == other.addr |
81447b5b | 192 | |
81447b5b | 193 | |
811644b8 | 194 | class _SourceComponent(_Component): |
2f16a6a2 | 195 | _borrow_component_class_ptr = staticmethod(native_bt.component_source_borrow_class_const) |
601c0026 | 196 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
2f16a6a2 PP |
197 | _as_component_class_ptr = staticmethod(native_bt.component_class_source_as_component_class) |
198 | _as_component_ptr = staticmethod(native_bt.component_source_as_component_const) | |
81447b5b | 199 | |
81447b5b | 200 | |
811644b8 | 201 | class _FilterComponent(_Component): |
2f16a6a2 | 202 | _borrow_component_class_ptr = staticmethod(native_bt.component_filter_borrow_class_const) |
601c0026 | 203 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
2f16a6a2 PP |
204 | _as_component_class_ptr = staticmethod(native_bt.component_class_filter_as_component_class) |
205 | _as_component_ptr = staticmethod(native_bt.component_filter_as_component_const) | |
81447b5b | 206 | |
81447b5b | 207 | |
811644b8 | 208 | class _SinkComponent(_Component): |
2f16a6a2 | 209 | _borrow_component_class_ptr = staticmethod(native_bt.component_sink_borrow_class_const) |
601c0026 | 210 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
2f16a6a2 PP |
211 | _as_component_class_ptr = staticmethod(native_bt.component_class_sink_as_component_class) |
212 | _as_component_ptr = staticmethod(native_bt.component_sink_as_component_const) | |
81447b5b PP |
213 | |
214 | ||
215 | # This is analogous to _GenericSourceComponentClass, but for source | |
216 | # component objects. | |
78288f58 | 217 | class _GenericSourceComponent(object._SharedObject, _SourceComponent): |
894a8df5 SM |
218 | _get_ref = staticmethod(native_bt.component_source_get_ref) |
219 | _put_ref = staticmethod(native_bt.component_source_put_ref) | |
220 | ||
811644b8 PP |
221 | @property |
222 | def output_ports(self): | |
894a8df5 SM |
223 | return _ComponentPorts(self._ptr, |
224 | native_bt.component_source_borrow_output_port_by_name_const, | |
225 | native_bt.component_source_borrow_output_port_by_index_const, | |
226 | native_bt.component_source_get_output_port_count, | |
227 | bt2.port._OutputPort) | |
81447b5b PP |
228 | |
229 | ||
230 | # This is analogous to _GenericFilterComponentClass, but for filter | |
231 | # component objects. | |
78288f58 | 232 | class _GenericFilterComponent(object._SharedObject, _FilterComponent): |
894a8df5 SM |
233 | _get_ref = staticmethod(native_bt.component_filter_get_ref) |
234 | _put_ref = staticmethod(native_bt.component_filter_put_ref) | |
235 | ||
811644b8 PP |
236 | @property |
237 | def output_ports(self): | |
894a8df5 SM |
238 | return _ComponentPorts(self._ptr, |
239 | native_bt.component_filter_borrow_output_port_by_name_const, | |
240 | native_bt.component_filter_borrow_output_port_by_index_const, | |
241 | native_bt.component_filter_get_output_port_count, | |
242 | bt2.port._OutputPort) | |
811644b8 PP |
243 | |
244 | @property | |
245 | def input_ports(self): | |
894a8df5 SM |
246 | return _ComponentPorts(self._ptr, |
247 | native_bt.component_filter_borrow_input_port_by_name_const, | |
248 | native_bt.component_filter_borrow_input_port_by_index_const, | |
249 | native_bt.component_filter_get_input_port_count, | |
250 | bt2.port._InputPort) | |
81447b5b PP |
251 | |
252 | ||
253 | # This is analogous to _GenericSinkComponentClass, but for sink | |
254 | # component objects. | |
78288f58 | 255 | class _GenericSinkComponent(object._SharedObject, _SinkComponent): |
2f16a6a2 PP |
256 | _get_ref = staticmethod(native_bt.component_sink_get_ref) |
257 | _put_ref = staticmethod(native_bt.component_sink_put_ref) | |
601c0026 | 258 | |
811644b8 PP |
259 | @property |
260 | def input_ports(self): | |
894a8df5 SM |
261 | return _ComponentPorts(self._ptr, |
262 | native_bt.component_sink_borrow_input_port_by_name_const, | |
263 | native_bt.component_sink_borrow_input_port_by_index_const, | |
264 | native_bt.component_sink_get_input_port_count, | |
265 | bt2.port._InputPort) | |
81447b5b PP |
266 | |
267 | ||
811644b8 | 268 | _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = { |
81447b5b PP |
269 | native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponent, |
270 | native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponent, | |
271 | native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponent, | |
272 | } | |
273 | ||
274 | ||
811644b8 | 275 | _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = { |
81447b5b PP |
276 | native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponentClass, |
277 | native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponentClass, | |
278 | native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponentClass, | |
279 | } | |
280 | ||
281 | ||
601c0026 SM |
282 | # Create a component Python object of type _GenericSourceComponent, |
283 | # _GenericFilterComponent or _GenericSinkComponent, depending on | |
284 | # comp_cls_type. | |
285 | # | |
286 | # Steals the reference to ptr from the caller. | |
287 | ||
288 | def _create_component_from_ptr(ptr, comp_cls_type): | |
811644b8 | 289 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr) |
81447b5b | 290 | |
5f25509b SM |
291 | |
292 | # Same as the above, but acquire a new reference instead of stealing the | |
293 | # reference from the caller. | |
294 | ||
295 | def _create_component_from_ptr_and_get_ref(ptr, comp_cls_type): | |
296 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr_and_get_ref(ptr) | |
297 | ||
298 | ||
601c0026 SM |
299 | # Create a component class Python object of type |
300 | # _GenericSourceComponentClass, _GenericFilterComponentClass or | |
301 | # _GenericSinkComponentClass, depending on comp_cls_type. | |
302 | # | |
303 | # Acquires a new reference to ptr. | |
81447b5b | 304 | |
601c0026 SM |
305 | def _create_component_class_from_ptr_and_get_ref(ptr, comp_cls_type): |
306 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type]._create_from_ptr_and_get_ref(ptr) | |
81447b5b PP |
307 | |
308 | ||
40910fbb PP |
309 | def _trim_docstring(docstring): |
310 | lines = docstring.expandtabs().splitlines() | |
311 | indent = sys.maxsize | |
312 | ||
313 | for line in lines[1:]: | |
314 | stripped = line.lstrip() | |
315 | ||
316 | if stripped: | |
317 | indent = min(indent, len(line) - len(stripped)) | |
318 | ||
319 | trimmed = [lines[0].strip()] | |
320 | ||
321 | if indent < sys.maxsize: | |
322 | for line in lines[1:]: | |
323 | trimmed.append(line[indent:].rstrip()) | |
324 | ||
325 | while trimmed and not trimmed[-1]: | |
326 | trimmed.pop() | |
327 | ||
328 | while trimmed and not trimmed[0]: | |
329 | trimmed.pop(0) | |
330 | ||
331 | return '\n'.join(trimmed) | |
332 | ||
333 | ||
81447b5b PP |
334 | # Metaclass for component classes defined by Python code. |
335 | # | |
336 | # The Python user can create a standard Python class which inherits one | |
811644b8 PP |
337 | # of the three base classes (_UserSourceComponent, _UserFilterComponent, |
338 | # or _UserSinkComponent). Those base classes set this class | |
81447b5b PP |
339 | # (_UserComponentType) as their metaclass. |
340 | # | |
341 | # Once the body of a user-defined component class is executed, this | |
342 | # metaclass is used to create and initialize the class. The metaclass | |
343 | # creates a native BT component class of the corresponding type and | |
344 | # associates it with this user-defined class. The metaclass also defines | |
345 | # class methods like the `name` and `description` properties to match | |
346 | # the _GenericComponentClass interface. | |
347 | # | |
348 | # The component class name which is used is either: | |
349 | # | |
350 | # * The `name` parameter of the class: | |
351 | # | |
352 | # class MySink(bt2.SinkComponent, name='my-custom-sink'): | |
353 | # ... | |
354 | # | |
355 | # * If the `name` class parameter is not used: the name of the class | |
356 | # itself (`MySink` in the example above). | |
357 | # | |
358 | # The component class description which is used is the user-defined | |
359 | # class's docstring: | |
360 | # | |
361 | # class MySink(bt2.SinkComponent): | |
362 | # 'Description goes here' | |
363 | # ... | |
364 | # | |
365 | # A user-defined Python component class can have an __init__() method | |
366 | # which must at least accept the `params` and `name` arguments: | |
367 | # | |
368 | # def __init__(self, params, name, something_else): | |
369 | # ... | |
370 | # | |
811644b8 | 371 | # The user-defined component class can also have a _finalize() method |
81447b5b | 372 | # (do NOT use __del__()) to be notified when the component object is |
811644b8 | 373 | # finalized. |
81447b5b PP |
374 | # |
375 | # User-defined source and filter component classes must use the | |
5602ef81 SM |
376 | # `message_iterator_class` class parameter to specify the |
377 | # message iterator class to use for this component class: | |
81447b5b | 378 | # |
5602ef81 | 379 | # class MyMessageIterator(bt2._UserMessageIterator): |
81447b5b PP |
380 | # ... |
381 | # | |
811644b8 | 382 | # class MySource(bt2._UserSourceComponent, |
5602ef81 | 383 | # message_iterator_class=MyMessageIterator): |
81447b5b PP |
384 | # ... |
385 | # | |
5602ef81 SM |
386 | # This message iterator class must inherit |
387 | # bt2._UserMessageIterator, and it must define the _get() and | |
388 | # _next() methods. The message iterator class can also define an | |
81447b5b PP |
389 | # __init__() method: this method has access to the original Python |
390 | # component object which was used to create it as the `component` | |
5602ef81 | 391 | # property. The message iterator class can also define a |
811644b8 | 392 | # _finalize() method (again, do NOT use __del__()): this is called when |
5602ef81 | 393 | # the message iterator is (really) destroyed. |
81447b5b PP |
394 | # |
395 | # When the user-defined class is destroyed, this metaclass's __del__() | |
396 | # method is called: the native BT component class pointer is put (not | |
397 | # needed anymore, at least not by any Python code since all references | |
398 | # are dropped for __del__() to be called). | |
399 | class _UserComponentType(type): | |
400 | # __new__() is used to catch custom class parameters | |
401 | def __new__(meta_cls, class_name, bases, attrs, **kwargs): | |
402 | return super().__new__(meta_cls, class_name, bases, attrs) | |
403 | ||
404 | def __init__(cls, class_name, bases, namespace, **kwargs): | |
405 | super().__init__(class_name, bases, namespace) | |
406 | ||
407 | # skip our own bases; they are never directly instantiated by the user | |
811644b8 PP |
408 | own_bases = ( |
409 | '_UserComponent', | |
410 | '_UserFilterSinkComponent', | |
411 | '_UserSourceComponent', | |
412 | '_UserFilterComponent', | |
413 | '_UserSinkComponent', | |
414 | ) | |
415 | ||
416 | if class_name in own_bases: | |
81447b5b PP |
417 | return |
418 | ||
419 | comp_cls_name = kwargs.get('name', class_name) | |
811644b8 | 420 | utils._check_str(comp_cls_name) |
40910fbb PP |
421 | comp_cls_descr = None |
422 | comp_cls_help = None | |
423 | ||
424 | if hasattr(cls, '__doc__') and cls.__doc__ is not None: | |
811644b8 | 425 | utils._check_str(cls.__doc__) |
40910fbb PP |
426 | docstring = _trim_docstring(cls.__doc__) |
427 | lines = docstring.splitlines() | |
428 | ||
429 | if len(lines) >= 1: | |
430 | comp_cls_descr = lines[0] | |
431 | ||
432 | if len(lines) >= 3: | |
433 | comp_cls_help = '\n'.join(lines[2:]) | |
434 | ||
5602ef81 | 435 | iter_cls = kwargs.get('message_iterator_class') |
81447b5b | 436 | |
811644b8 | 437 | if _UserSourceComponent in bases: |
81447b5b | 438 | _UserComponentType._set_iterator_class(cls, iter_cls) |
d24d5663 | 439 | cc_ptr = native_bt.bt2_component_class_source_create(cls, |
81447b5b PP |
440 | comp_cls_name, |
441 | comp_cls_descr, | |
811644b8 PP |
442 | comp_cls_help) |
443 | elif _UserFilterComponent in bases: | |
81447b5b | 444 | _UserComponentType._set_iterator_class(cls, iter_cls) |
d24d5663 | 445 | cc_ptr = native_bt.bt2_component_class_filter_create(cls, |
81447b5b PP |
446 | comp_cls_name, |
447 | comp_cls_descr, | |
811644b8 PP |
448 | comp_cls_help) |
449 | elif _UserSinkComponent in bases: | |
81447b5b | 450 | if not hasattr(cls, '_consume'): |
811644b8 | 451 | raise bt2.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name)) |
81447b5b | 452 | |
d24d5663 | 453 | cc_ptr = native_bt.bt2_component_class_sink_create(cls, |
81447b5b | 454 | comp_cls_name, |
40910fbb PP |
455 | comp_cls_descr, |
456 | comp_cls_help) | |
81447b5b | 457 | else: |
811644b8 | 458 | raise bt2.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name)) |
81447b5b PP |
459 | |
460 | if cc_ptr is None: | |
461 | raise bt2.CreationError("cannot create component class '{}'".format(class_name)) | |
462 | ||
463 | cls._cc_ptr = cc_ptr | |
464 | ||
811644b8 PP |
465 | def _init_from_native(cls, comp_ptr, params_ptr): |
466 | # create instance, not user-initialized yet | |
81447b5b PP |
467 | self = cls.__new__(cls) |
468 | ||
601c0026 | 469 | # pointer to native self component object (weak/borrowed) |
811644b8 | 470 | self._ptr = comp_ptr |
81447b5b | 471 | |
811644b8 PP |
472 | # call user's __init__() method |
473 | if params_ptr is not None: | |
601c0026 | 474 | params = bt2.value._create_from_ptr_and_get_ref(params_ptr) |
811644b8 PP |
475 | else: |
476 | params = None | |
81447b5b | 477 | |
811644b8 | 478 | self.__init__(params) |
81447b5b PP |
479 | return self |
480 | ||
811644b8 PP |
481 | def __call__(cls, *args, **kwargs): |
482 | raise bt2.Error('cannot directly instantiate a user component from a Python module') | |
81447b5b PP |
483 | |
484 | @staticmethod | |
485 | def _set_iterator_class(cls, iter_cls): | |
486 | if iter_cls is None: | |
5602ef81 | 487 | raise bt2.IncompleteUserClass("cannot create component class '{}': missing message iterator class".format(cls.__name__)) |
81447b5b | 488 | |
5602ef81 SM |
489 | if not issubclass(iter_cls, bt2.message_iterator._UserMessageIterator): |
490 | raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(cls.__name__)) | |
81447b5b | 491 | |
811644b8 | 492 | if not hasattr(iter_cls, '__next__'): |
5602ef81 | 493 | raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class is missing a __next__() method".format(cls.__name__)) |
81447b5b PP |
494 | |
495 | cls._iter_cls = iter_cls | |
496 | ||
497 | @property | |
498 | def name(cls): | |
601c0026 SM |
499 | ptr = cls._as_component_class_ptr(cls._cc_ptr) |
500 | return native_bt.component_class_get_name(ptr) | |
81447b5b PP |
501 | |
502 | @property | |
503 | def description(cls): | |
601c0026 SM |
504 | ptr = cls._as_component_class_ptr(cls._cc_ptr) |
505 | return native_bt.component_class_get_description(ptr) | |
81447b5b | 506 | |
40910fbb PP |
507 | @property |
508 | def help(cls): | |
601c0026 SM |
509 | ptr = cls._as_component_class_ptr(cls._cc_ptr) |
510 | return native_bt.component_class_get_help(ptr) | |
40910fbb | 511 | |
81447b5b PP |
512 | @property |
513 | def addr(cls): | |
514 | return int(cls._cc_ptr) | |
515 | ||
f4e38e70 | 516 | def _query_from_native(cls, query_exec_ptr, obj, params_ptr, log_level): |
911dec08 | 517 | # this can raise, in which case the native call to |
a67681c1 | 518 | # bt_component_class_query() returns NULL |
811644b8 | 519 | if params_ptr is not None: |
601c0026 | 520 | params = bt2.value._create_from_ptr_and_get_ref(params_ptr) |
811644b8 PP |
521 | else: |
522 | params = None | |
523 | ||
601c0026 SM |
524 | query_exec = bt2.QueryExecutor._create_from_ptr_and_get_ref( |
525 | query_exec_ptr) | |
811644b8 | 526 | |
c7eee084 | 527 | # this can raise, but the native side checks the exception |
f4e38e70 | 528 | results = cls._query(query_exec, obj, params, log_level) |
811644b8 | 529 | |
c7eee084 PP |
530 | # this can raise, but the native side checks the exception |
531 | results = bt2.create_value(results) | |
911dec08 PP |
532 | |
533 | if results is None: | |
534 | results_addr = int(native_bt.value_null) | |
535 | else: | |
811644b8 | 536 | # return new reference |
601c0026 | 537 | results_addr = int(results._release()) |
911dec08 PP |
538 | |
539 | return results_addr | |
540 | ||
9cf25bf2 | 541 | def _query(cls, query_executor, obj, params, log_level): |
601c0026 | 542 | raise NotImplementedError |
811644b8 | 543 | |
601c0026 SM |
544 | def _component_class_ptr(self): |
545 | return self._as_component_class_ptr(self._cc_ptr) | |
911dec08 | 546 | |
81447b5b PP |
547 | def __del__(cls): |
548 | if hasattr(cls, '_cc_ptr'): | |
601c0026 SM |
549 | cc_ptr = cls._as_component_class_ptr(cls._cc_ptr) |
550 | native_bt.component_class_put_ref(cc_ptr) | |
81447b5b | 551 | |
1c9ed2ff SM |
552 | # Subclasses must provide these methods or property: |
553 | # | |
554 | # - _as_not_self_specific_component_ptr: static method, must return the passed | |
555 | # specialized self component pointer (e.g. 'bt_self_component_sink *') as a | |
556 | # specialized non-self pointer (e.g. 'bt_component_sink *'). | |
557 | # - _borrow_component_class_ptr: static method, must return a pointer to the | |
558 | # specialized component class (e.g. 'bt_component_class_sink *') of the | |
559 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
560 | # - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* | |
561 | # constants. | |
81447b5b | 562 | |
811644b8 PP |
563 | class _UserComponent(metaclass=_UserComponentType): |
564 | @property | |
565 | def name(self): | |
1c9ed2ff SM |
566 | ptr = self._as_not_self_specific_component_ptr(self._ptr) |
567 | ptr = self._as_component_ptr(ptr) | |
568 | name = native_bt.component_get_name(ptr) | |
569 | assert name is not None | |
811644b8 | 570 | return name |
81447b5b | 571 | |
e874da19 PP |
572 | @property |
573 | def logging_level(self): | |
574 | ptr = self._as_not_self_specific_component_ptr(self._ptr) | |
575 | ptr = self._as_component_ptr(ptr) | |
576 | return native_bt.component_get_logging_level(ptr) | |
577 | ||
811644b8 | 578 | @property |
e8ac1aae | 579 | def cls(self): |
1c9ed2ff SM |
580 | comp_ptr = self._as_not_self_specific_component_ptr(self._ptr) |
581 | cc_ptr = self._borrow_component_class_ptr(comp_ptr) | |
582 | return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type) | |
81447b5b | 583 | |
81447b5b PP |
584 | @property |
585 | def addr(self): | |
586 | return int(self._ptr) | |
587 | ||
811644b8 | 588 | def __init__(self, params=None): |
81447b5b PP |
589 | pass |
590 | ||
811644b8 | 591 | def _finalize(self): |
81447b5b PP |
592 | pass |
593 | ||
811644b8 PP |
594 | def _port_connected(self, port, other_port): |
595 | pass | |
81447b5b | 596 | |
894a8df5 SM |
597 | def _port_connected_from_native(self, self_port_ptr, self_port_type, other_port_ptr): |
598 | port = bt2.port._create_self_from_ptr_and_get_ref( | |
599 | self_port_ptr, self_port_type) | |
2c67587a SM |
600 | |
601 | if self_port_type == native_bt.PORT_TYPE_OUTPUT: | |
602 | other_port_type = native_bt.PORT_TYPE_INPUT | |
603 | else: | |
604 | other_port_type = native_bt.PORT_TYPE_OUTPUT | |
605 | ||
894a8df5 SM |
606 | other_port = bt2.port._create_from_ptr_and_get_ref( |
607 | other_port_ptr, other_port_type) | |
608 | self._port_connected(port, other_port) | |
81447b5b | 609 | |
5f25509b SM |
610 | def _graph_is_configured_from_native(self): |
611 | self._graph_is_configured() | |
612 | ||
fbbe9302 SM |
613 | def _create_trace_class(self, env=None, uuid=None, |
614 | assigns_automatic_stream_class_id=True): | |
615 | ptr = self._as_self_component_ptr(self._ptr) | |
616 | tc_ptr = native_bt.trace_class_create(ptr) | |
617 | ||
618 | if tc_ptr is None: | |
619 | raise bt2.CreationError('could not create trace class') | |
620 | ||
2c6f8520 | 621 | tc = bt2._TraceClass._create_from_ptr(tc_ptr) |
fbbe9302 SM |
622 | |
623 | if env is not None: | |
624 | for key, value in env.items(): | |
625 | tc.env[key] = value | |
626 | ||
627 | if uuid is not None: | |
628 | tc._uuid = uuid | |
629 | ||
630 | tc._assigns_automatic_stream_class_id = assigns_automatic_stream_class_id | |
631 | ||
632 | return tc | |
633 | ||
be7bbff9 SM |
634 | def _create_clock_class(self, frequency=None, name=None, description=None, |
635 | precision=None, offset=None, origin_is_unix_epoch=True, | |
636 | uuid=None): | |
3cdfbaea SM |
637 | ptr = self._as_self_component_ptr(self._ptr) |
638 | cc_ptr = native_bt.clock_class_create(ptr) | |
639 | ||
640 | if cc_ptr is None: | |
641 | raise bt2.CreationError('could not create clock class') | |
642 | ||
be7bbff9 | 643 | cc = bt2.clock_class._ClockClass._create_from_ptr(cc_ptr) |
2ae9f48c SM |
644 | |
645 | if frequency is not None: | |
646 | cc._frequency = frequency | |
647 | ||
be7bbff9 SM |
648 | if name is not None: |
649 | cc._name = name | |
650 | ||
651 | if description is not None: | |
652 | cc._description = description | |
653 | ||
654 | if precision is not None: | |
655 | cc._precision = precision | |
656 | ||
657 | if offset is not None: | |
658 | cc._offset = offset | |
659 | ||
660 | cc._origin_is_unix_epoch = origin_is_unix_epoch | |
661 | ||
662 | if uuid is not None: | |
663 | cc._uuid = uuid | |
664 | ||
2ae9f48c | 665 | return cc |
3cdfbaea | 666 | |
81447b5b | 667 | |
811644b8 | 668 | class _UserSourceComponent(_UserComponent, _SourceComponent): |
2f16a6a2 | 669 | _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_source_as_component_source) |
fbbe9302 | 670 | _as_self_component_ptr = staticmethod(native_bt.self_component_source_as_self_component) |
1c9ed2ff | 671 | |
81447b5b | 672 | @property |
811644b8 | 673 | def _output_ports(self): |
894a8df5 SM |
674 | def get_output_port_count(self_ptr): |
675 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
676 | return native_bt.component_source_get_output_port_count(ptr) | |
677 | ||
678 | return _ComponentPorts(self._ptr, | |
679 | native_bt.self_component_source_borrow_output_port_by_name, | |
680 | native_bt.self_component_source_borrow_output_port_by_index, | |
681 | get_output_port_count, | |
682 | bt2.port._UserComponentOutputPort) | |
811644b8 | 683 | |
2e00bc76 | 684 | def _add_output_port(self, name, user_data=None): |
811644b8 | 685 | utils._check_str(name) |
894a8df5 | 686 | fn = native_bt.self_component_source_add_output_port |
2e00bc76 | 687 | comp_status, self_port_ptr = fn(self._ptr, name, user_data) |
d24d5663 PP |
688 | utils._handle_func_status(comp_status, |
689 | 'cannot add output port to source component object') | |
894a8df5 SM |
690 | assert self_port_ptr is not None |
691 | return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr) | |
811644b8 PP |
692 | |
693 | ||
694 | class _UserFilterComponent(_UserComponent, _FilterComponent): | |
2f16a6a2 | 695 | _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_filter_as_component_filter) |
fbbe9302 | 696 | _as_self_component_ptr = staticmethod(native_bt.self_component_filter_as_self_component) |
1c9ed2ff | 697 | |
811644b8 PP |
698 | @property |
699 | def _output_ports(self): | |
894a8df5 SM |
700 | def get_output_port_count(self_ptr): |
701 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
702 | return native_bt.component_filter_get_output_port_count(ptr) | |
703 | ||
704 | return _ComponentPorts(self._ptr, | |
705 | native_bt.self_component_filter_borrow_output_port_by_name, | |
706 | native_bt.self_component_filter_borrow_output_port_by_index, | |
707 | get_output_port_count, | |
708 | bt2.port._UserComponentOutputPort) | |
81447b5b | 709 | |
811644b8 PP |
710 | @property |
711 | def _input_ports(self): | |
894a8df5 SM |
712 | def get_input_port_count(self_ptr): |
713 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
714 | return native_bt.component_filter_get_input_port_count(ptr) | |
715 | ||
716 | return _ComponentPorts(self._ptr, | |
717 | native_bt.self_component_filter_borrow_input_port_by_name, | |
718 | native_bt.self_component_filter_borrow_input_port_by_index, | |
719 | get_input_port_count, | |
720 | bt2.port._UserComponentInputPort) | |
811644b8 | 721 | |
2e00bc76 | 722 | def _add_output_port(self, name, user_data=None): |
811644b8 | 723 | utils._check_str(name) |
894a8df5 | 724 | fn = native_bt.self_component_filter_add_output_port |
2e00bc76 | 725 | comp_status, self_port_ptr = fn(self._ptr, name, user_data) |
d24d5663 PP |
726 | utils._handle_func_status(comp_status, |
727 | 'cannot add output port to filter component object') | |
894a8df5 SM |
728 | assert self_port_ptr |
729 | return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr) | |
811644b8 | 730 | |
2e00bc76 | 731 | def _add_input_port(self, name, user_data=None): |
811644b8 | 732 | utils._check_str(name) |
894a8df5 | 733 | fn = native_bt.self_component_filter_add_input_port |
2e00bc76 | 734 | comp_status, self_port_ptr = fn(self._ptr, name, user_data) |
d24d5663 PP |
735 | utils._handle_func_status(comp_status, |
736 | 'cannot add input port to filter component object') | |
894a8df5 SM |
737 | assert self_port_ptr |
738 | return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr) | |
811644b8 PP |
739 | |
740 | ||
741 | class _UserSinkComponent(_UserComponent, _SinkComponent): | |
2f16a6a2 | 742 | _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_sink_as_component_sink) |
fbbe9302 | 743 | _as_self_component_ptr = staticmethod(native_bt.self_component_sink_as_self_component) |
1c9ed2ff | 744 | |
811644b8 PP |
745 | @property |
746 | def _input_ports(self): | |
894a8df5 SM |
747 | def get_input_port_count(self_ptr): |
748 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
749 | return native_bt.component_sink_get_input_port_count(ptr) | |
750 | ||
751 | return _ComponentPorts(self._ptr, | |
752 | native_bt.self_component_sink_borrow_input_port_by_name, | |
753 | native_bt.self_component_sink_borrow_input_port_by_index, | |
754 | get_input_port_count, | |
755 | bt2.port._UserComponentInputPort) | |
811644b8 | 756 | |
2e00bc76 | 757 | def _add_input_port(self, name, user_data=None): |
811644b8 | 758 | utils._check_str(name) |
894a8df5 | 759 | fn = native_bt.self_component_sink_add_input_port |
2e00bc76 | 760 | comp_status, self_port_ptr = fn(self._ptr, name, user_data) |
d24d5663 PP |
761 | utils._handle_func_status(comp_status, |
762 | 'cannot add input port to sink component object') | |
5f25509b SM |
763 | assert self_port_ptr |
764 | return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr) |