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