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
31 class GraphListenerType
:
35 PORTS_DISCONNECTED
= 3
38 def _graph_port_added_listener_from_native(user_listener
, port_ptr
):
40 port
= bt2
.port
._create
_from
_ptr
(port_ptr
)
47 def _graph_port_removed_listener_from_native(user_listener
, port_ptr
):
49 port
= bt2
.port
._create
_from
_ptr
(port_ptr
)
56 def _graph_ports_connected_listener_from_native(user_listener
,
60 upstream_port
= bt2
.port
._create
_from
_ptr
(upstream_port_ptr
)
62 downstream_port
= bt2
.port
._create
_from
_ptr
(downstream_port_ptr
)
63 downstream_port
._get
()
64 user_listener(upstream_port
, downstream_port
)
69 def _graph_ports_disconnected_listener_from_native(user_listener
,
75 upstream_comp
= bt2
.component
._create
_generic
_component
_from
_ptr
(upstream_comp_ptr
)
77 downstream_comp
= bt2
.component
._create
_generic
_component
_from
_ptr
(downstream_comp_ptr
)
78 downstream_comp
._get
()
79 upstream_port
= bt2
.port
._create
_from
_ptr
(upstream_port_ptr
)
81 downstream_port
= bt2
.port
._create
_from
_ptr
(downstream_port_ptr
)
82 downstream_port
._get
()
83 user_listener(upstream_comp
, downstream_comp
, upstream_port
,
89 class Graph(object._Object
):
91 ptr
= native_bt
.graph_create()
94 raise bt2
.CreationError('cannot create graph object')
98 def _handle_status(self
, status
, gen_error_msg
):
99 if status
== native_bt
.GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
100 raise bt2
.PortConnectionRefused
101 elif status
== native_bt
.GRAPH_STATUS_CANCELED
:
102 raise bt2
.GraphCanceled
103 elif status
== native_bt
.GRAPH_STATUS_END
:
105 elif status
== native_bt
.GRAPH_STATUS_AGAIN
:
107 elif status
== native_bt
.GRAPH_STATUS_NO_SINK
:
108 raise bt2
.NoSinkComponent
110 raise bt2
.Error(gen_error_msg
)
112 def add_component(self
, component_class
, name
, params
=None):
113 if isinstance(component_class
, bt2
.component
._GenericComponentClass
):
114 cc_ptr
= component_class
._ptr
115 elif issubclass(component_class
, bt2
.component
._UserComponent
):
116 cc_ptr
= component_class
._cc
_ptr
118 raise TypeError("'{}' is not a component class".format(component_class
.__class
__.__name
__))
120 utils
._check
_str
(name
)
121 params
= bt2
.create_value(params
)
126 params_ptr
= params
._ptr
128 status
, comp_ptr
= native_bt
.graph_add_component(self
._ptr
, cc_ptr
,
130 self
._handle
_status
(status
, 'cannot add component to graph')
132 return bt2
.component
._create
_generic
_component
_from
_ptr
(comp_ptr
)
134 def connect_ports(self
, upstream_port
, downstream_port
):
135 utils
._check
_type
(upstream_port
, bt2
.port
._OutputPort
)
136 utils
._check
_type
(downstream_port
, bt2
.port
._InputPort
)
137 status
, conn_ptr
= native_bt
.graph_connect_ports(self
._ptr
,
139 downstream_port
._ptr
)
140 self
._handle
_status
(status
, 'cannot connect component ports within graph')
142 return bt2
.connection
._Connection
._create
_from
_ptr
(conn_ptr
)
144 def add_listener(self
, listener_type
, listener
):
145 if not hasattr(listener
, '__call__'):
146 raise TypeError("'listener' parameter is not callable")
148 if listener_type
== GraphListenerType
.PORT_ADDED
:
149 fn
= native_bt
.py3_graph_add_port_added_listener
150 listener_from_native
= functools
.partial(_graph_port_added_listener_from_native
,
152 elif listener_type
== GraphListenerType
.PORT_REMOVED
:
153 fn
= native_bt
.py3_graph_add_port_removed_listener
154 listener_from_native
= functools
.partial(_graph_port_removed_listener_from_native
,
156 elif listener_type
== GraphListenerType
.PORTS_CONNECTED
:
157 fn
= native_bt
.py3_graph_add_ports_connected_listener
158 listener_from_native
= functools
.partial(_graph_ports_connected_listener_from_native
,
160 elif listener_type
== GraphListenerType
.PORTS_DISCONNECTED
:
161 fn
= native_bt
.py3_graph_add_ports_disconnected_listener
162 listener_from_native
= functools
.partial(_graph_ports_disconnected_listener_from_native
,
167 listener_id
= fn(self
._ptr
, listener_from_native
)
168 utils
._handle
_ret
(listener_id
, 'cannot add listener to graph object')
169 return bt2
._ListenerHandle(listener_id
, self
)
172 status
= native_bt
.graph_run(self
._ptr
)
174 if status
== native_bt
.GRAPH_STATUS_END
:
177 self
._handle
_status
(status
, 'graph object stopped running because of an unexpected error')
180 status
= native_bt
.graph_cancel(self
._ptr
)
181 self
._handle
_status
(status
, 'cannot cancel graph object')
184 def is_canceled(self
):
185 is_canceled
= native_bt
.graph_is_canceled(self
._ptr
)
186 assert(is_canceled
>= 0)
187 return is_canceled
> 0
189 def __eq__(self
, other
):
190 if type(other
) is not type(self
):
193 return self
.addr
== other
.addr