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