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