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