python: standardize intra-bt2 imports
[babeltrace.git] / src / bindings / python / bt2 / bt2 / stream_class.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
81447b5b 2#
811644b8 3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b 4
e5914347
SM
5from bt2 import native_bt
6from bt2 import object as bt2_object
7from bt2 import utils as bt2_utils
3fb99a22
PP
8from bt2 import field_class as bt2_field_class
9from bt2 import event_class as bt2_event_class
3fb99a22 10from bt2 import clock_class as bt2_clock_class
5783664e 11from bt2 import value as bt2_value
81447b5b 12import collections.abc
81447b5b
PP
13
14
79935628
SM
15def _bt2_trace_class():
16 from bt2 import trace_class as bt2_trace_class
17
18 return bt2_trace_class
19
20
e5914347 21class _StreamClassConst(bt2_object._SharedObject, collections.abc.Mapping):
9dee90bd
SM
22 @staticmethod
23 def _get_ref(ptr):
24 native_bt.stream_class_get_ref(ptr)
25
26 @staticmethod
27 def _put_ref(ptr):
28 native_bt.stream_class_put_ref(ptr)
29
f0a42b33
FD
30 _borrow_event_class_ptr_by_id = staticmethod(
31 native_bt.stream_class_borrow_event_class_by_id_const
32 )
33 _borrow_event_class_ptr_by_index = staticmethod(
34 native_bt.stream_class_borrow_event_class_by_index_const
35 )
36 _borrow_trace_class_ptr = staticmethod(
37 native_bt.stream_class_borrow_trace_class_const
38 )
39 _borrow_packet_context_field_class_ptr = staticmethod(
40 native_bt.stream_class_borrow_packet_context_field_class_const
41 )
42 _borrow_event_common_context_field_class_ptr = staticmethod(
43 native_bt.stream_class_borrow_event_common_context_field_class_const
44 )
45 _borrow_default_clock_class_ptr = staticmethod(
46 native_bt.stream_class_borrow_default_clock_class_const
47 )
2550c437
SM
48 _borrow_user_attributes_ptr = staticmethod(
49 native_bt.stream_class_borrow_user_attributes_const
50 )
f0a42b33
FD
51
52 _event_class_cls = property(lambda _: bt2_event_class._EventClassConst)
79935628 53 _trace_class_cls = property(lambda _: _bt2_trace_class()._TraceClassConst)
f0a42b33 54 _clock_class_cls = property(lambda _: bt2_clock_class._ClockClassConst)
fbbe9302 55
81447b5b 56 def __getitem__(self, key):
e5914347 57 bt2_utils._check_int64(key)
f0a42b33 58 ec_ptr = self._borrow_event_class_ptr_by_id(self._ptr, key)
81447b5b
PP
59
60 if ec_ptr is None:
61 raise KeyError(key)
62
f0a42b33 63 return self._event_class_cls._create_from_ptr_and_get_ref(ec_ptr)
81447b5b
PP
64
65 def __len__(self):
50842bdc 66 count = native_bt.stream_class_get_event_class_count(self._ptr)
3cdfbaea 67 assert count >= 0
81447b5b
PP
68 return count
69
70 def __iter__(self):
3cdfbaea 71 for idx in range(len(self)):
f0a42b33 72 ec_ptr = self._borrow_event_class_ptr_by_index(self._ptr, idx)
3cdfbaea
SM
73 assert ec_ptr is not None
74
75 id = native_bt.event_class_get_id(ec_ptr)
76 assert id >= 0
77
78 yield id
81447b5b 79
f0a42b33
FD
80 @property
81 def trace_class(self):
82 tc_ptr = self._borrow_trace_class_ptr(self._ptr)
83
84 if tc_ptr is not None:
85 return self._trace_class_cls._create_from_ptr_and_get_ref(tc_ptr)
86
87 @property
88 def user_attributes(self):
2550c437 89 ptr = self._borrow_user_attributes_ptr(self._ptr)
f0a42b33
FD
90 assert ptr is not None
91 return bt2_value._create_from_ptr_and_get_ref(ptr)
92
93 @property
94 def name(self):
95 return native_bt.stream_class_get_name(self._ptr)
96
97 @property
98 def assigns_automatic_event_class_id(self):
99 return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr)
100
101 @property
102 def assigns_automatic_stream_id(self):
103 return native_bt.stream_class_assigns_automatic_stream_id(self._ptr)
104
105 @property
106 def supports_packets(self):
107 return native_bt.stream_class_supports_packets(self._ptr)
108
109 @property
110 def packets_have_beginning_default_clock_snapshot(self):
111 return native_bt.stream_class_packets_have_beginning_default_clock_snapshot(
112 self._ptr
113 )
114
115 @property
116 def packets_have_end_default_clock_snapshot(self):
117 return native_bt.stream_class_packets_have_end_default_clock_snapshot(self._ptr)
118
119 @property
120 def supports_discarded_events(self):
121 return native_bt.stream_class_supports_discarded_events(self._ptr)
122
123 @property
124 def discarded_events_have_default_clock_snapshots(self):
125 return native_bt.stream_class_discarded_events_have_default_clock_snapshots(
126 self._ptr
127 )
128
129 @property
130 def supports_discarded_packets(self):
131 return native_bt.stream_class_supports_discarded_packets(self._ptr)
132
133 @property
134 def discarded_packets_have_default_clock_snapshots(self):
135 return native_bt.stream_class_discarded_packets_have_default_clock_snapshots(
136 self._ptr
137 )
138
139 @property
140 def id(self):
141 id = native_bt.stream_class_get_id(self._ptr)
142
143 if id < 0:
144 return
145
146 return id
147
148 @property
149 def packet_context_field_class(self):
150 fc_ptr = self._borrow_packet_context_field_class_ptr(self._ptr)
151
152 if fc_ptr is None:
153 return
154
155 return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)
156
157 @property
158 def event_common_context_field_class(self):
159 fc_ptr = self._borrow_event_common_context_field_class_ptr(self._ptr)
160
161 if fc_ptr is None:
162 return
163
164 return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)
165
166 @property
167 def default_clock_class(self):
168 cc_ptr = self._borrow_default_clock_class_ptr(self._ptr)
169 if cc_ptr is None:
170 return
171
172 return self._clock_class_cls._create_from_ptr_and_get_ref(cc_ptr)
173
174
175class _StreamClass(_StreamClassConst):
9dee90bd
SM
176 @staticmethod
177 def _get_ref(ptr):
178 native_bt.stream_class_get_ref(ptr)
179
180 @staticmethod
181 def _put_ref(ptr):
182 native_bt.stream_class_put_ref(ptr)
183
f0a42b33
FD
184 _borrow_event_class_ptr_by_id = staticmethod(
185 native_bt.stream_class_borrow_event_class_by_id
186 )
187 _borrow_event_class_ptr_by_index = staticmethod(
188 native_bt.stream_class_borrow_event_class_by_index
189 )
190 _borrow_trace_class_ptr = staticmethod(native_bt.stream_class_borrow_trace_class)
191 _borrow_packet_context_field_class_ptr = staticmethod(
192 native_bt.stream_class_borrow_packet_context_field_class
193 )
194 _borrow_event_common_context_field_class_ptr = staticmethod(
195 native_bt.stream_class_borrow_event_common_context_field_class
196 )
197 _borrow_default_clock_class_ptr = staticmethod(
198 native_bt.stream_class_borrow_default_clock_class
199 )
2550c437
SM
200 _borrow_user_attributes_ptr = staticmethod(
201 native_bt.stream_class_borrow_user_attributes
202 )
203
f0a42b33 204 _event_class_cls = property(lambda s: bt2_event_class._EventClass)
79935628 205 _trace_class_cls = property(lambda s: _bt2_trace_class()._TraceClass)
f0a42b33
FD
206 _clock_class_cls = property(lambda s: bt2_clock_class._ClockClass)
207
cfbd7cf3
FD
208 def create_event_class(
209 self,
210 id=None,
211 name=None,
5783664e 212 user_attributes=None,
cfbd7cf3
FD
213 log_level=None,
214 emf_uri=None,
215 specific_context_field_class=None,
216 payload_field_class=None,
217 ):
3321d9b0
SM
218 # Validate parameters before we create the object.
219 bt2_event_class._EventClass._validate_create_params(
220 name,
221 user_attributes,
222 log_level,
223 emf_uri,
224 specific_context_field_class,
225 payload_field_class,
226 )
227
3cdfbaea
SM
228 if self.assigns_automatic_event_class_id:
229 if id is not None:
cfbd7cf3 230 raise ValueError(
f5567ea8 231 "id provided, but stream class assigns automatic event class ids"
cfbd7cf3 232 )
3cdfbaea
SM
233
234 ec_ptr = native_bt.event_class_create(self._ptr)
235 else:
236 if id is None:
cfbd7cf3 237 raise ValueError(
f5567ea8 238 "id not provided, but stream class does not assign automatic event class ids"
cfbd7cf3 239 )
3cdfbaea 240
e5914347 241 bt2_utils._check_uint64(id)
3cdfbaea
SM
242 ec_ptr = native_bt.event_class_create_with_id(self._ptr, id)
243
3fb99a22 244 event_class = bt2_event_class._EventClass._create_from_ptr(ec_ptr)
af4bbfc7
SM
245
246 if name is not None:
247 event_class._name = name
248
5783664e
PP
249 if user_attributes is not None:
250 event_class._user_attributes = user_attributes
251
af4bbfc7
SM
252 if log_level is not None:
253 event_class._log_level = log_level
254
255 if emf_uri is not None:
256 event_class._emf_uri = emf_uri
257
258 if specific_context_field_class is not None:
259 event_class._specific_context_field_class = specific_context_field_class
260
261 if payload_field_class is not None:
262 event_class._payload_field_class = payload_field_class
263
264 return event_class
81447b5b 265
5783664e
PP
266 def _user_attributes(self, user_attributes):
267 value = bt2_value.create_value(user_attributes)
5783664e
PP
268 native_bt.stream_class_set_user_attributes(self._ptr, value._ptr)
269
270 _user_attributes = property(fset=_user_attributes)
271
3cdfbaea 272 def _name(self, name):
d24d5663 273 status = native_bt.stream_class_set_name(self._ptr, name)
e5914347 274 bt2_utils._handle_func_status(status, "cannot set stream class object's name")
81447b5b 275
3cdfbaea
SM
276 _name = property(fset=_name)
277
3cdfbaea 278 def _assigns_automatic_event_class_id(self, auto_id):
d3bf1370 279 native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id)
3cdfbaea
SM
280
281 _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id)
282
8c2367b8 283 def _assigns_automatic_stream_id(self, auto_id):
d3bf1370 284 native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id)
8c2367b8
SM
285
286 _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id)
287
26fc5aed 288 def _set_supports_packets(self, supports, with_begin_cs=False, with_end_cs=False):
cfbd7cf3
FD
289 native_bt.stream_class_set_supports_packets(
290 self._ptr, supports, with_begin_cs, with_end_cs
291 )
3cdfbaea 292
2e90378a 293 def _set_supports_discarded_events(self, supports, with_cs=False):
cfbd7cf3
FD
294 native_bt.stream_class_set_supports_discarded_events(
295 self._ptr, supports, with_cs
296 )
2e90378a 297
f0a42b33 298 _supports_discarded_events = property(fset=_set_supports_discarded_events)
2e90378a
PP
299
300 def _set_supports_discarded_packets(self, supports, with_cs):
cfbd7cf3
FD
301 native_bt.stream_class_set_supports_discarded_packets(
302 self._ptr, supports, with_cs
303 )
2e90378a 304
f0a42b33 305 _supports_discarded_packets = property(fset=_set_supports_discarded_packets)
81447b5b 306
3cdfbaea 307 def _packet_context_field_class(self, packet_context_field_class):
d3bf1370
SM
308 status = native_bt.stream_class_set_packet_context_field_class(
309 self._ptr, packet_context_field_class._ptr
310 )
e5914347 311 bt2_utils._handle_func_status(
d3bf1370
SM
312 status, "cannot set stream class' packet context field class"
313 )
314
315 _packet_context_field_class = property(fset=_packet_context_field_class)
316
317 def _event_common_context_field_class(self, event_common_context_field_class):
318 set_context_fn = native_bt.stream_class_set_event_common_context_field_class
319 status = set_context_fn(self._ptr, event_common_context_field_class._ptr)
e5914347 320 bt2_utils._handle_func_status(
d3bf1370
SM
321 status, "cannot set stream class' event context field type"
322 )
26fc5aed 323
d3bf1370
SM
324 _event_common_context_field_class = property(fset=_event_common_context_field_class)
325
326 def _default_clock_class(self, clock_class):
327 native_bt.stream_class_set_default_clock_class(self._ptr, clock_class._ptr)
328
329 _default_clock_class = property(fset=_default_clock_class)
330
331 @classmethod
332 def _validate_create_params(
333 cls,
334 name,
335 user_attributes,
336 packet_context_field_class,
337 event_common_context_field_class,
338 default_clock_class,
339 assigns_automatic_event_class_id,
340 assigns_automatic_stream_id,
341 supports_packets,
342 packets_have_beginning_default_clock_snapshot,
343 packets_have_end_default_clock_snapshot,
344 supports_discarded_events,
345 discarded_events_have_default_clock_snapshots,
346 supports_discarded_packets,
347 discarded_packets_have_default_clock_snapshots,
348 ):
349 # Name
350 if name is not None:
e5914347 351 bt2_utils._check_str(name)
d3bf1370
SM
352
353 # User attributes
354 if user_attributes is not None:
355 value = bt2_value.create_value(user_attributes)
e5914347 356 bt2_utils._check_type(value, bt2_value.MapValue)
d3bf1370
SM
357
358 # Packet context field class
359 if packet_context_field_class is not None:
360 if not supports_packets:
91910435 361 raise ValueError(
f5567ea8 362 "cannot have a packet context field class without supporting packets"
91910435 363 )
26fc5aed 364
e5914347 365 bt2_utils._check_type(
d3bf1370 366 packet_context_field_class, bt2_field_class._StructureFieldClass
cfbd7cf3 367 )
81447b5b 368
d3bf1370 369 # Event common context field class
3cdfbaea 370 if event_common_context_field_class is not None:
e5914347 371 bt2_utils._check_type(
3fb99a22 372 event_common_context_field_class, bt2_field_class._StructureFieldClass
cfbd7cf3 373 )
81447b5b 374
d3bf1370
SM
375 # Default clock class
376 if default_clock_class is not None:
e5914347 377 bt2_utils._check_type(default_clock_class, bt2_clock_class._ClockClass)
d3bf1370
SM
378
379 # Assigns automatic event class id
e5914347 380 bt2_utils._check_bool(assigns_automatic_event_class_id)
d3bf1370
SM
381
382 # Assigns automatic stream id
e5914347 383 bt2_utils._check_bool(assigns_automatic_stream_id)
d3bf1370
SM
384
385 # Packets
e5914347
SM
386 bt2_utils._check_bool(supports_packets)
387 bt2_utils._check_bool(packets_have_beginning_default_clock_snapshot)
388 bt2_utils._check_bool(packets_have_end_default_clock_snapshot)
d3bf1370
SM
389
390 if not supports_packets:
391 if packets_have_beginning_default_clock_snapshot:
392 raise ValueError(
f5567ea8 393 "cannot not support packets, but have packet beginning default clock snapshot"
d3bf1370
SM
394 )
395 if packets_have_end_default_clock_snapshot:
396 raise ValueError(
f5567ea8 397 "cannot not support packets, but have packet end default clock snapshots"
d3bf1370
SM
398 )
399
400 # Discarded events
e5914347
SM
401 bt2_utils._check_bool(supports_discarded_events)
402 bt2_utils._check_bool(discarded_events_have_default_clock_snapshots)
d3bf1370 403
aa740722
SM
404 if discarded_events_have_default_clock_snapshots:
405 if not supports_discarded_events:
406 raise ValueError(
f5567ea8 407 "cannot not support discarded events, but have default clock snapshots for discarded event messages"
aa740722
SM
408 )
409
410 if default_clock_class is None:
411 raise ValueError(
f5567ea8 412 "cannot have no default clock class, but have default clock snapshots for discarded event messages"
aa740722 413 )
81447b5b 414
d3bf1370 415 # Discarded packets
e5914347
SM
416 bt2_utils._check_bool(supports_discarded_packets)
417 bt2_utils._check_bool(discarded_packets_have_default_clock_snapshots)
81447b5b 418
d3bf1370
SM
419 if supports_discarded_packets and not supports_packets:
420 raise ValueError(
f5567ea8 421 "cannot support discarded packets, but not support packets"
d3bf1370 422 )
81447b5b 423
aa740722
SM
424 if discarded_packets_have_default_clock_snapshots:
425 if not supports_discarded_packets:
426 raise ValueError(
f5567ea8 427 "cannot not support discarded packets, but have default clock snapshots for discarded packet messages"
aa740722
SM
428 )
429
430 if default_clock_class is None:
431 raise ValueError(
f5567ea8 432 "cannot have no default clock class, but have default clock snapshots for discarded packet messages"
aa740722 433 )
This page took 0.09927 seconds and 4 git commands to generate.