bt2: remove unused import
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4 # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
5 # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24
25 from bt2 import native_bt, utils, object
26 from bt2 import stream_class as bt2_stream_class
27 from bt2 import field_class as bt2_field_class
28 from bt2 import trace as bt2_trace
29 from bt2 import value as bt2_value
30 import collections.abc
31 import functools
32 import bt2
33
34
35 def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
36 trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
37 user_listener(trace_class)
38
39
40 class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
41 _get_ref = staticmethod(native_bt.trace_class_get_ref)
42 _put_ref = staticmethod(native_bt.trace_class_put_ref)
43 _borrow_stream_class_ptr_by_index = staticmethod(
44 native_bt.trace_class_borrow_stream_class_by_index_const
45 )
46 _borrow_stream_class_ptr_by_id = staticmethod(
47 native_bt.trace_class_borrow_stream_class_by_id_const
48 )
49 _borrow_user_attributes_ptr = staticmethod(
50 native_bt.trace_class_borrow_user_attributes_const
51 )
52 _stream_class_pycls = bt2_stream_class._StreamClassConst
53 _create_value_from_ptr_and_get_ref = staticmethod(
54 bt2_value._create_from_const_ptr_and_get_ref
55 )
56
57 @property
58 def user_attributes(self):
59 ptr = self._borrow_user_attributes_ptr(self._ptr)
60 assert ptr is not None
61 return self._create_value_from_ptr_and_get_ref(ptr)
62
63 # Number of stream classes in this trace class.
64
65 def __len__(self):
66 count = native_bt.trace_class_get_stream_class_count(self._ptr)
67 assert count >= 0
68 return count
69
70 # Get a stream class by stream id.
71
72 def __getitem__(self, key):
73 utils._check_uint64(key)
74
75 sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key)
76 if sc_ptr is None:
77 raise KeyError(key)
78
79 return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr)
80
81 def __iter__(self):
82 for idx in range(len(self)):
83 sc_ptr = self._borrow_stream_class_ptr_by_index(self._ptr, idx)
84 assert sc_ptr is not None
85
86 id = native_bt.stream_class_get_id(sc_ptr)
87 assert id >= 0
88
89 yield id
90
91 @property
92 def assigns_automatic_stream_class_id(self):
93 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
94
95 # Add a listener to be called when the trace class is destroyed.
96
97 def add_destruction_listener(self, listener):
98
99 if not callable(listener):
100 raise TypeError("'listener' parameter is not callable")
101
102 fn = native_bt.bt2_trace_class_add_destruction_listener
103 listener_from_native = functools.partial(
104 _trace_class_destruction_listener_from_native, listener
105 )
106
107 status, listener_id = fn(self._ptr, listener_from_native)
108 utils._handle_func_status(
109 status, 'cannot add destruction listener to trace class object'
110 )
111
112 return utils._ListenerHandle(listener_id, self)
113
114
115 class _TraceClass(_TraceClassConst):
116 _borrow_stream_class_ptr_by_index = staticmethod(
117 native_bt.trace_class_borrow_stream_class_by_index
118 )
119 _borrow_stream_class_ptr_by_id = staticmethod(
120 native_bt.trace_class_borrow_stream_class_by_id
121 )
122 _borrow_user_attributes_ptr = staticmethod(
123 native_bt.trace_class_borrow_user_attributes
124 )
125 _stream_class_pycls = bt2_stream_class._StreamClass
126 _create_value_from_ptr_and_get_ref = staticmethod(
127 bt2_value._create_from_ptr_and_get_ref
128 )
129
130 # Instantiate a trace of this class.
131
132 def __call__(self, name=None, user_attributes=None, uuid=None, environment=None):
133 trace_ptr = native_bt.trace_create(self._ptr)
134
135 if trace_ptr is None:
136 raise bt2._MemoryError('cannot create trace class object')
137
138 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
139
140 if name is not None:
141 trace._name = name
142
143 if user_attributes is not None:
144 trace._user_attributes = user_attributes
145
146 if uuid is not None:
147 trace._uuid = uuid
148
149 if environment is not None:
150 for key, value in environment.items():
151 trace.environment[key] = value
152
153 return trace
154
155 def create_stream_class(
156 self,
157 id=None,
158 name=None,
159 user_attributes=None,
160 packet_context_field_class=None,
161 event_common_context_field_class=None,
162 default_clock_class=None,
163 assigns_automatic_event_class_id=True,
164 assigns_automatic_stream_id=True,
165 supports_packets=False,
166 packets_have_beginning_default_clock_snapshot=False,
167 packets_have_end_default_clock_snapshot=False,
168 supports_discarded_events=False,
169 discarded_events_have_default_clock_snapshots=False,
170 supports_discarded_packets=False,
171 discarded_packets_have_default_clock_snapshots=False,
172 ):
173
174 if self.assigns_automatic_stream_class_id:
175 if id is not None:
176 raise ValueError(
177 'id provided, but trace class assigns automatic stream class ids'
178 )
179
180 sc_ptr = native_bt.stream_class_create(self._ptr)
181 else:
182 if id is None:
183 raise ValueError(
184 'id not provided, but trace class does not assign automatic stream class ids'
185 )
186
187 utils._check_uint64(id)
188 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
189
190 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
191
192 if name is not None:
193 sc._name = name
194
195 if user_attributes is not None:
196 sc._user_attributes = user_attributes
197
198 if event_common_context_field_class is not None:
199 sc._event_common_context_field_class = event_common_context_field_class
200
201 if default_clock_class is not None:
202 sc._default_clock_class = default_clock_class
203
204 # call after `sc._default_clock_class` because, if
205 # `packets_have_beginning_default_clock_snapshot` or
206 # `packets_have_end_default_clock_snapshot` is true, then this
207 # stream class needs a default clock class already.
208 sc._set_supports_packets(
209 supports_packets,
210 packets_have_beginning_default_clock_snapshot,
211 packets_have_end_default_clock_snapshot,
212 )
213
214 # call after sc._set_supports_packets() because, if
215 # `packet_context_field_class` is not `None`, then this stream
216 # class needs to support packets already.
217 if packet_context_field_class is not None:
218 sc._packet_context_field_class = packet_context_field_class
219
220 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
221 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
222 sc._set_supports_discarded_events(
223 supports_discarded_events, discarded_events_have_default_clock_snapshots
224 )
225 sc._set_supports_discarded_packets(
226 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
227 )
228 return sc
229
230 def _user_attributes(self, user_attributes):
231 value = bt2_value.create_value(user_attributes)
232 utils._check_type(value, bt2_value.MapValue)
233 native_bt.trace_class_set_user_attributes(self._ptr, value._ptr)
234
235 _user_attributes = property(fset=_user_attributes)
236
237 def _assigns_automatic_stream_class_id(self, auto_id):
238 utils._check_bool(auto_id)
239 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
240 self._ptr, auto_id
241 )
242
243 _assigns_automatic_stream_class_id = property(
244 fset=_assigns_automatic_stream_class_id
245 )
246
247 # Field class creation methods.
248
249 def _check_field_class_create_status(self, ptr, type_name):
250 if ptr is None:
251 raise bt2._MemoryError('cannot create {} field class'.format(type_name))
252
253 @staticmethod
254 def _set_field_class_user_attrs(fc, user_attributes):
255 if user_attributes is not None:
256 fc._user_attributes = user_attributes
257
258 def create_bool_field_class(self, user_attributes=None):
259 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
260 self._check_field_class_create_status(field_class_ptr, 'boolean')
261 fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
262 self._set_field_class_user_attrs(fc, user_attributes)
263 return fc
264
265 def create_bit_array_field_class(self, length, user_attributes=None):
266 utils._check_uint64(length)
267
268 if length < 1 or length > 64:
269 raise ValueError(
270 'invalid length {}: expecting a value in the [1, 64] range'.format(
271 length
272 )
273 )
274
275 field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length)
276 self._check_field_class_create_status(field_class_ptr, 'bit array')
277 fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr)
278 self._set_field_class_user_attrs(fc, user_attributes)
279 return fc
280
281 def _create_integer_field_class(
282 self,
283 create_func,
284 py_cls,
285 type_name,
286 field_value_range,
287 preferred_display_base,
288 user_attributes,
289 ):
290 field_class_ptr = create_func(self._ptr)
291 self._check_field_class_create_status(field_class_ptr, type_name)
292
293 field_class = py_cls._create_from_ptr(field_class_ptr)
294
295 if field_value_range is not None:
296 field_class._field_value_range = field_value_range
297
298 if preferred_display_base is not None:
299 field_class._preferred_display_base = preferred_display_base
300
301 self._set_field_class_user_attrs(field_class, user_attributes)
302 return field_class
303
304 def create_signed_integer_field_class(
305 self, field_value_range=None, preferred_display_base=None, user_attributes=None
306 ):
307 return self._create_integer_field_class(
308 native_bt.field_class_integer_signed_create,
309 bt2_field_class._SignedIntegerFieldClass,
310 'signed integer',
311 field_value_range,
312 preferred_display_base,
313 user_attributes,
314 )
315
316 def create_unsigned_integer_field_class(
317 self, field_value_range=None, preferred_display_base=None, user_attributes=None
318 ):
319 return self._create_integer_field_class(
320 native_bt.field_class_integer_unsigned_create,
321 bt2_field_class._UnsignedIntegerFieldClass,
322 'unsigned integer',
323 field_value_range,
324 preferred_display_base,
325 user_attributes,
326 )
327
328 def create_signed_enumeration_field_class(
329 self, field_value_range=None, preferred_display_base=None, user_attributes=None
330 ):
331 return self._create_integer_field_class(
332 native_bt.field_class_enumeration_signed_create,
333 bt2_field_class._SignedEnumerationFieldClass,
334 'signed enumeration',
335 field_value_range,
336 preferred_display_base,
337 user_attributes,
338 )
339
340 def create_unsigned_enumeration_field_class(
341 self, field_value_range=None, preferred_display_base=None, user_attributes=None
342 ):
343 return self._create_integer_field_class(
344 native_bt.field_class_enumeration_unsigned_create,
345 bt2_field_class._UnsignedEnumerationFieldClass,
346 'unsigned enumeration',
347 field_value_range,
348 preferred_display_base,
349 user_attributes,
350 )
351
352 def create_real_field_class(self, is_single_precision=False, user_attributes=None):
353 field_class_ptr = native_bt.field_class_real_create(self._ptr)
354 self._check_field_class_create_status(field_class_ptr, 'real')
355
356 field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr)
357
358 field_class._is_single_precision = is_single_precision
359 self._set_field_class_user_attrs(field_class, user_attributes)
360
361 return field_class
362
363 def create_structure_field_class(self, user_attributes=None):
364 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
365 self._check_field_class_create_status(field_class_ptr, 'structure')
366 fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
367 self._set_field_class_user_attrs(fc, user_attributes)
368 return fc
369
370 def create_string_field_class(self, user_attributes=None):
371 field_class_ptr = native_bt.field_class_string_create(self._ptr)
372 self._check_field_class_create_status(field_class_ptr, 'string')
373 fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
374 self._set_field_class_user_attrs(fc, user_attributes)
375 return fc
376
377 def create_static_array_field_class(self, elem_fc, length, user_attributes=None):
378 utils._check_type(elem_fc, bt2_field_class._FieldClass)
379 utils._check_uint64(length)
380 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
381 self._check_field_class_create_status(ptr, 'static array')
382 fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr)
383 self._set_field_class_user_attrs(fc, user_attributes)
384 return fc
385
386 def create_dynamic_array_field_class(
387 self, elem_fc, length_fc=None, user_attributes=None
388 ):
389 utils._check_type(elem_fc, bt2_field_class._FieldClass)
390 length_fc_ptr = None
391
392 if length_fc is not None:
393 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
394 length_fc_ptr = length_fc._ptr
395
396 ptr = native_bt.field_class_array_dynamic_create(
397 self._ptr, elem_fc._ptr, length_fc_ptr
398 )
399 self._check_field_class_create_status(ptr, 'dynamic array')
400 fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
401 self._set_field_class_user_attrs(fc, user_attributes)
402 return fc
403
404 def create_option_field_class(
405 self, content_fc, selector_fc=None, user_attributes=None
406 ):
407 utils._check_type(content_fc, bt2_field_class._FieldClass)
408
409 selector_fc_ptr = None
410
411 if selector_fc is not None:
412 utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
413 selector_fc_ptr = selector_fc._ptr
414
415 ptr = native_bt.field_class_option_create(
416 self._ptr, content_fc._ptr, selector_fc_ptr
417 )
418 self._check_field_class_create_status(ptr, 'option')
419 fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
420 self._set_field_class_user_attrs(fc, user_attributes)
421 return fc
422
423 def create_variant_field_class(self, selector_fc=None, user_attributes=None):
424 selector_fc_ptr = None
425
426 if selector_fc is not None:
427 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
428 selector_fc_ptr = selector_fc._ptr
429
430 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
431 self._check_field_class_create_status(ptr, 'variant')
432 fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
433 self._set_field_class_user_attrs(fc, user_attributes)
434 return fc
This page took 0.040409 seconds and 5 git commands to generate.