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