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 collections
.abc
31 def _create_from_ptr(ptr
):
32 port_type
= native_bt
.port_get_type(ptr
)
34 if port_type
== native_bt
.PORT_TYPE_INPUT
:
36 elif port_type
== native_bt
.PORT_TYPE_OUTPUT
:
39 raise bt2
.Error('unknown port type: {}'.format(port_type
))
41 return cls
._create
_from
_ptr
(ptr
)
44 def _create_private_from_ptr(ptr
):
45 pub_ptr
= native_bt
.port_from_private_port(ptr
)
46 utils
._handle
_ptr
(pub_ptr
, 'cannot get port object from private port object')
47 port_type
= native_bt
.port_get_type(pub_ptr
)
48 assert(port_type
== native_bt
.PORT_TYPE_INPUT
or port_type
== native_bt
.PORT_TYPE_OUTPUT
)
50 if port_type
== native_bt
.PORT_TYPE_INPUT
:
51 cls
= _PrivateInputPort
52 elif port_type
== native_bt
.PORT_TYPE_OUTPUT
:
53 cls
= _PrivateOutputPort
55 obj
= cls
._create
_from
_ptr
(ptr
)
56 obj
._pub
_ptr
= pub_ptr
60 class _Port(object._Object
):
63 name
= native_bt
.port_get_name(ptr
)
64 assert(name
is not None)
69 status
= native_bt
.port_disconnect(ptr
)
72 raise bt2
.Error('cannot disconnect port')
76 return self
._name
(self
._ptr
)
80 comp_ptr
= native_bt
.port_get_component(self
._ptr
)
85 return bt2
.component
._create
_generic
_component
_from
_ptr
(comp_ptr
)
89 conn_ptr
= native_bt
.port_get_connection(self
._ptr
)
94 return bt2
.connection
._Connection
._create
_from
_ptr
(conn_ptr
)
97 def is_connected(self
):
98 return self
.connection
is not None
100 def disconnect(self
):
101 self
._disconnect
(self
._ptr
)
103 def __eq__(self
, other
):
104 if type(other
) is not type(self
):
107 return self
.addr
== other
.addr
110 class _InputPort(_Port
):
114 class _OutputPort(_Port
):
118 class _PrivatePort(object._PrivateObject
, _Port
):
121 return self
._name
(self
._pub
_ptr
)
125 comp_ptr
= native_bt
.private_port_get_private_component(self
._ptr
)
130 pub_comp_ptr
= native_bt
.component_from_private_component(comp_ptr
)
132 comp
= bt2
.component
._create
_generic
_component
_from
_ptr
(pub_comp_ptr
)
133 native_bt
.put(comp_ptr
)
137 def connection(self
):
138 conn_ptr
= native_bt
.private_port_get_private_connection(self
._ptr
)
143 return bt2
.connection
._create
_private
_from
_ptr
(conn_ptr
)
145 def remove_from_component(self
):
146 status
= native_bt
.private_port_remove_from_component(self
._ptr
)
149 raise bt2
.Error("cannot remove port from component")
151 def disconnect(self
):
152 self
._disconnect
(self
._pub
_ptr
)
155 class _PrivateInputPort(_PrivatePort
, _InputPort
):
159 class _PrivateOutputPort(_PrivatePort
, _OutputPort
):
This page took 0.032261 seconds and 4 git commands to generate.