bt2: Force usage of MapValue object on component init
[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
f6a5e476 27import functools
c946c9de
PP
28from bt2 import port as bt2_port
29from bt2 import logging as bt2_logging
f6a5e476
PP
30import bt2
31
32
61d96b89
FD
33def _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
61d96b89
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):
5265f5e3 52 upstream_component = bt2_component._create_component_from_const_ptr_and_get_ref(
61d96b89
FD
53 upstream_component_ptr, upstream_component_type
54 )
49e6b55c 55 upstream_port = bt2_port._create_from_const_ptr_and_get_ref(
61d96b89
FD
56 upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT
57 )
5265f5e3 58 downstream_component = bt2_component._create_component_from_const_ptr_and_get_ref(
61d96b89
FD
59 downstream_component_ptr, downstream_component_type
60 )
49e6b55c 61 downstream_port = bt2_port._create_from_const_ptr_and_get_ref(
61d96b89
FD
62 downstream_port_ptr, native_bt.PORT_TYPE_INPUT
63 )
64 user_listener(
65 upstream_component, upstream_port, downstream_component, downstream_port
66 )
f6a5e476
PP
67
68
c3044a97 69class Graph(object._SharedObject):
a49e2cc3
PP
70 _get_ref = staticmethod(native_bt.graph_get_ref)
71 _put_ref = staticmethod(native_bt.graph_put_ref)
bbb3650f 72
253a9ecc
PP
73 def __init__(self, mip_version=0):
74 utils._check_uint64(mip_version)
75
76 if mip_version > bt2.get_maximal_mip_version():
77 raise ValueError('unknown MIP version {}'.format(mip_version))
78
79 ptr = native_bt.graph_create(mip_version)
f6a5e476
PP
80
81 if ptr is None:
614743a5 82 raise bt2._MemoryError('cannot create graph object')
f6a5e476
PP
83
84 super().__init__(ptr)
85
61d96b89
FD
86 def add_component(
87 self,
88 component_class,
89 name,
90 params=None,
b20382e2 91 obj=None,
c946c9de 92 logging_level=bt2_logging.LoggingLevel.NONE,
61d96b89 93 ):
5265f5e3 94 if isinstance(component_class, bt2_component._SourceComponentClassConst):
871a292a 95 cc_ptr = component_class._ptr
b20382e2 96 add_fn = native_bt.bt2_graph_add_source_component
bc5c9924 97 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
5265f5e3 98 elif isinstance(component_class, bt2_component._FilterComponentClassConst):
871a292a 99 cc_ptr = component_class._ptr
b20382e2 100 add_fn = native_bt.bt2_graph_add_filter_component
bc5c9924 101 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
5265f5e3 102 elif isinstance(component_class, bt2_component._SinkComponentClassConst):
871a292a 103 cc_ptr = component_class._ptr
b20382e2 104 add_fn = native_bt.bt2_graph_add_sink_component
871a292a 105 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
c946c9de 106 elif issubclass(component_class, bt2_component._UserSourceComponent):
deec48a6 107 cc_ptr = component_class._bt_cc_ptr
b20382e2 108 add_fn = native_bt.bt2_graph_add_source_component
871a292a 109 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
c946c9de 110 elif issubclass(component_class, bt2_component._UserSinkComponent):
deec48a6 111 cc_ptr = component_class._bt_cc_ptr
b20382e2 112 add_fn = native_bt.bt2_graph_add_sink_component
bc5c9924 113 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
c946c9de 114 elif issubclass(component_class, bt2_component._UserFilterComponent):
deec48a6 115 cc_ptr = component_class._bt_cc_ptr
b20382e2 116 add_fn = native_bt.bt2_graph_add_filter_component
871a292a 117 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
f6a5e476 118 else:
61d96b89
FD
119 raise TypeError(
120 "'{}' is not a component class".format(
121 component_class.__class__.__name__
122 )
123 )
f6a5e476
PP
124
125 utils._check_str(name)
cc81b5ab 126 utils._check_log_level(logging_level)
b20382e2
PP
127 base_cc_ptr = component_class._bt_component_class_ptr()
128
129 if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr):
130 raise ValueError('cannot pass a Python object to a non-Python component')
f6a5e476 131
3e03e5e3
FD
132 if params is not None and not isinstance(params, (dict, bt2.MapValue)):
133 raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.")
134
b20382e2 135 params = bt2.create_value(params)
3e03e5e3 136
bbb3650f 137 params_ptr = params._ptr if params is not None else None
f6a5e476 138
b20382e2
PP
139 status, comp_ptr = add_fn(
140 self._ptr, cc_ptr, name, params_ptr, obj, logging_level
141 )
fb25b9e3 142 utils._handle_func_status(status, 'cannot add component to graph')
bc5c9924 143 assert comp_ptr
5265f5e3 144 return bt2_component._create_component_from_const_ptr(comp_ptr, cc_type)
f6a5e476
PP
145
146 def connect_ports(self, upstream_port, downstream_port):
49e6b55c
FD
147 utils._check_type(upstream_port, bt2_port._OutputPortConst)
148 utils._check_type(downstream_port, bt2_port._InputPortConst)
61d96b89
FD
149 status, conn_ptr = native_bt.graph_connect_ports(
150 self._ptr, upstream_port._ptr, downstream_port._ptr
151 )
152 utils._handle_func_status(status, 'cannot connect component ports within graph')
153 assert conn_ptr
9012866a 154 return bt2_connection._ConnectionConst._create_from_ptr(conn_ptr)
f6a5e476 155
871a292a
SM
156 def add_port_added_listener(self, listener):
157 if not callable(listener):
f6a5e476
PP
158 raise TypeError("'listener' parameter is not callable")
159
fb25b9e3 160 fn = native_bt.bt2_graph_add_port_added_listener
61d96b89
FD
161 listener_from_native = functools.partial(
162 _graph_port_added_listener_from_native, listener
163 )
f6a5e476 164
871a292a
SM
165 listener_ids = fn(self._ptr, listener_from_native)
166 if listener_ids is None:
614743a5 167 raise bt2._Error('cannot add listener to graph object')
d09c69f7 168
c946c9de 169 return utils._ListenerHandle(listener_ids, self)
871a292a
SM
170
171 def add_ports_connected_listener(self, listener):
172 if not callable(listener):
173 raise TypeError("'listener' parameter is not callable")
174
fb25b9e3 175 fn = native_bt.bt2_graph_add_ports_connected_listener
61d96b89
FD
176 listener_from_native = functools.partial(
177 _graph_ports_connected_listener_from_native, listener
178 )
871a292a
SM
179
180 listener_ids = fn(self._ptr, listener_from_native)
181 if listener_ids is None:
614743a5 182 raise bt2._Error('cannot add listener to graph object')
d09c69f7 183
c946c9de 184 return utils._ListenerHandle(listener_ids, self)
f6a5e476 185
22b4de6a
PP
186 def run_once(self):
187 status = native_bt.graph_run_once(self._ptr)
188 utils._handle_func_status(status, 'graph object could not run once')
189
f6a5e476
PP
190 def run(self):
191 status = native_bt.graph_run(self._ptr)
192
fb25b9e3 193 try:
d73bb381 194 utils._handle_func_status(status, 'graph object stopped running')
fb25b9e3
PP
195 except bt2.Stop:
196 # done
f6a5e476 197 return
fb25b9e3
PP
198 except Exception:
199 raise
f6a5e476 200
d73bb381 201 def add_interrupter(self, interrupter):
c946c9de 202 utils._check_type(interrupter, bt2_interrupter.Interrupter)
d73bb381 203 native_bt.graph_add_interrupter(self._ptr, interrupter._ptr)
f6a5e476 204
d73bb381
PP
205 def interrupt(self):
206 native_bt.graph_interrupt(self._ptr)
This page took 0.050772 seconds and 4 git commands to generate.