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