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