1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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
20 def _graph_port_added_listener_from_native(
21 user_listener
, component_ptr
, component_type
, port_ptr
, port_type
23 component
= bt2_component
._create
_component
_from
_const
_ptr
_and
_get
_ref
(
24 component_ptr
, component_type
26 port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(port_ptr
, port_type
)
27 user_listener(component
, port
)
30 class Graph(bt2_object
._SharedObject
):
33 native_bt
.graph_get_ref(ptr
)
37 native_bt
.graph_put_ref(ptr
)
39 def __init__(self
, mip_version
=0):
40 bt2_utils
._check
_uint
64(mip_version
)
42 if mip_version
> bt2_mip
.get_maximal_mip_version():
43 raise ValueError("unknown MIP version {}".format(mip_version
))
45 ptr
= native_bt
.graph_create(mip_version
)
48 raise bt2_error
._MemoryError
("cannot create graph object")
52 # list of listener partials to keep a reference as long as
54 self
._listener
_partials
= []
62 logging_level
=bt2_logging
.LoggingLevel
.NONE
,
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
90 "'{}' is not a component class".format(
91 component_class
.__class
__.__name
__
95 bt2_utils
._check
_str
(name
)
96 bt2_utils
._check
_log
_level
(logging_level
)
97 base_cc_ptr
= component_class
._bt
_component
_class
_ptr
()
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")
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'.")
105 params
= bt2_value
.create_value(params
)
107 params_ptr
= params
._ptr
if params
is not None else None
109 status
, comp_ptr
= add_fn(
110 self
._ptr
, cc_ptr
, name
, params_ptr
, obj
, logging_level
112 bt2_utils
._handle
_func
_status
(status
, "cannot add component to graph")
114 return bt2_component
._create
_component
_from
_const
_ptr
_and
_get
_ref
(
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
124 bt2_utils
._handle
_func
_status
(
125 status
, "cannot connect component ports within graph"
128 return bt2_connection
._ConnectionConst
._create
_from
_ptr
_and
_get
_ref
(conn_ptr
)
130 def add_port_added_listener(self
, listener
):
131 if not callable(listener
):
132 raise TypeError("'listener' parameter is not callable")
134 fn
= native_bt
.bt2_graph_add_port_added_listener
135 listener_from_native
= functools
.partial(
136 _graph_port_added_listener_from_native
, listener
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")
143 # keep the partial's reference
144 self
._listener
_partials
.append(listener_from_native
)
147 status
= native_bt
.graph_run_once(self
._ptr
)
148 bt2_utils
._handle
_func
_status
(status
, "graph object could not run once")
151 status
= native_bt
.graph_run(self
._ptr
)
152 bt2_utils
._handle
_func
_status
(status
, "graph object stopped running")
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
)
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
)