bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / src / bindings / python / bt2 / bt2 / stream_class.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
811644b8 3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b
PP
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22
23from bt2 import native_bt, object, utils
3fb99a22
PP
24from bt2 import field_class as bt2_field_class
25from bt2 import event_class as bt2_event_class
26from bt2 import trace_class as bt2_trace_class
27from bt2 import clock_class as bt2_clock_class
5783664e 28from bt2 import value as bt2_value
81447b5b 29import collections.abc
81447b5b
PP
30
31
f0a42b33 32class _StreamClassConst(object._SharedObject, collections.abc.Mapping):
fbbe9302
SM
33 _get_ref = staticmethod(native_bt.stream_class_get_ref)
34 _put_ref = staticmethod(native_bt.stream_class_put_ref)
f0a42b33
FD
35 _borrow_event_class_ptr_by_id = staticmethod(
36 native_bt.stream_class_borrow_event_class_by_id_const
37 )
38 _borrow_event_class_ptr_by_index = staticmethod(
39 native_bt.stream_class_borrow_event_class_by_index_const
40 )
41 _borrow_trace_class_ptr = staticmethod(
42 native_bt.stream_class_borrow_trace_class_const
43 )
44 _borrow_packet_context_field_class_ptr = staticmethod(
45 native_bt.stream_class_borrow_packet_context_field_class_const
46 )
47 _borrow_event_common_context_field_class_ptr = staticmethod(
48 native_bt.stream_class_borrow_event_common_context_field_class_const
49 )
50 _borrow_default_clock_class_ptr = staticmethod(
51 native_bt.stream_class_borrow_default_clock_class_const
52 )
53
54 _event_class_cls = property(lambda _: bt2_event_class._EventClassConst)
55 _trace_class_cls = property(lambda _: bt2_trace_class._TraceClassConst)
56 _clock_class_cls = property(lambda _: bt2_clock_class._ClockClassConst)
fbbe9302 57
81447b5b 58 def __getitem__(self, key):
811644b8 59 utils._check_int64(key)
f0a42b33 60 ec_ptr = self._borrow_event_class_ptr_by_id(self._ptr, key)
81447b5b
PP
61
62 if ec_ptr is None:
63 raise KeyError(key)
64
f0a42b33 65 return self._event_class_cls._create_from_ptr_and_get_ref(ec_ptr)
81447b5b
PP
66
67 def __len__(self):
50842bdc 68 count = native_bt.stream_class_get_event_class_count(self._ptr)
3cdfbaea 69 assert count >= 0
81447b5b
PP
70 return count
71
72 def __iter__(self):
3cdfbaea 73 for idx in range(len(self)):
f0a42b33 74 ec_ptr = self._borrow_event_class_ptr_by_index(self._ptr, idx)
3cdfbaea
SM
75 assert ec_ptr is not None
76
77 id = native_bt.event_class_get_id(ec_ptr)
78 assert id >= 0
79
80 yield id
81447b5b 81
f0a42b33
FD
82 @property
83 def trace_class(self):
84 tc_ptr = self._borrow_trace_class_ptr(self._ptr)
85
86 if tc_ptr is not None:
87 return self._trace_class_cls._create_from_ptr_and_get_ref(tc_ptr)
88
89 @property
90 def user_attributes(self):
91 ptr = native_bt.stream_class_borrow_user_attributes(self._ptr)
92 assert ptr is not None
93 return bt2_value._create_from_ptr_and_get_ref(ptr)
94
95 @property
96 def name(self):
97 return native_bt.stream_class_get_name(self._ptr)
98
99 @property
100 def assigns_automatic_event_class_id(self):
101 return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr)
102
103 @property
104 def assigns_automatic_stream_id(self):
105 return native_bt.stream_class_assigns_automatic_stream_id(self._ptr)
106
107 @property
108 def supports_packets(self):
109 return native_bt.stream_class_supports_packets(self._ptr)
110
111 @property
112 def packets_have_beginning_default_clock_snapshot(self):
113 return native_bt.stream_class_packets_have_beginning_default_clock_snapshot(
114 self._ptr
115 )
116
117 @property
118 def packets_have_end_default_clock_snapshot(self):
119 return native_bt.stream_class_packets_have_end_default_clock_snapshot(self._ptr)
120
121 @property
122 def supports_discarded_events(self):
123 return native_bt.stream_class_supports_discarded_events(self._ptr)
124
125 @property
126 def discarded_events_have_default_clock_snapshots(self):
127 return native_bt.stream_class_discarded_events_have_default_clock_snapshots(
128 self._ptr
129 )
130
131 @property
132 def supports_discarded_packets(self):
133 return native_bt.stream_class_supports_discarded_packets(self._ptr)
134
135 @property
136 def discarded_packets_have_default_clock_snapshots(self):
137 return native_bt.stream_class_discarded_packets_have_default_clock_snapshots(
138 self._ptr
139 )
140
141 @property
142 def id(self):
143 id = native_bt.stream_class_get_id(self._ptr)
144
145 if id < 0:
146 return
147
148 return id
149
150 @property
151 def packet_context_field_class(self):
152 fc_ptr = self._borrow_packet_context_field_class_ptr(self._ptr)
153
154 if fc_ptr is None:
155 return
156
157 return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)
158
159 @property
160 def event_common_context_field_class(self):
161 fc_ptr = self._borrow_event_common_context_field_class_ptr(self._ptr)
162
163 if fc_ptr is None:
164 return
165
166 return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)
167
168 @property
169 def default_clock_class(self):
170 cc_ptr = self._borrow_default_clock_class_ptr(self._ptr)
171 if cc_ptr is None:
172 return
173
174 return self._clock_class_cls._create_from_ptr_and_get_ref(cc_ptr)
175
176
177class _StreamClass(_StreamClassConst):
178 _get_ref = staticmethod(native_bt.stream_class_get_ref)
179 _put_ref = staticmethod(native_bt.stream_class_put_ref)
180 _borrow_event_class_ptr_by_id = staticmethod(
181 native_bt.stream_class_borrow_event_class_by_id
182 )
183 _borrow_event_class_ptr_by_index = staticmethod(
184 native_bt.stream_class_borrow_event_class_by_index
185 )
186 _borrow_trace_class_ptr = staticmethod(native_bt.stream_class_borrow_trace_class)
187 _borrow_packet_context_field_class_ptr = staticmethod(
188 native_bt.stream_class_borrow_packet_context_field_class
189 )
190 _borrow_event_common_context_field_class_ptr = staticmethod(
191 native_bt.stream_class_borrow_event_common_context_field_class
192 )
193 _borrow_default_clock_class_ptr = staticmethod(
194 native_bt.stream_class_borrow_default_clock_class
195 )
196 _event_class_cls = property(lambda s: bt2_event_class._EventClass)
197 _trace_class_cls = property(lambda s: bt2_trace_class._TraceClass)
198 _clock_class_cls = property(lambda s: bt2_clock_class._ClockClass)
199
cfbd7cf3
FD
200 def create_event_class(
201 self,
202 id=None,
203 name=None,
5783664e 204 user_attributes=None,
cfbd7cf3
FD
205 log_level=None,
206 emf_uri=None,
207 specific_context_field_class=None,
208 payload_field_class=None,
209 ):
3cdfbaea
SM
210 if self.assigns_automatic_event_class_id:
211 if id is not None:
cfbd7cf3
FD
212 raise ValueError(
213 'id provided, but stream class assigns automatic event class ids'
214 )
3cdfbaea
SM
215
216 ec_ptr = native_bt.event_class_create(self._ptr)
217 else:
218 if id is None:
cfbd7cf3
FD
219 raise ValueError(
220 'id not provided, but stream class does not assign automatic event class ids'
221 )
3cdfbaea
SM
222
223 utils._check_uint64(id)
224 ec_ptr = native_bt.event_class_create_with_id(self._ptr, id)
225
3fb99a22 226 event_class = bt2_event_class._EventClass._create_from_ptr(ec_ptr)
af4bbfc7
SM
227
228 if name is not None:
229 event_class._name = name
230
5783664e
PP
231 if user_attributes is not None:
232 event_class._user_attributes = user_attributes
233
af4bbfc7
SM
234 if log_level is not None:
235 event_class._log_level = log_level
236
237 if emf_uri is not None:
238 event_class._emf_uri = emf_uri
239
240 if specific_context_field_class is not None:
241 event_class._specific_context_field_class = specific_context_field_class
242
243 if payload_field_class is not None:
244 event_class._payload_field_class = payload_field_class
245
246 return event_class
81447b5b 247
5783664e
PP
248 def _user_attributes(self, user_attributes):
249 value = bt2_value.create_value(user_attributes)
250 utils._check_type(value, bt2_value.MapValue)
251 native_bt.stream_class_set_user_attributes(self._ptr, value._ptr)
252
253 _user_attributes = property(fset=_user_attributes)
254
3cdfbaea 255 def _name(self, name):
81447b5b 256 utils._check_str(name)
d24d5663 257 status = native_bt.stream_class_set_name(self._ptr, name)
cfbd7cf3 258 utils._handle_func_status(status, "cannot set stream class object's name")
81447b5b 259
3cdfbaea
SM
260 _name = property(fset=_name)
261
3cdfbaea
SM
262 def _assigns_automatic_event_class_id(self, auto_id):
263 utils._check_bool(auto_id)
cfbd7cf3
FD
264 return native_bt.stream_class_set_assigns_automatic_event_class_id(
265 self._ptr, auto_id
266 )
3cdfbaea
SM
267
268 _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id)
269
8c2367b8
SM
270 def _assigns_automatic_stream_id(self, auto_id):
271 utils._check_bool(auto_id)
cfbd7cf3
FD
272 return native_bt.stream_class_set_assigns_automatic_stream_id(
273 self._ptr, auto_id
274 )
8c2367b8
SM
275
276 _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id)
277
26fc5aed
PP
278 def _set_supports_packets(self, supports, with_begin_cs=False, with_end_cs=False):
279 utils._check_bool(supports)
280 utils._check_bool(with_begin_cs)
281 utils._check_bool(with_end_cs)
282
283 if not supports and (with_begin_cs or with_end_cs):
cfbd7cf3
FD
284 raise ValueError(
285 'cannot not support packets, but have default clock snapshots'
286 )
26fc5aed
PP
287
288 if not supports and self.packet_context_field_class is not None:
289 raise ValueError('stream class already has a packet context field class')
3cdfbaea 290
cfbd7cf3
FD
291 native_bt.stream_class_set_supports_packets(
292 self._ptr, supports, with_begin_cs, with_end_cs
293 )
3cdfbaea 294
2e90378a
PP
295 def _set_supports_discarded_events(self, supports, with_cs=False):
296 utils._check_bool(supports)
297 utils._check_bool(with_cs)
298
299 if not supports and with_cs:
cfbd7cf3
FD
300 raise ValueError(
301 'cannot not support discarded events, but have default clock snapshots'
302 )
2e90378a 303
cfbd7cf3
FD
304 native_bt.stream_class_set_supports_discarded_events(
305 self._ptr, supports, with_cs
306 )
2e90378a 307
f0a42b33 308 _supports_discarded_events = property(fset=_set_supports_discarded_events)
2e90378a
PP
309
310 def _set_supports_discarded_packets(self, supports, with_cs):
311 utils._check_bool(supports)
312 utils._check_bool(with_cs)
313
26fc5aed 314 if supports and not self.supports_packets:
cfbd7cf3
FD
315 raise ValueError(
316 'cannot support discarded packets, but not support packets'
317 )
26fc5aed 318
2e90378a 319 if not supports and with_cs:
cfbd7cf3
FD
320 raise ValueError(
321 'cannot not support discarded packets, but have default clock snapshots'
322 )
2e90378a 323
cfbd7cf3
FD
324 native_bt.stream_class_set_supports_discarded_packets(
325 self._ptr, supports, with_cs
326 )
2e90378a 327
f0a42b33 328 _supports_discarded_packets = property(fset=_set_supports_discarded_packets)
81447b5b 329
3cdfbaea 330 def _packet_context_field_class(self, packet_context_field_class):
b4f45851 331 if packet_context_field_class is not None:
cfbd7cf3 332 utils._check_type(
3fb99a22 333 packet_context_field_class, bt2_field_class._StructureFieldClass
cfbd7cf3 334 )
26fc5aed
PP
335
336 if not self.supports_packets:
337 raise ValueError('stream class does not support packets')
338
cfbd7cf3
FD
339 status = native_bt.stream_class_set_packet_context_field_class(
340 self._ptr, packet_context_field_class._ptr
341 )
342 utils._handle_func_status(
343 status, "cannot set stream class' packet context field class"
344 )
81447b5b 345
3cdfbaea 346 _packet_context_field_class = property(fset=_packet_context_field_class)
81447b5b 347
3cdfbaea
SM
348 def _event_common_context_field_class(self, event_common_context_field_class):
349 if event_common_context_field_class is not None:
cfbd7cf3 350 utils._check_type(
3fb99a22 351 event_common_context_field_class, bt2_field_class._StructureFieldClass
cfbd7cf3 352 )
81447b5b 353
3cdfbaea 354 set_context_fn = native_bt.stream_class_set_event_common_context_field_class
d24d5663 355 status = set_context_fn(self._ptr, event_common_context_field_class._ptr)
cfbd7cf3
FD
356 utils._handle_func_status(
357 status, "cannot set stream class' event context field type"
358 )
81447b5b 359
3cdfbaea 360 _event_common_context_field_class = property(fset=_event_common_context_field_class)
81447b5b 361
3cdfbaea 362 def _default_clock_class(self, clock_class):
3fb99a22 363 utils._check_type(clock_class, bt2_clock_class._ClockClass)
cfbd7cf3 364 native_bt.stream_class_set_default_clock_class(self._ptr, clock_class._ptr)
81447b5b 365
3cdfbaea 366 _default_clock_class = property(fset=_default_clock_class)
This page took 0.063065 seconds and 4 git commands to generate.