python: make all _get_ref/_put_ref proper static methods
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
1 # SPDX-License-Identifier: 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 from bt2 import native_bt, utils, object
8 from bt2 import stream_class as bt2_stream_class
9 from bt2 import field_class as bt2_field_class
10 from bt2 import integer_range_set as bt2_integer_range_set
11 from bt2 import trace as bt2_trace
12 from bt2 import value as bt2_value
13 import collections.abc
14 import functools
15 import bt2
16
17
18 def _trace_class_destruction_listener_from_native(
19 user_listener, handle, trace_class_ptr
20 ):
21 trace_class = _TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
22 user_listener(trace_class)
23 handle._invalidate()
24
25
26 class _TraceClassConst(object._SharedObject, collections.abc.Mapping):
27 @staticmethod
28 def _get_ref(ptr):
29 native_bt.trace_class_get_ref(ptr)
30
31 @staticmethod
32 def _put_ref(ptr):
33 native_bt.trace_class_put_ref(ptr)
34
35 _borrow_stream_class_ptr_by_index = staticmethod(
36 native_bt.trace_class_borrow_stream_class_by_index_const
37 )
38 _borrow_stream_class_ptr_by_id = staticmethod(
39 native_bt.trace_class_borrow_stream_class_by_id_const
40 )
41 _borrow_user_attributes_ptr = staticmethod(
42 native_bt.trace_class_borrow_user_attributes_const
43 )
44 _stream_class_pycls = bt2_stream_class._StreamClassConst
45 _create_value_from_ptr_and_get_ref = staticmethod(
46 bt2_value._create_from_const_ptr_and_get_ref
47 )
48
49 @property
50 def user_attributes(self):
51 ptr = self._borrow_user_attributes_ptr(self._ptr)
52 assert ptr is not None
53 return self._create_value_from_ptr_and_get_ref(ptr)
54
55 # Number of stream classes in this trace class.
56
57 def __len__(self):
58 count = native_bt.trace_class_get_stream_class_count(self._ptr)
59 assert count >= 0
60 return count
61
62 # Get a stream class by stream id.
63
64 def __getitem__(self, key):
65 utils._check_uint64(key)
66
67 sc_ptr = self._borrow_stream_class_ptr_by_id(self._ptr, key)
68 if sc_ptr is None:
69 raise KeyError(key)
70
71 return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr)
72
73 def __iter__(self):
74 for idx in range(len(self)):
75 sc_ptr = self._borrow_stream_class_ptr_by_index(self._ptr, idx)
76 assert sc_ptr is not None
77
78 id = native_bt.stream_class_get_id(sc_ptr)
79 assert id >= 0
80
81 yield id
82
83 @property
84 def assigns_automatic_stream_class_id(self):
85 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
86
87 # Add a listener to be called when the trace class is destroyed.
88
89 def add_destruction_listener(self, listener):
90
91 if not callable(listener):
92 raise TypeError("'listener' parameter is not callable")
93
94 handle = utils._ListenerHandle(self.addr)
95
96 listener_from_native = functools.partial(
97 _trace_class_destruction_listener_from_native, listener, handle
98 )
99
100 fn = native_bt.bt2_trace_class_add_destruction_listener
101 status, listener_id = fn(self._ptr, listener_from_native)
102 utils._handle_func_status(
103 status, "cannot add destruction listener to trace class object"
104 )
105
106 handle._set_listener_id(listener_id)
107
108 return handle
109
110 def remove_destruction_listener(self, listener_handle):
111 utils._check_type(listener_handle, utils._ListenerHandle)
112
113 if listener_handle._addr != self.addr:
114 raise ValueError(
115 "This trace class destruction listener does not match the trace class object."
116 )
117
118 if listener_handle._listener_id is None:
119 raise ValueError(
120 "This trace class destruction listener was already removed."
121 )
122
123 status = native_bt.trace_class_remove_destruction_listener(
124 self._ptr, listener_handle._listener_id
125 )
126 utils._handle_func_status(status)
127 listener_handle._invalidate()
128
129
130 class _TraceClass(_TraceClassConst):
131 _borrow_stream_class_ptr_by_index = staticmethod(
132 native_bt.trace_class_borrow_stream_class_by_index
133 )
134 _borrow_stream_class_ptr_by_id = staticmethod(
135 native_bt.trace_class_borrow_stream_class_by_id
136 )
137 _borrow_user_attributes_ptr = staticmethod(
138 native_bt.trace_class_borrow_user_attributes
139 )
140 _stream_class_pycls = bt2_stream_class._StreamClass
141 _create_value_from_ptr_and_get_ref = staticmethod(
142 bt2_value._create_from_ptr_and_get_ref
143 )
144
145 # Instantiate a trace of this class.
146
147 def __call__(self, name=None, user_attributes=None, uuid=None, environment=None):
148 trace_ptr = native_bt.trace_create(self._ptr)
149
150 if trace_ptr is None:
151 raise bt2._MemoryError("cannot create trace class object")
152
153 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
154
155 if name is not None:
156 trace._name = name
157
158 if user_attributes is not None:
159 trace._user_attributes = user_attributes
160
161 if uuid is not None:
162 trace._uuid = uuid
163
164 if environment is not None:
165 for key, value in environment.items():
166 trace.environment[key] = value
167
168 return trace
169
170 def create_stream_class(
171 self,
172 id=None,
173 name=None,
174 user_attributes=None,
175 packet_context_field_class=None,
176 event_common_context_field_class=None,
177 default_clock_class=None,
178 assigns_automatic_event_class_id=True,
179 assigns_automatic_stream_id=True,
180 supports_packets=False,
181 packets_have_beginning_default_clock_snapshot=False,
182 packets_have_end_default_clock_snapshot=False,
183 supports_discarded_events=False,
184 discarded_events_have_default_clock_snapshots=False,
185 supports_discarded_packets=False,
186 discarded_packets_have_default_clock_snapshots=False,
187 ):
188 # Validate parameters before we create the object.
189 bt2_stream_class._StreamClass._validate_create_params(
190 name,
191 user_attributes,
192 packet_context_field_class,
193 event_common_context_field_class,
194 default_clock_class,
195 assigns_automatic_event_class_id,
196 assigns_automatic_stream_id,
197 supports_packets,
198 packets_have_beginning_default_clock_snapshot,
199 packets_have_end_default_clock_snapshot,
200 supports_discarded_events,
201 discarded_events_have_default_clock_snapshots,
202 supports_discarded_packets,
203 discarded_packets_have_default_clock_snapshots,
204 )
205
206 if self.assigns_automatic_stream_class_id:
207 if id is not None:
208 raise ValueError(
209 "id provided, but trace class assigns automatic stream class ids"
210 )
211
212 sc_ptr = native_bt.stream_class_create(self._ptr)
213 else:
214 if id is None:
215 raise ValueError(
216 "id not provided, but trace class does not assign automatic stream class ids"
217 )
218
219 utils._check_uint64(id)
220 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
221
222 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
223
224 if name is not None:
225 sc._name = name
226
227 if user_attributes is not None:
228 sc._user_attributes = user_attributes
229
230 if event_common_context_field_class is not None:
231 sc._event_common_context_field_class = event_common_context_field_class
232
233 if default_clock_class is not None:
234 sc._default_clock_class = default_clock_class
235
236 # call after `sc._default_clock_class` because, if
237 # `packets_have_beginning_default_clock_snapshot` or
238 # `packets_have_end_default_clock_snapshot` is true, then this
239 # stream class needs a default clock class already.
240 sc._set_supports_packets(
241 supports_packets,
242 packets_have_beginning_default_clock_snapshot,
243 packets_have_end_default_clock_snapshot,
244 )
245
246 # call after sc._set_supports_packets() because, if
247 # `packet_context_field_class` is not `None`, then this stream
248 # class needs to support packets already.
249 if packet_context_field_class is not None:
250 sc._packet_context_field_class = packet_context_field_class
251
252 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
253 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
254 sc._set_supports_discarded_events(
255 supports_discarded_events, discarded_events_have_default_clock_snapshots
256 )
257 sc._set_supports_discarded_packets(
258 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
259 )
260 return sc
261
262 def _user_attributes(self, user_attributes):
263 value = bt2_value.create_value(user_attributes)
264 utils._check_type(value, bt2_value.MapValue)
265 native_bt.trace_class_set_user_attributes(self._ptr, value._ptr)
266
267 _user_attributes = property(fset=_user_attributes)
268
269 def _assigns_automatic_stream_class_id(self, auto_id):
270 utils._check_bool(auto_id)
271 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
272 self._ptr, auto_id
273 )
274
275 _assigns_automatic_stream_class_id = property(
276 fset=_assigns_automatic_stream_class_id
277 )
278
279 # Field class creation methods.
280
281 def _check_field_class_create_status(self, ptr, type_name):
282 if ptr is None:
283 raise bt2._MemoryError("cannot create {} field class".format(type_name))
284
285 @staticmethod
286 def _set_field_class_user_attrs(fc, user_attributes):
287 if user_attributes is not None:
288 fc._user_attributes = user_attributes
289
290 def create_bool_field_class(self, user_attributes=None):
291 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
292 self._check_field_class_create_status(field_class_ptr, "boolean")
293 fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
294 self._set_field_class_user_attrs(fc, user_attributes)
295 return fc
296
297 def create_bit_array_field_class(self, length, user_attributes=None):
298 utils._check_uint64(length)
299
300 if length < 1 or length > 64:
301 raise ValueError(
302 "invalid length {}: expecting a value in the [1, 64] range".format(
303 length
304 )
305 )
306
307 field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length)
308 self._check_field_class_create_status(field_class_ptr, "bit array")
309 fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr)
310 self._set_field_class_user_attrs(fc, user_attributes)
311 return fc
312
313 def _create_integer_field_class(
314 self,
315 create_func,
316 py_cls,
317 type_name,
318 field_value_range,
319 preferred_display_base,
320 user_attributes,
321 ):
322 field_class_ptr = create_func(self._ptr)
323 self._check_field_class_create_status(field_class_ptr, type_name)
324
325 field_class = py_cls._create_from_ptr(field_class_ptr)
326
327 if field_value_range is not None:
328 field_class._field_value_range = field_value_range
329
330 if preferred_display_base is not None:
331 field_class._preferred_display_base = preferred_display_base
332
333 self._set_field_class_user_attrs(field_class, user_attributes)
334 return field_class
335
336 def create_signed_integer_field_class(
337 self, field_value_range=None, preferred_display_base=None, user_attributes=None
338 ):
339 return self._create_integer_field_class(
340 native_bt.field_class_integer_signed_create,
341 bt2_field_class._SignedIntegerFieldClass,
342 "signed integer",
343 field_value_range,
344 preferred_display_base,
345 user_attributes,
346 )
347
348 def create_unsigned_integer_field_class(
349 self, field_value_range=None, preferred_display_base=None, user_attributes=None
350 ):
351 return self._create_integer_field_class(
352 native_bt.field_class_integer_unsigned_create,
353 bt2_field_class._UnsignedIntegerFieldClass,
354 "unsigned integer",
355 field_value_range,
356 preferred_display_base,
357 user_attributes,
358 )
359
360 def create_signed_enumeration_field_class(
361 self, field_value_range=None, preferred_display_base=None, user_attributes=None
362 ):
363 return self._create_integer_field_class(
364 native_bt.field_class_enumeration_signed_create,
365 bt2_field_class._SignedEnumerationFieldClass,
366 "signed enumeration",
367 field_value_range,
368 preferred_display_base,
369 user_attributes,
370 )
371
372 def create_unsigned_enumeration_field_class(
373 self, field_value_range=None, preferred_display_base=None, user_attributes=None
374 ):
375 return self._create_integer_field_class(
376 native_bt.field_class_enumeration_unsigned_create,
377 bt2_field_class._UnsignedEnumerationFieldClass,
378 "unsigned enumeration",
379 field_value_range,
380 preferred_display_base,
381 user_attributes,
382 )
383
384 def create_single_precision_real_field_class(self, user_attributes=None):
385 field_class_ptr = native_bt.field_class_real_single_precision_create(self._ptr)
386 self._check_field_class_create_status(field_class_ptr, "single-precision real")
387
388 field_class = bt2_field_class._SinglePrecisionRealFieldClass._create_from_ptr(
389 field_class_ptr
390 )
391
392 self._set_field_class_user_attrs(field_class, user_attributes)
393
394 return field_class
395
396 def create_double_precision_real_field_class(self, user_attributes=None):
397 field_class_ptr = native_bt.field_class_real_double_precision_create(self._ptr)
398 self._check_field_class_create_status(field_class_ptr, "double-precision real")
399
400 field_class = bt2_field_class._DoublePrecisionRealFieldClass._create_from_ptr(
401 field_class_ptr
402 )
403
404 self._set_field_class_user_attrs(field_class, user_attributes)
405
406 return field_class
407
408 def create_structure_field_class(self, user_attributes=None):
409 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
410 self._check_field_class_create_status(field_class_ptr, "structure")
411 fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
412 self._set_field_class_user_attrs(fc, user_attributes)
413 return fc
414
415 def create_string_field_class(self, user_attributes=None):
416 field_class_ptr = native_bt.field_class_string_create(self._ptr)
417 self._check_field_class_create_status(field_class_ptr, "string")
418 fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
419 self._set_field_class_user_attrs(fc, user_attributes)
420 return fc
421
422 def create_static_array_field_class(self, elem_fc, length, user_attributes=None):
423 utils._check_type(elem_fc, bt2_field_class._FieldClass)
424 utils._check_uint64(length)
425 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
426 self._check_field_class_create_status(ptr, "static array")
427 fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr(ptr)
428 self._set_field_class_user_attrs(fc, user_attributes)
429 return fc
430
431 def create_dynamic_array_field_class(
432 self, elem_fc, length_fc=None, user_attributes=None
433 ):
434 utils._check_type(elem_fc, bt2_field_class._FieldClass)
435 length_fc_ptr = None
436
437 if length_fc is not None:
438 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
439 length_fc_ptr = length_fc._ptr
440
441 ptr = native_bt.field_class_array_dynamic_create(
442 self._ptr, elem_fc._ptr, length_fc_ptr
443 )
444 self._check_field_class_create_status(ptr, "dynamic array")
445 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
446 self._set_field_class_user_attrs(fc, user_attributes)
447 return fc
448
449 def create_option_without_selector_field_class(
450 self, content_fc, user_attributes=None
451 ):
452 utils._check_type(content_fc, bt2_field_class._FieldClass)
453 ptr = native_bt.field_class_option_without_selector_create(
454 self._ptr, content_fc._ptr
455 )
456 self._check_field_class_create_status(ptr, "option")
457 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
458 self._set_field_class_user_attrs(fc, user_attributes)
459 return fc
460
461 def create_option_with_bool_selector_field_class(
462 self, content_fc, selector_fc, selector_is_reversed=False, user_attributes=None
463 ):
464 utils._check_type(content_fc, bt2_field_class._FieldClass)
465 utils._check_bool(selector_is_reversed)
466 utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
467 ptr = native_bt.field_class_option_with_selector_field_bool_create(
468 self._ptr, content_fc._ptr, selector_fc._ptr
469 )
470 self._check_field_class_create_status(ptr, "option")
471 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
472 self._set_field_class_user_attrs(fc, user_attributes)
473 fc._selector_is_reversed = selector_is_reversed
474 return fc
475
476 def create_option_with_integer_selector_field_class(
477 self, content_fc, selector_fc, ranges, user_attributes=None
478 ):
479 utils._check_type(content_fc, bt2_field_class._FieldClass)
480 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
481
482 if len(ranges) == 0:
483 raise ValueError("integer range set is empty")
484
485 if isinstance(selector_fc, bt2_field_class._UnsignedIntegerFieldClass):
486 utils._check_type(ranges, bt2_integer_range_set.UnsignedIntegerRangeSet)
487 ptr = native_bt.field_class_option_with_selector_field_integer_unsigned_create(
488 self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
489 )
490 else:
491 utils._check_type(ranges, bt2_integer_range_set.SignedIntegerRangeSet)
492 ptr = (
493 native_bt.field_class_option_with_selector_field_integer_signed_create(
494 self._ptr, content_fc._ptr, selector_fc._ptr, ranges._ptr
495 )
496 )
497
498 self._check_field_class_create_status(ptr, "option")
499 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
500 self._set_field_class_user_attrs(fc, user_attributes)
501 return fc
502
503 def create_variant_field_class(self, selector_fc=None, user_attributes=None):
504 selector_fc_ptr = None
505
506 if selector_fc is not None:
507 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
508 selector_fc_ptr = selector_fc._ptr
509
510 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
511 self._check_field_class_create_status(ptr, "variant")
512 fc = bt2_field_class._obj_type_from_field_class_ptr(ptr)._create_from_ptr(ptr)
513 self._set_field_class_user_attrs(fc, user_attributes)
514 return fc
This page took 0.039694 seconds and 4 git commands to generate.