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