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