| 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 |
| 24 | from bt2 import component as bt2_component |
| 25 | import bt2 |
| 26 | |
| 27 | |
| 28 | def _is_source_comp_cls(comp_cls): |
| 29 | if isinstance(comp_cls, bt2_component._SourceComponentClassConst): |
| 30 | return True |
| 31 | |
| 32 | try: |
| 33 | return issubclass(comp_cls, bt2_component._UserSourceComponent) |
| 34 | except Exception: |
| 35 | return False |
| 36 | |
| 37 | |
| 38 | def _is_filter_comp_cls(comp_cls): |
| 39 | if isinstance(comp_cls, bt2_component._FilterComponentClassConst): |
| 40 | return True |
| 41 | |
| 42 | try: |
| 43 | return issubclass(comp_cls, bt2_component._UserFilterComponent) |
| 44 | except Exception: |
| 45 | return False |
| 46 | |
| 47 | |
| 48 | def _is_sink_comp_cls(comp_cls): |
| 49 | if isinstance(comp_cls, bt2_component._SinkComponentClassConst): |
| 50 | return True |
| 51 | |
| 52 | try: |
| 53 | return issubclass(comp_cls, bt2_component._UserSinkComponent) |
| 54 | except Exception: |
| 55 | return False |
| 56 | |
| 57 | |
| 58 | class ComponentDescriptor: |
| 59 | def __init__(self, component_class, params=None, obj=None): |
| 60 | if ( |
| 61 | not _is_source_comp_cls(component_class) |
| 62 | and not _is_filter_comp_cls(component_class) |
| 63 | and not _is_sink_comp_cls(component_class) |
| 64 | ): |
| 65 | raise TypeError( |
| 66 | "'{}' is not a component class".format( |
| 67 | component_class.__class__.__name__ |
| 68 | ) |
| 69 | ) |
| 70 | |
| 71 | base_cc_ptr = component_class._bt_component_class_ptr() |
| 72 | |
| 73 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): |
| 74 | raise ValueError('cannot pass a Python object to a non-Python component') |
| 75 | |
| 76 | self._comp_cls = component_class |
| 77 | self._params = bt2.create_value(params) |
| 78 | self._obj = obj |
| 79 | |
| 80 | @property |
| 81 | def component_class(self): |
| 82 | return self._comp_cls |
| 83 | |
| 84 | @property |
| 85 | def params(self): |
| 86 | return self._params |
| 87 | |
| 88 | @property |
| 89 | def obj(self): |
| 90 | return self._obj |