Remove `skip-string-normalization` in Python formatter config
[babeltrace.git] / src / bindings / python / bt2 / bt2 / message_iterator.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
81447b5b
PP
2#
3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b
PP
4
5from bt2 import native_bt, object, utils
3fb99a22 6from bt2 import message as bt2_message
81447b5b 7import collections.abc
3fb99a22
PP
8from bt2 import stream as bt2_stream
9from bt2 import event_class as bt2_event_class
10from bt2 import packet as bt2_packet
11from bt2 import port as bt2_port
12from bt2 import clock_class as bt2_clock_class
81447b5b
PP
13import bt2
14
15
5602ef81 16class _MessageIterator(collections.abc.Iterator):
81447b5b 17 def __next__(self):
811644b8 18 raise NotImplementedError
81447b5b
PP
19
20
1975af3d 21class _UserComponentInputPortMessageIterator(object._SharedObject, _MessageIterator):
9a2c8b8e
PP
22 _get_ref = staticmethod(native_bt.message_iterator_get_ref)
23 _put_ref = staticmethod(native_bt.message_iterator_put_ref)
1975af3d 24
2ae9f48c 25 def __init__(self, ptr):
cfbd7cf3
FD
26 self._current_msgs = []
27 self._at = 0
28 super().__init__(ptr)
811644b8
PP
29
30 def __next__(self):
2ae9f48c 31 if len(self._current_msgs) == self._at:
1975af3d
SM
32 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
33 self._ptr
34 )
cfbd7cf3 35 utils._handle_func_status(
f5567ea8 36 status, "unexpected error: cannot advance the message iterator"
cfbd7cf3 37 )
2ae9f48c
SM
38 self._current_msgs = msgs
39 self._at = 0
40
41 msg_ptr = self._current_msgs[self._at]
42 self._at += 1
43
3fb99a22 44 return bt2_message._create_from_ptr(msg_ptr)
81447b5b 45
f00b8d40 46 def can_seek_beginning(self):
9a2c8b8e 47 (status, res) = native_bt.message_iterator_can_seek_beginning(self._ptr)
f2fb1b32
SM
48 utils._handle_func_status(
49 status,
f5567ea8 50 "cannot check whether or not message iterator can seek its beginning",
f2fb1b32 51 )
f00b8d40
SM
52 return res != 0
53
54 def seek_beginning(self):
b6909b73 55 # Forget about buffered messages, they won't be valid after seeking.
f00b8d40
SM
56 self._current_msgs.clear()
57 self._at = 0
58
9a2c8b8e 59 status = native_bt.message_iterator_seek_beginning(self._ptr)
f5567ea8 60 utils._handle_func_status(status, "cannot seek message iterator beginning")
f00b8d40 61
c182d7dd
SM
62 def can_seek_ns_from_origin(self, ns_from_origin):
63 utils._check_int64(ns_from_origin)
9a2c8b8e 64 (status, res) = native_bt.message_iterator_can_seek_ns_from_origin(
c182d7dd
SM
65 self._ptr, ns_from_origin
66 )
67 utils._handle_func_status(
68 status,
f5567ea8 69 "cannot check whether or not message iterator can seek given ns from origin",
c182d7dd
SM
70 )
71 return res != 0
72
73 def seek_ns_from_origin(self, ns_from_origin):
74 utils._check_int64(ns_from_origin)
75
76 # Forget about buffered messages, they won't be valid after seeking.
77 self._current_msgs.clear()
78 self._at = 0
79
9a2c8b8e 80 status = native_bt.message_iterator_seek_ns_from_origin(
c182d7dd
SM
81 self._ptr, ns_from_origin
82 )
83 utils._handle_func_status(
f5567ea8 84 status, "message iterator cannot seek given ns from origin"
c182d7dd
SM
85 )
86
8d8b141d
SM
87 @property
88 def can_seek_forward(self):
9a2c8b8e 89 return native_bt.message_iterator_can_seek_forward(self._ptr)
8d8b141d
SM
90
91
92class _MessageIteratorConfiguration:
93 def __init__(self, ptr):
94 self._ptr = ptr
95
96 def can_seek_forward(self, value):
97 utils._check_bool(value)
98 native_bt.self_message_iterator_configuration_set_can_seek_forward(
99 self._ptr, value
100 )
101
102 can_seek_forward = property(fset=can_seek_forward)
103
811644b8 104
c5f330cd
SM
105# This is extended by the user to implement component classes in Python. It
106# is created for a given output port when an input port message iterator is
7a2d4e2d 107# created on the input port on the other side of the connection.
c5f330cd
SM
108#
109# Its purpose is to feed the messages that should go out through this output
110# port.
5602ef81 111class _UserMessageIterator(_MessageIterator):
81447b5b 112 def __new__(cls, ptr):
811644b8 113 # User iterator objects are always created by the native side,
81447b5b
PP
114 # that is, never instantiated directly by Python code.
115 #
811644b8
PP
116 # The native code calls this, then manually calls
117 # self.__init__() without the `ptr` argument. The user has
118 # access to self.component during this call, thanks to this
85906b6b 119 # self._bt_ptr argument being set.
81447b5b 120 #
85906b6b 121 # self._bt_ptr is NOT owned by this object here, so there's nothing
81447b5b
PP
122 # to do in __del__().
123 self = super().__new__(cls)
85906b6b 124 self._bt_ptr = ptr
81447b5b
PP
125 return self
126
8d8b141d 127 def _bt_init_from_native(self, config_ptr, self_output_port_ptr):
3fb99a22 128 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
cfbd7cf3
FD
129 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
130 )
8d8b141d
SM
131 config = _MessageIteratorConfiguration(config_ptr)
132 self.__init__(config, self_output_port)
c5f330cd 133
8d8b141d 134 def __init__(self, config, self_output_port):
81447b5b
PP
135 pass
136
137 @property
811644b8 138 def _component(self):
85906b6b 139 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
81447b5b 140
14503fb1
SM
141 @property
142 def _port(self):
143 port_ptr = native_bt.self_message_iterator_borrow_port(self._bt_ptr)
144 assert port_ptr is not None
145 return bt2_port._create_self_from_ptr_and_get_ref(
146 port_ptr, native_bt.PORT_TYPE_OUTPUT
147 )
148
81447b5b
PP
149 @property
150 def addr(self):
85906b6b 151 return int(self._bt_ptr)
81447b5b 152
9b4f9b42
PP
153 @property
154 def _is_interrupted(self):
155 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
156
6a91742b 157 def _user_finalize(self):
81447b5b
PP
158 pass
159
811644b8
PP
160 def __next__(self):
161 raise bt2.Stop
162
85906b6b 163 def _bt_next_from_native(self):
811644b8
PP
164 # this can raise anything: it's catched by the native part
165 try:
5602ef81 166 msg = next(self)
811644b8
PP
167 except StopIteration:
168 raise bt2.Stop
4c4935bf 169 except Exception:
811644b8
PP
170 raise
171
f0a42b33 172 utils._check_type(msg, bt2_message._MessageConst)
81447b5b 173
d79a8353
SM
174 # The reference we return will be given to the message array.
175 # However, the `msg` Python object may stay alive, if the user has kept
176 # a reference to it. Acquire a new reference to account for that.
177 msg._get_ref(msg._ptr)
178 return int(msg._ptr)
2ae9f48c 179
85906b6b 180 def _bt_can_seek_beginning_from_native(self):
f00b8d40
SM
181 # Here, we mimic the behavior of the C API:
182 #
14cfc8ce 183 # - If the iterator has a _user_can_seek_beginning method,
6a91742b 184 # read it and use that result.
5a096c63 185 # - Otherwise, the presence or absence of a `_user_seek_beginning`
f00b8d40 186 # method indicates whether the iterator can seek beginning.
f5567ea8 187 if hasattr(self, "_user_can_seek_beginning"):
14cfc8ce 188 can_seek_beginning = self._user_can_seek_beginning()
f00b8d40
SM
189 utils._check_bool(can_seek_beginning)
190 return can_seek_beginning
191 else:
f5567ea8 192 return hasattr(self, "_user_seek_beginning")
f00b8d40 193
85906b6b 194 def _bt_seek_beginning_from_native(self):
6a91742b 195 self._user_seek_beginning()
f00b8d40 196
c182d7dd 197 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
c0e46a7c
SM
198 # Return whether the iterator can seek ns from origin using the
199 # user-implemented seek_ns_from_origin method. We mimic the behavior
200 # of the C API:
c182d7dd
SM
201 #
202 # - If the iterator has a _user_can_seek_ns_from_origin method,
203 # call it and use its return value.
204 # - Otherwise, if there is a `_user_seek_ns_from_origin` method,
205 # we presume it's possible.
c0e46a7c 206
f5567ea8 207 if hasattr(self, "_user_can_seek_ns_from_origin"):
c182d7dd
SM
208 can_seek_ns_from_origin = self._user_can_seek_ns_from_origin(ns_from_origin)
209 utils._check_bool(can_seek_ns_from_origin)
210 return can_seek_ns_from_origin
c182d7dd 211 else:
f5567ea8 212 return hasattr(self, "_user_seek_ns_from_origin")
c182d7dd
SM
213
214 def _bt_seek_ns_from_origin_from_native(self, ns_from_origin):
215 self._user_seek_ns_from_origin(ns_from_origin)
216
9a2c8b8e 217 def _create_message_iterator(self, input_port):
3fb99a22 218 utils._check_type(input_port, bt2_port._UserComponentInputPort)
ca02df0a 219
415d43a1 220 if not input_port.is_connected:
f5567ea8 221 raise ValueError("input port is not connected")
415d43a1 222
75882e97
FD
223 (
224 status,
225 msg_iter_ptr,
9a2c8b8e 226 ) = native_bt.bt2_message_iterator_create_from_message_iterator(
ca02df0a
PP
227 self._bt_ptr, input_port._ptr
228 )
f5567ea8 229 utils._handle_func_status(status, "cannot create message iterator object")
e803df70 230 assert msg_iter_ptr is not None
ca02df0a
PP
231
232 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
233
36d9460d 234 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
3fb99a22 235 utils._check_type(event_class, bt2_event_class._EventClass)
26fc5aed
PP
236
237 if event_class.stream_class.supports_packets:
3fb99a22 238 utils._check_type(parent, bt2_packet._Packet)
26fc5aed 239 else:
3fb99a22 240 utils._check_type(parent, bt2_stream._Stream)
2ae9f48c
SM
241
242 if default_clock_snapshot is not None:
c6af194f 243 if event_class.stream_class.default_clock_class is None:
cfbd7cf3 244 raise ValueError(
f5567ea8 245 "event messages in this stream must not have a default clock snapshot"
cfbd7cf3 246 )
c6af194f 247
2ae9f48c 248 utils._check_uint64(default_clock_snapshot)
26fc5aed
PP
249
250 if event_class.stream_class.supports_packets:
251 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
cfbd7cf3
FD
252 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
253 )
26fc5aed
PP
254 else:
255 ptr = native_bt.message_event_create_with_default_clock_snapshot(
cfbd7cf3
FD
256 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
257 )
2ae9f48c 258 else:
c6af194f 259 if event_class.stream_class.default_clock_class is not None:
cfbd7cf3 260 raise ValueError(
f5567ea8 261 "event messages in this stream must have a default clock snapshot"
cfbd7cf3 262 )
c6af194f 263
26fc5aed
PP
264 if event_class.stream_class.supports_packets:
265 ptr = native_bt.message_event_create_with_packet(
cfbd7cf3
FD
266 self._bt_ptr, event_class._ptr, parent._ptr
267 )
26fc5aed
PP
268 else:
269 ptr = native_bt.message_event_create(
cfbd7cf3
FD
270 self._bt_ptr, event_class._ptr, parent._ptr
271 )
2ae9f48c
SM
272
273 if ptr is None:
f5567ea8 274 raise bt2._MemoryError("cannot create event message object")
2ae9f48c 275
3fb99a22 276 return bt2_message._EventMessage(ptr)
2ae9f48c 277
9ec609ec 278 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
3fb99a22 279 utils._check_type(clock_class, bt2_clock_class._ClockClass)
9ec609ec 280 ptr = native_bt.message_message_iterator_inactivity_create(
cfbd7cf3
FD
281 self._bt_ptr, clock_class._ptr, clock_snapshot
282 )
9ec609ec
SM
283
284 if ptr is None:
f5567ea8 285 raise bt2._MemoryError("cannot create inactivity message object")
9ec609ec 286
3fb99a22 287 return bt2_message._MessageIteratorInactivityMessage(ptr)
9ec609ec 288
188edac1 289 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
3fb99a22 290 utils._check_type(stream, bt2_stream._Stream)
2ae9f48c 291
85906b6b 292 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
2ae9f48c 293 if ptr is None:
f5567ea8 294 raise bt2._MemoryError("cannot create stream beginning message object")
2ae9f48c 295
3fb99a22 296 msg = bt2_message._StreamBeginningMessage(ptr)
9ec609ec 297
188edac1
SM
298 if default_clock_snapshot is not None:
299 msg._default_clock_snapshot = default_clock_snapshot
9ec609ec 300
9ec609ec
SM
301 return msg
302
188edac1 303 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
3fb99a22 304 utils._check_type(stream, bt2_stream._Stream)
5f25509b 305
85906b6b 306 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
5f25509b 307 if ptr is None:
f5567ea8 308 raise bt2._MemoryError("cannot create stream end message object")
5f25509b 309
3fb99a22 310 msg = bt2_message._StreamEndMessage(ptr)
188edac1
SM
311
312 if default_clock_snapshot is not None:
313 msg._default_clock_snapshot = default_clock_snapshot
314
315 return msg
5f25509b 316
2ae9f48c 317 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
3fb99a22 318 utils._check_type(packet, bt2_packet._Packet)
2ae9f48c 319
e8ac1aae 320 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
2ae9f48c 321 if default_clock_snapshot is None:
cfbd7cf3
FD
322 raise ValueError(
323 "packet beginning messages in this stream must have a default clock snapshot"
324 )
2ae9f48c
SM
325
326 utils._check_uint64(default_clock_snapshot)
327 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
cfbd7cf3
FD
328 self._bt_ptr, packet._ptr, default_clock_snapshot
329 )
2ae9f48c
SM
330 else:
331 if default_clock_snapshot is not None:
cfbd7cf3
FD
332 raise ValueError(
333 "packet beginning messages in this stream must not have a default clock snapshot"
334 )
2ae9f48c 335
85906b6b 336 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
2ae9f48c
SM
337
338 if ptr is None:
f5567ea8 339 raise bt2._MemoryError("cannot create packet beginning message object")
2ae9f48c 340
3fb99a22 341 return bt2_message._PacketBeginningMessage(ptr)
5f25509b
SM
342
343 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
3fb99a22 344 utils._check_type(packet, bt2_packet._Packet)
5f25509b 345
e8ac1aae 346 if packet.stream.cls.packets_have_end_default_clock_snapshot:
9ec609ec 347 if default_clock_snapshot is None:
cfbd7cf3
FD
348 raise ValueError(
349 "packet end messages in this stream must have a default clock snapshot"
350 )
9ec609ec 351
5f25509b
SM
352 utils._check_uint64(default_clock_snapshot)
353 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
cfbd7cf3
FD
354 self._bt_ptr, packet._ptr, default_clock_snapshot
355 )
5f25509b 356 else:
9ec609ec 357 if default_clock_snapshot is not None:
cfbd7cf3
FD
358 raise ValueError(
359 "packet end messages in this stream must not have a default clock snapshot"
360 )
9ec609ec 361
85906b6b 362 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
5f25509b
SM
363
364 if ptr is None:
f5567ea8 365 raise bt2._MemoryError("cannot create packet end message object")
5f25509b 366
3fb99a22 367 return bt2_message._PacketEndMessage(ptr)
9ec609ec 368
cfbd7cf3
FD
369 def _create_discarded_events_message(
370 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
371 ):
3fb99a22 372 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 373
e8ac1aae 374 if not stream.cls.supports_discarded_events:
f5567ea8 375 raise ValueError("stream class does not support discarded events")
2e90378a 376
e8ac1aae 377 if stream.cls.discarded_events_have_default_clock_snapshots:
2e90378a 378 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3 379 raise ValueError(
f5567ea8 380 "discarded events have default clock snapshots for this stream class"
cfbd7cf3 381 )
2e90378a 382
9ec609ec
SM
383 utils._check_uint64(beg_clock_snapshot)
384 utils._check_uint64(end_clock_snapshot)
5d9ef4cb
SM
385
386 if beg_clock_snapshot > end_clock_snapshot:
387 raise ValueError(
f5567ea8 388 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
5d9ef4cb
SM
389 beg_clock_snapshot, end_clock_snapshot
390 )
391 )
392
776a2a25
PP
393 ptr = (
394 native_bt.message_discarded_events_create_with_default_clock_snapshots(
395 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
396 )
cfbd7cf3 397 )
9ec609ec 398 else:
2e90378a 399 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3 400 raise ValueError(
f5567ea8 401 "discarded events have no default clock snapshots for this stream class"
cfbd7cf3 402 )
2e90378a 403
cfbd7cf3 404 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
405
406 if ptr is None:
f5567ea8 407 raise bt2._MemoryError("cannot discarded events message object")
9ec609ec 408
3fb99a22 409 msg = bt2_message._DiscardedEventsMessage(ptr)
9ec609ec
SM
410
411 if count is not None:
412 msg._count = count
413
414 return msg
415
cfbd7cf3
FD
416 def _create_discarded_packets_message(
417 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
418 ):
3fb99a22 419 utils._check_type(stream, bt2_stream._Stream)
9ec609ec 420
e8ac1aae 421 if not stream.cls.supports_discarded_packets:
f5567ea8 422 raise ValueError("stream class does not support discarded packets")
2e90378a 423
e8ac1aae 424 if stream.cls.discarded_packets_have_default_clock_snapshots:
2e90378a 425 if beg_clock_snapshot is None or end_clock_snapshot is None:
cfbd7cf3 426 raise ValueError(
f5567ea8 427 "discarded packets have default clock snapshots for this stream class"
cfbd7cf3 428 )
2e90378a 429
9ec609ec
SM
430 utils._check_uint64(beg_clock_snapshot)
431 utils._check_uint64(end_clock_snapshot)
5d9ef4cb
SM
432
433 if beg_clock_snapshot > end_clock_snapshot:
434 raise ValueError(
f5567ea8 435 "beginning default clock snapshot value ({}) is greater than end default clock snapshot value ({})".format(
5d9ef4cb
SM
436 beg_clock_snapshot, end_clock_snapshot
437 )
438 )
439
776a2a25
PP
440 ptr = (
441 native_bt.message_discarded_packets_create_with_default_clock_snapshots(
442 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
443 )
cfbd7cf3 444 )
9ec609ec 445 else:
2e90378a 446 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
cfbd7cf3 447 raise ValueError(
f5567ea8 448 "discarded packets have no default clock snapshots for this stream class"
cfbd7cf3 449 )
2e90378a 450
cfbd7cf3 451 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
9ec609ec
SM
452
453 if ptr is None:
f5567ea8 454 raise bt2._MemoryError("cannot discarded packets message object")
9ec609ec 455
3fb99a22 456 msg = bt2_message._DiscardedPacketsMessage(ptr)
9ec609ec
SM
457
458 if count is not None:
459 msg._count = count
460
461 return msg
This page took 0.098561 seconds and 4 git commands to generate.