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