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 import bt2
.interrupter
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
)
74 ptr
= native_bt
.graph_create()
77 raise bt2
._MemoryError('cannot create graph object')
86 logging_level
=bt2
.logging
.LoggingLevel
.NONE
,
88 if isinstance(component_class
, bt2
.component
._GenericSourceComponentClass
):
89 cc_ptr
= component_class
._ptr
90 add_fn
= native_bt
.graph_add_source_component
91 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
92 elif isinstance(component_class
, bt2
.component
._GenericFilterComponentClass
):
93 cc_ptr
= component_class
._ptr
94 add_fn
= native_bt
.graph_add_filter_component
95 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
96 elif isinstance(component_class
, bt2
.component
._GenericSinkComponentClass
):
97 cc_ptr
= component_class
._ptr
98 add_fn
= native_bt
.graph_add_sink_component
99 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
100 elif issubclass(component_class
, bt2
.component
._UserSourceComponent
):
101 cc_ptr
= component_class
._bt
_cc
_ptr
102 add_fn
= native_bt
.graph_add_source_component
103 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
104 elif issubclass(component_class
, bt2
.component
._UserSinkComponent
):
105 cc_ptr
= component_class
._bt
_cc
_ptr
106 add_fn
= native_bt
.graph_add_sink_component
107 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
108 elif issubclass(component_class
, bt2
.component
._UserFilterComponent
):
109 cc_ptr
= component_class
._bt
_cc
_ptr
110 add_fn
= native_bt
.graph_add_filter_component
111 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
114 "'{}' is not a component class".format(
115 component_class
.__class
__.__name
__
119 utils
._check
_str
(name
)
120 utils
._check
_log
_level
(logging_level
)
121 params
= bt2
.create_value(params
)
123 params_ptr
= params
._ptr
if params
is not None else None
125 status
, comp_ptr
= add_fn(self
._ptr
, cc_ptr
, name
, params_ptr
, logging_level
)
126 utils
._handle
_func
_status
(status
, 'cannot add component to graph')
128 return bt2
.component
._create
_component
_from
_ptr
(comp_ptr
, cc_type
)
130 def connect_ports(self
, upstream_port
, downstream_port
):
131 utils
._check
_type
(upstream_port
, bt2
.port
._OutputPort
)
132 utils
._check
_type
(downstream_port
, bt2
.port
._InputPort
)
133 status
, conn_ptr
= native_bt
.graph_connect_ports(
134 self
._ptr
, upstream_port
._ptr
, downstream_port
._ptr
136 utils
._handle
_func
_status
(status
, 'cannot connect component ports within graph')
138 return bt2
.connection
._Connection
._create
_from
_ptr
(conn_ptr
)
140 def add_port_added_listener(self
, listener
):
141 if not callable(listener
):
142 raise TypeError("'listener' parameter is not callable")
144 fn
= native_bt
.bt2_graph_add_port_added_listener
145 listener_from_native
= functools
.partial(
146 _graph_port_added_listener_from_native
, listener
149 listener_ids
= fn(self
._ptr
, listener_from_native
)
150 if listener_ids
is None:
151 raise bt2
._Error('cannot add listener to graph object')
153 return bt2
._ListenerHandle(listener_ids
, self
)
155 def add_ports_connected_listener(self
, listener
):
156 if not callable(listener
):
157 raise TypeError("'listener' parameter is not callable")
159 fn
= native_bt
.bt2_graph_add_ports_connected_listener
160 listener_from_native
= functools
.partial(
161 _graph_ports_connected_listener_from_native
, listener
164 listener_ids
= fn(self
._ptr
, listener_from_native
)
165 if listener_ids
is None:
166 raise bt2
._Error('cannot add listener to graph object')
168 return bt2
._ListenerHandle(listener_ids
, self
)
171 status
= native_bt
.graph_run(self
._ptr
)
174 utils
._handle
_func
_status
(status
, 'graph object stopped running')
181 def add_interrupter(self
, interrupter
):
182 utils
._check
_type
(interrupter
, bt2
.interrupter
.Interrupter
)
183 native_bt
.graph_add_interrupter(self
._ptr
, interrupter
._ptr
)
186 native_bt
.graph_interrupt(self
._ptr
)
188 def create_output_port_message_iterator(self
, output_port
):
189 utils
._check
_type
(output_port
, bt2
.port
._OutputPort
)
190 msg_iter_ptr
= native_bt
.port_output_message_iterator_create(
191 self
._ptr
, output_port
._ptr
194 if msg_iter_ptr
is None:
195 raise bt2
._MemoryError('cannot create output port message iterator')
197 return bt2
.message_iterator
._OutputPortMessageIterator
(msg_iter_ptr
)