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