python: standardize intra-bt2 imports
[babeltrace.git] / src / bindings / python / bt2 / bt2 / graph.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
811644b8
PP
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
811644b8 4
e5914347
SM
5from bt2 import native_bt
6from bt2 import object as bt2_object
7from bt2 import utils as bt2_utils
3fb99a22
PP
8from bt2 import interrupter as bt2_interrupter
9from bt2 import connection as bt2_connection
10from bt2 import component as bt2_component
811644b8 11import functools
3fb99a22
PP
12from bt2 import port as bt2_port
13from bt2 import logging as bt2_logging
811644b8
PP
14import bt2
15
16
cfbd7cf3
FD
17def _graph_port_added_listener_from_native(
18 user_listener, component_ptr, component_type, port_ptr, port_type
19):
615238be 20 component = bt2_component._create_component_from_const_ptr_and_get_ref(
cfbd7cf3
FD
21 component_ptr, component_type
22 )
5813b3a3 23 port = bt2_port._create_from_const_ptr_and_get_ref(port_ptr, port_type)
5f25509b 24 user_listener(component, port)
811644b8
PP
25
26
e5914347 27class Graph(bt2_object._SharedObject):
9dee90bd
SM
28 @staticmethod
29 def _get_ref(ptr):
30 native_bt.graph_get_ref(ptr)
31
32 @staticmethod
33 def _put_ref(ptr):
34 native_bt.graph_put_ref(ptr)
601c0026 35
056deb59 36 def __init__(self, mip_version=0):
e5914347 37 bt2_utils._check_uint64(mip_version)
056deb59
PP
38
39 if mip_version > bt2.get_maximal_mip_version():
f5567ea8 40 raise ValueError("unknown MIP version {}".format(mip_version))
056deb59
PP
41
42 ptr = native_bt.graph_create(mip_version)
811644b8
PP
43
44 if ptr is None:
f5567ea8 45 raise bt2._MemoryError("cannot create graph object")
811644b8
PP
46
47 super().__init__(ptr)
48
f3d6b4c2
PP
49 # list of listener partials to keep a reference as long as
50 # this graph exists
51 self._listener_partials = []
52
cfbd7cf3
FD
53 def add_component(
54 self,
55 component_class,
56 name,
57 params=None,
66964f3f 58 obj=None,
3fb99a22 59 logging_level=bt2_logging.LoggingLevel.NONE,
cfbd7cf3 60 ):
615238be 61 if isinstance(component_class, bt2_component._SourceComponentClassConst):
5f25509b 62 cc_ptr = component_class._ptr
66964f3f 63 add_fn = native_bt.bt2_graph_add_source_component
894a8df5 64 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
615238be 65 elif isinstance(component_class, bt2_component._FilterComponentClassConst):
5f25509b 66 cc_ptr = component_class._ptr
66964f3f 67 add_fn = native_bt.bt2_graph_add_filter_component
894a8df5 68 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
615238be 69 elif isinstance(component_class, bt2_component._SinkComponentClassConst):
5f25509b 70 cc_ptr = component_class._ptr
66964f3f 71 add_fn = native_bt.bt2_graph_add_sink_component
5f25509b 72 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
3fb99a22 73 elif issubclass(component_class, bt2_component._UserSourceComponent):
85906b6b 74 cc_ptr = component_class._bt_cc_ptr
66964f3f 75 add_fn = native_bt.bt2_graph_add_source_component
5f25509b 76 cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE
3fb99a22 77 elif issubclass(component_class, bt2_component._UserSinkComponent):
85906b6b 78 cc_ptr = component_class._bt_cc_ptr
66964f3f 79 add_fn = native_bt.bt2_graph_add_sink_component
894a8df5 80 cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK
3fb99a22 81 elif issubclass(component_class, bt2_component._UserFilterComponent):
85906b6b 82 cc_ptr = component_class._bt_cc_ptr
66964f3f 83 add_fn = native_bt.bt2_graph_add_filter_component
5f25509b 84 cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER
811644b8 85 else:
cfbd7cf3
FD
86 raise TypeError(
87 "'{}' is not a component class".format(
88 component_class.__class__.__name__
89 )
90 )
811644b8 91
e5914347
SM
92 bt2_utils._check_str(name)
93 bt2_utils._check_log_level(logging_level)
66964f3f
PP
94 base_cc_ptr = component_class._bt_component_class_ptr()
95
96 if obj is not None and not native_bt.bt2_is_python_component_class(base_cc_ptr):
f5567ea8 97 raise ValueError("cannot pass a Python object to a non-Python component")
811644b8 98
401b7022
FD
99 if params is not None and not isinstance(params, (dict, bt2.MapValue)):
100 raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.")
101
66964f3f 102 params = bt2.create_value(params)
401b7022 103
601c0026 104 params_ptr = params._ptr if params is not None else None
811644b8 105
66964f3f
PP
106 status, comp_ptr = add_fn(
107 self._ptr, cc_ptr, name, params_ptr, obj, logging_level
108 )
e5914347 109 bt2_utils._handle_func_status(status, "cannot add component to graph")
894a8df5 110 assert comp_ptr
a91462c9
PP
111 return bt2_component._create_component_from_const_ptr_and_get_ref(
112 comp_ptr, cc_type
113 )
811644b8
PP
114
115 def connect_ports(self, upstream_port, downstream_port):
e5914347
SM
116 bt2_utils._check_type(upstream_port, bt2_port._OutputPortConst)
117 bt2_utils._check_type(downstream_port, bt2_port._InputPortConst)
cfbd7cf3
FD
118 status, conn_ptr = native_bt.graph_connect_ports(
119 self._ptr, upstream_port._ptr, downstream_port._ptr
120 )
e5914347
SM
121 bt2_utils._handle_func_status(
122 status, "cannot connect component ports within graph"
123 )
cfbd7cf3 124 assert conn_ptr
a91462c9 125 return bt2_connection._ConnectionConst._create_from_ptr_and_get_ref(conn_ptr)
811644b8 126
5f25509b
SM
127 def add_port_added_listener(self, listener):
128 if not callable(listener):
811644b8
PP
129 raise TypeError("'listener' parameter is not callable")
130
d24d5663 131 fn = native_bt.bt2_graph_add_port_added_listener
cfbd7cf3
FD
132 listener_from_native = functools.partial(
133 _graph_port_added_listener_from_native, listener
134 )
811644b8 135
5f25509b
SM
136 listener_ids = fn(self._ptr, listener_from_native)
137 if listener_ids is None:
f5567ea8 138 raise bt2._Error("cannot add listener to graph object")
416379bc 139
f3d6b4c2
PP
140 # keep the partial's reference
141 self._listener_partials.append(listener_from_native)
142
8cc0e6ea
PP
143 def run_once(self):
144 status = native_bt.graph_run_once(self._ptr)
e5914347 145 bt2_utils._handle_func_status(status, "graph object could not run once")
8cc0e6ea 146
811644b8
PP
147 def run(self):
148 status = native_bt.graph_run(self._ptr)
e5914347 149 bt2_utils._handle_func_status(status, "graph object stopped running")
811644b8 150
9b4f9b42 151 def add_interrupter(self, interrupter):
e5914347 152 bt2_utils._check_type(interrupter, bt2_interrupter.Interrupter)
9b4f9b42 153 native_bt.graph_add_interrupter(self._ptr, interrupter._ptr)
811644b8 154
c513d240
SM
155 @property
156 def default_interrupter(self):
157 ptr = native_bt.graph_borrow_default_interrupter(self._ptr)
158 return bt2_interrupter.Interrupter._create_from_ptr_and_get_ref(ptr)
This page took 0.0787 seconds and 4 git commands to generate.