bt2: Add remaining trace-ir `*Const` classes and adapt tests
[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
3fb99a22
PP
24from bt2 import clock_snapshot as bt2_clock_snapshot
25from bt2 import packet as bt2_packet
26from bt2 import stream as bt2_stream
27from bt2 import event as bt2_event
81447b5b
PP
28
29
30def _create_from_ptr(ptr):
5602ef81 31 msg_type = native_bt.message_get_type(ptr)
5602ef81 32 return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
81447b5b
PP
33
34
f0a42b33 35class _MessageConst(object._SharedObject):
2ae9f48c
SM
36 _get_ref = staticmethod(native_bt.message_get_ref)
37 _put_ref = staticmethod(native_bt.message_put_ref)
81447b5b 38
2e90378a
PP
39 @staticmethod
40 def _check_has_default_clock_class(clock_class):
41 if clock_class is None:
1153eccb 42 raise ValueError(
cfbd7cf3
FD
43 'cannot get default clock snapshot: stream class has no default clock class'
44 )
2e90378a 45
81447b5b 46
f0a42b33
FD
47class _Message(_MessageConst):
48 pass
49
50
9ec609ec
SM
51class _MessageWithDefaultClockSnapshot:
52 def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr):
9ec609ec 53 snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr)
811644b8 54
eddea575 55 return bt2_clock_snapshot._ClockSnapshotConst._create_from_ptr_and_get_ref(
cfbd7cf3
FD
56 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
57 )
811644b8 58
9ec609ec 59
f0a42b33
FD
60class _EventMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot):
61 _borrow_default_clock_snapshot = staticmethod(
cfbd7cf3
FD
62 native_bt.message_event_borrow_default_clock_snapshot_const
63 )
f0a42b33
FD
64 _borrow_event = staticmethod(native_bt.message_event_borrow_event_const)
65 _event_pycls = property(lambda _: bt2_event._EventConst)
9ec609ec 66
9ec609ec
SM
67 @property
68 def default_clock_snapshot(self):
26fc5aed 69 self._check_has_default_clock_class(self.event.stream.cls.default_clock_class)
f0a42b33 70 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot)
811644b8 71
2ae9f48c
SM
72 @property
73 def event(self):
f0a42b33 74 event_ptr = self._borrow_event(self._ptr)
2ae9f48c 75 assert event_ptr is not None
f0a42b33 76 return self._event_pycls._create_from_ptr_and_get_ref(
cfbd7cf3
FD
77 event_ptr, self._ptr, self._get_ref, self._put_ref
78 )
81447b5b 79
81447b5b 80
f0a42b33
FD
81class _EventMessage(_EventMessageConst, _Message):
82 _borrow_event = staticmethod(native_bt.message_event_borrow_event)
83 _stream_pycls = property(lambda _: bt2_stream._Stream)
84 _event_pycls = property(lambda _: bt2_event._Event)
85
86
87class _PacketMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot):
88 _packet_pycls = bt2_packet._PacketConst
89
81447b5b 90 @property
9ec609ec 91 def default_clock_snapshot(self):
e8ac1aae 92 self._check_has_default_clock_class(self.packet.stream.cls.default_clock_class)
9ec609ec 93 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
81447b5b 94
81447b5b
PP
95 @property
96 def packet(self):
f0a42b33 97 packet_ptr = self._borrow_packet(self._ptr)
9ec609ec 98 assert packet_ptr is not None
f0a42b33 99 return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr)
811644b8 100
811644b8 101
f0a42b33
FD
102class _PacketMessage(_PacketMessageConst, _Message):
103 _packet_pycls = bt2_packet._Packet
104
105
106class _PacketBeginningMessageConst(_PacketMessageConst):
107 _borrow_packet = staticmethod(
108 native_bt.message_packet_beginning_borrow_packet_const
109 )
cfbd7cf3
FD
110 _borrow_default_clock_snapshot_ptr = staticmethod(
111 native_bt.message_packet_beginning_borrow_default_clock_snapshot_const
112 )
811644b8 113
811644b8 114
f0a42b33
FD
115class _PacketBeginningMessage(_PacketMessage):
116 _borrow_packet = staticmethod(native_bt.message_packet_beginning_borrow_packet)
117
118
119class _PacketEndMessageConst(_PacketMessageConst):
120 _borrow_packet = staticmethod(native_bt.message_packet_end_borrow_packet_const)
cfbd7cf3
FD
121 _borrow_default_clock_snapshot_ptr = staticmethod(
122 native_bt.message_packet_end_borrow_default_clock_snapshot_const
123 )
811644b8 124
81447b5b 125
f0a42b33
FD
126class _PacketEndMessage(_PacketMessage):
127 _borrow_packet = staticmethod(native_bt.message_packet_end_borrow_packet)
128
129
130class _StreamMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot):
131 _stream_pycls = property(lambda _: bt2_stream._StreamConst)
132
81447b5b
PP
133 @property
134 def stream(self):
9ec609ec
SM
135 stream_ptr = self._borrow_stream_ptr(self._ptr)
136 assert stream_ptr
f0a42b33 137 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
811644b8 138
81447b5b 139 @property
9ec609ec 140 def default_clock_snapshot(self):
188edac1
SM
141 self._check_has_default_clock_class(self.stream.cls.default_clock_class)
142
9ec609ec 143 status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr)
81447b5b 144
188edac1 145 if status == native_bt.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN:
3fb99a22 146 return bt2_clock_snapshot._UnknownClockSnapshot()
81447b5b 147
eddea575 148 return bt2_clock_snapshot._ClockSnapshotConst._create_from_ptr_and_get_ref(
cfbd7cf3
FD
149 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
150 )
811644b8 151
f0a42b33
FD
152
153class _StreamMessage(_StreamMessageConst, _Message):
188edac1
SM
154 def _default_clock_snapshot(self, raw_value):
155 utils._check_uint64(raw_value)
156 self._set_default_clock_snapshot(self._ptr, raw_value)
811644b8 157
f0a42b33
FD
158 _default_clock_snapshot = property(
159 fget=_StreamMessageConst.default_clock_snapshot.fget,
160 fset=_default_clock_snapshot,
161 )
162 _stream_pycls = property(lambda _: bt2_stream._Stream)
811644b8 163
811644b8 164
f0a42b33
FD
165class _StreamBeginningMessageConst(_StreamMessageConst):
166 _borrow_stream_ptr = staticmethod(
167 native_bt.message_stream_beginning_borrow_stream_const
168 )
cfbd7cf3
FD
169 _borrow_default_clock_snapshot_ptr = staticmethod(
170 native_bt.message_stream_beginning_borrow_default_clock_snapshot_const
171 )
f0a42b33
FD
172
173
174class _StreamBeginningMessage(_StreamMessage):
175 _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream)
cfbd7cf3
FD
176 _set_default_clock_snapshot = staticmethod(
177 native_bt.message_stream_beginning_set_default_clock_snapshot
178 )
811644b8 179
188edac1 180
f0a42b33
FD
181class _StreamEndMessageConst(_StreamMessageConst):
182 _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream_const)
cfbd7cf3
FD
183 _borrow_default_clock_snapshot_ptr = staticmethod(
184 native_bt.message_stream_end_borrow_default_clock_snapshot_const
185 )
f0a42b33
FD
186
187
188class _StreamEndMessage(_StreamMessage):
189 _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream)
cfbd7cf3
FD
190 _set_default_clock_snapshot = staticmethod(
191 native_bt.message_stream_end_set_default_clock_snapshot
192 )
811644b8
PP
193
194
f0a42b33
FD
195class _MessageIteratorInactivityMessageConst(
196 _MessageConst, _MessageWithDefaultClockSnapshot
197):
cfbd7cf3
FD
198 _borrow_default_clock_snapshot_ptr = staticmethod(
199 native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
200 )
811644b8
PP
201
202 @property
9ec609ec 203 def default_clock_snapshot(self):
2e90378a
PP
204 # This kind of message always has a default clock class: no
205 # need to call self._check_has_default_clock_class() here.
9ec609ec
SM
206 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
207
811644b8 208
f0a42b33
FD
209class _MessageIteratorInactivityMessage(
210 _MessageIteratorInactivityMessageConst, _Message
211):
212 pass
213
214
215class _DiscardedMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot):
216 _stream_pycls = property(lambda _: bt2_stream._StreamConst)
217
811644b8
PP
218 @property
219 def stream(self):
9ec609ec
SM
220 stream_ptr = self._borrow_stream_ptr(self._ptr)
221 assert stream_ptr
f0a42b33 222 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
811644b8 223
811644b8
PP
224 @property
225 def count(self):
9ec609ec
SM
226 avail, count = self._get_count(self._ptr)
227 if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE:
228 return count
811644b8 229
2e90378a
PP
230 def _check_has_default_clock_snapshots(self):
231 if not self._has_default_clock_snapshots:
1153eccb 232 raise ValueError(
cfbd7cf3
FD
233 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
234 )
2e90378a 235
811644b8 236 @property
9ec609ec 237 def beginning_default_clock_snapshot(self):
2e90378a 238 self._check_has_default_clock_snapshots()
cfbd7cf3
FD
239 return self._get_default_clock_snapshot(
240 self._borrow_beginning_clock_snapshot_ptr
241 )
811644b8 242
9ec609ec
SM
243 @property
244 def end_default_clock_snapshot(self):
2e90378a 245 self._check_has_default_clock_snapshots()
9ec609ec 246 return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr)
811644b8 247
811644b8 248
f0a42b33
FD
249class _DiscardedMessage(_DiscardedMessageConst, _Message):
250 _stream_pycls = property(lambda _: bt2_stream._Stream)
251
252 def _set_count(self, count):
253 utils._check_uint64(count)
254 self._set_count(self._ptr, count)
255
256 _count = property(fget=_DiscardedMessageConst.count.fget, fset=_set_count)
257
258
259class _DiscardedEventsMessageConst(_DiscardedMessageConst):
cfbd7cf3
FD
260 _borrow_stream_ptr = staticmethod(
261 native_bt.message_discarded_events_borrow_stream_const
262 )
9ec609ec 263 _get_count = staticmethod(native_bt.message_discarded_events_get_count)
cfbd7cf3
FD
264 _borrow_beginning_clock_snapshot_ptr = staticmethod(
265 native_bt.message_discarded_events_borrow_beginning_default_clock_snapshot_const
266 )
267 _borrow_end_clock_snapshot_ptr = staticmethod(
268 native_bt.message_discarded_events_borrow_end_default_clock_snapshot_const
269 )
811644b8 270
2e90378a
PP
271 @property
272 def _has_default_clock_snapshots(self):
e8ac1aae 273 return self.stream.cls.discarded_events_have_default_clock_snapshots
2e90378a 274
811644b8 275
f0a42b33
FD
276class _DiscardedEventsMessage(_DiscardedMessage):
277 _borrow_stream_ptr = staticmethod(native_bt.message_discarded_events_borrow_stream)
278 _set_count = staticmethod(native_bt.message_discarded_events_set_count)
279
280
281class _DiscardedPacketsMessageConst(_DiscardedMessageConst):
cfbd7cf3
FD
282 _borrow_stream_ptr = staticmethod(
283 native_bt.message_discarded_packets_borrow_stream_const
284 )
9ec609ec 285 _get_count = staticmethod(native_bt.message_discarded_packets_get_count)
cfbd7cf3
FD
286 _borrow_beginning_clock_snapshot_ptr = staticmethod(
287 native_bt.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
288 )
289 _borrow_end_clock_snapshot_ptr = staticmethod(
290 native_bt.message_discarded_packets_borrow_end_default_clock_snapshot_const
291 )
81447b5b 292
2e90378a
PP
293 @property
294 def _has_default_clock_snapshots(self):
e8ac1aae 295 return self.stream.cls.discarded_packets_have_default_clock_snapshots
2e90378a 296
81447b5b 297
f0a42b33
FD
298class _DiscardedPacketsMessage(_DiscardedPacketsMessageConst, _DiscardedMessage):
299 _borrow_stream_ptr = staticmethod(native_bt.message_discarded_packets_borrow_stream)
300 _set_count = staticmethod(native_bt.message_discarded_packets_set_count)
301
302
5602ef81 303_MESSAGE_TYPE_TO_CLS = {
2ae9f48c 304 native_bt.MESSAGE_TYPE_EVENT: _EventMessage,
9ec609ec 305 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage,
2ae9f48c 306 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage,
5f25509b 307 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage,
9ec609ec
SM
308 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage,
309 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage,
5602ef81 310 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage,
9ec609ec 311 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage,
81447b5b 312}
f0a42b33
FD
313
314_MESSAGE_TYPE_TO_CLS = {
315 native_bt.MESSAGE_TYPE_EVENT: _EventMessageConst,
316 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessageConst,
317 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessageConst,
318 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessageConst,
319 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessageConst,
320 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessageConst,
321 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessageConst,
322 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessageConst,
323}
This page took 0.062602 seconds and 4 git commands to generate.