1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import bt2
.notification
25 import collections
.abc
30 class 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
36 class _GenericNotificationIteratorMethods(collections
.abc
.Iterator
):
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
)
43 def _handle_status(self
, status
, gen_error_msg
):
44 if status
== native_bt
.NOTIFICATION_ITERATOR_STATUS_END
:
46 elif status
== native_bt
.NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
:
47 raise bt2
.UnsupportedFeature
49 raise bt2
.Error(gen_error_msg
)
52 status
= native_bt
.notification_iterator_next(self
._ptr
)
53 self
._handle
_status
(status
,
54 'unexpected error: cannot go to the next notification')
58 return self
.notification
60 def seek_to_time(self
, origin
, time
):
61 utils
._check
_int
64(origin
)
62 utils
._check
_int
64(time
)
63 status
= native_bt
.notification_iterator_seek_time(self
._ptr
, origin
,
65 self
._handle
_status
(status
,
66 'unexpected error: cannot seek notification iterator to time')
69 class _GenericNotificationIterator(object._Object
, _GenericNotificationIteratorMethods
):
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
)
77 class 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.
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
87 # self._ptr is NOT owned by this object here, so there's nothing
89 self
= super().__new
__(cls
)
98 return native_bt
.py3_get_component_from_notif_iter(self
._ptr
)
102 return int(self
._ptr
)
107 def _get_from_bt(self
):
108 # this can raise anything: it's catched by the system
110 utils
._check
_type
(notif
, bt2
.notification
._Notification
)
112 # steal the underlying native notification object for the caller
113 notif_ptr
= notif
._ptr
115 return int(notif_ptr
)