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