bt2: Add `_Clock*Const` classes and adapt tests
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
CommitLineData
fbbe9302
SM
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
2c6f8520 25__all__ = ['_TraceClass']
fbbe9302
SM
26
27import bt2
28from bt2 import native_bt, utils, object
3fb99a22
PP
29from bt2 import stream_class as bt2_stream_class
30from bt2 import field_class as bt2_field_class
31from bt2 import trace as bt2_trace
32from bt2 import trace_class as bt2_trace_class
5783664e 33from bt2 import value as bt2_value
fbbe9302
SM
34import collections.abc
35import functools
36
37
fbbe9302 38def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
3fb99a22 39 trace_class = bt2_trace_class._TraceClass._create_from_ptr_and_get_ref(
cfbd7cf3
FD
40 trace_class_ptr
41 )
fbbe9302
SM
42 user_listener(trace_class)
43
44
2c6f8520 45class _TraceClass(object._SharedObject, collections.abc.Mapping):
fbbe9302
SM
46 _get_ref = staticmethod(native_bt.trace_class_get_ref)
47 _put_ref = staticmethod(native_bt.trace_class_put_ref)
48
8c2367b8
SM
49 # Instantiate a trace of this class.
50
b7bc733b 51 def __call__(self, name=None, user_attributes=None, uuid=None, environment=None):
8c2367b8
SM
52 trace_ptr = native_bt.trace_create(self._ptr)
53
54 if trace_ptr is None:
694c792b 55 raise bt2._MemoryError('cannot create trace class object')
8c2367b8 56
3fb99a22 57 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
8c2367b8
SM
58
59 if name is not None:
60 trace._name = name
61
5783664e
PP
62 if user_attributes is not None:
63 trace._user_attributes = user_attributes
64
335a2da5
PP
65 if uuid is not None:
66 trace._uuid = uuid
67
b7bc733b
PP
68 if environment is not None:
69 for key, value in environment.items():
70 trace.environment[key] = value
335a2da5 71
8c2367b8
SM
72 return trace
73
fbbe9302
SM
74 # Number of stream classes in this trace class.
75
76 def __len__(self):
77 count = native_bt.trace_class_get_stream_class_count(self._ptr)
78 assert count >= 0
79 return count
80
81 # Get a stream class by stream id.
82
83 def __getitem__(self, key):
84 utils._check_uint64(key)
85
86 sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key)
87 if sc_ptr is None:
88 raise KeyError(key)
89
3fb99a22 90 return bt2_stream_class._StreamClass._create_from_ptr_and_get_ref(sc_ptr)
fbbe9302
SM
91
92 def __iter__(self):
93 for idx in range(len(self)):
cfbd7cf3
FD
94 sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const(
95 self._ptr, idx
96 )
fbbe9302
SM
97 assert sc_ptr is not None
98
99 id = native_bt.stream_class_get_id(sc_ptr)
100 assert id >= 0
101
102 yield id
103
cfbd7cf3
FD
104 def create_stream_class(
105 self,
106 id=None,
107 name=None,
5783664e 108 user_attributes=None,
cfbd7cf3
FD
109 packet_context_field_class=None,
110 event_common_context_field_class=None,
111 default_clock_class=None,
112 assigns_automatic_event_class_id=True,
113 assigns_automatic_stream_id=True,
114 supports_packets=False,
115 packets_have_beginning_default_clock_snapshot=False,
116 packets_have_end_default_clock_snapshot=False,
117 supports_discarded_events=False,
118 discarded_events_have_default_clock_snapshots=False,
119 supports_discarded_packets=False,
120 discarded_packets_have_default_clock_snapshots=False,
121 ):
fbbe9302
SM
122
123 if self.assigns_automatic_stream_class_id:
124 if id is not None:
cfbd7cf3
FD
125 raise ValueError(
126 'id provided, but trace class assigns automatic stream class ids'
127 )
fbbe9302
SM
128
129 sc_ptr = native_bt.stream_class_create(self._ptr)
130 else:
131 if id is None:
cfbd7cf3
FD
132 raise ValueError(
133 'id not provided, but trace class does not assign automatic stream class ids'
134 )
fbbe9302
SM
135
136 utils._check_uint64(id)
137 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
138
3fb99a22 139 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
8c2367b8 140
3cdfbaea
SM
141 if name is not None:
142 sc._name = name
143
5783664e
PP
144 if user_attributes is not None:
145 sc._user_attributes = user_attributes
146
3cdfbaea
SM
147 if event_common_context_field_class is not None:
148 sc._event_common_context_field_class = event_common_context_field_class
149
150 if default_clock_class is not None:
151 sc._default_clock_class = default_clock_class
152
26fc5aed
PP
153 # call after `sc._default_clock_class` because, if
154 # `packets_have_beginning_default_clock_snapshot` or
155 # `packets_have_end_default_clock_snapshot` is true, then this
156 # stream class needs a default clock class already.
cfbd7cf3
FD
157 sc._set_supports_packets(
158 supports_packets,
159 packets_have_beginning_default_clock_snapshot,
160 packets_have_end_default_clock_snapshot,
161 )
26fc5aed
PP
162
163 # call after sc._set_supports_packets() because, if
164 # `packet_context_field_class` is not `None`, then this stream
165 # class needs to support packets already.
166 if packet_context_field_class is not None:
167 sc._packet_context_field_class = packet_context_field_class
168
3cdfbaea 169 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
8c2367b8 170 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
cfbd7cf3
FD
171 sc._set_supports_discarded_events(
172 supports_discarded_events, discarded_events_have_default_clock_snapshots
173 )
174 sc._set_supports_discarded_packets(
175 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
176 )
8c2367b8 177 return sc
fbbe9302 178
5783664e
PP
179 @property
180 def user_attributes(self):
181 ptr = native_bt.trace_class_borrow_user_attributes(self._ptr)
182 assert ptr is not None
183 return bt2_value._create_from_ptr_and_get_ref(ptr)
184
185 def _user_attributes(self, user_attributes):
186 value = bt2_value.create_value(user_attributes)
187 utils._check_type(value, bt2_value.MapValue)
188 native_bt.trace_class_set_user_attributes(self._ptr, value._ptr)
189
190 _user_attributes = property(fset=_user_attributes)
191
fbbe9302
SM
192 @property
193 def assigns_automatic_stream_class_id(self):
194 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
195
196 def _assigns_automatic_stream_class_id(self, auto_id):
197 utils._check_bool(auto_id)
cfbd7cf3
FD
198 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
199 self._ptr, auto_id
200 )
fbbe9302 201
cfbd7cf3
FD
202 _assigns_automatic_stream_class_id = property(
203 fset=_assigns_automatic_stream_class_id
204 )
fbbe9302 205
3cdfbaea
SM
206 # Field class creation methods.
207
aae30e61 208 def _check_field_class_create_status(self, ptr, type_name):
3cdfbaea 209 if ptr is None:
694c792b 210 raise bt2._MemoryError('cannot create {} field class'.format(type_name))
3cdfbaea 211
5783664e
PP
212 @staticmethod
213 def _set_field_class_user_attrs(fc, user_attributes):
214 if user_attributes is not None:
215 fc._user_attributes = user_attributes
216
217 def create_bool_field_class(self, user_attributes=None):
aae30e61
PP
218 field_class_ptr = native_bt.field_class_bool_create(self._ptr)
219 self._check_field_class_create_status(field_class_ptr, 'boolean')
5783664e
PP
220 fc = bt2_field_class._BoolFieldClass._create_from_ptr(field_class_ptr)
221 self._set_field_class_user_attrs(fc, user_attributes)
222 return fc
aae30e61 223
5783664e 224 def create_bit_array_field_class(self, length, user_attributes=None):
ead8c3d4
PP
225 utils._check_uint64(length)
226
227 if length < 1 or length > 64:
228 raise ValueError(
229 'invalid length {}: expecting a value in the [1, 64] range'.format(
230 length
231 )
232 )
233
234 field_class_ptr = native_bt.field_class_bit_array_create(self._ptr, length)
235 self._check_field_class_create_status(field_class_ptr, 'bit array')
5783664e
PP
236 fc = bt2_field_class._BitArrayFieldClass._create_from_ptr(field_class_ptr)
237 self._set_field_class_user_attrs(fc, user_attributes)
238 return fc
ead8c3d4 239
cfbd7cf3 240 def _create_integer_field_class(
5783664e
PP
241 self,
242 create_func,
243 py_cls,
244 type_name,
245 field_value_range,
246 preferred_display_base,
247 user_attributes,
cfbd7cf3 248 ):
af4bbfc7 249 field_class_ptr = create_func(self._ptr)
aae30e61 250 self._check_field_class_create_status(field_class_ptr, type_name)
af4bbfc7
SM
251
252 field_class = py_cls._create_from_ptr(field_class_ptr)
253
d47b87ac
SM
254 if field_value_range is not None:
255 field_class._field_value_range = field_value_range
af4bbfc7 256
d47b87ac
SM
257 if preferred_display_base is not None:
258 field_class._preferred_display_base = preferred_display_base
af4bbfc7 259
5783664e 260 self._set_field_class_user_attrs(field_class, user_attributes)
af4bbfc7
SM
261 return field_class
262
cfbd7cf3 263 def create_signed_integer_field_class(
5783664e 264 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
265 ):
266 return self._create_integer_field_class(
9c08c816 267 native_bt.field_class_integer_signed_create,
3fb99a22 268 bt2_field_class._SignedIntegerFieldClass,
cfbd7cf3
FD
269 'signed integer',
270 field_value_range,
271 preferred_display_base,
5783664e 272 user_attributes,
cfbd7cf3
FD
273 )
274
275 def create_unsigned_integer_field_class(
5783664e 276 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
277 ):
278 return self._create_integer_field_class(
9c08c816 279 native_bt.field_class_integer_unsigned_create,
3fb99a22 280 bt2_field_class._UnsignedIntegerFieldClass,
cfbd7cf3
FD
281 'unsigned integer',
282 field_value_range,
283 preferred_display_base,
5783664e 284 user_attributes,
cfbd7cf3
FD
285 )
286
287 def create_signed_enumeration_field_class(
5783664e 288 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
289 ):
290 return self._create_integer_field_class(
9c08c816 291 native_bt.field_class_enumeration_signed_create,
3fb99a22 292 bt2_field_class._SignedEnumerationFieldClass,
cfbd7cf3
FD
293 'signed enumeration',
294 field_value_range,
295 preferred_display_base,
5783664e 296 user_attributes,
cfbd7cf3
FD
297 )
298
299 def create_unsigned_enumeration_field_class(
5783664e 300 self, field_value_range=None, preferred_display_base=None, user_attributes=None
cfbd7cf3
FD
301 ):
302 return self._create_integer_field_class(
9c08c816 303 native_bt.field_class_enumeration_unsigned_create,
3fb99a22 304 bt2_field_class._UnsignedEnumerationFieldClass,
cfbd7cf3
FD
305 'unsigned enumeration',
306 field_value_range,
307 preferred_display_base,
5783664e 308 user_attributes,
cfbd7cf3 309 )
2ae9f48c 310
5783664e 311 def create_real_field_class(self, is_single_precision=False, user_attributes=None):
2ae9f48c 312 field_class_ptr = native_bt.field_class_real_create(self._ptr)
aae30e61 313 self._check_field_class_create_status(field_class_ptr, 'real')
2ae9f48c 314
3fb99a22 315 field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr)
2ae9f48c 316
d47b87ac 317 field_class._is_single_precision = is_single_precision
5783664e 318 self._set_field_class_user_attrs(field_class, user_attributes)
2ae9f48c
SM
319
320 return field_class
321
5783664e 322 def create_structure_field_class(self, user_attributes=None):
3cdfbaea 323 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
aae30e61 324 self._check_field_class_create_status(field_class_ptr, 'structure')
5783664e
PP
325 fc = bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
326 self._set_field_class_user_attrs(fc, user_attributes)
327 return fc
3cdfbaea 328
5783664e 329 def create_string_field_class(self, user_attributes=None):
3cdfbaea 330 field_class_ptr = native_bt.field_class_string_create(self._ptr)
aae30e61 331 self._check_field_class_create_status(field_class_ptr, 'string')
5783664e
PP
332 fc = bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
333 self._set_field_class_user_attrs(fc, user_attributes)
334 return fc
3cdfbaea 335
5783664e 336 def create_static_array_field_class(self, elem_fc, length, user_attributes=None):
3fb99a22 337 utils._check_type(elem_fc, bt2_field_class._FieldClass)
d47b87ac 338 utils._check_uint64(length)
9c08c816 339 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
aae30e61 340 self._check_field_class_create_status(ptr, 'static array')
5783664e
PP
341 fc = bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr)
342 self._set_field_class_user_attrs(fc, user_attributes)
343 return fc
d47b87ac 344
5783664e
PP
345 def create_dynamic_array_field_class(
346 self, elem_fc, length_fc=None, user_attributes=None
347 ):
3fb99a22 348 utils._check_type(elem_fc, bt2_field_class._FieldClass)
1367bc7c 349 length_fc_ptr = None
d47b87ac
SM
350
351 if length_fc is not None:
3fb99a22 352 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
1367bc7c 353 length_fc_ptr = length_fc._ptr
d47b87ac 354
9c08c816 355 ptr = native_bt.field_class_array_dynamic_create(
cfbd7cf3
FD
356 self._ptr, elem_fc._ptr, length_fc_ptr
357 )
aae30e61 358 self._check_field_class_create_status(ptr, 'dynamic array')
5783664e
PP
359 fc = bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
360 self._set_field_class_user_attrs(fc, user_attributes)
361 return fc
d47b87ac 362
5783664e
PP
363 def create_option_field_class(
364 self, content_fc, selector_fc=None, user_attributes=None
365 ):
cec0261d
PP
366 utils._check_type(content_fc, bt2_field_class._FieldClass)
367
368 selector_fc_ptr = None
369
370 if selector_fc is not None:
371 utils._check_type(selector_fc, bt2_field_class._BoolFieldClass)
372 selector_fc_ptr = selector_fc._ptr
373
374 ptr = native_bt.field_class_option_create(
375 self._ptr, content_fc._ptr, selector_fc_ptr
376 )
377 self._check_field_class_create_status(ptr, 'option')
5783664e
PP
378 fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
379 self._set_field_class_user_attrs(fc, user_attributes)
380 return fc
cec0261d 381
5783664e 382 def create_variant_field_class(self, selector_fc=None, user_attributes=None):
45c51519 383 selector_fc_ptr = None
d47b87ac
SM
384
385 if selector_fc is not None:
3fb99a22 386 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
45c51519 387 selector_fc_ptr = selector_fc._ptr
d47b87ac 388
45c51519 389 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
aae30e61 390 self._check_field_class_create_status(ptr, 'variant')
5783664e
PP
391 fc = bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
392 self._set_field_class_user_attrs(fc, user_attributes)
393 return fc
3cdfbaea 394
fbbe9302
SM
395 # Add a listener to be called when the trace class is destroyed.
396
397 def add_destruction_listener(self, listener):
398
399 if not callable(listener):
400 raise TypeError("'listener' parameter is not callable")
401
d24d5663 402 fn = native_bt.bt2_trace_class_add_destruction_listener
cfbd7cf3
FD
403 listener_from_native = functools.partial(
404 _trace_class_destruction_listener_from_native, listener
405 )
fbbe9302 406
ee2cad25
SM
407 status, listener_id = fn(self._ptr, listener_from_native)
408 utils._handle_func_status(
409 status, 'cannot add destruction listener to trace class object'
410 )
fbbe9302 411
3fb99a22 412 return utils._ListenerHandle(listener_id, self)
This page took 0.053064 seconds and 4 git commands to generate.