Apply black code formatter on all Python code
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message.py
CommitLineData
81447b5b
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
f192fc47 24import bt2.clock_snapshot
81447b5b
PP
25import bt2.packet
26import bt2.stream
27import bt2.event
28import bt2
29
30
31def _create_from_ptr(ptr):
fa4c33e3 32 msg_type = native_bt.message_get_type(ptr)
81447b5b 33
fa4c33e3
SM
34 if msg_type not in _MESSAGE_TYPE_TO_CLS:
35 raise bt2.Error('unknown message type: {}'.format(msg_type))
81447b5b 36
fa4c33e3 37 return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
81447b5b
PP
38
39
c3044a97 40class _Message(object._SharedObject):
27d97a3f
SM
41 _get_ref = staticmethod(native_bt.message_get_ref)
42 _put_ref = staticmethod(native_bt.message_put_ref)
81447b5b 43
77037b2b
PP
44 @staticmethod
45 def _check_has_default_clock_class(clock_class):
46 if clock_class is None:
61d96b89
FD
47 raise bt2.NonexistentClockSnapshot(
48 'cannot get default clock snapshot: stream class has no default clock class'
49 )
77037b2b 50
81447b5b 51
0010c8b0
SM
52class _MessageWithDefaultClockSnapshot:
53 def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr):
0010c8b0 54 snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr)
f6a5e476 55
0010c8b0 56 return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
61d96b89
FD
57 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
58 )
f6a5e476 59
0010c8b0
SM
60
61class _EventMessage(_Message, _MessageWithDefaultClockSnapshot):
61d96b89
FD
62 _borrow_default_clock_snapshot_ptr = staticmethod(
63 native_bt.message_event_borrow_default_clock_snapshot_const
64 )
0010c8b0 65
0010c8b0
SM
66 @property
67 def default_clock_snapshot(self):
37a93d41 68 self._check_has_default_clock_class(self.event.stream.cls.default_clock_class)
0010c8b0 69 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
f6a5e476 70
27d97a3f
SM
71 @property
72 def event(self):
73 event_ptr = native_bt.message_event_borrow_event(self._ptr)
74 assert event_ptr is not None
75 return bt2.event._Event._create_from_ptr_and_get_ref(
61d96b89
FD
76 event_ptr, self._ptr, self._get_ref, self._put_ref
77 )
81447b5b 78
81447b5b 79
0010c8b0 80class _PacketMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b 81 @property
0010c8b0 82 def default_clock_snapshot(self):
c88be1c8 83 self._check_has_default_clock_class(self.packet.stream.cls.default_clock_class)
0010c8b0 84 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
81447b5b 85
81447b5b
PP
86 @property
87 def packet(self):
0010c8b0
SM
88 packet_ptr = self._borrow_packet_ptr(self._ptr)
89 assert packet_ptr is not None
90 return bt2.packet._Packet._create_from_ptr_and_get_ref(packet_ptr)
f6a5e476 91
f6a5e476 92
0010c8b0
SM
93class _PacketBeginningMessage(_PacketMessage):
94 _borrow_packet_ptr = staticmethod(native_bt.message_packet_beginning_borrow_packet)
61d96b89
FD
95 _borrow_default_clock_snapshot_ptr = staticmethod(
96 native_bt.message_packet_beginning_borrow_default_clock_snapshot_const
97 )
f6a5e476 98
f6a5e476 99
0010c8b0
SM
100class _PacketEndMessage(_PacketMessage):
101 _borrow_packet_ptr = staticmethod(native_bt.message_packet_end_borrow_packet)
61d96b89
FD
102 _borrow_default_clock_snapshot_ptr = staticmethod(
103 native_bt.message_packet_end_borrow_default_clock_snapshot_const
104 )
f6a5e476 105
81447b5b 106
b7cbc799 107class _StreamMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b
PP
108 @property
109 def stream(self):
0010c8b0
SM
110 stream_ptr = self._borrow_stream_ptr(self._ptr)
111 assert stream_ptr
112 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
f6a5e476 113
81447b5b 114 @property
0010c8b0 115 def default_clock_snapshot(self):
b7cbc799
SM
116 self._check_has_default_clock_class(self.stream.cls.default_clock_class)
117
0010c8b0 118 status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr)
81447b5b 119
b7cbc799 120 if status == native_bt.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN:
dcd94213 121 return bt2.clock_snapshot._UnknownClockSnapshot()
81447b5b 122
b7cbc799 123 return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
61d96b89
FD
124 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
125 )
f6a5e476 126
b7cbc799
SM
127 def _default_clock_snapshot(self, raw_value):
128 utils._check_uint64(raw_value)
129 self._set_default_clock_snapshot(self._ptr, raw_value)
f6a5e476 130
b7cbc799 131 _default_clock_snapshot = property(fset=_default_clock_snapshot)
f6a5e476 132
f6a5e476 133
b7cbc799
SM
134class _StreamBeginningMessage(_StreamMessage):
135 _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream)
61d96b89
FD
136 _borrow_default_clock_snapshot_ptr = staticmethod(
137 native_bt.message_stream_beginning_borrow_default_clock_snapshot_const
138 )
139 _set_default_clock_snapshot = staticmethod(
140 native_bt.message_stream_beginning_set_default_clock_snapshot
141 )
f6a5e476 142
b7cbc799
SM
143
144class _StreamEndMessage(_StreamMessage):
145 _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream)
61d96b89
FD
146 _borrow_default_clock_snapshot_ptr = staticmethod(
147 native_bt.message_stream_end_borrow_default_clock_snapshot_const
148 )
149 _set_default_clock_snapshot = staticmethod(
150 native_bt.message_stream_end_set_default_clock_snapshot
151 )
f6a5e476
PP
152
153
0010c8b0 154class _MessageIteratorInactivityMessage(_Message, _MessageWithDefaultClockSnapshot):
61d96b89
FD
155 _borrow_default_clock_snapshot_ptr = staticmethod(
156 native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
157 )
f6a5e476
PP
158
159 @property
0010c8b0 160 def default_clock_snapshot(self):
77037b2b
PP
161 # This kind of message always has a default clock class: no
162 # need to call self._check_has_default_clock_class() here.
0010c8b0
SM
163 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
164
f6a5e476 165
0010c8b0 166class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot):
f6a5e476
PP
167 @property
168 def stream(self):
0010c8b0
SM
169 stream_ptr = self._borrow_stream_ptr(self._ptr)
170 assert stream_ptr
171 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
f6a5e476 172
f6a5e476
PP
173 @property
174 def count(self):
0010c8b0
SM
175 avail, count = self._get_count(self._ptr)
176 if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE:
177 return count
f6a5e476 178
0010c8b0
SM
179 def _set_count(self, count):
180 utils._check_uint64(count)
181 self._set_count(self._ptr, count)
182
183 _count = property(fset=_set_count)
f6a5e476 184
77037b2b
PP
185 def _check_has_default_clock_snapshots(self):
186 if not self._has_default_clock_snapshots:
61d96b89
FD
187 raise bt2.NonexistentClockSnapshot(
188 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
189 )
77037b2b 190
f6a5e476 191 @property
0010c8b0 192 def beginning_default_clock_snapshot(self):
77037b2b 193 self._check_has_default_clock_snapshots()
61d96b89
FD
194 return self._get_default_clock_snapshot(
195 self._borrow_beginning_clock_snapshot_ptr
196 )
f6a5e476 197
0010c8b0
SM
198 @property
199 def end_default_clock_snapshot(self):
77037b2b 200 self._check_has_default_clock_snapshots()
0010c8b0 201 return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr)
f6a5e476 202
f6a5e476 203
0010c8b0 204class _DiscardedEventsMessage(_DiscardedMessage):
61d96b89
FD
205 _borrow_stream_ptr = staticmethod(
206 native_bt.message_discarded_events_borrow_stream_const
207 )
0010c8b0
SM
208 _get_count = staticmethod(native_bt.message_discarded_events_get_count)
209 _set_count = staticmethod(native_bt.message_discarded_events_set_count)
61d96b89
FD
210 _borrow_beginning_clock_snapshot_ptr = staticmethod(
211 native_bt.message_discarded_events_borrow_beginning_default_clock_snapshot_const
212 )
213 _borrow_end_clock_snapshot_ptr = staticmethod(
214 native_bt.message_discarded_events_borrow_end_default_clock_snapshot_const
215 )
f6a5e476 216
77037b2b
PP
217 @property
218 def _has_default_clock_snapshots(self):
c88be1c8 219 return self.stream.cls.discarded_events_have_default_clock_snapshots
77037b2b 220
f6a5e476 221
0010c8b0 222class _DiscardedPacketsMessage(_DiscardedMessage):
61d96b89
FD
223 _borrow_stream_ptr = staticmethod(
224 native_bt.message_discarded_packets_borrow_stream_const
225 )
0010c8b0
SM
226 _get_count = staticmethod(native_bt.message_discarded_packets_get_count)
227 _set_count = staticmethod(native_bt.message_discarded_packets_set_count)
61d96b89
FD
228 _borrow_beginning_clock_snapshot_ptr = staticmethod(
229 native_bt.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
230 )
231 _borrow_end_clock_snapshot_ptr = staticmethod(
232 native_bt.message_discarded_packets_borrow_end_default_clock_snapshot_const
233 )
81447b5b 234
77037b2b
PP
235 @property
236 def _has_default_clock_snapshots(self):
c88be1c8 237 return self.stream.cls.discarded_packets_have_default_clock_snapshots
77037b2b 238
81447b5b 239
fa4c33e3 240_MESSAGE_TYPE_TO_CLS = {
27d97a3f 241 native_bt.MESSAGE_TYPE_EVENT: _EventMessage,
0010c8b0 242 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage,
27d97a3f 243 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage,
871a292a 244 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage,
0010c8b0
SM
245 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage,
246 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage,
fa4c33e3 247 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage,
0010c8b0 248 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage,
81447b5b 249}
This page took 0.082997 seconds and 4 git commands to generate.