| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | |
| 5 | from bt2 import value as bt2_value |
| 6 | from bt2 import component as bt2_component |
| 7 | from bt2 import native_bt |
| 8 | |
| 9 | |
| 10 | def _is_source_comp_cls(comp_cls): |
| 11 | if isinstance(comp_cls, bt2_component._SourceComponentClassConst): |
| 12 | return True |
| 13 | |
| 14 | try: |
| 15 | return issubclass(comp_cls, bt2_component._UserSourceComponent) |
| 16 | except Exception: |
| 17 | return False |
| 18 | |
| 19 | |
| 20 | def _is_filter_comp_cls(comp_cls): |
| 21 | if isinstance(comp_cls, bt2_component._FilterComponentClassConst): |
| 22 | return True |
| 23 | |
| 24 | try: |
| 25 | return issubclass(comp_cls, bt2_component._UserFilterComponent) |
| 26 | except Exception: |
| 27 | return False |
| 28 | |
| 29 | |
| 30 | def _is_sink_comp_cls(comp_cls): |
| 31 | if isinstance(comp_cls, bt2_component._SinkComponentClassConst): |
| 32 | return True |
| 33 | |
| 34 | try: |
| 35 | return issubclass(comp_cls, bt2_component._UserSinkComponent) |
| 36 | except Exception: |
| 37 | return False |
| 38 | |
| 39 | |
| 40 | class ComponentDescriptor: |
| 41 | def __init__(self, component_class, params=None, obj=None): |
| 42 | if ( |
| 43 | not _is_source_comp_cls(component_class) |
| 44 | and not _is_filter_comp_cls(component_class) |
| 45 | and not _is_sink_comp_cls(component_class) |
| 46 | ): |
| 47 | raise TypeError( |
| 48 | "'{}' is not a component class".format( |
| 49 | component_class.__class__.__name__ |
| 50 | ) |
| 51 | ) |
| 52 | |
| 53 | base_cc_ptr = component_class._bt_component_class_ptr() |
| 54 | |
| 55 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): |
| 56 | raise ValueError("cannot pass a Python object to a non-Python component") |
| 57 | |
| 58 | self._comp_cls = component_class |
| 59 | self._params = bt2_value.create_value(params) |
| 60 | self._obj = obj |
| 61 | |
| 62 | @property |
| 63 | def component_class(self): |
| 64 | return self._comp_cls |
| 65 | |
| 66 | @property |
| 67 | def params(self): |
| 68 | return self._params |
| 69 | |
| 70 | @property |
| 71 | def obj(self): |
| 72 | return self._obj |