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 | ||
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. | |
bbb3650f 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 | ||
c3044a97 | 48 | class _GenericComponentClass(object._SharedObject): |
81447b5b PP |
49 | @property |
50 | def name(self): | |
bbb3650f 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 | |
f6a5e476 | 54 | return name |
81447b5b PP |
55 | |
56 | @property | |
57 | def description(self): | |
bbb3650f 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): | |
bbb3650f 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 | |
f6a5e476 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 | |
f6a5e476 | 77 | return self.addr == other.addr |
81447b5b PP |
78 | |
79 | ||
80 | class _GenericSourceComponentClass(_GenericComponentClass): | |
a49e2cc3 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): | |
a49e2cc3 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): | |
a49e2cc3 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 | ||
f6a5e476 | 98 | def _handle_component_status(status, gen_error_msg): |
bc5c9924 | 99 | if status == native_bt.SELF_COMPONENT_STATUS_END: |
f6a5e476 | 100 | raise bt2.Stop |
bc5c9924 | 101 | elif status == native_bt.SELF_COMPONENT_STATUS_AGAIN: |
f6a5e476 | 102 | raise bt2.TryAgain |
bc5c9924 | 103 | elif status == native_bt.SELF_COMPONENT_STATUS_REFUSE_PORT_CONNECTION: |
f6a5e476 | 104 | raise bt2.PortConnectionRefused |
f6a5e476 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 | |
bc5c9924 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 | |
f6a5e476 | 126 | |
f6a5e476 PP |
127 | self._at += 1 |
128 | return name | |
129 | ||
130 | ||
131 | class _ComponentPorts(collections.abc.Mapping): | |
bc5c9924 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 | |
f6a5e476 PP |
147 | |
148 | def __getitem__(self, key): | |
149 | utils._check_str(key) | |
bc5c9924 | 150 | port_ptr = self._borrow_port_ptr_by_name(self._component_ptr, key) |
f6a5e476 PP |
151 | |
152 | if port_ptr is None: | |
153 | raise KeyError(key) | |
154 | ||
bc5c9924 | 155 | return self._port_pycls._create_from_ptr_and_get_ref(port_ptr) |
f6a5e476 PP |
156 | |
157 | def __len__(self): | |
bc5c9924 SM |
158 | count = self._get_port_count(self._component_ptr) |
159 | assert count >= 0 | |
f6a5e476 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 |
bbb3650f 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. | |
e34fb94a SM |
176 | # - _as_component_ptr: static method, must return the passed specialized |
177 | # component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'. | |
bbb3650f | 178 | |
f6a5e476 | 179 | class _Component: |
81447b5b PP |
180 | @property |
181 | def name(self): | |
e34fb94a SM |
182 | ptr = self._as_component_ptr(self._ptr) |
183 | name = native_bt.component_get_name(ptr) | |
184 | assert name is not None | |
f6a5e476 PP |
185 | return name |
186 | ||
81447b5b PP |
187 | @property |
188 | def component_class(self): | |
bbb3650f 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 | |
f6a5e476 PP |
193 | def __eq__(self, other): |
194 | if not hasattr(other, 'addr'): | |
195 | return False | |
81447b5b | 196 | |
f6a5e476 | 197 | return self.addr == other.addr |
81447b5b | 198 | |
81447b5b | 199 | |
f6a5e476 | 200 | class _SourceComponent(_Component): |
a49e2cc3 | 201 | _borrow_component_class_ptr = staticmethod(native_bt.component_source_borrow_class_const) |
bbb3650f | 202 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
a49e2cc3 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 | |
f6a5e476 | 207 | class _FilterComponent(_Component): |
a49e2cc3 | 208 | _borrow_component_class_ptr = staticmethod(native_bt.component_filter_borrow_class_const) |
bbb3650f | 209 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
a49e2cc3 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 | |
f6a5e476 | 214 | class _SinkComponent(_Component): |
a49e2cc3 | 215 | _borrow_component_class_ptr = staticmethod(native_bt.component_sink_borrow_class_const) |
bbb3650f | 216 | _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
a49e2cc3 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. | |
c3044a97 | 223 | class _GenericSourceComponent(object._SharedObject, _SourceComponent): |
bc5c9924 SM |
224 | _get_ref = staticmethod(native_bt.component_source_get_ref) |
225 | _put_ref = staticmethod(native_bt.component_source_put_ref) | |
226 | ||
f6a5e476 PP |
227 | @property |
228 | def output_ports(self): | |
bc5c9924 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. | |
c3044a97 | 238 | class _GenericFilterComponent(object._SharedObject, _FilterComponent): |
bc5c9924 SM |
239 | _get_ref = staticmethod(native_bt.component_filter_get_ref) |
240 | _put_ref = staticmethod(native_bt.component_filter_put_ref) | |
241 | ||
f6a5e476 PP |
242 | @property |
243 | def output_ports(self): | |
bc5c9924 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) | |
f6a5e476 PP |
249 | |
250 | @property | |
251 | def input_ports(self): | |
bc5c9924 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. | |
c3044a97 | 261 | class _GenericSinkComponent(object._SharedObject, _SinkComponent): |
a49e2cc3 PP |
262 | _get_ref = staticmethod(native_bt.component_sink_get_ref) |
263 | _put_ref = staticmethod(native_bt.component_sink_put_ref) | |
bbb3650f | 264 | |
f6a5e476 PP |
265 | @property |
266 | def input_ports(self): | |
bc5c9924 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 | ||
f6a5e476 | 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 | ||
f6a5e476 | 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 | ||
bbb3650f 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): | |
f6a5e476 | 295 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr) |
81447b5b | 296 | |
bbb3650f SM |
297 | # Create a component class Python object of type |
298 | # _GenericSourceComponentClass, _GenericFilterComponentClass or | |
299 | # _GenericSinkComponentClass, depending on comp_cls_type. | |
300 | # | |
301 | # Acquires a new reference to ptr. | |
81447b5b | 302 | |
bbb3650f SM |
303 | def _create_component_class_from_ptr_and_get_ref(ptr, comp_cls_type): |
304 | return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type]._create_from_ptr_and_get_ref(ptr) | |
81447b5b PP |
305 | |
306 | ||
40910fbb PP |
307 | def _trim_docstring(docstring): |
308 | lines = docstring.expandtabs().splitlines() | |
309 | indent = sys.maxsize | |
310 | ||
311 | for line in lines[1:]: | |
312 | stripped = line.lstrip() | |
313 | ||
314 | if stripped: | |
315 | indent = min(indent, len(line) - len(stripped)) | |
316 | ||
317 | trimmed = [lines[0].strip()] | |
318 | ||
319 | if indent < sys.maxsize: | |
320 | for line in lines[1:]: | |
321 | trimmed.append(line[indent:].rstrip()) | |
322 | ||
323 | while trimmed and not trimmed[-1]: | |
324 | trimmed.pop() | |
325 | ||
326 | while trimmed and not trimmed[0]: | |
327 | trimmed.pop(0) | |
328 | ||
329 | return '\n'.join(trimmed) | |
330 | ||
331 | ||
81447b5b PP |
332 | # Metaclass for component classes defined by Python code. |
333 | # | |
334 | # The Python user can create a standard Python class which inherits one | |
f6a5e476 PP |
335 | # of the three base classes (_UserSourceComponent, _UserFilterComponent, |
336 | # or _UserSinkComponent). Those base classes set this class | |
81447b5b PP |
337 | # (_UserComponentType) as their metaclass. |
338 | # | |
339 | # Once the body of a user-defined component class is executed, this | |
340 | # metaclass is used to create and initialize the class. The metaclass | |
341 | # creates a native BT component class of the corresponding type and | |
342 | # associates it with this user-defined class. The metaclass also defines | |
343 | # class methods like the `name` and `description` properties to match | |
344 | # the _GenericComponentClass interface. | |
345 | # | |
346 | # The component class name which is used is either: | |
347 | # | |
348 | # * The `name` parameter of the class: | |
349 | # | |
350 | # class MySink(bt2.SinkComponent, name='my-custom-sink'): | |
351 | # ... | |
352 | # | |
353 | # * If the `name` class parameter is not used: the name of the class | |
354 | # itself (`MySink` in the example above). | |
355 | # | |
356 | # The component class description which is used is the user-defined | |
357 | # class's docstring: | |
358 | # | |
359 | # class MySink(bt2.SinkComponent): | |
360 | # 'Description goes here' | |
361 | # ... | |
362 | # | |
363 | # A user-defined Python component class can have an __init__() method | |
364 | # which must at least accept the `params` and `name` arguments: | |
365 | # | |
366 | # def __init__(self, params, name, something_else): | |
367 | # ... | |
368 | # | |
f6a5e476 | 369 | # The user-defined component class can also have a _finalize() method |
81447b5b | 370 | # (do NOT use __del__()) to be notified when the component object is |
f6a5e476 | 371 | # finalized. |
81447b5b PP |
372 | # |
373 | # User-defined source and filter component classes must use the | |
fa4c33e3 SM |
374 | # `message_iterator_class` class parameter to specify the |
375 | # message iterator class to use for this component class: | |
81447b5b | 376 | # |
fa4c33e3 | 377 | # class MyMessageIterator(bt2._UserMessageIterator): |
81447b5b PP |
378 | # ... |
379 | # | |
f6a5e476 | 380 | # class MySource(bt2._UserSourceComponent, |
fa4c33e3 | 381 | # message_iterator_class=MyMessageIterator): |
81447b5b PP |
382 | # ... |
383 | # | |
fa4c33e3 SM |
384 | # This message iterator class must inherit |
385 | # bt2._UserMessageIterator, and it must define the _get() and | |
386 | # _next() methods. The message iterator class can also define an | |
81447b5b PP |
387 | # __init__() method: this method has access to the original Python |
388 | # component object which was used to create it as the `component` | |
fa4c33e3 | 389 | # property. The message iterator class can also define a |
f6a5e476 | 390 | # _finalize() method (again, do NOT use __del__()): this is called when |
fa4c33e3 | 391 | # the message iterator is (really) destroyed. |
81447b5b PP |
392 | # |
393 | # When the user-defined class is destroyed, this metaclass's __del__() | |
394 | # method is called: the native BT component class pointer is put (not | |
395 | # needed anymore, at least not by any Python code since all references | |
396 | # are dropped for __del__() to be called). | |
397 | class _UserComponentType(type): | |
398 | # __new__() is used to catch custom class parameters | |
399 | def __new__(meta_cls, class_name, bases, attrs, **kwargs): | |
400 | return super().__new__(meta_cls, class_name, bases, attrs) | |
401 | ||
402 | def __init__(cls, class_name, bases, namespace, **kwargs): | |
403 | super().__init__(class_name, bases, namespace) | |
404 | ||
405 | # skip our own bases; they are never directly instantiated by the user | |
f6a5e476 PP |
406 | own_bases = ( |
407 | '_UserComponent', | |
408 | '_UserFilterSinkComponent', | |
409 | '_UserSourceComponent', | |
410 | '_UserFilterComponent', | |
411 | '_UserSinkComponent', | |
412 | ) | |
413 | ||
414 | if class_name in own_bases: | |
81447b5b PP |
415 | return |
416 | ||
417 | comp_cls_name = kwargs.get('name', class_name) | |
f6a5e476 | 418 | utils._check_str(comp_cls_name) |
40910fbb PP |
419 | comp_cls_descr = None |
420 | comp_cls_help = None | |
421 | ||
422 | if hasattr(cls, '__doc__') and cls.__doc__ is not None: | |
f6a5e476 | 423 | utils._check_str(cls.__doc__) |
40910fbb PP |
424 | docstring = _trim_docstring(cls.__doc__) |
425 | lines = docstring.splitlines() | |
426 | ||
427 | if len(lines) >= 1: | |
428 | comp_cls_descr = lines[0] | |
429 | ||
430 | if len(lines) >= 3: | |
431 | comp_cls_help = '\n'.join(lines[2:]) | |
432 | ||
fa4c33e3 | 433 | iter_cls = kwargs.get('message_iterator_class') |
81447b5b | 434 | |
f6a5e476 | 435 | if _UserSourceComponent in bases: |
81447b5b | 436 | _UserComponentType._set_iterator_class(cls, iter_cls) |
81447b5b PP |
437 | cc_ptr = native_bt.py3_component_class_source_create(cls, |
438 | comp_cls_name, | |
439 | comp_cls_descr, | |
f6a5e476 PP |
440 | comp_cls_help) |
441 | elif _UserFilterComponent in bases: | |
81447b5b | 442 | _UserComponentType._set_iterator_class(cls, iter_cls) |
81447b5b PP |
443 | cc_ptr = native_bt.py3_component_class_filter_create(cls, |
444 | comp_cls_name, | |
445 | comp_cls_descr, | |
f6a5e476 PP |
446 | comp_cls_help) |
447 | elif _UserSinkComponent in bases: | |
81447b5b | 448 | if not hasattr(cls, '_consume'): |
f6a5e476 | 449 | raise bt2.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name)) |
81447b5b PP |
450 | |
451 | cc_ptr = native_bt.py3_component_class_sink_create(cls, | |
452 | comp_cls_name, | |
40910fbb PP |
453 | comp_cls_descr, |
454 | comp_cls_help) | |
81447b5b | 455 | else: |
f6a5e476 | 456 | raise bt2.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name)) |
81447b5b PP |
457 | |
458 | if cc_ptr is None: | |
459 | raise bt2.CreationError("cannot create component class '{}'".format(class_name)) | |
460 | ||
461 | cls._cc_ptr = cc_ptr | |
462 | ||
f6a5e476 PP |
463 | def _init_from_native(cls, comp_ptr, params_ptr): |
464 | # create instance, not user-initialized yet | |
81447b5b PP |
465 | self = cls.__new__(cls) |
466 | ||
bbb3650f | 467 | # pointer to native self component object (weak/borrowed) |
f6a5e476 | 468 | self._ptr = comp_ptr |
81447b5b | 469 | |
f6a5e476 PP |
470 | # call user's __init__() method |
471 | if params_ptr is not None: | |
bbb3650f | 472 | params = bt2.value._create_from_ptr_and_get_ref(params_ptr) |
f6a5e476 PP |
473 | else: |
474 | params = None | |
81447b5b | 475 | |
f6a5e476 | 476 | self.__init__(params) |
81447b5b PP |
477 | return self |
478 | ||
f6a5e476 PP |
479 | def __call__(cls, *args, **kwargs): |
480 | raise bt2.Error('cannot directly instantiate a user component from a Python module') | |
81447b5b PP |
481 | |
482 | @staticmethod | |
483 | def _set_iterator_class(cls, iter_cls): | |
484 | if iter_cls is None: | |
fa4c33e3 | 485 | raise bt2.IncompleteUserClass("cannot create component class '{}': missing message iterator class".format(cls.__name__)) |
81447b5b | 486 | |
fa4c33e3 SM |
487 | if not issubclass(iter_cls, bt2.message_iterator._UserMessageIterator): |
488 | raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(cls.__name__)) | |
81447b5b | 489 | |
f6a5e476 | 490 | if not hasattr(iter_cls, '__next__'): |
fa4c33e3 | 491 | raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class is missing a __next__() method".format(cls.__name__)) |
81447b5b PP |
492 | |
493 | cls._iter_cls = iter_cls | |
494 | ||
495 | @property | |
496 | def name(cls): | |
bbb3650f SM |
497 | ptr = cls._as_component_class_ptr(cls._cc_ptr) |
498 | return native_bt.component_class_get_name(ptr) | |
81447b5b PP |
499 | |
500 | @property | |
501 | def description(cls): | |
bbb3650f SM |
502 | ptr = cls._as_component_class_ptr(cls._cc_ptr) |
503 | return native_bt.component_class_get_description(ptr) | |
81447b5b | 504 | |
40910fbb PP |
505 | @property |
506 | def help(cls): | |
bbb3650f SM |
507 | ptr = cls._as_component_class_ptr(cls._cc_ptr) |
508 | return native_bt.component_class_get_help(ptr) | |
40910fbb | 509 | |
81447b5b PP |
510 | @property |
511 | def addr(cls): | |
512 | return int(cls._cc_ptr) | |
513 | ||
1286dcbb | 514 | def _query_from_native(cls, query_exec_ptr, obj, params_ptr): |
911dec08 | 515 | # this can raise, in which case the native call to |
a67681c1 | 516 | # bt_component_class_query() returns NULL |
f6a5e476 | 517 | if params_ptr is not None: |
bbb3650f | 518 | params = bt2.value._create_from_ptr_and_get_ref(params_ptr) |
f6a5e476 PP |
519 | else: |
520 | params = None | |
521 | ||
bbb3650f SM |
522 | query_exec = bt2.QueryExecutor._create_from_ptr_and_get_ref( |
523 | query_exec_ptr) | |
f6a5e476 | 524 | |
1286dcbb PP |
525 | # this can raise, but the native side checks the exception |
526 | results = cls._query(query_exec, obj, params) | |
f6a5e476 | 527 | |
1286dcbb PP |
528 | # this can raise, but the native side checks the exception |
529 | results = bt2.create_value(results) | |
911dec08 PP |
530 | |
531 | if results is None: | |
532 | results_addr = int(native_bt.value_null) | |
533 | else: | |
f6a5e476 | 534 | # return new reference |
bbb3650f | 535 | results_addr = int(results._release()) |
911dec08 PP |
536 | |
537 | return results_addr | |
538 | ||
1286dcbb | 539 | def _query(cls, query_executor, obj, params): |
bbb3650f | 540 | raise NotImplementedError |
f6a5e476 | 541 | |
bbb3650f SM |
542 | def _component_class_ptr(self): |
543 | return self._as_component_class_ptr(self._cc_ptr) | |
911dec08 | 544 | |
81447b5b PP |
545 | def __del__(cls): |
546 | if hasattr(cls, '_cc_ptr'): | |
bbb3650f SM |
547 | cc_ptr = cls._as_component_class_ptr(cls._cc_ptr) |
548 | native_bt.component_class_put_ref(cc_ptr) | |
81447b5b | 549 | |
e34fb94a SM |
550 | # Subclasses must provide these methods or property: |
551 | # | |
552 | # - _as_not_self_specific_component_ptr: static method, must return the passed | |
553 | # specialized self component pointer (e.g. 'bt_self_component_sink *') as a | |
554 | # specialized non-self pointer (e.g. 'bt_component_sink *'). | |
555 | # - _borrow_component_class_ptr: static method, must return a pointer to the | |
556 | # specialized component class (e.g. 'bt_component_class_sink *') of the | |
557 | # passed specialized component pointer (e.g. 'bt_component_sink *'). | |
558 | # - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_* | |
559 | # constants. | |
81447b5b | 560 | |
f6a5e476 PP |
561 | class _UserComponent(metaclass=_UserComponentType): |
562 | @property | |
563 | def name(self): | |
e34fb94a SM |
564 | ptr = self._as_not_self_specific_component_ptr(self._ptr) |
565 | ptr = self._as_component_ptr(ptr) | |
566 | name = native_bt.component_get_name(ptr) | |
567 | assert name is not None | |
f6a5e476 | 568 | return name |
81447b5b | 569 | |
f6a5e476 PP |
570 | @property |
571 | def component_class(self): | |
e34fb94a SM |
572 | comp_ptr = self._as_not_self_specific_component_ptr(self._ptr) |
573 | cc_ptr = self._borrow_component_class_ptr(comp_ptr) | |
574 | return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type) | |
81447b5b | 575 | |
81447b5b PP |
576 | @property |
577 | def addr(self): | |
578 | return int(self._ptr) | |
579 | ||
f6a5e476 | 580 | def __init__(self, params=None): |
81447b5b PP |
581 | pass |
582 | ||
f6a5e476 | 583 | def _finalize(self): |
81447b5b PP |
584 | pass |
585 | ||
f6a5e476 PP |
586 | def _accept_port_connection(self, port, other_port): |
587 | return True | |
81447b5b | 588 | |
bc5c9924 SM |
589 | def _accept_port_connection_from_native(self, self_port_ptr, self_port_type, other_port_ptr): |
590 | port = bt2.port._create_self_from_ptr_and_get_ref( | |
591 | self_port_ptr, self_port_type) | |
824ce8b6 SM |
592 | |
593 | if self_port_type == native_bt.PORT_TYPE_OUTPUT: | |
594 | other_port_type = native_bt.PORT_TYPE_INPUT | |
595 | else: | |
596 | other_port_type = native_bt.PORT_TYPE_OUTPUT | |
597 | ||
bc5c9924 SM |
598 | other_port = bt2.port._create_from_ptr_and_get_ref( |
599 | other_port_ptr, other_port_type) | |
f6a5e476 | 600 | res = self._accept_port_connection(port, other_port_ptr) |
81447b5b | 601 | |
f6a5e476 PP |
602 | if type(res) is not bool: |
603 | raise TypeError("'{}' is not a 'bool' object") | |
81447b5b | 604 | |
f6a5e476 | 605 | return res |
81447b5b | 606 | |
f6a5e476 PP |
607 | def _port_connected(self, port, other_port): |
608 | pass | |
81447b5b | 609 | |
bc5c9924 SM |
610 | def _port_connected_from_native(self, self_port_ptr, self_port_type, other_port_ptr): |
611 | port = bt2.port._create_self_from_ptr_and_get_ref( | |
612 | self_port_ptr, self_port_type) | |
824ce8b6 SM |
613 | |
614 | if self_port_type == native_bt.PORT_TYPE_OUTPUT: | |
615 | other_port_type = native_bt.PORT_TYPE_INPUT | |
616 | else: | |
617 | other_port_type = native_bt.PORT_TYPE_OUTPUT | |
618 | ||
bc5c9924 SM |
619 | other_port = bt2.port._create_from_ptr_and_get_ref( |
620 | other_port_ptr, other_port_type) | |
621 | self._port_connected(port, other_port) | |
81447b5b | 622 | |
81447b5b | 623 | |
f6a5e476 | 624 | class _UserSourceComponent(_UserComponent, _SourceComponent): |
a49e2cc3 | 625 | _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_source_as_component_source) |
e34fb94a | 626 | |
81447b5b | 627 | @property |
f6a5e476 | 628 | def _output_ports(self): |
bc5c9924 SM |
629 | def get_output_port_count(self_ptr): |
630 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
631 | return native_bt.component_source_get_output_port_count(ptr) | |
632 | ||
633 | return _ComponentPorts(self._ptr, | |
634 | native_bt.self_component_source_borrow_output_port_by_name, | |
635 | native_bt.self_component_source_borrow_output_port_by_index, | |
636 | get_output_port_count, | |
637 | bt2.port._UserComponentOutputPort) | |
f6a5e476 PP |
638 | |
639 | def _add_output_port(self, name): | |
640 | utils._check_str(name) | |
bc5c9924 SM |
641 | fn = native_bt.self_component_source_add_output_port |
642 | comp_status, self_port_ptr = fn(self._ptr, name, None) | |
f6a5e476 PP |
643 | _handle_component_status(comp_status, |
644 | 'cannot add output port to source component object') | |
bc5c9924 SM |
645 | assert self_port_ptr is not None |
646 | return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr) | |
f6a5e476 PP |
647 | |
648 | ||
649 | class _UserFilterComponent(_UserComponent, _FilterComponent): | |
a49e2cc3 | 650 | _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_filter_as_component_filter) |
e34fb94a | 651 | |
f6a5e476 PP |
652 | @property |
653 | def _output_ports(self): | |
bc5c9924 SM |
654 | def get_output_port_count(self_ptr): |
655 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
656 | return native_bt.component_filter_get_output_port_count(ptr) | |
657 | ||
658 | return _ComponentPorts(self._ptr, | |
659 | native_bt.self_component_filter_borrow_output_port_by_name, | |
660 | native_bt.self_component_filter_borrow_output_port_by_index, | |
661 | get_output_port_count, | |
662 | bt2.port._UserComponentOutputPort) | |
81447b5b | 663 | |
f6a5e476 PP |
664 | @property |
665 | def _input_ports(self): | |
bc5c9924 SM |
666 | def get_input_port_count(self_ptr): |
667 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
668 | return native_bt.component_filter_get_input_port_count(ptr) | |
669 | ||
670 | return _ComponentPorts(self._ptr, | |
671 | native_bt.self_component_filter_borrow_input_port_by_name, | |
672 | native_bt.self_component_filter_borrow_input_port_by_index, | |
673 | get_input_port_count, | |
674 | bt2.port._UserComponentInputPort) | |
f6a5e476 PP |
675 | |
676 | def _add_output_port(self, name): | |
677 | utils._check_str(name) | |
bc5c9924 SM |
678 | fn = native_bt.self_component_filter_add_output_port |
679 | comp_status, self_port_ptr = fn(self._ptr, name, None) | |
f6a5e476 PP |
680 | _handle_component_status(comp_status, |
681 | 'cannot add output port to filter component object') | |
bc5c9924 SM |
682 | assert self_port_ptr |
683 | return bt2.port._UserComponentOutputPort._create_from_ptr(self_port_ptr) | |
f6a5e476 PP |
684 | |
685 | def _add_input_port(self, name): | |
686 | utils._check_str(name) | |
bc5c9924 SM |
687 | fn = native_bt.self_component_filter_add_input_port |
688 | comp_status, self_port_ptr = fn(self._ptr, name, None) | |
f6a5e476 PP |
689 | _handle_component_status(comp_status, |
690 | 'cannot add input port to filter component object') | |
bc5c9924 SM |
691 | assert self_port_ptr |
692 | return bt2.port._UserComponentInputPort._create_from_ptr(self_port_ptr) | |
f6a5e476 PP |
693 | |
694 | ||
695 | class _UserSinkComponent(_UserComponent, _SinkComponent): | |
a49e2cc3 | 696 | _as_not_self_specific_component_ptr = staticmethod(native_bt.self_component_sink_as_component_sink) |
e34fb94a | 697 | |
f6a5e476 PP |
698 | @property |
699 | def _input_ports(self): | |
bc5c9924 SM |
700 | def get_input_port_count(self_ptr): |
701 | ptr = self._as_not_self_specific_component_ptr(self_ptr) | |
702 | return native_bt.component_sink_get_input_port_count(ptr) | |
703 | ||
704 | return _ComponentPorts(self._ptr, | |
705 | native_bt.self_component_sink_borrow_input_port_by_name, | |
706 | native_bt.self_component_sink_borrow_input_port_by_index, | |
707 | get_input_port_count, | |
708 | bt2.port._UserComponentInputPort) | |
f6a5e476 PP |
709 | |
710 | def _add_input_port(self, name): | |
711 | utils._check_str(name) | |
bc5c9924 | 712 | fn = native_bt.self_component_sink_add_input_port |
f6a5e476 PP |
713 | comp_status, priv_port_ptr = fn(self._ptr, name, None) |
714 | _handle_component_status(comp_status, | |
715 | 'cannot add input port to sink component object') | |
bc5c9924 SM |
716 | assert priv_port_ptr |
717 | return bt2.port._UserComponentInputPort._create_from_ptr(priv_port_ptr) |