Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2016-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 | |
3fb99a22 PP |
24 | from bt2 import event_class as bt2_event_class |
25 | from bt2 import packet as bt2_packet | |
26 | from bt2 import stream as bt2_stream | |
27 | from bt2 import field as bt2_field | |
f03b6364 | 28 | import collections.abc |
81447b5b PP |
29 | |
30 | ||
f03b6364 | 31 | class _EventConst(object._UniqueObject, collections.abc.Mapping): |
f0a42b33 FD |
32 | _borrow_class_ptr = staticmethod(native_bt.event_borrow_class_const) |
33 | _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet_const) | |
34 | _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream_const) | |
35 | _borrow_common_context_field_ptr = staticmethod( | |
36 | native_bt.event_borrow_common_context_field_const | |
37 | ) | |
38 | _borrow_specific_context_field_ptr = staticmethod( | |
39 | native_bt.event_borrow_specific_context_field_const | |
40 | ) | |
41 | _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field_const) | |
42 | _create_field_from_ptr = staticmethod(bt2_field._create_field_from_const_ptr) | |
43 | ||
44 | _event_class_pycls = property(lambda _: bt2_event_class._EventClassConst) | |
45 | _packet_pycls = property(lambda _: bt2_packet._PacketConst) | |
46 | _stream_pycls = property(lambda _: bt2_stream._StreamConst) | |
47 | ||
81447b5b | 48 | @property |
e8ac1aae | 49 | def cls(self): |
f0a42b33 | 50 | event_class_ptr = self._borrow_class_ptr(self._ptr) |
2ae9f48c | 51 | assert event_class_ptr is not None |
f0a42b33 | 52 | return self._event_class_pycls._create_from_ptr_and_get_ref(event_class_ptr) |
81447b5b PP |
53 | |
54 | @property | |
55 | def name(self): | |
e8ac1aae | 56 | return self.cls.name |
81447b5b PP |
57 | |
58 | @property | |
59 | def id(self): | |
e8ac1aae | 60 | return self.cls.id |
81447b5b PP |
61 | |
62 | @property | |
63 | def packet(self): | |
f0a42b33 | 64 | packet_ptr = self._borrow_packet_ptr(self._ptr) |
26fc5aed PP |
65 | |
66 | if packet_ptr is None: | |
67 | return | |
68 | ||
f0a42b33 | 69 | return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr) |
81447b5b PP |
70 | |
71 | @property | |
72 | def stream(self): | |
f0a42b33 | 73 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
2ae9f48c | 74 | assert stream_ptr is not None |
f0a42b33 | 75 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
76 | |
77 | @property | |
2ae9f48c | 78 | def common_context_field(self): |
f0a42b33 | 79 | field_ptr = self._borrow_common_context_field_ptr(self._ptr) |
81447b5b PP |
80 | |
81 | if field_ptr is None: | |
82 | return | |
83 | ||
f0a42b33 | 84 | return self._create_field_from_ptr( |
cfbd7cf3 FD |
85 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
86 | ) | |
81447b5b PP |
87 | |
88 | @property | |
2ae9f48c | 89 | def specific_context_field(self): |
f0a42b33 | 90 | field_ptr = self._borrow_specific_context_field_ptr(self._ptr) |
81447b5b PP |
91 | |
92 | if field_ptr is None: | |
93 | return | |
94 | ||
f0a42b33 | 95 | return self._create_field_from_ptr( |
cfbd7cf3 FD |
96 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
97 | ) | |
81447b5b PP |
98 | |
99 | @property | |
100 | def payload_field(self): | |
f0a42b33 | 101 | field_ptr = self._borrow_payload_field_ptr(self._ptr) |
81447b5b PP |
102 | |
103 | if field_ptr is None: | |
104 | return | |
105 | ||
f0a42b33 | 106 | return self._create_field_from_ptr( |
cfbd7cf3 FD |
107 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
108 | ) | |
81447b5b PP |
109 | |
110 | def __getitem__(self, key): | |
111 | utils._check_str(key) | |
112 | payload_field = self.payload_field | |
113 | ||
114 | if payload_field is not None and key in payload_field: | |
115 | return payload_field[key] | |
116 | ||
2ae9f48c | 117 | specific_context_field = self.specific_context_field |
81447b5b | 118 | |
2ae9f48c SM |
119 | if specific_context_field is not None and key in specific_context_field: |
120 | return specific_context_field[key] | |
81447b5b | 121 | |
2ae9f48c | 122 | common_context_field = self.common_context_field |
81447b5b | 123 | |
2ae9f48c SM |
124 | if common_context_field is not None and key in common_context_field: |
125 | return common_context_field[key] | |
81447b5b | 126 | |
3e05e718 PP |
127 | if self.packet: |
128 | packet_context_field = self.packet.context_field | |
81447b5b | 129 | |
3e05e718 PP |
130 | if packet_context_field is not None and key in packet_context_field: |
131 | return packet_context_field[key] | |
81447b5b PP |
132 | |
133 | raise KeyError(key) | |
f0a42b33 | 134 | |
f03b6364 PP |
135 | def __iter__(self): |
136 | # To only yield unique keys, keep a set of member names that are | |
137 | # already yielded. Two root structure fields (for example, | |
138 | # payload and common context) can contain immediate members | |
139 | # which share the same name. | |
140 | member_names = set() | |
141 | ||
142 | if self.payload_field is not None: | |
143 | for field_name in self.payload_field: | |
144 | yield field_name | |
145 | member_names.add(field_name) | |
146 | ||
147 | if self.specific_context_field is not None: | |
148 | for field_name in self.specific_context_field: | |
149 | if field_name not in member_names: | |
150 | yield field_name | |
151 | member_names.add(field_name) | |
152 | ||
153 | if self.common_context_field is not None: | |
154 | for field_name in self.common_context_field: | |
155 | if field_name not in member_names: | |
156 | yield field_name | |
157 | member_names.add(field_name) | |
158 | ||
159 | if self.packet and self.packet.context_field is not None: | |
160 | for field_name in self.packet.context_field: | |
161 | if field_name not in member_names: | |
162 | yield field_name | |
163 | member_names.add(field_name) | |
164 | ||
165 | def __len__(self): | |
166 | return sum(1 for _ in self) | |
167 | ||
f0a42b33 FD |
168 | |
169 | class _Event(_EventConst): | |
170 | _borrow_class_ptr = staticmethod(native_bt.event_borrow_class) | |
171 | _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet) | |
172 | _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream) | |
173 | _borrow_common_context_field_ptr = staticmethod( | |
174 | native_bt.event_borrow_common_context_field | |
175 | ) | |
176 | _borrow_specific_context_field_ptr = staticmethod( | |
177 | native_bt.event_borrow_specific_context_field | |
178 | ) | |
179 | _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field) | |
180 | _create_field_from_ptr = staticmethod(bt2_field._create_field_from_ptr) | |
181 | ||
182 | _event_class_pycls = property(lambda _: bt2_event_class._EventClass) | |
183 | _packet_pycls = property(lambda _: bt2_packet._Packet) | |
184 | _stream_pycls = property(lambda _: bt2_stream._Stream) |