Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
811644b8 PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
811644b8 | 4 | |
811644b8 | 5 | import functools |
5995b304 | 6 | |
c345b078 | 7 | from bt2 import mip as bt2_mip |
5995b304 | 8 | from bt2 import port as bt2_port |
c345b078 | 9 | from bt2 import error as bt2_error |
5995b304 | 10 | from bt2 import utils as bt2_utils |
c345b078 | 11 | from bt2 import value as bt2_value |
5995b304 SM |
12 | from bt2 import object as bt2_object |
13 | from bt2 import logging as bt2_logging | |
14 | from bt2 import component as bt2_component | |
15 | from bt2 import native_bt | |
16 | from bt2 import connection as bt2_connection | |
17 | from bt2 import interrupter as bt2_interrupter | |
811644b8 PP |
18 | |
19 | ||
cfbd7cf3 FD |
20 | def _graph_port_added_listener_from_native( |
21 | user_listener, component_ptr, component_type, port_ptr, port_type | |
22 | ): | |
615238be | 23 | component = bt2_component._create_component_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
24 | component_ptr, component_type |
25 | ) | |
5813b3a3 | 26 | port = bt2_port._create_from_const_ptr_and_get_ref(port_ptr, port_type) |
5f25509b | 27 | user_listener(component, port) |
811644b8 PP |
28 | |
29 | ||
e5914347 | 30 | class Graph(bt2_object._SharedObject): |
9dee90bd SM |
31 | @staticmethod |
32 | def _get_ref(ptr): | |
33 | native_bt.graph_get_ref(ptr) | |
34 | ||
35 | @staticmethod | |
36 | def _put_ref(ptr): | |
37 | native_bt.graph_put_ref(ptr) | |
601c0026 | 38 | |
056deb59 | 39 | def __init__(self, mip_version=0): |
e5914347 | 40 | bt2_utils._check_uint64(mip_version) |
056deb59 | 41 | |
c345b078 | 42 | if mip_version > bt2_mip.get_maximal_mip_version(): |
f5567ea8 | 43 | raise ValueError("unknown MIP version {}".format(mip_version)) |
056deb59 PP |
44 | |
45 | ptr = native_bt.graph_create(mip_version) | |
811644b8 PP |
46 | |
47 | if ptr is None: | |
c345b078 | 48 | raise bt2_error._MemoryError("cannot create graph object") |
811644b8 PP |
49 | |
50 | super().__init__(ptr) | |
51 | ||
f3d6b4c2 PP |
52 | # list of listener partials to keep a reference as long as |
53 | # this graph exists | |
54 | self._listener_partials = [] | |
55 | ||
cfbd7cf3 FD |
56 | def add_component( |
57 | self, | |
58 | component_class, | |
59 | name, | |
60 | params=None, | |
66964f3f | 61 | obj=None, |
3fb99a22 | 62 | logging_level=bt2_logging.LoggingLevel.NONE, |
cfbd7cf3 | 63 | ): |
615238be | 64 | if isinstance(component_class, bt2_component._SourceComponentClassConst): |
5f25509b | 65 | cc_ptr = component_class._ptr |
66964f3f | 66 | add_fn = native_bt.bt2_graph_add_source_component |
894a8df5 | 67 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
615238be | 68 | elif isinstance(component_class, bt2_component._FilterComponentClassConst): |
5f25509b | 69 | cc_ptr = component_class._ptr |
66964f3f | 70 | add_fn = native_bt.bt2_graph_add_filter_component |
894a8df5 | 71 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
615238be | 72 | elif isinstance(component_class, bt2_component._SinkComponentClassConst): |
5f25509b | 73 | cc_ptr = component_class._ptr |
66964f3f | 74 | add_fn = native_bt.bt2_graph_add_sink_component |
5f25509b | 75 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
3fb99a22 | 76 | elif issubclass(component_class, bt2_component._UserSourceComponent): |
85906b6b | 77 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 78 | add_fn = native_bt.bt2_graph_add_source_component |
5f25509b | 79 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
3fb99a22 | 80 | elif issubclass(component_class, bt2_component._UserSinkComponent): |
85906b6b | 81 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 82 | add_fn = native_bt.bt2_graph_add_sink_component |
894a8df5 | 83 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
3fb99a22 | 84 | elif issubclass(component_class, bt2_component._UserFilterComponent): |
85906b6b | 85 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 86 | add_fn = native_bt.bt2_graph_add_filter_component |
5f25509b | 87 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
811644b8 | 88 | else: |
cfbd7cf3 FD |
89 | raise TypeError( |
90 | "'{}' is not a component class".format( | |
91 | component_class.__class__.__name__ | |
92 | ) | |
93 | ) | |
811644b8 | 94 | |
e5914347 SM |
95 | bt2_utils._check_str(name) |
96 | bt2_utils._check_log_level(logging_level) | |
66964f3f PP |
97 | base_cc_ptr = component_class._bt_component_class_ptr() |
98 | ||
99 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): | |
f5567ea8 | 100 | raise ValueError("cannot pass a Python object to a non-Python component") |
811644b8 | 101 | |
c345b078 | 102 | if params is not None and not isinstance(params, (dict, bt2_value.MapValue)): |
401b7022 FD |
103 | raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.") |
104 | ||
c345b078 | 105 | params = bt2_value.create_value(params) |
401b7022 | 106 | |
601c0026 | 107 | params_ptr = params._ptr if params is not None else None |
811644b8 | 108 | |
66964f3f PP |
109 | status, comp_ptr = add_fn( |
110 | self._ptr, cc_ptr, name, params_ptr, obj, logging_level | |
111 | ) | |
e5914347 | 112 | bt2_utils._handle_func_status(status, "cannot add component to graph") |
894a8df5 | 113 | assert comp_ptr |
a91462c9 PP |
114 | return bt2_component._create_component_from_const_ptr_and_get_ref( |
115 | comp_ptr, cc_type | |
116 | ) | |
811644b8 PP |
117 | |
118 | def connect_ports(self, upstream_port, downstream_port): | |
e5914347 SM |
119 | bt2_utils._check_type(upstream_port, bt2_port._OutputPortConst) |
120 | bt2_utils._check_type(downstream_port, bt2_port._InputPortConst) | |
cfbd7cf3 FD |
121 | status, conn_ptr = native_bt.graph_connect_ports( |
122 | self._ptr, upstream_port._ptr, downstream_port._ptr | |
123 | ) | |
e5914347 SM |
124 | bt2_utils._handle_func_status( |
125 | status, "cannot connect component ports within graph" | |
126 | ) | |
cfbd7cf3 | 127 | assert conn_ptr |
a91462c9 | 128 | return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
811644b8 | 129 | |
5f25509b SM |
130 | def add_port_added_listener(self, listener): |
131 | if not callable(listener): | |
811644b8 PP |
132 | raise TypeError("'listener' parameter is not callable") |
133 | ||
d24d5663 | 134 | fn = native_bt.bt2_graph_add_port_added_listener |
cfbd7cf3 FD |
135 | listener_from_native = functools.partial( |
136 | _graph_port_added_listener_from_native, listener | |
137 | ) | |
811644b8 | 138 | |
5f25509b SM |
139 | listener_ids = fn(self._ptr, listener_from_native) |
140 | if listener_ids is None: | |
c345b078 | 141 | raise bt2_error._Error("cannot add listener to graph object") |
416379bc | 142 | |
f3d6b4c2 PP |
143 | # keep the partial's reference |
144 | self._listener_partials.append(listener_from_native) | |
145 | ||
8cc0e6ea PP |
146 | def run_once(self): |
147 | status = native_bt.graph_run_once(self._ptr) | |
e5914347 | 148 | bt2_utils._handle_func_status(status, "graph object could not run once") |
8cc0e6ea | 149 | |
811644b8 PP |
150 | def run(self): |
151 | status = native_bt.graph_run(self._ptr) | |
e5914347 | 152 | bt2_utils._handle_func_status(status, "graph object stopped running") |
811644b8 | 153 | |
9b4f9b42 | 154 | def add_interrupter(self, interrupter): |
e5914347 | 155 | bt2_utils._check_type(interrupter, bt2_interrupter.Interrupter) |
9b4f9b42 | 156 | native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) |
811644b8 | 157 | |
c513d240 SM |
158 | @property |
159 | def default_interrupter(self): | |
160 | ptr = native_bt.graph_borrow_default_interrupter(self._ptr) | |
161 | return bt2_interrupter.Interrupter._create_from_ptr_and_get_ref(ptr) |