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