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