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