1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from bt2
import native_bt
, object, utils
24 from bt2
import interrupter
as bt2_interrupter
25 from bt2
import connection
as bt2_connection
26 from bt2
import component
as bt2_component
28 from bt2
import port
as bt2_port
29 from bt2
import logging
as bt2_logging
33 def _graph_port_added_listener_from_native(
34 user_listener
, component_ptr
, component_type
, port_ptr
, port_type
36 component
= bt2_component
._create
_component
_from
_ptr
_and
_get
_ref
(
37 component_ptr
, component_type
39 port
= bt2_port
._create
_from
_ptr
_and
_get
_ref
(port_ptr
, port_type
)
40 user_listener(component
, port
)
43 def _graph_ports_connected_listener_from_native(
45 upstream_component_ptr
,
46 upstream_component_type
,
48 downstream_component_ptr
,
49 downstream_component_type
,
52 upstream_component
= bt2_component
._create
_component
_from
_ptr
_and
_get
_ref
(
53 upstream_component_ptr
, upstream_component_type
55 upstream_port
= bt2_port
._create
_from
_ptr
_and
_get
_ref
(
56 upstream_port_ptr
, native_bt
.PORT_TYPE_OUTPUT
58 downstream_component
= bt2_component
._create
_component
_from
_ptr
_and
_get
_ref
(
59 downstream_component_ptr
, downstream_component_type
61 downstream_port
= bt2_port
._create
_from
_ptr
_and
_get
_ref
(
62 downstream_port_ptr
, native_bt
.PORT_TYPE_INPUT
65 upstream_component
, upstream_port
, downstream_component
, downstream_port
69 class Graph(object._SharedObject
):
70 _get_ref
= staticmethod(native_bt
.graph_get_ref
)
71 _put_ref
= staticmethod(native_bt
.graph_put_ref
)
73 def __init__(self
, mip_version
=0):
74 utils
._check
_uint
64(mip_version
)
76 if mip_version
> bt2
.get_maximal_mip_version():
77 raise ValueError('unknown MIP version {}'.format(mip_version
))
79 ptr
= native_bt
.graph_create(mip_version
)
82 raise bt2
._MemoryError('cannot create graph object')
92 logging_level
=bt2_logging
.LoggingLevel
.NONE
,
94 if isinstance(component_class
, bt2_component
._SourceComponentClass
):
95 cc_ptr
= component_class
._ptr
96 add_fn
= native_bt
.bt2_graph_add_source_component
97 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
98 elif isinstance(component_class
, bt2_component
._FilterComponentClass
):
99 cc_ptr
= component_class
._ptr
100 add_fn
= native_bt
.bt2_graph_add_filter_component
101 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
102 elif isinstance(component_class
, bt2_component
._SinkComponentClass
):
103 cc_ptr
= component_class
._ptr
104 add_fn
= native_bt
.bt2_graph_add_sink_component
105 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
106 elif issubclass(component_class
, bt2_component
._UserSourceComponent
):
107 cc_ptr
= component_class
._bt
_cc
_ptr
108 add_fn
= native_bt
.bt2_graph_add_source_component
109 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
110 elif issubclass(component_class
, bt2_component
._UserSinkComponent
):
111 cc_ptr
= component_class
._bt
_cc
_ptr
112 add_fn
= native_bt
.bt2_graph_add_sink_component
113 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
114 elif issubclass(component_class
, bt2_component
._UserFilterComponent
):
115 cc_ptr
= component_class
._bt
_cc
_ptr
116 add_fn
= native_bt
.bt2_graph_add_filter_component
117 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
120 "'{}' is not a component class".format(
121 component_class
.__class
__.__name
__
125 utils
._check
_str
(name
)
126 utils
._check
_log
_level
(logging_level
)
127 base_cc_ptr
= component_class
._bt
_component
_class
_ptr
()
129 if obj
is not None and not native_bt
.bt2_is_python_component_class(base_cc_ptr
):
130 raise ValueError('cannot pass a Python object to a non-Python component')
132 params
= bt2
.create_value(params
)
133 params_ptr
= params
._ptr
if params
is not None else None
135 status
, comp_ptr
= add_fn(
136 self
._ptr
, cc_ptr
, name
, params_ptr
, obj
, logging_level
138 utils
._handle
_func
_status
(status
, 'cannot add component to graph')
140 return bt2_component
._create
_component
_from
_ptr
(comp_ptr
, cc_type
)
142 def connect_ports(self
, upstream_port
, downstream_port
):
143 utils
._check
_type
(upstream_port
, bt2_port
._OutputPort
)
144 utils
._check
_type
(downstream_port
, bt2_port
._InputPort
)
145 status
, conn_ptr
= native_bt
.graph_connect_ports(
146 self
._ptr
, upstream_port
._ptr
, downstream_port
._ptr
148 utils
._handle
_func
_status
(status
, 'cannot connect component ports within graph')
150 return bt2_connection
._Connection
._create
_from
_ptr
(conn_ptr
)
152 def add_port_added_listener(self
, listener
):
153 if not callable(listener
):
154 raise TypeError("'listener' parameter is not callable")
156 fn
= native_bt
.bt2_graph_add_port_added_listener
157 listener_from_native
= functools
.partial(
158 _graph_port_added_listener_from_native
, listener
161 listener_ids
= fn(self
._ptr
, listener_from_native
)
162 if listener_ids
is None:
163 raise bt2
._Error('cannot add listener to graph object')
165 return utils
._ListenerHandle
(listener_ids
, self
)
167 def add_ports_connected_listener(self
, listener
):
168 if not callable(listener
):
169 raise TypeError("'listener' parameter is not callable")
171 fn
= native_bt
.bt2_graph_add_ports_connected_listener
172 listener_from_native
= functools
.partial(
173 _graph_ports_connected_listener_from_native
, listener
176 listener_ids
= fn(self
._ptr
, listener_from_native
)
177 if listener_ids
is None:
178 raise bt2
._Error('cannot add listener to graph object')
180 return utils
._ListenerHandle
(listener_ids
, self
)
183 status
= native_bt
.graph_run_once(self
._ptr
)
184 utils
._handle
_func
_status
(status
, 'graph object could not run once')
187 status
= native_bt
.graph_run(self
._ptr
)
190 utils
._handle
_func
_status
(status
, 'graph object stopped running')
197 def add_interrupter(self
, interrupter
):
198 utils
._check
_type
(interrupter
, bt2_interrupter
.Interrupter
)
199 native_bt
.graph_add_interrupter(self
._ptr
, interrupter
._ptr
)
202 native_bt
.graph_interrupt(self
._ptr
)