lib: rename bt_graph_consume() -> bt_graph_run_once()
[babeltrace.git] / src / bindings / python / bt2 / bt2 / graph.py
CommitLineData
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
23from bt2 import native_bt, object, utils
c946c9de
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
f6a5e476 28import functools
c946c9de
PP
29from bt2 import port as bt2_port
30from bt2 import logging as bt2_logging
f6a5e476
PP
31import bt2
32
33
61d96b89
FD
34def _graph_port_added_listener_from_native(
35 user_listener, component_ptr, component_type, port_ptr, port_type
36):
c946c9de 37 component = bt2_component._create_component_from_ptr_and_get_ref(
61d96b89
FD
38 component_ptr, component_type
39 )
c946c9de 40 port = bt2_port._create_from_ptr_and_get_ref(port_ptr, port_type)
871a292a 41 user_listener(component, port)
f6a5e476
PP
42
43
61d96b89
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):
c946c9de 53 upstream_component = bt2_component._create_component_from_ptr_and_get_ref(
61d96b89
FD
54 upstream_component_ptr, upstream_component_type
55 )
c946c9de 56 upstream_port = bt2_port._create_from_ptr_and_get_ref(
61d96b89
FD
57 upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT
58 )
c946c9de 59 downstream_component = bt2_component._create_component_from_ptr_and_get_ref(
61d96b89
FD
60 downstream_component_ptr, downstream_component_type
61 )
c946c9de 62 downstream_port = bt2_port._create_from_ptr_and_get_ref(
61d96b89
FD
63 downstream_port_ptr, native_bt.PORT_TYPE_INPUT
64 )
65 user_listener(
66 upstream_component, upstream_port, downstream_component, downstream_port
67 )
f6a5e476
PP
68
69
c3044a97 70class Graph(object._SharedObject):
a49e2cc3
PP
71 _get_ref = staticmethod(native_bt.graph_get_ref)
72 _put_ref = staticmethod(native_bt.graph_put_ref)
bbb3650f 73
f6a5e476
PP
74 def __init__(self):
75 ptr = native_bt.graph_create()
76
77 if ptr is None:
614743a5 78 raise bt2._MemoryError('cannot create graph object')
f6a5e476
PP
79
80 super().__init__(ptr)
81
61d96b89
FD
82 def add_component(
83 self,
84 component_class,
85 name,
86 params=None,
b20382e2 87 obj=None,
c946c9de 88 logging_level=bt2_logging.LoggingLevel.NONE,
61d96b89 89 ):
c946c9de 90 if isinstance(component_class, bt2_component._SourceComponentClass):
871a292a 91 cc_ptr = component_class._ptr
b20382e2 92 add_fn = native_bt.bt2_graph_add_source_component
bc5c9924 93 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
c946c9de 94 elif isinstance(component_class, bt2_component._FilterComponentClass):
871a292a 95 cc_ptr = component_class._ptr
b20382e2 96 add_fn = native_bt.bt2_graph_add_filter_component
bc5c9924 97 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
c946c9de 98 elif isinstance(component_class, bt2_component._SinkComponentClass):
871a292a 99 cc_ptr = component_class._ptr
b20382e2 100 add_fn = native_bt.bt2_graph_add_sink_component
871a292a 101 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
c946c9de 102 elif issubclass(component_class, bt2_component._UserSourceComponent):
deec48a6 103 cc_ptr = component_class._bt_cc_ptr
b20382e2 104 add_fn = native_bt.bt2_graph_add_source_component
871a292a 105 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
c946c9de 106 elif issubclass(component_class, bt2_component._UserSinkComponent):
deec48a6 107 cc_ptr = component_class._bt_cc_ptr
b20382e2 108 add_fn = native_bt.bt2_graph_add_sink_component
bc5c9924 109 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
c946c9de 110 elif issubclass(component_class, bt2_component._UserFilterComponent):
deec48a6 111 cc_ptr = component_class._bt_cc_ptr
b20382e2 112 add_fn = native_bt.bt2_graph_add_filter_component
871a292a 113 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
f6a5e476 114 else:
61d96b89
FD
115 raise TypeError(
116 "'{}' is not a component class".format(
117 component_class.__class__.__name__
118 )
119 )
f6a5e476
PP
120
121 utils._check_str(name)
cc81b5ab 122 utils._check_log_level(logging_level)
b20382e2
PP
123 base_cc_ptr = component_class._bt_component_class_ptr()
124
125 if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr):
126 raise ValueError('cannot pass a Python object to a non-Python component')
f6a5e476 127
b20382e2 128 params = bt2.create_value(params)
bbb3650f 129 params_ptr = params._ptr if params is not None else None
f6a5e476 130
b20382e2
PP
131 status, comp_ptr = add_fn(
132 self._ptr, cc_ptr, name, params_ptr, obj, logging_level
133 )
fb25b9e3 134 utils._handle_func_status(status, 'cannot add component to graph')
bc5c9924 135 assert comp_ptr
c946c9de 136 return bt2_component._create_component_from_ptr(comp_ptr, cc_type)
f6a5e476
PP
137
138 def connect_ports(self, upstream_port, downstream_port):
c946c9de
PP
139 utils._check_type(upstream_port, bt2_port._OutputPort)
140 utils._check_type(downstream_port, bt2_port._InputPort)
61d96b89
FD
141 status, conn_ptr = native_bt.graph_connect_ports(
142 self._ptr, upstream_port._ptr, downstream_port._ptr
143 )
144 utils._handle_func_status(status, 'cannot connect component ports within graph')
145 assert conn_ptr
c946c9de 146 return bt2_connection._Connection._create_from_ptr(conn_ptr)
f6a5e476 147
871a292a
SM
148 def add_port_added_listener(self, listener):
149 if not callable(listener):
f6a5e476
PP
150 raise TypeError("'listener' parameter is not callable")
151
fb25b9e3 152 fn = native_bt.bt2_graph_add_port_added_listener
61d96b89
FD
153 listener_from_native = functools.partial(
154 _graph_port_added_listener_from_native, listener
155 )
f6a5e476 156
871a292a
SM
157 listener_ids = fn(self._ptr, listener_from_native)
158 if listener_ids is None:
614743a5 159 raise bt2._Error('cannot add listener to graph object')
d09c69f7 160
c946c9de 161 return utils._ListenerHandle(listener_ids, self)
871a292a
SM
162
163 def add_ports_connected_listener(self, listener):
164 if not callable(listener):
165 raise TypeError("'listener' parameter is not callable")
166
fb25b9e3 167 fn = native_bt.bt2_graph_add_ports_connected_listener
61d96b89
FD
168 listener_from_native = functools.partial(
169 _graph_ports_connected_listener_from_native, listener
170 )
871a292a
SM
171
172 listener_ids = fn(self._ptr, listener_from_native)
173 if listener_ids is None:
614743a5 174 raise bt2._Error('cannot add listener to graph object')
d09c69f7 175
c946c9de 176 return utils._ListenerHandle(listener_ids, self)
f6a5e476
PP
177
178 def run(self):
179 status = native_bt.graph_run(self._ptr)
180
fb25b9e3 181 try:
d73bb381 182 utils._handle_func_status(status, 'graph object stopped running')
fb25b9e3
PP
183 except bt2.Stop:
184 # done
f6a5e476 185 return
fb25b9e3
PP
186 except Exception:
187 raise
f6a5e476 188
d73bb381 189 def add_interrupter(self, interrupter):
c946c9de 190 utils._check_type(interrupter, bt2_interrupter.Interrupter)
d73bb381 191 native_bt.graph_add_interrupter(self._ptr, interrupter._ptr)
f6a5e476 192
d73bb381
PP
193 def interrupt(self):
194 native_bt.graph_interrupt(self._ptr)
f6a5e476 195
27d97a3f 196 def create_output_port_message_iterator(self, output_port):
c946c9de 197 utils._check_type(output_port, bt2_port._OutputPort)
61d96b89
FD
198 msg_iter_ptr = native_bt.port_output_message_iterator_create(
199 self._ptr, output_port._ptr
200 )
27d97a3f
SM
201
202 if msg_iter_ptr is None:
614743a5 203 raise bt2._MemoryError('cannot create output port message iterator')
27d97a3f 204
c946c9de 205 return bt2_message_iterator._OutputPortMessageIterator(msg_iter_ptr)
This page took 0.053263 seconds and 4 git commands to generate.