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