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