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