lib: set component's initial log level when adding it to the graph
[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
24import bt2.connection
25import bt2.component
26import functools
27import bt2.port
e874da19 28import bt2.logging
811644b8
PP
29import bt2
30
31
5f25509b
SM
32def _graph_port_added_listener_from_native(user_listener,
33 component_ptr, component_type,
34 port_ptr, port_type):
35 component = bt2.component._create_component_from_ptr_and_get_ref(component_ptr, component_type)
36 port = bt2.port._create_from_ptr_and_get_ref(port_ptr, port_type)
37 user_listener(component, port)
811644b8
PP
38
39
40def _graph_ports_connected_listener_from_native(user_listener,
5f25509b 41 upstream_component_ptr, upstream_component_type,
811644b8 42 upstream_port_ptr,
5f25509b 43 downstream_component_ptr, downstream_component_type,
811644b8 44 downstream_port_ptr):
5f25509b
SM
45 upstream_component = bt2.component._create_component_from_ptr_and_get_ref(
46 upstream_component_ptr, upstream_component_type)
47 upstream_port = bt2.port._create_from_ptr_and_get_ref(
48 upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT)
49 downstream_component = bt2.component._create_component_from_ptr_and_get_ref(
50 downstream_component_ptr, downstream_component_type)
51 downstream_port = bt2.port._create_from_ptr_and_get_ref(
52 downstream_port_ptr, native_bt.PORT_TYPE_INPUT)
53 user_listener(upstream_component, upstream_port, downstream_component, downstream_port)
811644b8
PP
54
55
78288f58 56class Graph(object._SharedObject):
2f16a6a2
PP
57 _get_ref = staticmethod(native_bt.graph_get_ref)
58 _put_ref = staticmethod(native_bt.graph_put_ref)
601c0026 59
811644b8
PP
60 def __init__(self):
61 ptr = native_bt.graph_create()
62
63 if ptr is None:
64 raise bt2.CreationError('cannot create graph object')
65
66 super().__init__(ptr)
67
68 def _handle_status(self, status, gen_error_msg):
69 if status == native_bt.GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
70 raise bt2.PortConnectionRefused
71 elif status == native_bt.GRAPH_STATUS_CANCELED:
72 raise bt2.GraphCanceled
73 elif status == native_bt.GRAPH_STATUS_END:
74 raise bt2.Stop
75 elif status == native_bt.GRAPH_STATUS_AGAIN:
76 raise bt2.TryAgain
811644b8
PP
77 elif status < 0:
78 raise bt2.Error(gen_error_msg)
79
e874da19
PP
80 def add_component(self, component_class, name, params=None,
81 logging_level=bt2.logging.LoggingLevel.NONE):
5f25509b
SM
82 if isinstance(component_class, bt2.component._GenericSourceComponentClass):
83 cc_ptr = component_class._ptr
894a8df5
SM
84 add_fn = native_bt.graph_add_source_component
85 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
5f25509b
SM
86 elif isinstance(component_class, bt2.component._GenericFilterComponentClass):
87 cc_ptr = component_class._ptr
894a8df5
SM
88 add_fn = native_bt.graph_add_filter_component
89 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
5f25509b
SM
90 elif isinstance(component_class, bt2.component._GenericSinkComponentClass):
91 cc_ptr = component_class._ptr
92 add_fn = native_bt.graph_add_sink_component
93 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
94 elif issubclass(component_class, bt2.component._UserSourceComponent):
95 cc_ptr = component_class._cc_ptr
96 add_fn = native_bt.graph_add_source_component
97 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
894a8df5
SM
98 elif issubclass(component_class, bt2.component._UserSinkComponent):
99 cc_ptr = component_class._cc_ptr
100 add_fn = native_bt.graph_add_sink_component
101 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
5f25509b
SM
102 elif issubclass(component_class, bt2.component._UserFilterComponent):
103 cc_ptr = component_class._cc_ptr
104 add_fn = native_bt.graph_add_filter_component
105 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 106 else:
894a8df5 107 raise TypeError("'{}' is not a component class".format(
601c0026 108 component_class.__class__.__name__))
811644b8
PP
109
110 utils._check_str(name)
e874da19 111 utils._check_log_level(logging_level)
811644b8
PP
112 params = bt2.create_value(params)
113
601c0026 114 params_ptr = params._ptr if params is not None else None
811644b8 115
e874da19
PP
116 status, comp_ptr = add_fn(self._ptr, cc_ptr, name,
117 params_ptr, logging_level)
894a8df5
SM
118 self._handle_status(status, 'cannot add component to graph')
119 assert comp_ptr
120 return bt2.component._create_component_from_ptr(comp_ptr, cc_type)
811644b8
PP
121
122 def connect_ports(self, upstream_port, downstream_port):
123 utils._check_type(upstream_port, bt2.port._OutputPort)
124 utils._check_type(downstream_port, bt2.port._InputPort)
125 status, conn_ptr = native_bt.graph_connect_ports(self._ptr,
126 upstream_port._ptr,
127 downstream_port._ptr)
128 self._handle_status(status, 'cannot connect component ports within graph')
129 assert(conn_ptr)
130 return bt2.connection._Connection._create_from_ptr(conn_ptr)
131
5f25509b
SM
132 def add_port_added_listener(self, listener):
133 if not callable(listener):
811644b8
PP
134 raise TypeError("'listener' parameter is not callable")
135
5f25509b
SM
136 fn = native_bt.py3_graph_add_port_added_listener
137 listener_from_native = functools.partial(_graph_port_added_listener_from_native,
138 listener)
811644b8 139
5f25509b
SM
140 listener_ids = fn(self._ptr, listener_from_native)
141 if listener_ids is None:
142 utils._raise_bt2_error('cannot add listener to graph object')
143 return bt2._ListenerHandle(listener_ids, self)
144
145 def add_ports_connected_listener(self, listener):
146 if not callable(listener):
147 raise TypeError("'listener' parameter is not callable")
148
149 fn = native_bt.py3_graph_add_ports_connected_listener
150 listener_from_native = functools.partial(_graph_ports_connected_listener_from_native,
151 listener)
152
153 listener_ids = fn(self._ptr, listener_from_native)
154 if listener_ids is None:
155 utils._raise_bt2_error('cannot add listener to graph object')
156 return bt2._ListenerHandle(listener_ids, self)
811644b8
PP
157
158 def run(self):
159 status = native_bt.graph_run(self._ptr)
160
161 if status == native_bt.GRAPH_STATUS_END:
162 return
163
164 self._handle_status(status, 'graph object stopped running because of an unexpected error')
165
166 def cancel(self):
167 status = native_bt.graph_cancel(self._ptr)
168 self._handle_status(status, 'cannot cancel graph object')
169
170 @property
171 def is_canceled(self):
172 is_canceled = native_bt.graph_is_canceled(self._ptr)
173 assert(is_canceled >= 0)
174 return is_canceled > 0
175
2ae9f48c
SM
176 def create_output_port_message_iterator(self, output_port):
177 utils._check_type(output_port, bt2.port._OutputPort)
178 msg_iter_ptr = native_bt.port_output_message_iterator_create(self._ptr, output_port._ptr)
179
180 if msg_iter_ptr is None:
181 raise bt2.CreationError('cannot create output port message iterator')
182
183 return bt2.message_iterator._OutputPortMessageIterator(msg_iter_ptr)
This page took 0.040011 seconds and 4 git commands to generate.