bt2: remove BT CC entry from global HT in _UserComponentType.__del__()
[babeltrace.git] / src / bindings / python / bt2 / bt2 / graph.py
CommitLineData
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
23from bt2 import native_bt, object, utils
3fb99a22
PP
24from bt2 import interrupter as bt2_interrupter
25from bt2 import connection as bt2_connection
26from bt2 import component as bt2_component
27from bt2 import message_iterator as bt2_message_iterator
811644b8 28import functools
3fb99a22
PP
29from bt2 import port as bt2_port
30from bt2 import logging as bt2_logging
811644b8
PP
31import bt2
32
33
cfbd7cf3
FD
34def _graph_port_added_listener_from_native(
35 user_listener, component_ptr, component_type, port_ptr, port_type
36):
3fb99a22 37 component = bt2_component._create_component_from_ptr_and_get_ref(
cfbd7cf3
FD
38 component_ptr, component_type
39 )
3fb99a22 40 port = bt2_port._create_from_ptr_and_get_ref(port_ptr, port_type)
5f25509b 41 user_listener(component, port)
811644b8
PP
42
43
cfbd7cf3
FD
44def _graph_ports_connected_listener_from_native(
45 user_listener,
46 upstream_component_ptr,
47 upstream_component_type,
48 upstream_port_ptr,
49 downstream_component_ptr,
50 downstream_component_type,
51 downstream_port_ptr,
52):
3fb99a22 53 upstream_component = bt2_component._create_component_from_ptr_and_get_ref(
cfbd7cf3
FD
54 upstream_component_ptr, upstream_component_type
55 )
3fb99a22 56 upstream_port = bt2_port._create_from_ptr_and_get_ref(
cfbd7cf3
FD
57 upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT
58 )
3fb99a22 59 downstream_component = bt2_component._create_component_from_ptr_and_get_ref(
cfbd7cf3
FD
60 downstream_component_ptr, downstream_component_type
61 )
3fb99a22 62 downstream_port = bt2_port._create_from_ptr_and_get_ref(
cfbd7cf3
FD
63 downstream_port_ptr, native_bt.PORT_TYPE_INPUT
64 )
65 user_listener(
66 upstream_component, upstream_port, downstream_component, downstream_port
67 )
811644b8
PP
68
69
78288f58 70class Graph(object._SharedObject):
2f16a6a2
PP
71 _get_ref = staticmethod(native_bt.graph_get_ref)
72 _put_ref = staticmethod(native_bt.graph_put_ref)
601c0026 73
811644b8
PP
74 def __init__(self):
75 ptr = native_bt.graph_create()
76
77 if ptr is None:
694c792b 78 raise bt2._MemoryError('cannot create graph object')
811644b8
PP
79
80 super().__init__(ptr)
81
cfbd7cf3
FD
82 def add_component(
83 self,
84 component_class,
85 name,
86 params=None,
3fb99a22 87 logging_level=bt2_logging.LoggingLevel.NONE,
cfbd7cf3 88 ):
3fb99a22 89 if isinstance(component_class, bt2_component._SourceComponentClass):
5f25509b 90 cc_ptr = component_class._ptr
894a8df5
SM
91 add_fn = native_bt.graph_add_source_component
92 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
3fb99a22 93 elif isinstance(component_class, bt2_component._FilterComponentClass):
5f25509b 94 cc_ptr = component_class._ptr
894a8df5
SM
95 add_fn = native_bt.graph_add_filter_component
96 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
3fb99a22 97 elif isinstance(component_class, bt2_component._SinkComponentClass):
5f25509b
SM
98 cc_ptr = component_class._ptr
99 add_fn = native_bt.graph_add_sink_component
100 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
3fb99a22 101 elif issubclass(component_class, bt2_component._UserSourceComponent):
85906b6b 102 cc_ptr = component_class._bt_cc_ptr
5f25509b
SM
103 add_fn = native_bt.graph_add_source_component
104 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
3fb99a22 105 elif issubclass(component_class, bt2_component._UserSinkComponent):
85906b6b 106 cc_ptr = component_class._bt_cc_ptr
894a8df5
SM
107 add_fn = native_bt.graph_add_sink_component
108 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
3fb99a22 109 elif issubclass(component_class, bt2_component._UserFilterComponent):
85906b6b 110 cc_ptr = component_class._bt_cc_ptr
5f25509b
SM
111 add_fn = native_bt.graph_add_filter_component
112 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 113 else:
cfbd7cf3
FD
114 raise TypeError(
115 "'{}' is not a component class".format(
116 component_class.__class__.__name__
117 )
118 )
811644b8
PP
119
120 utils._check_str(name)
e874da19 121 utils._check_log_level(logging_level)
811644b8
PP
122 params = bt2.create_value(params)
123
601c0026 124 params_ptr = params._ptr if params is not None else None
811644b8 125
cfbd7cf3 126 status, comp_ptr = add_fn(self._ptr, cc_ptr, name, params_ptr, logging_level)
d24d5663 127 utils._handle_func_status(status, 'cannot add component to graph')
894a8df5 128 assert comp_ptr
3fb99a22 129 return bt2_component._create_component_from_ptr(comp_ptr, cc_type)
811644b8
PP
130
131 def connect_ports(self, upstream_port, downstream_port):
3fb99a22
PP
132 utils._check_type(upstream_port, bt2_port._OutputPort)
133 utils._check_type(downstream_port, bt2_port._InputPort)
cfbd7cf3
FD
134 status, conn_ptr = native_bt.graph_connect_ports(
135 self._ptr, upstream_port._ptr, downstream_port._ptr
136 )
137 utils._handle_func_status(status, 'cannot connect component ports within graph')
138 assert conn_ptr
3fb99a22 139 return bt2_connection._Connection._create_from_ptr(conn_ptr)
811644b8 140
5f25509b
SM
141 def add_port_added_listener(self, listener):
142 if not callable(listener):
811644b8
PP
143 raise TypeError("'listener' parameter is not callable")
144
d24d5663 145 fn = native_bt.bt2_graph_add_port_added_listener
cfbd7cf3
FD
146 listener_from_native = functools.partial(
147 _graph_port_added_listener_from_native, listener
148 )
811644b8 149
5f25509b
SM
150 listener_ids = fn(self._ptr, listener_from_native)
151 if listener_ids is None:
694c792b 152 raise bt2._Error('cannot add listener to graph object')
416379bc 153
3fb99a22 154 return utils._ListenerHandle(listener_ids, self)
5f25509b
SM
155
156 def add_ports_connected_listener(self, listener):
157 if not callable(listener):
158 raise TypeError("'listener' parameter is not callable")
159
d24d5663 160 fn = native_bt.bt2_graph_add_ports_connected_listener
cfbd7cf3
FD
161 listener_from_native = functools.partial(
162 _graph_ports_connected_listener_from_native, listener
163 )
5f25509b
SM
164
165 listener_ids = fn(self._ptr, listener_from_native)
166 if listener_ids is None:
694c792b 167 raise bt2._Error('cannot add listener to graph object')
416379bc 168
3fb99a22 169 return utils._ListenerHandle(listener_ids, self)
811644b8
PP
170
171 def run(self):
172 status = native_bt.graph_run(self._ptr)
173
d24d5663 174 try:
9b4f9b42 175 utils._handle_func_status(status, 'graph object stopped running')
d24d5663
PP
176 except bt2.Stop:
177 # done
811644b8 178 return
d24d5663
PP
179 except Exception:
180 raise
811644b8 181
9b4f9b42 182 def add_interrupter(self, interrupter):
3fb99a22 183 utils._check_type(interrupter, bt2_interrupter.Interrupter)
9b4f9b42 184 native_bt.graph_add_interrupter(self._ptr, interrupter._ptr)
811644b8 185
9b4f9b42
PP
186 def interrupt(self):
187 native_bt.graph_interrupt(self._ptr)
811644b8 188
2ae9f48c 189 def create_output_port_message_iterator(self, output_port):
3fb99a22 190 utils._check_type(output_port, bt2_port._OutputPort)
cfbd7cf3
FD
191 msg_iter_ptr = native_bt.port_output_message_iterator_create(
192 self._ptr, output_port._ptr
193 )
2ae9f48c
SM
194
195 if msg_iter_ptr is None:
694c792b 196 raise bt2._MemoryError('cannot create output port message iterator')
2ae9f48c 197
3fb99a22 198 return bt2_message_iterator._OutputPortMessageIterator(msg_iter_ptr)
This page took 0.047246 seconds and 4 git commands to generate.