bindings/python/bt2/setup.py.in: put native module in `bt2` package
[babeltrace.git] / bindings / python / bt2 / bt2 / component.py
CommitLineData
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
23from bt2 import native_bt, object, utils
5602ef81 24import bt2.message_iterator
81447b5b 25import collections.abc
c4239792 26import bt2.value
811644b8
PP
27import traceback
28import bt2.port
40910fbb 29import sys
81447b5b 30import bt2
811644b8
PP
31import os
32
33
34_env_var = os.environ.get('BABELTRACE_PYTHON_BT2_NO_TRACEBACK')
35_NO_PRINT_TRACEBACK = _env_var == '1'
81447b5b
PP
36
37
38# This class wraps a component class pointer. This component class could
39# have been created by Python code, but since we only have the pointer,
40# we can only wrap it in a generic way and lose the original Python
41# class.
601c0026
SM
42#
43# Subclasses must implement some methods that this base class uses:
44#
45# - _as_component_class_ptr: static method, convert the passed component class
46# pointer to a 'bt_component_class *'.
47
78288f58 48class _GenericComponentClass(object._SharedObject):
81447b5b
PP
49 @property
50 def name(self):
601c0026
SM
51 ptr = self._as_component_class_ptr(self._ptr)
52 name = native_bt.component_class_get_name(ptr)
53 assert name is not None
811644b8 54 return name
81447b5b
PP
55
56 @property
57 def description(self):
601c0026
SM
58 ptr = self._as_component_class_ptr(self._ptr)
59 return native_bt.component_class_get_description(ptr)
81447b5b 60
40910fbb
PP
61 @property
62 def help(self):
601c0026
SM
63 ptr = self._as_component_class_ptr(self._ptr)
64 return native_bt.component_class_get_help(ptr)
65
66 def _component_class_ptr(self):
67 return self._as_component_class_ptr(self._ptr)
40910fbb 68
811644b8
PP
69 def __eq__(self, other):
70 if not isinstance(other, _GenericComponentClass):
71 try:
72 if not issubclass(other, _UserComponent):
73 return False
74 except TypeError:
75 return False
81447b5b 76
811644b8 77 return self.addr == other.addr
81447b5b
PP
78
79
80class _GenericSourceComponentClass(_GenericComponentClass):
601c0026
SM
81 _get_ref = native_bt.component_class_source_get_ref
82 _put_ref = native_bt.component_class_source_put_ref
83 _as_component_class_ptr = native_bt.component_class_source_as_component_class
81447b5b
PP
84
85
86class _GenericFilterComponentClass(_GenericComponentClass):
601c0026
SM
87 _get_ref = native_bt.component_class_filter_get_ref
88 _put_ref = native_bt.component_class_filter_put_ref
89 _as_component_class_ptr = native_bt.component_class_filter_as_component_class
81447b5b
PP
90
91
92class _GenericSinkComponentClass(_GenericComponentClass):
601c0026
SM
93 _get_ref = native_bt.component_class_sink_get_ref
94 _put_ref = native_bt.component_class_sink_put_ref
95 _as_component_class_ptr = native_bt.component_class_sink_as_component_class
81447b5b
PP
96
97
811644b8
PP
98def _handle_component_status(status, gen_error_msg):
99 if status == native_bt.COMPONENT_STATUS_END:
100 raise bt2.Stop
101 elif status == native_bt.COMPONENT_STATUS_AGAIN:
102 raise bt2.TryAgain
103 elif status == native_bt.COMPONENT_STATUS_UNSUPPORTED:
104 raise bt2.UnsupportedFeature
105 elif status == native_bt.COMPONENT_STATUS_REFUSE_PORT_CONNECTION:
106 raise bt2.PortConnectionRefused
107 elif status == native_bt.COMPONENT_STATUS_GRAPH_IS_CANCELED:
108 raise bt2.GraphCanceled
109 elif status < 0:
110 raise bt2.Error(gen_error_msg)
111
112
113class _PortIterator(collections.abc.Iterator):
114 def __init__(self, comp_ports):
115 self._comp_ports = comp_ports
116 self._at = 0
117
118 def __next__(self):
119 if self._at == len(self._comp_ports):
120 raise StopIteration
121
122 comp_ports = self._comp_ports
123 comp_ptr = comp_ports._component._ptr
124 port_ptr = comp_ports._get_port_at_index_fn(comp_ptr, self._at)
125 assert(port_ptr)
126
127 if comp_ports._is_private:
6d137876 128 port_pub_ptr = native_bt.port_from_private(port_ptr)
811644b8
PP
129 name = native_bt.port_get_name(port_pub_ptr)
130 native_bt.put(port_pub_ptr)
131 else:
132 name = native_bt.port_get_name(port_ptr)
133
134 assert(name is not None)
135 native_bt.put(port_ptr)
136 self._at += 1
137 return name
138
139
140class _ComponentPorts(collections.abc.Mapping):
141 def __init__(self, is_private, component,
142 get_port_by_name_fn, get_port_at_index_fn,
143 get_port_count_fn):
144 self._is_private = is_private
145 self._component = component
146 self._get_port_by_name_fn = get_port_by_name_fn
147 self._get_port_at_index_fn = get_port_at_index_fn
148 self._get_port_count_fn = get_port_count_fn
149
150 def __getitem__(self, key):
151 utils._check_str(key)
152 port_ptr = self._get_port_by_name_fn(self._component._ptr, key)
153
154 if port_ptr is None:
155 raise KeyError(key)
156
157 if self._is_private:
158 return bt2.port._create_private_from_ptr(port_ptr)
159 else:
160 return bt2.port._create_from_ptr(port_ptr)
161
162 def __len__(self):
163 if self._is_private:
6d137876 164 pub_ptr = native_bt.component_from_private(self._component._ptr)
811644b8
PP
165 count = self._get_port_count_fn(pub_ptr)
166 native_bt.put(pub_ptr)
167 else:
168 count = self._get_port_count_fn(self._component._ptr)
169
170 assert(count >= 0)
171 return count
172
173 def __iter__(self):
174 return _PortIterator(self)
175
176
81447b5b 177# This class holds the methods which are common to both generic
601c0026
SM
178# component objects and Python user component objects.
179#
180# Subclasses must provide these methods or property:
181#
182# - _borrow_component_class_ptr: static method, must return a pointer to the
183# specialized component class (e.g. 'bt_component_class_sink *') of the
184# passed specialized component pointer (e.g. 'bt_component_sink *').
185# - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
186# constants.
1c9ed2ff
SM
187# - _as_component_ptr: static method, must return the passed specialized
188# component pointer (e.g. 'bt_component_sink *') as a 'bt_component *'.
601c0026 189
811644b8 190class _Component:
81447b5b
PP
191 @property
192 def name(self):
1c9ed2ff
SM
193 ptr = self._as_component_ptr(self._ptr)
194 name = native_bt.component_get_name(ptr)
195 assert name is not None
811644b8
PP
196 return name
197
81447b5b
PP
198 @property
199 def component_class(self):
601c0026
SM
200 cc_ptr = self._borrow_component_class_ptr(self._ptr)
201 assert cc_ptr is not None
202 return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type)
81447b5b 203
811644b8
PP
204 def __eq__(self, other):
205 if not hasattr(other, 'addr'):
206 return False
81447b5b 207
811644b8 208 return self.addr == other.addr
81447b5b 209
81447b5b 210
811644b8 211class _SourceComponent(_Component):
601c0026
SM
212 _borrow_component_class_ptr = native_bt.component_source_borrow_class_const
213 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
214 _as_component_class_ptr = native_bt.component_class_source_as_component_class
1c9ed2ff 215 _as_component_ptr = native_bt.component_source_as_component_const
81447b5b 216
81447b5b 217
811644b8 218class _FilterComponent(_Component):
601c0026
SM
219 _borrow_component_class_ptr = native_bt.component_filter_borrow_class_const
220 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
221 _as_component_class_ptr = native_bt.component_class_filter_as_component_class
1c9ed2ff 222 _as_component_ptr = native_bt.component_filter_as_component_const
81447b5b 223
81447b5b 224
811644b8 225class _SinkComponent(_Component):
601c0026
SM
226 _borrow_component_class_ptr = native_bt.component_sink_borrow_class_const
227 _comp_cls_type = native_bt.COMPONENT_CLASS_TYPE_SINK
228 _as_component_class_ptr = native_bt.component_class_sink_as_component_class
1c9ed2ff 229 _as_component_ptr = native_bt.component_sink_as_component_const
81447b5b
PP
230
231
232# This is analogous to _GenericSourceComponentClass, but for source
233# component objects.
78288f58 234class _GenericSourceComponent(object._SharedObject, _SourceComponent):
811644b8
PP
235 @property
236 def output_ports(self):
237 return _ComponentPorts(False, self,
238 native_bt.component_source_get_output_port_by_name,
239 native_bt.component_source_get_output_port_by_index,
240 native_bt.component_source_get_output_port_count)
81447b5b
PP
241
242
243# This is analogous to _GenericFilterComponentClass, but for filter
244# component objects.
78288f58 245class _GenericFilterComponent(object._SharedObject, _FilterComponent):
811644b8
PP
246 @property
247 def output_ports(self):
248 return _ComponentPorts(False, self,
249 native_bt.component_filter_get_output_port_by_name,
250 native_bt.component_filter_get_output_port_by_index,
251 native_bt.component_filter_get_output_port_count)
252
253 @property
254 def input_ports(self):
255 return _ComponentPorts(False, self,
256 native_bt.component_filter_get_input_port_by_name,
257 native_bt.component_filter_get_input_port_by_index,
258 native_bt.component_filter_get_input_port_count)
81447b5b
PP
259
260
261# This is analogous to _GenericSinkComponentClass, but for sink
262# component objects.
78288f58 263class _GenericSinkComponent(object._SharedObject, _SinkComponent):
601c0026
SM
264 _get_ref = native_bt.component_sink_get_ref
265 _put_ref = native_bt.component_sink_put_ref
266
811644b8
PP
267 @property
268 def input_ports(self):
269 return _ComponentPorts(False, self,
270 native_bt.component_sink_get_input_port_by_name,
271 native_bt.component_sink_get_input_port_by_index,
272 native_bt.component_sink_get_input_port_count)
81447b5b
PP
273
274
811644b8 275_COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS = {
81447b5b
PP
276 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponent,
277 native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponent,
278 native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponent,
279}
280
281
811644b8 282_COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS = {
81447b5b
PP
283 native_bt.COMPONENT_CLASS_TYPE_SOURCE: _GenericSourceComponentClass,
284 native_bt.COMPONENT_CLASS_TYPE_FILTER: _GenericFilterComponentClass,
285 native_bt.COMPONENT_CLASS_TYPE_SINK: _GenericSinkComponentClass,
286}
287
288
601c0026
SM
289# Create a component Python object of type _GenericSourceComponent,
290# _GenericFilterComponent or _GenericSinkComponent, depending on
291# comp_cls_type.
292#
293# Steals the reference to ptr from the caller.
294
295def _create_component_from_ptr(ptr, comp_cls_type):
811644b8 296 return _COMP_CLS_TYPE_TO_GENERIC_COMP_PYCLS[comp_cls_type]._create_from_ptr(ptr)
81447b5b 297
601c0026
SM
298# Create a component class Python object of type
299# _GenericSourceComponentClass, _GenericFilterComponentClass or
300# _GenericSinkComponentClass, depending on comp_cls_type.
301#
302# Acquires a new reference to ptr.
81447b5b 303
601c0026
SM
304def _create_component_class_from_ptr_and_get_ref(ptr, comp_cls_type):
305 return _COMP_CLS_TYPE_TO_GENERIC_COMP_CLS_PYCLS[comp_cls_type]._create_from_ptr_and_get_ref(ptr)
81447b5b
PP
306
307
40910fbb
PP
308def _trim_docstring(docstring):
309 lines = docstring.expandtabs().splitlines()
310 indent = sys.maxsize
311
312 for line in lines[1:]:
313 stripped = line.lstrip()
314
315 if stripped:
316 indent = min(indent, len(line) - len(stripped))
317
318 trimmed = [lines[0].strip()]
319
320 if indent < sys.maxsize:
321 for line in lines[1:]:
322 trimmed.append(line[indent:].rstrip())
323
324 while trimmed and not trimmed[-1]:
325 trimmed.pop()
326
327 while trimmed and not trimmed[0]:
328 trimmed.pop(0)
329
330 return '\n'.join(trimmed)
331
332
81447b5b
PP
333# Metaclass for component classes defined by Python code.
334#
335# The Python user can create a standard Python class which inherits one
811644b8
PP
336# of the three base classes (_UserSourceComponent, _UserFilterComponent,
337# or _UserSinkComponent). Those base classes set this class
81447b5b
PP
338# (_UserComponentType) as their metaclass.
339#
340# Once the body of a user-defined component class is executed, this
341# metaclass is used to create and initialize the class. The metaclass
342# creates a native BT component class of the corresponding type and
343# associates it with this user-defined class. The metaclass also defines
344# class methods like the `name` and `description` properties to match
345# the _GenericComponentClass interface.
346#
347# The component class name which is used is either:
348#
349# * The `name` parameter of the class:
350#
351# class MySink(bt2.SinkComponent, name='my-custom-sink'):
352# ...
353#
354# * If the `name` class parameter is not used: the name of the class
355# itself (`MySink` in the example above).
356#
357# The component class description which is used is the user-defined
358# class's docstring:
359#
360# class MySink(bt2.SinkComponent):
361# 'Description goes here'
362# ...
363#
364# A user-defined Python component class can have an __init__() method
365# which must at least accept the `params` and `name` arguments:
366#
367# def __init__(self, params, name, something_else):
368# ...
369#
811644b8 370# The user-defined component class can also have a _finalize() method
81447b5b 371# (do NOT use __del__()) to be notified when the component object is
811644b8 372# finalized.
81447b5b
PP
373#
374# User-defined source and filter component classes must use the
5602ef81
SM
375# `message_iterator_class` class parameter to specify the
376# message iterator class to use for this component class:
81447b5b 377#
5602ef81 378# class MyMessageIterator(bt2._UserMessageIterator):
81447b5b
PP
379# ...
380#
811644b8 381# class MySource(bt2._UserSourceComponent,
5602ef81 382# message_iterator_class=MyMessageIterator):
81447b5b
PP
383# ...
384#
5602ef81
SM
385# This message iterator class must inherit
386# bt2._UserMessageIterator, and it must define the _get() and
387# _next() methods. The message iterator class can also define an
81447b5b
PP
388# __init__() method: this method has access to the original Python
389# component object which was used to create it as the `component`
5602ef81 390# property. The message iterator class can also define a
811644b8 391# _finalize() method (again, do NOT use __del__()): this is called when
5602ef81 392# the message iterator is (really) destroyed.
81447b5b
PP
393#
394# When the user-defined class is destroyed, this metaclass's __del__()
395# method is called: the native BT component class pointer is put (not
396# needed anymore, at least not by any Python code since all references
397# are dropped for __del__() to be called).
398class _UserComponentType(type):
399 # __new__() is used to catch custom class parameters
400 def __new__(meta_cls, class_name, bases, attrs, **kwargs):
401 return super().__new__(meta_cls, class_name, bases, attrs)
402
403 def __init__(cls, class_name, bases, namespace, **kwargs):
404 super().__init__(class_name, bases, namespace)
405
406 # skip our own bases; they are never directly instantiated by the user
811644b8
PP
407 own_bases = (
408 '_UserComponent',
409 '_UserFilterSinkComponent',
410 '_UserSourceComponent',
411 '_UserFilterComponent',
412 '_UserSinkComponent',
413 )
414
415 if class_name in own_bases:
81447b5b
PP
416 return
417
418 comp_cls_name = kwargs.get('name', class_name)
811644b8 419 utils._check_str(comp_cls_name)
40910fbb
PP
420 comp_cls_descr = None
421 comp_cls_help = None
422
423 if hasattr(cls, '__doc__') and cls.__doc__ is not None:
811644b8 424 utils._check_str(cls.__doc__)
40910fbb
PP
425 docstring = _trim_docstring(cls.__doc__)
426 lines = docstring.splitlines()
427
428 if len(lines) >= 1:
429 comp_cls_descr = lines[0]
430
431 if len(lines) >= 3:
432 comp_cls_help = '\n'.join(lines[2:])
433
5602ef81 434 iter_cls = kwargs.get('message_iterator_class')
81447b5b 435
811644b8 436 if _UserSourceComponent in bases:
81447b5b 437 _UserComponentType._set_iterator_class(cls, iter_cls)
81447b5b
PP
438 cc_ptr = native_bt.py3_component_class_source_create(cls,
439 comp_cls_name,
440 comp_cls_descr,
811644b8
PP
441 comp_cls_help)
442 elif _UserFilterComponent in bases:
81447b5b 443 _UserComponentType._set_iterator_class(cls, iter_cls)
81447b5b
PP
444 cc_ptr = native_bt.py3_component_class_filter_create(cls,
445 comp_cls_name,
446 comp_cls_descr,
811644b8
PP
447 comp_cls_help)
448 elif _UserSinkComponent in bases:
81447b5b 449 if not hasattr(cls, '_consume'):
811644b8 450 raise bt2.IncompleteUserClass("cannot create component class '{}': missing a _consume() method".format(class_name))
81447b5b
PP
451
452 cc_ptr = native_bt.py3_component_class_sink_create(cls,
453 comp_cls_name,
40910fbb
PP
454 comp_cls_descr,
455 comp_cls_help)
81447b5b 456 else:
811644b8 457 raise bt2.IncompleteUserClass("cannot find a known component class base in the bases of '{}'".format(class_name))
81447b5b
PP
458
459 if cc_ptr is None:
460 raise bt2.CreationError("cannot create component class '{}'".format(class_name))
461
462 cls._cc_ptr = cc_ptr
463
811644b8
PP
464 def _init_from_native(cls, comp_ptr, params_ptr):
465 # create instance, not user-initialized yet
81447b5b
PP
466 self = cls.__new__(cls)
467
601c0026 468 # pointer to native self component object (weak/borrowed)
811644b8 469 self._ptr = comp_ptr
81447b5b 470
811644b8
PP
471 # call user's __init__() method
472 if params_ptr is not None:
601c0026 473 params = bt2.value._create_from_ptr_and_get_ref(params_ptr)
811644b8
PP
474 else:
475 params = None
81447b5b 476
811644b8 477 self.__init__(params)
81447b5b
PP
478 return self
479
811644b8
PP
480 def __call__(cls, *args, **kwargs):
481 raise bt2.Error('cannot directly instantiate a user component from a Python module')
81447b5b
PP
482
483 @staticmethod
484 def _set_iterator_class(cls, iter_cls):
485 if iter_cls is None:
5602ef81 486 raise bt2.IncompleteUserClass("cannot create component class '{}': missing message iterator class".format(cls.__name__))
81447b5b 487
5602ef81
SM
488 if not issubclass(iter_cls, bt2.message_iterator._UserMessageIterator):
489 raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class does not inherit bt2._UserMessageIterator".format(cls.__name__))
81447b5b 490
811644b8 491 if not hasattr(iter_cls, '__next__'):
5602ef81 492 raise bt2.IncompleteUserClass("cannot create component class '{}': message iterator class is missing a __next__() method".format(cls.__name__))
81447b5b
PP
493
494 cls._iter_cls = iter_cls
495
496 @property
497 def name(cls):
601c0026
SM
498 ptr = cls._as_component_class_ptr(cls._cc_ptr)
499 return native_bt.component_class_get_name(ptr)
81447b5b
PP
500
501 @property
502 def description(cls):
601c0026
SM
503 ptr = cls._as_component_class_ptr(cls._cc_ptr)
504 return native_bt.component_class_get_description(ptr)
81447b5b 505
40910fbb
PP
506 @property
507 def help(cls):
601c0026
SM
508 ptr = cls._as_component_class_ptr(cls._cc_ptr)
509 return native_bt.component_class_get_help(ptr)
40910fbb 510
81447b5b
PP
511 @property
512 def addr(cls):
513 return int(cls._cc_ptr)
514
c7eee084 515 def _query_from_native(cls, query_exec_ptr, obj, params_ptr):
911dec08 516 # this can raise, in which case the native call to
a67681c1 517 # bt_component_class_query() returns NULL
811644b8 518 if params_ptr is not None:
601c0026 519 params = bt2.value._create_from_ptr_and_get_ref(params_ptr)
811644b8
PP
520 else:
521 params = None
522
601c0026
SM
523 query_exec = bt2.QueryExecutor._create_from_ptr_and_get_ref(
524 query_exec_ptr)
811644b8 525
c7eee084
PP
526 # this can raise, but the native side checks the exception
527 results = cls._query(query_exec, obj, params)
811644b8 528
c7eee084
PP
529 # this can raise, but the native side checks the exception
530 results = bt2.create_value(results)
911dec08
PP
531
532 if results is None:
533 results_addr = int(native_bt.value_null)
534 else:
811644b8 535 # return new reference
601c0026 536 results_addr = int(results._release())
911dec08
PP
537
538 return results_addr
539
c7eee084 540 def _query(cls, query_executor, obj, params):
601c0026 541 raise NotImplementedError
811644b8 542
601c0026
SM
543 def _component_class_ptr(self):
544 return self._as_component_class_ptr(self._cc_ptr)
911dec08 545
81447b5b
PP
546 def __del__(cls):
547 if hasattr(cls, '_cc_ptr'):
601c0026
SM
548 cc_ptr = cls._as_component_class_ptr(cls._cc_ptr)
549 native_bt.component_class_put_ref(cc_ptr)
81447b5b 550
1c9ed2ff
SM
551# Subclasses must provide these methods or property:
552#
553# - _as_not_self_specific_component_ptr: static method, must return the passed
554# specialized self component pointer (e.g. 'bt_self_component_sink *') as a
555# specialized non-self pointer (e.g. 'bt_component_sink *').
556# - _borrow_component_class_ptr: static method, must return a pointer to the
557# specialized component class (e.g. 'bt_component_class_sink *') of the
558# passed specialized component pointer (e.g. 'bt_component_sink *').
559# - _comp_cls_type: property, one of the native_bt.COMPONENT_CLASS_TYPE_*
560# constants.
81447b5b 561
811644b8
PP
562class _UserComponent(metaclass=_UserComponentType):
563 @property
564 def name(self):
1c9ed2ff
SM
565 ptr = self._as_not_self_specific_component_ptr(self._ptr)
566 ptr = self._as_component_ptr(ptr)
567 name = native_bt.component_get_name(ptr)
568 assert name is not None
811644b8 569 return name
81447b5b 570
811644b8
PP
571 @property
572 def component_class(self):
1c9ed2ff
SM
573 comp_ptr = self._as_not_self_specific_component_ptr(self._ptr)
574 cc_ptr = self._borrow_component_class_ptr(comp_ptr)
575 return _create_component_class_from_ptr_and_get_ref(cc_ptr, self._comp_cls_type)
81447b5b 576
81447b5b
PP
577 @property
578 def addr(self):
579 return int(self._ptr)
580
811644b8 581 def __init__(self, params=None):
81447b5b
PP
582 pass
583
811644b8 584 def _finalize(self):
81447b5b
PP
585 pass
586
811644b8
PP
587 def _accept_port_connection(self, port, other_port):
588 return True
81447b5b 589
811644b8
PP
590 def _accept_port_connection_from_native(self, port_ptr, other_port_ptr):
591 native_bt.get(port_ptr)
592 native_bt.get(other_port_ptr)
593 port = bt2.port._create_private_from_ptr(port_ptr)
594 other_port = bt2.port._create_from_ptr(other_port_ptr)
595 res = self._accept_port_connection(port, other_port_ptr)
81447b5b 596
811644b8
PP
597 if type(res) is not bool:
598 raise TypeError("'{}' is not a 'bool' object")
81447b5b 599
811644b8 600 return res
81447b5b 601
811644b8
PP
602 def _port_connected(self, port, other_port):
603 pass
81447b5b 604
811644b8
PP
605 def _port_connected_from_native(self, port_ptr, other_port_ptr):
606 native_bt.get(port_ptr)
607 native_bt.get(other_port_ptr)
608 port = bt2.port._create_private_from_ptr(port_ptr)
609 other_port = bt2.port._create_from_ptr(other_port_ptr)
81447b5b 610
811644b8 611 try:
88a77ab5 612 self._port_connected(port, other_port)
811644b8
PP
613 except:
614 if not _NO_PRINT_TRACEBACK:
615 traceback.print_exc()
81447b5b 616
811644b8 617 def _port_disconnected(self, port):
81447b5b
PP
618 pass
619
811644b8
PP
620 def _port_disconnected_from_native(self, port_ptr):
621 native_bt.get(port_ptr)
622 port = bt2.port._create_private_from_ptr(port_ptr)
81447b5b 623
811644b8
PP
624 try:
625 self._port_disconnected(port)
626 except:
627 if not _NO_PRINT_TRACEBACK:
628 traceback.print_exc()
81447b5b 629
81447b5b 630
811644b8 631class _UserSourceComponent(_UserComponent, _SourceComponent):
1c9ed2ff
SM
632 _as_not_self_specific_component_ptr = native_bt.self_component_source_as_component_source
633
81447b5b 634 @property
811644b8
PP
635 def _output_ports(self):
636 return _ComponentPorts(True, self,
637 native_bt.private_component_source_get_output_private_port_by_name,
638 native_bt.private_component_source_get_output_private_port_by_index,
639 native_bt.component_source_get_output_port_count)
640
641 def _add_output_port(self, name):
642 utils._check_str(name)
643 fn = native_bt.private_component_source_add_output_private_port
644 comp_status, priv_port_ptr = fn(self._ptr, name, None)
645 _handle_component_status(comp_status,
646 'cannot add output port to source component object')
647 assert(priv_port_ptr)
648 return bt2.port._create_private_from_ptr(priv_port_ptr)
649
650
651class _UserFilterComponent(_UserComponent, _FilterComponent):
1c9ed2ff
SM
652 _as_not_self_specific_component_ptr = native_bt.self_component_filter_as_component_filter
653
811644b8
PP
654 @property
655 def _output_ports(self):
656 return _ComponentPorts(True, self,
657 native_bt.private_component_filter_get_output_private_port_by_name,
658 native_bt.private_component_filter_get_output_private_port_by_index,
659 native_bt.component_filter_get_output_port_count)
81447b5b 660
811644b8
PP
661 @property
662 def _input_ports(self):
663 return _ComponentPorts(True, self,
664 native_bt.private_component_filter_get_input_private_port_by_name,
665 native_bt.private_component_filter_get_input_private_port_by_index,
666 native_bt.component_filter_get_input_port_count)
667
668 def _add_output_port(self, name):
669 utils._check_str(name)
670 fn = native_bt.private_component_filter_add_output_private_port
671 comp_status, priv_port_ptr = fn(self._ptr, name, None)
672 _handle_component_status(comp_status,
673 'cannot add output port to filter component object')
674 assert(priv_port_ptr)
675 return bt2.port._create_private_from_ptr(priv_port_ptr)
676
677 def _add_input_port(self, name):
678 utils._check_str(name)
679 fn = native_bt.private_component_filter_add_input_private_port
680 comp_status, priv_port_ptr = fn(self._ptr, name, None)
681 _handle_component_status(comp_status,
682 'cannot add input port to filter component object')
683 assert(priv_port_ptr)
684 return bt2.port._create_private_from_ptr(priv_port_ptr)
685
686
687class _UserSinkComponent(_UserComponent, _SinkComponent):
1c9ed2ff
SM
688 _as_not_self_specific_component_ptr = native_bt.self_component_sink_as_component_sink
689
811644b8
PP
690 @property
691 def _input_ports(self):
692 return _ComponentPorts(True, self,
693 native_bt.private_component_sink_get_input_private_port_by_name,
694 native_bt.private_component_sink_get_input_private_port_by_index,
695 native_bt.component_sink_get_input_port_count)
696
697 def _add_input_port(self, name):
698 utils._check_str(name)
699 fn = native_bt.private_component_sink_add_input_private_port
700 comp_status, priv_port_ptr = fn(self._ptr, name, None)
701 _handle_component_status(comp_status,
702 'cannot add input port to sink component object')
703 assert(priv_port_ptr)
704 return bt2.port._create_private_from_ptr(priv_port_ptr)
This page took 0.067567 seconds and 4 git commands to generate.