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