Commit | Line | Data |
---|---|---|
811644b8 PP |
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 | |
3fb99a22 PP |
24 | from bt2 import interrupter as bt2_interrupter |
25 | from bt2 import connection as bt2_connection | |
26 | from bt2 import component as bt2_component | |
811644b8 | 27 | import functools |
3fb99a22 PP |
28 | from bt2 import port as bt2_port |
29 | from bt2 import logging as bt2_logging | |
811644b8 PP |
30 | import bt2 |
31 | ||
32 | ||
cfbd7cf3 FD |
33 | def _graph_port_added_listener_from_native( |
34 | user_listener, component_ptr, component_type, port_ptr, port_type | |
35 | ): | |
615238be | 36 | component = bt2_component._create_component_from_const_ptr_and_get_ref( |
cfbd7cf3 FD |
37 | component_ptr, component_type |
38 | ) | |
5813b3a3 | 39 | port = bt2_port._create_from_const_ptr_and_get_ref(port_ptr, port_type) |
5f25509b | 40 | user_listener(component, port) |
811644b8 PP |
41 | |
42 | ||
78288f58 | 43 | class Graph(object._SharedObject): |
2f16a6a2 PP |
44 | _get_ref = staticmethod(native_bt.graph_get_ref) |
45 | _put_ref = staticmethod(native_bt.graph_put_ref) | |
601c0026 | 46 | |
056deb59 PP |
47 | def __init__(self, mip_version=0): |
48 | utils._check_uint64(mip_version) | |
49 | ||
50 | if mip_version > bt2.get_maximal_mip_version(): | |
51 | raise ValueError('unknown MIP version {}'.format(mip_version)) | |
52 | ||
53 | ptr = native_bt.graph_create(mip_version) | |
811644b8 PP |
54 | |
55 | if ptr is None: | |
694c792b | 56 | raise bt2._MemoryError('cannot create graph object') |
811644b8 PP |
57 | |
58 | super().__init__(ptr) | |
59 | ||
cfbd7cf3 FD |
60 | def add_component( |
61 | self, | |
62 | component_class, | |
63 | name, | |
64 | params=None, | |
66964f3f | 65 | obj=None, |
3fb99a22 | 66 | logging_level=bt2_logging.LoggingLevel.NONE, |
cfbd7cf3 | 67 | ): |
615238be | 68 | if isinstance(component_class, bt2_component._SourceComponentClassConst): |
5f25509b | 69 | cc_ptr = component_class._ptr |
66964f3f | 70 | add_fn = native_bt.bt2_graph_add_source_component |
894a8df5 | 71 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
615238be | 72 | elif isinstance(component_class, bt2_component._FilterComponentClassConst): |
5f25509b | 73 | cc_ptr = component_class._ptr |
66964f3f | 74 | add_fn = native_bt.bt2_graph_add_filter_component |
894a8df5 | 75 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
615238be | 76 | elif isinstance(component_class, bt2_component._SinkComponentClassConst): |
5f25509b | 77 | cc_ptr = component_class._ptr |
66964f3f | 78 | add_fn = native_bt.bt2_graph_add_sink_component |
5f25509b | 79 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
3fb99a22 | 80 | elif issubclass(component_class, bt2_component._UserSourceComponent): |
85906b6b | 81 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 82 | add_fn = native_bt.bt2_graph_add_source_component |
5f25509b | 83 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
3fb99a22 | 84 | elif issubclass(component_class, bt2_component._UserSinkComponent): |
85906b6b | 85 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 86 | add_fn = native_bt.bt2_graph_add_sink_component |
894a8df5 | 87 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
3fb99a22 | 88 | elif issubclass(component_class, bt2_component._UserFilterComponent): |
85906b6b | 89 | cc_ptr = component_class._bt_cc_ptr |
66964f3f | 90 | add_fn = native_bt.bt2_graph_add_filter_component |
5f25509b | 91 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
811644b8 | 92 | else: |
cfbd7cf3 FD |
93 | raise TypeError( |
94 | "'{}' is not a component class".format( | |
95 | component_class.__class__.__name__ | |
96 | ) | |
97 | ) | |
811644b8 PP |
98 | |
99 | utils._check_str(name) | |
e874da19 | 100 | utils._check_log_level(logging_level) |
66964f3f PP |
101 | base_cc_ptr = component_class._bt_component_class_ptr() |
102 | ||
103 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): | |
104 | raise ValueError('cannot pass a Python object to a non-Python component') | |
811644b8 | 105 | |
401b7022 FD |
106 | if params is not None and not isinstance(params, (dict, bt2.MapValue)): |
107 | raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.") | |
108 | ||
66964f3f | 109 | params = bt2.create_value(params) |
401b7022 | 110 | |
601c0026 | 111 | params_ptr = params._ptr if params is not None else None |
811644b8 | 112 | |
66964f3f PP |
113 | status, comp_ptr = add_fn( |
114 | self._ptr, cc_ptr, name, params_ptr, obj, logging_level | |
115 | ) | |
d24d5663 | 116 | utils._handle_func_status(status, 'cannot add component to graph') |
894a8df5 | 117 | assert comp_ptr |
615238be | 118 | return bt2_component._create_component_from_const_ptr(comp_ptr, cc_type) |
811644b8 PP |
119 | |
120 | def connect_ports(self, upstream_port, downstream_port): | |
5813b3a3 FD |
121 | utils._check_type(upstream_port, bt2_port._OutputPortConst) |
122 | utils._check_type(downstream_port, bt2_port._InputPortConst) | |
cfbd7cf3 FD |
123 | status, conn_ptr = native_bt.graph_connect_ports( |
124 | self._ptr, upstream_port._ptr, downstream_port._ptr | |
125 | ) | |
126 | utils._handle_func_status(status, 'cannot connect component ports within graph') | |
127 | assert conn_ptr | |
c7e5224b | 128 | return bt2_connection._ConnectionConst._create_from_ptr(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: | |
694c792b | 141 | raise bt2._Error('cannot add listener to graph object') |
416379bc | 142 | |
8cc0e6ea PP |
143 | def run_once(self): |
144 | status = native_bt.graph_run_once(self._ptr) | |
145 | utils._handle_func_status(status, 'graph object could not run once') | |
146 | ||
811644b8 PP |
147 | def run(self): |
148 | status = native_bt.graph_run(self._ptr) | |
9669d693 | 149 | utils._handle_func_status(status, 'graph object stopped running') |
811644b8 | 150 | |
9b4f9b42 | 151 | def add_interrupter(self, interrupter): |
3fb99a22 | 152 | utils._check_type(interrupter, bt2_interrupter.Interrupter) |
9b4f9b42 | 153 | native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) |
811644b8 | 154 | |
9b4f9b42 PP |
155 | def interrupt(self): |
156 | native_bt.graph_interrupt(self._ptr) |