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