Add Babeltrace 2 Python bindings
[babeltrace.git] / bindings / python / bt2 / notification_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
24import bt2.notification
25import collections.abc
26import bt2.component
27import bt2
28
29
30class NotificationIteratorSeekOrigin:
31 BEGIN = native_bt.NOTIFICATION_ITERATOR_SEEK_ORIGIN_BEGIN
32 CURRENT = native_bt.NOTIFICATION_ITERATOR_SEEK_ORIGIN_CURRENT
33 END = native_bt.NOTIFICATION_ITERATOR_SEEK_ORIGIN_END
34
35
36class _GenericNotificationIteratorMethods(collections.abc.Iterator):
37 @property
38 def notification(self):
39 notif_ptr = native_bt.notification_iterator_get_notification(self._ptr)
40 utils._handle_ptr(notif_ptr, "cannot get notification iterator object's current notification object")
41 return bt2.notification._create_from_ptr(notif_ptr)
42
43 def _handle_status(self, status, gen_error_msg):
44 if status == native_bt.NOTIFICATION_ITERATOR_STATUS_END:
45 raise bt2.Stop
46 elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED:
47 raise bt2.UnsupportedFeature
48 elif status < 0:
49 raise bt2.Error(gen_error_msg)
50
51 def next(self):
52 status = native_bt.notification_iterator_next(self._ptr)
53 self._handle_status(status,
54 'unexpected error: cannot go to the next notification')
55
56 def __next__(self):
57 self.next()
58 return self.notification
59
60 def seek_to_time(self, origin, time):
61 utils._check_int64(origin)
62 utils._check_int64(time)
63 status = native_bt.notification_iterator_seek_time(self._ptr, origin,
64 time)
65 self._handle_status(status,
66 'unexpected error: cannot seek notification iterator to time')
67
68
69class _GenericNotificationIterator(object._Object, _GenericNotificationIteratorMethods):
70 @property
71 def component(self):
72 comp_ptr = native_bt.notification_iterator_get_component(self._ptr)
73 utils._handle_ptr(comp_ptr, "cannot get notification iterator object's component object")
74 return bt2.component._create_generic_component_from_ptr(comp_ptr)
75
76
77class UserNotificationIterator(_GenericNotificationIteratorMethods):
78 def __new__(cls, ptr):
79 # User iterator objects are always created by the BT system,
80 # that is, never instantiated directly by Python code.
81 #
82 # The system calls this, then manually calls self.__init__()
83 # without the `ptr` argument. The user has access to
84 # self.component during this call, thanks to this self._ptr
85 # argument being set.
86 #
87 # self._ptr is NOT owned by this object here, so there's nothing
88 # to do in __del__().
89 self = super().__new__(cls)
90 self._ptr = ptr
91 return self
92
93 def __init__(self):
94 pass
95
96 @property
97 def component(self):
98 return native_bt.py3_get_component_from_notif_iter(self._ptr)
99
100 @property
101 def addr(self):
102 return int(self._ptr)
103
104 def _destroy(self):
105 pass
106
107 def _get_from_bt(self):
108 # this can raise anything: it's catched by the system
109 notif = self._get()
110 utils._check_type(notif, bt2.notification._Notification)
111
112 # steal the underlying native notification object for the caller
113 notif_ptr = notif._ptr
114 notif._ptr = None
115 return int(notif_ptr)
This page took 0.030705 seconds and 4 git commands to generate.