| 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 | from bt2 import interrupter as bt2_interrupter |
| 25 | from bt2 import connection as bt2_connection |
| 26 | from bt2 import component as bt2_component |
| 27 | import functools |
| 28 | from bt2 import port as bt2_port |
| 29 | from bt2 import logging as bt2_logging |
| 30 | import bt2 |
| 31 | |
| 32 | |
| 33 | def _graph_port_added_listener_from_native( |
| 34 | user_listener, component_ptr, component_type, port_ptr, port_type |
| 35 | ): |
| 36 | component = bt2_component._create_component_from_ptr_and_get_ref( |
| 37 | component_ptr, component_type |
| 38 | ) |
| 39 | port = bt2_port._create_from_ptr_and_get_ref(port_ptr, port_type) |
| 40 | user_listener(component, port) |
| 41 | |
| 42 | |
| 43 | def _graph_ports_connected_listener_from_native( |
| 44 | user_listener, |
| 45 | upstream_component_ptr, |
| 46 | upstream_component_type, |
| 47 | upstream_port_ptr, |
| 48 | downstream_component_ptr, |
| 49 | downstream_component_type, |
| 50 | downstream_port_ptr, |
| 51 | ): |
| 52 | upstream_component = bt2_component._create_component_from_ptr_and_get_ref( |
| 53 | upstream_component_ptr, upstream_component_type |
| 54 | ) |
| 55 | upstream_port = bt2_port._create_from_ptr_and_get_ref( |
| 56 | upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT |
| 57 | ) |
| 58 | downstream_component = bt2_component._create_component_from_ptr_and_get_ref( |
| 59 | downstream_component_ptr, downstream_component_type |
| 60 | ) |
| 61 | downstream_port = bt2_port._create_from_ptr_and_get_ref( |
| 62 | downstream_port_ptr, native_bt.PORT_TYPE_INPUT |
| 63 | ) |
| 64 | user_listener( |
| 65 | upstream_component, upstream_port, downstream_component, downstream_port |
| 66 | ) |
| 67 | |
| 68 | |
| 69 | class Graph(object._SharedObject): |
| 70 | _get_ref = staticmethod(native_bt.graph_get_ref) |
| 71 | _put_ref = staticmethod(native_bt.graph_put_ref) |
| 72 | |
| 73 | def __init__(self): |
| 74 | ptr = native_bt.graph_create() |
| 75 | |
| 76 | if ptr is None: |
| 77 | raise bt2._MemoryError('cannot create graph object') |
| 78 | |
| 79 | super().__init__(ptr) |
| 80 | |
| 81 | def add_component( |
| 82 | self, |
| 83 | component_class, |
| 84 | name, |
| 85 | params=None, |
| 86 | obj=None, |
| 87 | logging_level=bt2_logging.LoggingLevel.NONE, |
| 88 | ): |
| 89 | if isinstance(component_class, bt2_component._SourceComponentClass): |
| 90 | cc_ptr = component_class._ptr |
| 91 | add_fn = native_bt.bt2_graph_add_source_component |
| 92 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
| 93 | elif isinstance(component_class, bt2_component._FilterComponentClass): |
| 94 | cc_ptr = component_class._ptr |
| 95 | add_fn = native_bt.bt2_graph_add_filter_component |
| 96 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
| 97 | elif isinstance(component_class, bt2_component._SinkComponentClass): |
| 98 | cc_ptr = component_class._ptr |
| 99 | add_fn = native_bt.bt2_graph_add_sink_component |
| 100 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
| 101 | elif issubclass(component_class, bt2_component._UserSourceComponent): |
| 102 | cc_ptr = component_class._bt_cc_ptr |
| 103 | add_fn = native_bt.bt2_graph_add_source_component |
| 104 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
| 105 | elif issubclass(component_class, bt2_component._UserSinkComponent): |
| 106 | cc_ptr = component_class._bt_cc_ptr |
| 107 | add_fn = native_bt.bt2_graph_add_sink_component |
| 108 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
| 109 | elif issubclass(component_class, bt2_component._UserFilterComponent): |
| 110 | cc_ptr = component_class._bt_cc_ptr |
| 111 | add_fn = native_bt.bt2_graph_add_filter_component |
| 112 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
| 113 | else: |
| 114 | raise TypeError( |
| 115 | "'{}' is not a component class".format( |
| 116 | component_class.__class__.__name__ |
| 117 | ) |
| 118 | ) |
| 119 | |
| 120 | utils._check_str(name) |
| 121 | utils._check_log_level(logging_level) |
| 122 | base_cc_ptr = component_class._bt_component_class_ptr() |
| 123 | |
| 124 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): |
| 125 | raise ValueError('cannot pass a Python object to a non-Python component') |
| 126 | |
| 127 | params = bt2.create_value(params) |
| 128 | params_ptr = params._ptr if params is not None else None |
| 129 | |
| 130 | status, comp_ptr = add_fn( |
| 131 | self._ptr, cc_ptr, name, params_ptr, obj, logging_level |
| 132 | ) |
| 133 | utils._handle_func_status(status, 'cannot add component to graph') |
| 134 | assert comp_ptr |
| 135 | return bt2_component._create_component_from_ptr(comp_ptr, cc_type) |
| 136 | |
| 137 | def connect_ports(self, upstream_port, downstream_port): |
| 138 | utils._check_type(upstream_port, bt2_port._OutputPort) |
| 139 | utils._check_type(downstream_port, bt2_port._InputPort) |
| 140 | status, conn_ptr = native_bt.graph_connect_ports( |
| 141 | self._ptr, upstream_port._ptr, downstream_port._ptr |
| 142 | ) |
| 143 | utils._handle_func_status(status, 'cannot connect component ports within graph') |
| 144 | assert conn_ptr |
| 145 | return bt2_connection._Connection._create_from_ptr(conn_ptr) |
| 146 | |
| 147 | def add_port_added_listener(self, listener): |
| 148 | if not callable(listener): |
| 149 | raise TypeError("'listener' parameter is not callable") |
| 150 | |
| 151 | fn = native_bt.bt2_graph_add_port_added_listener |
| 152 | listener_from_native = functools.partial( |
| 153 | _graph_port_added_listener_from_native, listener |
| 154 | ) |
| 155 | |
| 156 | listener_ids = fn(self._ptr, listener_from_native) |
| 157 | if listener_ids is None: |
| 158 | raise bt2._Error('cannot add listener to graph object') |
| 159 | |
| 160 | return utils._ListenerHandle(listener_ids, self) |
| 161 | |
| 162 | def add_ports_connected_listener(self, listener): |
| 163 | if not callable(listener): |
| 164 | raise TypeError("'listener' parameter is not callable") |
| 165 | |
| 166 | fn = native_bt.bt2_graph_add_ports_connected_listener |
| 167 | listener_from_native = functools.partial( |
| 168 | _graph_ports_connected_listener_from_native, listener |
| 169 | ) |
| 170 | |
| 171 | listener_ids = fn(self._ptr, listener_from_native) |
| 172 | if listener_ids is None: |
| 173 | raise bt2._Error('cannot add listener to graph object') |
| 174 | |
| 175 | return utils._ListenerHandle(listener_ids, self) |
| 176 | |
| 177 | def run_once(self): |
| 178 | status = native_bt.graph_run_once(self._ptr) |
| 179 | utils._handle_func_status(status, 'graph object could not run once') |
| 180 | |
| 181 | def run(self): |
| 182 | status = native_bt.graph_run(self._ptr) |
| 183 | |
| 184 | try: |
| 185 | utils._handle_func_status(status, 'graph object stopped running') |
| 186 | except bt2.Stop: |
| 187 | # done |
| 188 | return |
| 189 | except Exception: |
| 190 | raise |
| 191 | |
| 192 | def add_interrupter(self, interrupter): |
| 193 | utils._check_type(interrupter, bt2_interrupter.Interrupter) |
| 194 | native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) |
| 195 | |
| 196 | def interrupt(self): |
| 197 | native_bt.graph_interrupt(self._ptr) |