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