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