Commit | Line | Data |
---|---|---|
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 | ||
23 | from bt2 import native_bt, object, utils | |
24 | import bt2.notification | |
25 | import collections.abc | |
26 | import bt2.component | |
27 | import bt2 | |
28 | ||
29 | ||
f6a5e476 | 30 | class _NotificationIterator(collections.abc.Iterator): |
81447b5b | 31 | def _handle_status(self, status, gen_error_msg): |
f6a5e476 PP |
32 | if status == native_bt.NOTIFICATION_ITERATOR_STATUS_CANCELED: |
33 | raise bt2.NotificationIteratorCanceled | |
34 | elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
35 | raise bt2.TryAgain | |
36 | elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_END: | |
81447b5b PP |
37 | raise bt2.Stop |
38 | elif status == native_bt.NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED: | |
39 | raise bt2.UnsupportedFeature | |
40 | elif status < 0: | |
41 | raise bt2.Error(gen_error_msg) | |
42 | ||
81447b5b | 43 | def __next__(self): |
f6a5e476 | 44 | raise NotImplementedError |
81447b5b PP |
45 | |
46 | ||
f6a5e476 | 47 | class _GenericNotificationIterator(object._Object, _NotificationIterator): |
81447b5b PP |
48 | @property |
49 | def component(self): | |
50 | comp_ptr = native_bt.notification_iterator_get_component(self._ptr) | |
f6a5e476 | 51 | assert(comp_ptr) |
81447b5b PP |
52 | return bt2.component._create_generic_component_from_ptr(comp_ptr) |
53 | ||
f6a5e476 PP |
54 | def _get_notif(self): |
55 | notif_ptr = native_bt.notification_iterator_get_notification(self._ptr) | |
56 | utils._handle_ptr(notif_ptr, "cannot get notification iterator object's current notification object") | |
57 | return bt2.notification._create_from_ptr(notif_ptr) | |
58 | ||
59 | def _next(self): | |
60 | status = native_bt.notification_iterator_next(self._ptr) | |
61 | self._handle_status(status, | |
62 | 'unexpected error: cannot advance the notification iterator') | |
63 | ||
64 | def __next__(self): | |
65 | self._next() | |
66 | return self._get_notif() | |
81447b5b | 67 | |
f6a5e476 PP |
68 | |
69 | class _UserNotificationIterator(_NotificationIterator): | |
81447b5b | 70 | def __new__(cls, ptr): |
f6a5e476 | 71 | # User iterator objects are always created by the native side, |
81447b5b PP |
72 | # that is, never instantiated directly by Python code. |
73 | # | |
f6a5e476 PP |
74 | # The native code calls this, then manually calls |
75 | # self.__init__() without the `ptr` argument. The user has | |
76 | # access to self.component during this call, thanks to this | |
77 | # self._ptr argument being set. | |
81447b5b PP |
78 | # |
79 | # self._ptr is NOT owned by this object here, so there's nothing | |
80 | # to do in __del__(). | |
81 | self = super().__new__(cls) | |
82 | self._ptr = ptr | |
83 | return self | |
84 | ||
85 | def __init__(self): | |
86 | pass | |
87 | ||
88 | @property | |
f6a5e476 PP |
89 | def _component(self): |
90 | return native_bt.py3_get_user_component_from_user_notif_iter(self._ptr) | |
81447b5b PP |
91 | |
92 | @property | |
93 | def addr(self): | |
94 | return int(self._ptr) | |
95 | ||
f6a5e476 | 96 | def _finalize(self): |
81447b5b PP |
97 | pass |
98 | ||
f6a5e476 PP |
99 | def __next__(self): |
100 | raise bt2.Stop | |
101 | ||
102 | def _next_from_native(self): | |
103 | # this can raise anything: it's catched by the native part | |
104 | try: | |
105 | notif = next(self) | |
106 | except StopIteration: | |
107 | raise bt2.Stop | |
108 | except: | |
109 | raise | |
110 | ||
81447b5b PP |
111 | utils._check_type(notif, bt2.notification._Notification) |
112 | ||
f6a5e476 PP |
113 | # take a new reference for the native part |
114 | notif._get() | |
115 | return int(notif._ptr) |