1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
6 from bt2
import object as bt2_object
7 from bt2
import utils
as bt2_utils
8 from bt2
import interrupter
as bt2_interrupter
9 from bt2
import connection
as bt2_connection
10 from bt2
import component
as bt2_component
12 from bt2
import port
as bt2_port
13 from bt2
import logging
as bt2_logging
14 from bt2
import mip
as bt2_mip
15 from bt2
import error
as bt2_error
16 from bt2
import value
as bt2_value
19 def _graph_port_added_listener_from_native(
20 user_listener
, component_ptr
, component_type
, port_ptr
, port_type
22 component
= bt2_component
._create
_component
_from
_const
_ptr
_and
_get
_ref
(
23 component_ptr
, component_type
25 port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(port_ptr
, port_type
)
26 user_listener(component
, port
)
29 class Graph(bt2_object
._SharedObject
):
32 native_bt
.graph_get_ref(ptr
)
36 native_bt
.graph_put_ref(ptr
)
38 def __init__(self
, mip_version
=0):
39 bt2_utils
._check
_uint
64(mip_version
)
41 if mip_version
> bt2_mip
.get_maximal_mip_version():
42 raise ValueError("unknown MIP version {}".format(mip_version
))
44 ptr
= native_bt
.graph_create(mip_version
)
47 raise bt2_error
._MemoryError
("cannot create graph object")
51 # list of listener partials to keep a reference as long as
53 self
._listener
_partials
= []
61 logging_level
=bt2_logging
.LoggingLevel
.NONE
,
63 if isinstance(component_class
, bt2_component
._SourceComponentClassConst
):
64 cc_ptr
= component_class
._ptr
65 add_fn
= native_bt
.bt2_graph_add_source_component
66 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
67 elif isinstance(component_class
, bt2_component
._FilterComponentClassConst
):
68 cc_ptr
= component_class
._ptr
69 add_fn
= native_bt
.bt2_graph_add_filter_component
70 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
71 elif isinstance(component_class
, bt2_component
._SinkComponentClassConst
):
72 cc_ptr
= component_class
._ptr
73 add_fn
= native_bt
.bt2_graph_add_sink_component
74 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
75 elif issubclass(component_class
, bt2_component
._UserSourceComponent
):
76 cc_ptr
= component_class
._bt
_cc
_ptr
77 add_fn
= native_bt
.bt2_graph_add_source_component
78 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
79 elif issubclass(component_class
, bt2_component
._UserSinkComponent
):
80 cc_ptr
= component_class
._bt
_cc
_ptr
81 add_fn
= native_bt
.bt2_graph_add_sink_component
82 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
83 elif issubclass(component_class
, bt2_component
._UserFilterComponent
):
84 cc_ptr
= component_class
._bt
_cc
_ptr
85 add_fn
= native_bt
.bt2_graph_add_filter_component
86 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
89 "'{}' is not a component class".format(
90 component_class
.__class
__.__name
__
94 bt2_utils
._check
_str
(name
)
95 bt2_utils
._check
_log
_level
(logging_level
)
96 base_cc_ptr
= component_class
._bt
_component
_class
_ptr
()
98 if obj
is not None and not native_bt
.bt2_is_python_component_class(base_cc_ptr
):
99 raise ValueError("cannot pass a Python object to a non-Python component")
101 if params
is not None and not isinstance(params
, (dict, bt2_value
.MapValue
)):
102 raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.")
104 params
= bt2_value
.create_value(params
)
106 params_ptr
= params
._ptr
if params
is not None else None
108 status
, comp_ptr
= add_fn(
109 self
._ptr
, cc_ptr
, name
, params_ptr
, obj
, logging_level
111 bt2_utils
._handle
_func
_status
(status
, "cannot add component to graph")
113 return bt2_component
._create
_component
_from
_const
_ptr
_and
_get
_ref
(
117 def connect_ports(self
, upstream_port
, downstream_port
):
118 bt2_utils
._check
_type
(upstream_port
, bt2_port
._OutputPortConst
)
119 bt2_utils
._check
_type
(downstream_port
, bt2_port
._InputPortConst
)
120 status
, conn_ptr
= native_bt
.graph_connect_ports(
121 self
._ptr
, upstream_port
._ptr
, downstream_port
._ptr
123 bt2_utils
._handle
_func
_status
(
124 status
, "cannot connect component ports within graph"
127 return bt2_connection
._ConnectionConst
._create
_from
_ptr
_and
_get
_ref
(conn_ptr
)
129 def add_port_added_listener(self
, listener
):
130 if not callable(listener
):
131 raise TypeError("'listener' parameter is not callable")
133 fn
= native_bt
.bt2_graph_add_port_added_listener
134 listener_from_native
= functools
.partial(
135 _graph_port_added_listener_from_native
, listener
138 listener_ids
= fn(self
._ptr
, listener_from_native
)
139 if listener_ids
is None:
140 raise bt2_error
._Error
("cannot add listener to graph object")
142 # keep the partial's reference
143 self
._listener
_partials
.append(listener_from_native
)
146 status
= native_bt
.graph_run_once(self
._ptr
)
147 bt2_utils
._handle
_func
_status
(status
, "graph object could not run once")
150 status
= native_bt
.graph_run(self
._ptr
)
151 bt2_utils
._handle
_func
_status
(status
, "graph object stopped running")
153 def add_interrupter(self
, interrupter
):
154 bt2_utils
._check
_type
(interrupter
, bt2_interrupter
.Interrupter
)
155 native_bt
.graph_add_interrupter(self
._ptr
, interrupter
._ptr
)
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
)