Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
f865c2aa PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
f865c2aa | 4 | |
6a4dbecc | 5 | from bt2 import native_bt |
f865c2aa PP |
6 | from bt2 import component as bt2_component |
7 | import bt2 | |
8 | ||
9 | ||
10 | def _is_source_comp_cls(comp_cls): | |
615238be | 11 | if isinstance(comp_cls, bt2_component._SourceComponentClassConst): |
f865c2aa PP |
12 | return True |
13 | ||
14 | try: | |
15 | return issubclass(comp_cls, bt2_component._UserSourceComponent) | |
4c4935bf | 16 | except Exception: |
f865c2aa PP |
17 | return False |
18 | ||
19 | ||
20 | def _is_filter_comp_cls(comp_cls): | |
615238be | 21 | if isinstance(comp_cls, bt2_component._FilterComponentClassConst): |
f865c2aa PP |
22 | return True |
23 | ||
24 | try: | |
25 | return issubclass(comp_cls, bt2_component._UserFilterComponent) | |
4c4935bf | 26 | except Exception: |
f865c2aa PP |
27 | return False |
28 | ||
29 | ||
30 | def _is_sink_comp_cls(comp_cls): | |
615238be | 31 | if isinstance(comp_cls, bt2_component._SinkComponentClassConst): |
f865c2aa PP |
32 | return True |
33 | ||
34 | try: | |
35 | return issubclass(comp_cls, bt2_component._UserSinkComponent) | |
4c4935bf | 36 | except Exception: |
f865c2aa PP |
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.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 |