Commit | Line | Data |
---|---|---|
f6a5e476 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 | |
c946c9de 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 | |
f6a5e476 | 27 | import functools |
c946c9de PP |
28 | from bt2 import port as bt2_port |
29 | from bt2 import logging as bt2_logging | |
f6a5e476 PP |
30 | import bt2 |
31 | ||
32 | ||
61d96b89 FD |
33 | def _graph_port_added_listener_from_native( |
34 | user_listener, component_ptr, component_type, port_ptr, port_type | |
35 | ): | |
5265f5e3 | 36 | component = bt2_component._create_component_from_const_ptr_and_get_ref( |
61d96b89 FD |
37 | component_ptr, component_type |
38 | ) | |
49e6b55c | 39 | port = bt2_port._create_from_const_ptr_and_get_ref(port_ptr, port_type) |
871a292a | 40 | user_listener(component, port) |
f6a5e476 PP |
41 | |
42 | ||
c3044a97 | 43 | class Graph(object._SharedObject): |
a49e2cc3 PP |
44 | _get_ref = staticmethod(native_bt.graph_get_ref) |
45 | _put_ref = staticmethod(native_bt.graph_put_ref) | |
bbb3650f | 46 | |
253a9ecc 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) | |
f6a5e476 PP |
54 | |
55 | if ptr is None: | |
614743a5 | 56 | raise bt2._MemoryError('cannot create graph object') |
f6a5e476 PP |
57 | |
58 | super().__init__(ptr) | |
59 | ||
fecbdb3e PP |
60 | # list of listener partials to keep a reference as long as |
61 | # this graph exists | |
62 | self._listener_partials = [] | |
63 | ||
61d96b89 FD |
64 | def add_component( |
65 | self, | |
66 | component_class, | |
67 | name, | |
68 | params=None, | |
b20382e2 | 69 | obj=None, |
c946c9de | 70 | logging_level=bt2_logging.LoggingLevel.NONE, |
61d96b89 | 71 | ): |
5265f5e3 | 72 | if isinstance(component_class, bt2_component._SourceComponentClassConst): |
871a292a | 73 | cc_ptr = component_class._ptr |
b20382e2 | 74 | add_fn = native_bt.bt2_graph_add_source_component |
bc5c9924 | 75 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
5265f5e3 | 76 | elif isinstance(component_class, bt2_component._FilterComponentClassConst): |
871a292a | 77 | cc_ptr = component_class._ptr |
b20382e2 | 78 | add_fn = native_bt.bt2_graph_add_filter_component |
bc5c9924 | 79 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
5265f5e3 | 80 | elif isinstance(component_class, bt2_component._SinkComponentClassConst): |
871a292a | 81 | cc_ptr = component_class._ptr |
b20382e2 | 82 | add_fn = native_bt.bt2_graph_add_sink_component |
871a292a | 83 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
c946c9de | 84 | elif issubclass(component_class, bt2_component._UserSourceComponent): |
deec48a6 | 85 | cc_ptr = component_class._bt_cc_ptr |
b20382e2 | 86 | add_fn = native_bt.bt2_graph_add_source_component |
871a292a | 87 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE |
c946c9de | 88 | elif issubclass(component_class, bt2_component._UserSinkComponent): |
deec48a6 | 89 | cc_ptr = component_class._bt_cc_ptr |
b20382e2 | 90 | add_fn = native_bt.bt2_graph_add_sink_component |
bc5c9924 | 91 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK |
c946c9de | 92 | elif issubclass(component_class, bt2_component._UserFilterComponent): |
deec48a6 | 93 | cc_ptr = component_class._bt_cc_ptr |
b20382e2 | 94 | add_fn = native_bt.bt2_graph_add_filter_component |
871a292a | 95 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER |
f6a5e476 | 96 | else: |
61d96b89 FD |
97 | raise TypeError( |
98 | "'{}' is not a component class".format( | |
99 | component_class.__class__.__name__ | |
100 | ) | |
101 | ) | |
f6a5e476 PP |
102 | |
103 | utils._check_str(name) | |
cc81b5ab | 104 | utils._check_log_level(logging_level) |
b20382e2 PP |
105 | base_cc_ptr = component_class._bt_component_class_ptr() |
106 | ||
107 | if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr): | |
108 | raise ValueError('cannot pass a Python object to a non-Python component') | |
f6a5e476 | 109 | |
3e03e5e3 FD |
110 | if params is not None and not isinstance(params, (dict, bt2.MapValue)): |
111 | raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.") | |
112 | ||
b20382e2 | 113 | params = bt2.create_value(params) |
3e03e5e3 | 114 | |
bbb3650f | 115 | params_ptr = params._ptr if params is not None else None |
f6a5e476 | 116 | |
b20382e2 PP |
117 | status, comp_ptr = add_fn( |
118 | self._ptr, cc_ptr, name, params_ptr, obj, logging_level | |
119 | ) | |
fb25b9e3 | 120 | utils._handle_func_status(status, 'cannot add component to graph') |
bc5c9924 | 121 | assert comp_ptr |
246d5223 PP |
122 | return bt2_component._create_component_from_const_ptr_and_get_ref( |
123 | comp_ptr, cc_type | |
124 | ) | |
f6a5e476 PP |
125 | |
126 | def connect_ports(self, upstream_port, downstream_port): | |
49e6b55c FD |
127 | utils._check_type(upstream_port, bt2_port._OutputPortConst) |
128 | utils._check_type(downstream_port, bt2_port._InputPortConst) | |
61d96b89 FD |
129 | status, conn_ptr = native_bt.graph_connect_ports( |
130 | self._ptr, upstream_port._ptr, downstream_port._ptr | |
131 | ) | |
132 | utils._handle_func_status(status, 'cannot connect component ports within graph') | |
133 | assert conn_ptr | |
246d5223 | 134 | return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr) |
f6a5e476 | 135 | |
871a292a SM |
136 | def add_port_added_listener(self, listener): |
137 | if not callable(listener): | |
f6a5e476 PP |
138 | raise TypeError("'listener' parameter is not callable") |
139 | ||
fb25b9e3 | 140 | fn = native_bt.bt2_graph_add_port_added_listener |
61d96b89 FD |
141 | listener_from_native = functools.partial( |
142 | _graph_port_added_listener_from_native, listener | |
143 | ) | |
f6a5e476 | 144 | |
871a292a SM |
145 | listener_ids = fn(self._ptr, listener_from_native) |
146 | if listener_ids is None: | |
614743a5 | 147 | raise bt2._Error('cannot add listener to graph object') |
d09c69f7 | 148 | |
fecbdb3e PP |
149 | # keep the partial's reference |
150 | self._listener_partials.append(listener_from_native) | |
151 | ||
22b4de6a PP |
152 | def run_once(self): |
153 | status = native_bt.graph_run_once(self._ptr) | |
154 | utils._handle_func_status(status, 'graph object could not run once') | |
155 | ||
f6a5e476 PP |
156 | def run(self): |
157 | status = native_bt.graph_run(self._ptr) | |
0e312dbd | 158 | utils._handle_func_status(status, 'graph object stopped running') |
f6a5e476 | 159 | |
d73bb381 | 160 | def add_interrupter(self, interrupter): |
c946c9de | 161 | utils._check_type(interrupter, bt2_interrupter.Interrupter) |
d73bb381 | 162 | native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) |
f6a5e476 | 163 | |
f09b3ae9 SM |
164 | @property |
165 | def default_interrupter(self): | |
166 | ptr = native_bt.graph_borrow_default_interrupter(self._ptr) | |
167 | return bt2_interrupter.Interrupter._create_from_ptr_and_get_ref(ptr) |