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