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