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