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
c946c9de 24from bt2 import message as bt2_message
81447b5b 25import collections.abc
c946c9de
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
fa4c33e3 34class _MessageIterator(collections.abc.Iterator):
81447b5b 35 def __next__(self):
f6a5e476 36 raise NotImplementedError
81447b5b
PP
37
38
a43da05a
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
27d97a3f 47 def __init__(self, ptr):
61d96b89
FD
48 self._current_msgs = []
49 self._at = 0
50 super().__init__(ptr)
f6a5e476
PP
51
52 def __next__(self):
27d97a3f 53 if len(self._current_msgs) == self._at:
a43da05a
SM
54 status, msgs = native_bt.bt2_self_component_port_input_get_msg_range(
55 self._ptr
56 )
61d96b89
FD
57 utils._handle_func_status(
58 status, 'unexpected error: cannot advance the message iterator'
59 )
27d97a3f
SM
60 self._current_msgs = msgs
61 self._at = 0
62
63 msg_ptr = self._current_msgs[self._at]
64 self._at += 1
65
c946c9de 66 return bt2_message._create_from_ptr(msg_ptr)
81447b5b 67
39ddfa44 68 def can_seek_beginning(self):
c1491996
FD
69 (
70 status,
71 res,
72 ) = native_bt.self_component_port_input_message_iterator_can_seek_beginning(
a43da05a
SM
73 self._ptr
74 )
9e8e8b43
SM
75 utils._handle_func_status(
76 status,
77 'cannot check whether or not message iterator can seek its beginning',
78 )
39ddfa44
SM
79 return res != 0
80
81 def seek_beginning(self):
2a02ed93 82 # Forget about buffered messages, they won't be valid after seeking.
39ddfa44
SM
83 self._current_msgs.clear()
84 self._at = 0
85
a43da05a
SM
86 status = native_bt.self_component_port_input_message_iterator_seek_beginning(
87 self._ptr
88 )
61d96b89 89 utils._handle_func_status(status, 'cannot seek message iterator beginning')
39ddfa44 90
24a68958
SM
91 def can_seek_ns_from_origin(self, ns_from_origin):
92 utils._check_int64(ns_from_origin)
c1491996
FD
93 (
94 status,
95 res,
96 ) = native_bt.self_component_port_input_message_iterator_can_seek_ns_from_origin(
24a68958
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
9415de1c
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
f6a5e476 138
a4dcfa96
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.
fa4c33e3 146class _UserMessageIterator(_MessageIterator):
81447b5b 147 def __new__(cls, ptr):
f6a5e476 148 # User iterator objects are always created by the native side,
81447b5b
PP
149 # that is, never instantiated directly by Python code.
150 #
f6a5e476
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
deec48a6 154 # self._bt_ptr argument being set.
81447b5b 155 #
deec48a6 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)
deec48a6 159 self._bt_ptr = ptr
81447b5b
PP
160 return self
161
9415de1c 162 def _bt_init_from_native(self, config_ptr, self_output_port_ptr):
c946c9de 163 self_output_port = bt2_port._create_self_from_ptr_and_get_ref(
61d96b89
FD
164 self_output_port_ptr, native_bt.PORT_TYPE_OUTPUT
165 )
9415de1c
SM
166 config = _MessageIteratorConfiguration(config_ptr)
167 self.__init__(config, self_output_port)
a4dcfa96 168
9415de1c 169 def __init__(self, config, self_output_port):
81447b5b
PP
170 pass
171
172 @property
f6a5e476 173 def _component(self):
deec48a6 174 return native_bt.bt2_get_user_component_from_user_msg_iter(self._bt_ptr)
81447b5b 175
3f9a359f
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):
deec48a6 186 return int(self._bt_ptr)
81447b5b 187
d73bb381
PP
188 @property
189 def _is_interrupted(self):
190 return bool(native_bt.self_message_iterator_is_interrupted(self._bt_ptr))
191
819d0ae7 192 def _user_finalize(self):
81447b5b
PP
193 pass
194
f6a5e476
PP
195 def __next__(self):
196 raise bt2.Stop
197
deec48a6 198 def _bt_next_from_native(self):
f6a5e476
PP
199 # this can raise anything: it's catched by the native part
200 try:
fa4c33e3 201 msg = next(self)
f6a5e476
PP
202 except StopIteration:
203 raise bt2.Stop
5760d571 204 except Exception:
f6a5e476
PP
205 raise
206
9cbe0c59 207 utils._check_type(msg, bt2_message._MessageConst)
81447b5b 208
4e853135
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)
27d97a3f 214
deec48a6 215 def _bt_can_seek_beginning_from_native(self):
39ddfa44
SM
216 # Here, we mimic the behavior of the C API:
217 #
5c836b95 218 # - If the iterator has a _user_can_seek_beginning method,
819d0ae7 219 # read it and use that result.
9fa5c517 220 # - Otherwise, the presence or absence of a `_user_seek_beginning`
39ddfa44 221 # method indicates whether the iterator can seek beginning.
819d0ae7 222 if hasattr(self, '_user_can_seek_beginning'):
5c836b95 223 can_seek_beginning = self._user_can_seek_beginning()
39ddfa44
SM
224 utils._check_bool(can_seek_beginning)
225 return can_seek_beginning
226 else:
819d0ae7 227 return hasattr(self, '_user_seek_beginning')
39ddfa44 228
deec48a6 229 def _bt_seek_beginning_from_native(self):
819d0ae7 230 self._user_seek_beginning()
39ddfa44 231
24a68958 232 def _bt_can_seek_ns_from_origin_from_native(self, ns_from_origin):
c49bf79b
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:
24a68958
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.
c49bf79b 241
24a68958
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
24a68958 246 else:
c49bf79b 247 return hasattr(self, '_user_seek_ns_from_origin')
24a68958
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
692f1a01 252 def _create_input_port_message_iterator(self, input_port):
c946c9de 253 utils._check_type(input_port, bt2_port._UserComponentInputPort)
692f1a01 254
c1491996
FD
255 (
256 status,
257 msg_iter_ptr,
258 ) = native_bt.bt2_self_component_port_input_message_iterator_create_from_message_iterator(
692f1a01
PP
259 self._bt_ptr, input_port._ptr
260 )
ab8b2b1b
SM
261 utils._handle_func_status(status, 'cannot create message iterator object')
262 assert msg_iter_ptr is not None
692f1a01
PP
263
264 return _UserComponentInputPortMessageIterator(msg_iter_ptr)
265
363f4bcf 266 def _create_event_message(self, event_class, parent, default_clock_snapshot=None):
c946c9de 267 utils._check_type(event_class, bt2_event_class._EventClass)
37a93d41
PP
268
269 if event_class.stream_class.supports_packets:
c946c9de 270 utils._check_type(parent, bt2_packet._Packet)
37a93d41 271 else:
c946c9de 272 utils._check_type(parent, bt2_stream._Stream)
27d97a3f
SM
273
274 if default_clock_snapshot is not None:
dcd94213 275 if event_class.stream_class.default_clock_class is None:
61d96b89
FD
276 raise ValueError(
277 'event messages in this stream must not have a default clock snapshot'
278 )
dcd94213 279
27d97a3f 280 utils._check_uint64(default_clock_snapshot)
37a93d41
PP
281
282 if event_class.stream_class.supports_packets:
283 ptr = native_bt.message_event_create_with_packet_and_default_clock_snapshot(
61d96b89
FD
284 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
285 )
37a93d41
PP
286 else:
287 ptr = native_bt.message_event_create_with_default_clock_snapshot(
61d96b89
FD
288 self._bt_ptr, event_class._ptr, parent._ptr, default_clock_snapshot
289 )
27d97a3f 290 else:
dcd94213 291 if event_class.stream_class.default_clock_class is not None:
61d96b89
FD
292 raise ValueError(
293 'event messages in this stream must have a default clock snapshot'
294 )
dcd94213 295
37a93d41
PP
296 if event_class.stream_class.supports_packets:
297 ptr = native_bt.message_event_create_with_packet(
61d96b89
FD
298 self._bt_ptr, event_class._ptr, parent._ptr
299 )
37a93d41
PP
300 else:
301 ptr = native_bt.message_event_create(
61d96b89
FD
302 self._bt_ptr, event_class._ptr, parent._ptr
303 )
27d97a3f
SM
304
305 if ptr is None:
614743a5 306 raise bt2._MemoryError('cannot create event message object')
27d97a3f 307
c946c9de 308 return bt2_message._EventMessage(ptr)
27d97a3f 309
0010c8b0 310 def _create_message_iterator_inactivity_message(self, clock_class, clock_snapshot):
c946c9de 311 utils._check_type(clock_class, bt2_clock_class._ClockClass)
0010c8b0 312 ptr = native_bt.message_message_iterator_inactivity_create(
61d96b89
FD
313 self._bt_ptr, clock_class._ptr, clock_snapshot
314 )
0010c8b0
SM
315
316 if ptr is None:
614743a5 317 raise bt2._MemoryError('cannot create inactivity message object')
0010c8b0 318
c946c9de 319 return bt2_message._MessageIteratorInactivityMessage(ptr)
0010c8b0 320
b7cbc799 321 def _create_stream_beginning_message(self, stream, default_clock_snapshot=None):
c946c9de 322 utils._check_type(stream, bt2_stream._Stream)
27d97a3f 323
deec48a6 324 ptr = native_bt.message_stream_beginning_create(self._bt_ptr, stream._ptr)
27d97a3f 325 if ptr is None:
614743a5 326 raise bt2._MemoryError('cannot create stream beginning message object')
27d97a3f 327
c946c9de 328 msg = bt2_message._StreamBeginningMessage(ptr)
0010c8b0 329
b7cbc799
SM
330 if default_clock_snapshot is not None:
331 msg._default_clock_snapshot = default_clock_snapshot
0010c8b0 332
0010c8b0
SM
333 return msg
334
b7cbc799 335 def _create_stream_end_message(self, stream, default_clock_snapshot=None):
c946c9de 336 utils._check_type(stream, bt2_stream._Stream)
871a292a 337
deec48a6 338 ptr = native_bt.message_stream_end_create(self._bt_ptr, stream._ptr)
871a292a 339 if ptr is None:
614743a5 340 raise bt2._MemoryError('cannot create stream end message object')
871a292a 341
c946c9de 342 msg = bt2_message._StreamEndMessage(ptr)
b7cbc799
SM
343
344 if default_clock_snapshot is not None:
345 msg._default_clock_snapshot = default_clock_snapshot
346
347 return msg
871a292a 348
27d97a3f 349 def _create_packet_beginning_message(self, packet, default_clock_snapshot=None):
c946c9de 350 utils._check_type(packet, bt2_packet._Packet)
27d97a3f 351
c88be1c8 352 if packet.stream.cls.packets_have_beginning_default_clock_snapshot:
27d97a3f 353 if default_clock_snapshot is None:
61d96b89
FD
354 raise ValueError(
355 "packet beginning messages in this stream must have a default clock snapshot"
356 )
27d97a3f
SM
357
358 utils._check_uint64(default_clock_snapshot)
359 ptr = native_bt.message_packet_beginning_create_with_default_clock_snapshot(
61d96b89
FD
360 self._bt_ptr, packet._ptr, default_clock_snapshot
361 )
27d97a3f
SM
362 else:
363 if default_clock_snapshot is not None:
61d96b89
FD
364 raise ValueError(
365 "packet beginning messages in this stream must not have a default clock snapshot"
366 )
27d97a3f 367
deec48a6 368 ptr = native_bt.message_packet_beginning_create(self._bt_ptr, packet._ptr)
27d97a3f
SM
369
370 if ptr is None:
614743a5 371 raise bt2._MemoryError('cannot create packet beginning message object')
27d97a3f 372
c946c9de 373 return bt2_message._PacketBeginningMessage(ptr)
871a292a
SM
374
375 def _create_packet_end_message(self, packet, default_clock_snapshot=None):
c946c9de 376 utils._check_type(packet, bt2_packet._Packet)
871a292a 377
c88be1c8 378 if packet.stream.cls.packets_have_end_default_clock_snapshot:
0010c8b0 379 if default_clock_snapshot is None:
61d96b89
FD
380 raise ValueError(
381 "packet end messages in this stream must have a default clock snapshot"
382 )
0010c8b0 383
871a292a
SM
384 utils._check_uint64(default_clock_snapshot)
385 ptr = native_bt.message_packet_end_create_with_default_clock_snapshot(
61d96b89
FD
386 self._bt_ptr, packet._ptr, default_clock_snapshot
387 )
871a292a 388 else:
0010c8b0 389 if default_clock_snapshot is not None:
61d96b89
FD
390 raise ValueError(
391 "packet end messages in this stream must not have a default clock snapshot"
392 )
0010c8b0 393
deec48a6 394 ptr = native_bt.message_packet_end_create(self._bt_ptr, packet._ptr)
871a292a
SM
395
396 if ptr is None:
614743a5 397 raise bt2._MemoryError('cannot create packet end message object')
871a292a 398
c946c9de 399 return bt2_message._PacketEndMessage(ptr)
0010c8b0 400
61d96b89
FD
401 def _create_discarded_events_message(
402 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
403 ):
c946c9de 404 utils._check_type(stream, bt2_stream._Stream)
0010c8b0 405
c88be1c8 406 if not stream.cls.supports_discarded_events:
77037b2b
PP
407 raise ValueError('stream class does not support discarded events')
408
c88be1c8 409 if stream.cls.discarded_events_have_default_clock_snapshots:
77037b2b 410 if beg_clock_snapshot is None or end_clock_snapshot is None:
61d96b89
FD
411 raise ValueError(
412 'discarded events have default clock snapshots for this stream class'
413 )
77037b2b 414
0010c8b0
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(
61d96b89
FD
418 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
419 )
0010c8b0 420 else:
77037b2b 421 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
61d96b89
FD
422 raise ValueError(
423 'discarded events have no default clock snapshots for this stream class'
424 )
77037b2b 425
61d96b89 426 ptr = native_bt.message_discarded_events_create(self._bt_ptr, stream._ptr)
0010c8b0
SM
427
428 if ptr is None:
614743a5 429 raise bt2._MemoryError('cannot discarded events message object')
0010c8b0 430
c946c9de 431 msg = bt2_message._DiscardedEventsMessage(ptr)
0010c8b0
SM
432
433 if count is not None:
434 msg._count = count
435
436 return msg
437
61d96b89
FD
438 def _create_discarded_packets_message(
439 self, stream, count=None, beg_clock_snapshot=None, end_clock_snapshot=None
440 ):
c946c9de 441 utils._check_type(stream, bt2_stream._Stream)
0010c8b0 442
c88be1c8 443 if not stream.cls.supports_discarded_packets:
77037b2b
PP
444 raise ValueError('stream class does not support discarded packets')
445
c88be1c8 446 if stream.cls.discarded_packets_have_default_clock_snapshots:
77037b2b 447 if beg_clock_snapshot is None or end_clock_snapshot is None:
61d96b89
FD
448 raise ValueError(
449 'discarded packets have default clock snapshots for this stream class'
450 )
77037b2b 451
0010c8b0
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(
61d96b89
FD
455 self._bt_ptr, stream._ptr, beg_clock_snapshot, end_clock_snapshot
456 )
0010c8b0 457 else:
77037b2b 458 if beg_clock_snapshot is not None or end_clock_snapshot is not None:
61d96b89
FD
459 raise ValueError(
460 'discarded packets have no default clock snapshots for this stream class'
461 )
77037b2b 462
61d96b89 463 ptr = native_bt.message_discarded_packets_create(self._bt_ptr, stream._ptr)
0010c8b0
SM
464
465 if ptr is None:
614743a5 466 raise bt2._MemoryError('cannot discarded packets message object')
0010c8b0 467
c946c9de 468 msg = bt2_message._DiscardedPacketsMessage(ptr)
0010c8b0
SM
469
470 if count is not None:
471 msg._count = count
472
473 return msg
This page took 0.080362 seconds and 4 git commands to generate.