Add configuration for python code formatter black
[babeltrace.git] / bindings / python / bt2 / bt2 / port.py
CommitLineData
811644b8
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4#
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:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
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
21# THE SOFTWARE.
22
23from bt2 import native_bt, object, utils
24import collections.abc
25import bt2.component
26import bt2.connection
5602ef81
SM
27import bt2.message_iterator
28import bt2.message
811644b8
PP
29import copy
30import bt2
31
32
33def _create_from_ptr(ptr):
34 port_type = native_bt.port_get_type(ptr)
35
36 if port_type == native_bt.PORT_TYPE_INPUT:
37 cls = _InputPort
38 elif port_type == native_bt.PORT_TYPE_OUTPUT:
39 cls = _OutputPort
40 else:
41 raise bt2.Error('unknown port type: {}'.format(port_type))
42
43 return cls._create_from_ptr(ptr)
44
45
46def _create_private_from_ptr(ptr):
6d137876 47 pub_ptr = native_bt.port_from_private(ptr)
811644b8
PP
48 utils._handle_ptr(pub_ptr, 'cannot get port object from private port object')
49 port_type = native_bt.port_get_type(pub_ptr)
50 assert(port_type == native_bt.PORT_TYPE_INPUT or port_type == native_bt.PORT_TYPE_OUTPUT)
51
52 if port_type == native_bt.PORT_TYPE_INPUT:
53 cls = _PrivateInputPort
54 elif port_type == native_bt.PORT_TYPE_OUTPUT:
55 cls = _PrivateOutputPort
56
57 obj = cls._create_from_ptr(ptr)
58 obj._pub_ptr = pub_ptr
59 return obj
60
61
78288f58 62class _Port(object._SharedObject):
811644b8
PP
63 @staticmethod
64 def _name(ptr):
65 name = native_bt.port_get_name(ptr)
66 assert(name is not None)
67 return name
68
69 @staticmethod
70 def _disconnect(ptr):
71 status = native_bt.port_disconnect(ptr)
72
73 if status < 0:
74 raise bt2.Error('cannot disconnect port')
75
76 @property
77 def name(self):
78 return self._name(self._ptr)
79
80 @property
81 def component(self):
82 comp_ptr = native_bt.port_get_component(self._ptr)
83
84 if comp_ptr is None:
85 return
86
87 return bt2.component._create_generic_component_from_ptr(comp_ptr)
88
89 @property
90 def connection(self):
91 conn_ptr = native_bt.port_get_connection(self._ptr)
92
93 if conn_ptr is None:
94 return
95
96 return bt2.connection._Connection._create_from_ptr(conn_ptr)
97
98 @property
99 def is_connected(self):
100 return self.connection is not None
101
102 def disconnect(self):
103 self._disconnect(self._ptr)
104
105 def __eq__(self, other):
106 if type(other) is not type(self):
107 return False
108
109 return self.addr == other.addr
110
111
112class _InputPort(_Port):
113 pass
114
115
116class _OutputPort(_Port):
5602ef81 117 def create_message_iterator(self, message_types=None,
dc43190b 118 colander_component_name=None):
5602ef81 119 msg_types = bt2.message._msg_types_from_msg_classes(message_types)
dc43190b
PP
120
121 if colander_component_name is not None:
122 utils._check_str(colander_component_name)
123
5602ef81 124 msg_iter_ptr = native_bt.py3_create_output_port_msg_iter(int(self._ptr),
dc43190b 125 colander_component_name,
5602ef81 126 msg_types)
dc43190b 127
5602ef81
SM
128 if msg_iter_ptr is None:
129 raise bt2.CreationError('cannot create output port message iterator')
dc43190b 130
5602ef81 131 return bt2.message_iterator._OutputPortMessageIterator._create_from_ptr(msg_iter_ptr)
811644b8
PP
132
133
78288f58 134class _PrivatePort(_Port):
811644b8
PP
135 @property
136 def name(self):
137 return self._name(self._pub_ptr)
138
139 @property
140 def component(self):
141 comp_ptr = native_bt.private_port_get_private_component(self._ptr)
142
143 if comp_ptr is None:
144 return
145
6d137876 146 pub_comp_ptr = native_bt.component_from_private(comp_ptr)
811644b8
PP
147 assert(pub_comp_ptr)
148 comp = bt2.component._create_generic_component_from_ptr(pub_comp_ptr)
149 native_bt.put(comp_ptr)
150 return comp
151
152 @property
153 def connection(self):
154 conn_ptr = native_bt.private_port_get_private_connection(self._ptr)
155
156 if conn_ptr is None:
157 return
158
159 return bt2.connection._create_private_from_ptr(conn_ptr)
160
161 def remove_from_component(self):
162 status = native_bt.private_port_remove_from_component(self._ptr)
163
164 if status < 0:
165 raise bt2.Error("cannot remove port from component")
166
167 def disconnect(self):
168 self._disconnect(self._pub_ptr)
169
170
171class _PrivateInputPort(_PrivatePort, _InputPort):
172 pass
173
174
175class _PrivateOutputPort(_PrivatePort, _OutputPort):
176 pass
This page took 0.035598 seconds and 4 git commands to generate.