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
_const
_ptr
_and
_get
_ref
(
37 component_ptr
, component_type
39 port
= bt2_port
._create
_from
_const
_ptr
_and
_get
_ref
(port_ptr
, port_type
)
40 user_listener(component
, port
)
43 class Graph(object._SharedObject
):
44 _get_ref
= staticmethod(native_bt
.graph_get_ref
)
45 _put_ref
= staticmethod(native_bt
.graph_put_ref
)
47 def __init__(self
, mip_version
=0):
48 utils
._check
_uint
64(mip_version
)
50 if mip_version
> bt2
.get_maximal_mip_version():
51 raise ValueError('unknown MIP version {}'.format(mip_version
))
53 ptr
= native_bt
.graph_create(mip_version
)
56 raise bt2
._MemoryError('cannot create graph object')
60 # list of listener partials to keep a reference as long as
62 self
._listener
_partials
= []
70 logging_level
=bt2_logging
.LoggingLevel
.NONE
,
72 if isinstance(component_class
, bt2_component
._SourceComponentClassConst
):
73 cc_ptr
= component_class
._ptr
74 add_fn
= native_bt
.bt2_graph_add_source_component
75 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
76 elif isinstance(component_class
, bt2_component
._FilterComponentClassConst
):
77 cc_ptr
= component_class
._ptr
78 add_fn
= native_bt
.bt2_graph_add_filter_component
79 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
80 elif isinstance(component_class
, bt2_component
._SinkComponentClassConst
):
81 cc_ptr
= component_class
._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
._UserSourceComponent
):
85 cc_ptr
= component_class
._bt
_cc
_ptr
86 add_fn
= native_bt
.bt2_graph_add_source_component
87 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SOURCE
88 elif issubclass(component_class
, bt2_component
._UserSinkComponent
):
89 cc_ptr
= component_class
._bt
_cc
_ptr
90 add_fn
= native_bt
.bt2_graph_add_sink_component
91 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_SINK
92 elif issubclass(component_class
, bt2_component
._UserFilterComponent
):
93 cc_ptr
= component_class
._bt
_cc
_ptr
94 add_fn
= native_bt
.bt2_graph_add_filter_component
95 cc_type
= native_bt
.COMPONENT_CLASS_TYPE_FILTER
98 "'{}' is not a component class".format(
99 component_class
.__class
__.__name
__
103 utils
._check
_str
(name
)
104 utils
._check
_log
_level
(logging_level
)
105 base_cc_ptr
= component_class
._bt
_component
_class
_ptr
()
107 if obj
is not None and not native_bt
.bt2_is_python_component_class(base_cc_ptr
):
108 raise ValueError('cannot pass a Python object to a non-Python component')
110 if params
is not None and not isinstance(params
, (dict, bt2
.MapValue
)):
111 raise TypeError("'params' parameter is not a 'dict' or a 'bt2.MapValue'.")
113 params
= bt2
.create_value(params
)
115 params_ptr
= params
._ptr
if params
is not None else None
117 status
, comp_ptr
= add_fn(
118 self
._ptr
, cc_ptr
, name
, params_ptr
, obj
, logging_level
120 utils
._handle
_func
_status
(status
, 'cannot add component to graph')
122 return bt2_component
._create
_component
_from
_const
_ptr
_and
_get
_ref
(
126 def connect_ports(self
, upstream_port
, downstream_port
):
127 utils
._check
_type
(upstream_port
, bt2_port
._OutputPortConst
)
128 utils
._check
_type
(downstream_port
, bt2_port
._InputPortConst
)
129 status
, conn_ptr
= native_bt
.graph_connect_ports(
130 self
._ptr
, upstream_port
._ptr
, downstream_port
._ptr
132 utils
._handle
_func
_status
(status
, 'cannot connect component ports within graph')
134 return bt2_connection
._ConnectionConst
._create
_from
_ptr
_and
_get
_ref
(conn_ptr
)
136 def add_port_added_listener(self
, listener
):
137 if not callable(listener
):
138 raise TypeError("'listener' parameter is not callable")
140 fn
= native_bt
.bt2_graph_add_port_added_listener
141 listener_from_native
= functools
.partial(
142 _graph_port_added_listener_from_native
, listener
145 listener_ids
= fn(self
._ptr
, listener_from_native
)
146 if listener_ids
is None:
147 raise bt2
._Error('cannot add listener to graph object')
149 # keep the partial's reference
150 self
._listener
_partials
.append(listener_from_native
)
153 status
= native_bt
.graph_run_once(self
._ptr
)
154 utils
._handle
_func
_status
(status
, 'graph object could not run once')
157 status
= native_bt
.graph_run(self
._ptr
)
158 utils
._handle
_func
_status
(status
, 'graph object stopped running')
160 def add_interrupter(self
, interrupter
):
161 utils
._check
_type
(interrupter
, bt2_interrupter
.Interrupter
)
162 native_bt
.graph_add_interrupter(self
._ptr
, interrupter
._ptr
)
165 def default_interrupter(self
):
166 ptr
= native_bt
.graph_borrow_default_interrupter(self
._ptr
)
167 return bt2_interrupter
.Interrupter
._create
_from
_ptr
_and
_get
_ref
(ptr
)