e7ac59abc162b92b4b5b7482f629f0bbf022f45c
[babeltrace.git] / src / bindings / python / bt2 / bt2 / event.py
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
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
28 import collections.abc
29
30
31 class _EventConst(object._UniqueObject, collections.abc.Mapping):
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
48 @property
49 def cls(self):
50 event_class_ptr = self._borrow_class_ptr(self._ptr)
51 assert event_class_ptr is not None
52 return self._event_class_pycls._create_from_ptr_and_get_ref(event_class_ptr)
53
54 @property
55 def name(self):
56 return self.cls.name
57
58 @property
59 def id(self):
60 return self.cls.id
61
62 @property
63 def packet(self):
64 packet_ptr = self._borrow_packet_ptr(self._ptr)
65
66 if packet_ptr is None:
67 return
68
69 return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr)
70
71 @property
72 def stream(self):
73 stream_ptr = self._borrow_stream_ptr(self._ptr)
74 assert stream_ptr is not None
75 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
76
77 @property
78 def common_context_field(self):
79 field_ptr = self._borrow_common_context_field_ptr(self._ptr)
80
81 if field_ptr is None:
82 return
83
84 return self._create_field_from_ptr(
85 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
86 )
87
88 @property
89 def specific_context_field(self):
90 field_ptr = self._borrow_specific_context_field_ptr(self._ptr)
91
92 if field_ptr is None:
93 return
94
95 return self._create_field_from_ptr(
96 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
97 )
98
99 @property
100 def payload_field(self):
101 field_ptr = self._borrow_payload_field_ptr(self._ptr)
102
103 if field_ptr is None:
104 return
105
106 return self._create_field_from_ptr(
107 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
108 )
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
117 specific_context_field = self.specific_context_field
118
119 if specific_context_field is not None and key in specific_context_field:
120 return specific_context_field[key]
121
122 common_context_field = self.common_context_field
123
124 if common_context_field is not None and key in common_context_field:
125 return common_context_field[key]
126
127 if self.packet:
128 packet_context_field = self.packet.context_field
129
130 if packet_context_field is not None and key in packet_context_field:
131 return packet_context_field[key]
132
133 raise KeyError(key)
134
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
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)
This page took 0.0389 seconds and 3 git commands to generate.