Python bt2 fix: erroneous imports following split of clock class and value
[babeltrace.git] / 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 import bt2.clock_class
25 import bt2.packet
26 import bt2.stream
27 import bt2.fields
28 import bt2.clock_value
29 import numbers
30 import copy
31 import abc
32 import bt2
33
34
35 def _create_from_ptr(ptr):
36 # recreate the event class wrapper of this event's class (the
37 # identity could be different, but the underlying address should be
38 # the same)
39 event_class_ptr = native_bt.ctf_event_get_class(ptr)
40 utils._handle_ptr(event_class_ptr, "cannot get event object's class")
41 event_class = bt2.EventClass._create_from_ptr(event_class_ptr)
42 event = _Event._create_from_ptr(ptr)
43 event._event_class = event_class
44 return event
45
46
47 class _Event(object._Object):
48 @property
49 def event_class(self):
50 return self._event_class
51
52 @property
53 def name(self):
54 return self._event_class.name
55
56 @property
57 def id(self):
58 return self._event_class.id
59
60 @property
61 def packet(self):
62 packet_ptr = native_bt.ctf_event_get_packet(self._ptr)
63
64 if packet_ptr is None:
65 return packet_ptr
66
67 return bt2.packet._Packet._create_from_ptr(packet_ptr)
68
69 @packet.setter
70 def packet(self, packet):
71 utils._check_type(packet, bt2.packet._Packet)
72 ret = native_bt.ctf_event_set_packet(self._ptr, packet._ptr)
73 utils._handle_ret(ret, "cannot set event object's packet object")
74
75 @property
76 def stream(self):
77 stream_ptr = native_bt.ctf_event_get_stream(self._ptr)
78
79 if stream_ptr is None:
80 return stream_ptr
81
82 return bt2.stream._Stream._create_from_ptr(stream_ptr)
83
84 @property
85 def header_field(self):
86 field_ptr = native_bt.ctf_event_get_header(self._ptr)
87
88 if field_ptr is None:
89 return
90
91 return bt2.fields._create_from_ptr(field_ptr)
92
93 @header_field.setter
94 def header_field(self, header_field):
95 header_field_ptr = None
96
97 if header_field is not None:
98 utils._check_type(header_field, bt2.fields._Field)
99 header_field_ptr = header_field._ptr
100
101 ret = native_bt.ctf_event_set_header(self._ptr, header_field_ptr)
102 utils._handle_ret(ret, "cannot set event object's header field")
103
104 @property
105 def stream_event_context_field(self):
106 field_ptr = native_bt.ctf_event_get_stream_event_context(self._ptr)
107
108 if field_ptr is None:
109 return
110
111 return bt2.fields._create_from_ptr(field_ptr)
112
113 @stream_event_context_field.setter
114 def stream_event_context_field(self, stream_event_context):
115 stream_event_context_ptr = None
116
117 if stream_event_context is not None:
118 utils._check_type(stream_event_context, bt2.fields._Field)
119 stream_event_context_ptr = stream_event_context._ptr
120
121 ret = native_bt.ctf_event_set_stream_event_context(self._ptr,
122 stream_event_context_ptr)
123 utils._handle_ret(ret, "cannot set event object's stream event context field")
124
125 @property
126 def context_field(self):
127 field_ptr = native_bt.ctf_event_get_event_context(self._ptr)
128
129 if field_ptr is None:
130 return
131
132 return bt2.fields._create_from_ptr(field_ptr)
133
134 @context_field.setter
135 def context_field(self, context):
136 context_ptr = None
137
138 if context is not None:
139 utils._check_type(context, bt2.fields._Field)
140 context_ptr = context._ptr
141
142 ret = native_bt.ctf_event_set_event_context(self._ptr, context_ptr)
143 utils._handle_ret(ret, "cannot set event object's context field")
144
145 @property
146 def payload_field(self):
147 field_ptr = native_bt.ctf_event_get_event_payload(self._ptr)
148
149 if field_ptr is None:
150 return
151
152 return bt2.fields._create_from_ptr(field_ptr)
153
154 @payload_field.setter
155 def payload_field(self, payload):
156 payload_ptr = None
157
158 if payload is not None:
159 utils._check_type(payload, bt2.fields._Field)
160 payload_ptr = payload._ptr
161
162 ret = native_bt.ctf_event_set_event_payload(self._ptr, payload_ptr)
163 utils._handle_ret(ret, "cannot set event object's payload field")
164
165 def _get_clock_value_cycles(self, clock_class_ptr):
166 clock_value_ptr = native_bt.ctf_event_get_clock_value(self._ptr,
167 clock_class_ptr)
168
169 if clock_value_ptr is None:
170 return
171
172 ret, cycles = native_bt.ctf_clock_value_get_value(clock_value_ptr)
173 native_bt.put(clock_value_ptr)
174 utils._handle_ret(ret, "cannot get clock value object's cycles")
175 return cycles
176
177 def clock_value(self, clock_class):
178 utils._check_type(clock_class, bt2.ClockClass)
179 clock_value_ptr = native_bt.ctf_event_get_clock_value(self._ptr,
180 clock_class._ptr)
181
182 if clock_value_ptr is None:
183 return
184
185 clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
186 return clock_value
187
188 def add_clock_value(self, clock_value):
189 utils._check_type(clock_value, bt2.clock_value._ClockValue)
190 ret = native_bt.ctf_event_set_clock_value(self._ptr,
191 clock_value._ptr)
192 utils._handle_ret(ret, "cannot set event object's clock value")
193
194 def __getitem__(self, key):
195 utils._check_str(key)
196 payload_field = self.payload_field
197
198 if payload_field is not None and key in payload_field:
199 return payload_field[key]
200
201 context_field = self.context_field
202
203 if context_field is not None and key in context_field:
204 return context_field[key]
205
206 sec_field = self.stream_event_context_field
207
208 if sec_field is not None and key in sec_field:
209 return sec_field[key]
210
211 header_field = self.header_field
212
213 if header_field is not None and key in header_field:
214 return header_field[key]
215
216 packet = self.packet
217
218 if packet is None:
219 raise KeyError(key)
220
221 pkt_context_field = packet.context_field
222
223 if pkt_context_field is not None and key in pkt_context_field:
224 return pkt_context_field[key]
225
226 pkt_header_field = packet.header_field
227
228 if pkt_header_field is not None and key in pkt_header_field:
229 return pkt_header_field[key]
230
231 raise KeyError(key)
232
233 @property
234 def _clock_classes(self):
235 stream_class = self.event_class.stream_class
236
237 if stream_class is None:
238 return []
239
240 trace = stream_class.trace
241
242 if trace is None:
243 return []
244
245 clock_classes = []
246
247 for clock_class in trace.clock_classes.values():
248 clock_classes.append(clock_class)
249
250 return clock_classes
251
252 @property
253 def _clock_class_ptrs(self):
254 return [cc._ptr for cc in self._clock_classes]
255
256 def __eq__(self, other):
257 if type(other) is not type(self):
258 return False
259
260 if self.addr == other.addr:
261 return True
262
263 self_clock_values = {}
264 other_clock_values = {}
265
266 for clock_class_ptr in self._clock_class_ptrs:
267 self_clock_values[int(clock_class_ptr)] = self._get_clock_value_cycles(clock_class_ptr)
268
269 for clock_class_ptr in other._clock_class_ptrs:
270 other_clock_values[int(clock_class_ptr)] = self._get_clock_value_cycles(clock_class_ptr)
271
272 self_props = (
273 self.header_field,
274 self.stream_event_context_field,
275 self.context_field,
276 self.payload_field,
277 self_clock_values,
278 )
279 other_props = (
280 other.header_field,
281 other.stream_event_context_field,
282 other.context_field,
283 other.payload_field,
284 other_clock_values,
285 )
286 return self_props == other_props
287
288 def _copy(self, copy_func):
289 cpy = self.event_class()
290
291 # copy fields
292 cpy.header_field = copy_func(self.header_field)
293 cpy.stream_event_context_field = copy_func(self.stream_event_context_field)
294 cpy.context_field = copy_func(self.context_field)
295 cpy.payload_field = copy_func(self.payload_field)
296
297 # Copy known clock value references. It's not necessary to copy
298 # clock class or clock value objects because once a clock value
299 # is created from a clock class, the clock class is frozen.
300 # Thus even if we copy the clock class, the user cannot modify
301 # it, therefore it's useless to copy it.
302 for clock_class in self._clock_classes:
303 clock_value = self.clock_value(clock_class)
304
305 if clock_value is not None:
306 cpy.add_clock_value(clock_value)
307
308 return cpy
309
310 def __copy__(self):
311 return self._copy(copy.copy)
312
313 def __deepcopy__(self, memo):
314 cpy = self._copy(copy.deepcopy)
315 memo[id(self)] = cpy
316 return cpy
This page took 0.036826 seconds and 4 git commands to generate.