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 | |
32656995 | 24 | import bt2.field_class |
78668ecd | 25 | import bt2.event_class |
81447b5b | 26 | import collections.abc |
81447b5b | 27 | import bt2.stream |
81447b5b PP |
28 | import bt2 |
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 | ||
78668ecd | 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 SM |
50 | for idx in range(len(self)): |
51 | ec_ptr = native_bt.stream_class_borrow_event_class_by_index_const(self._ptr, idx) | |
52 | assert ec_ptr is not None | |
53 | ||
54 | id = native_bt.event_class_get_id(ec_ptr) | |
55 | assert id >= 0 | |
56 | ||
57 | yield id | |
81447b5b | 58 | |
a728f6c3 SM |
59 | def create_event_class(self, id=None, name=None, log_level=None, emf_uri=None, |
60 | specific_context_field_class=None, | |
61 | payload_field_class=None): | |
060aee52 SM |
62 | if self.assigns_automatic_event_class_id: |
63 | if id is not None: | |
116779e3 | 64 | raise ValueError('id provided, but stream class assigns automatic event class ids') |
060aee52 SM |
65 | |
66 | ec_ptr = native_bt.event_class_create(self._ptr) | |
67 | else: | |
68 | if id is None: | |
116779e3 | 69 | raise ValueError('id not provided, but stream class does not assign automatic event class ids') |
060aee52 SM |
70 | |
71 | utils._check_uint64(id) | |
72 | ec_ptr = native_bt.event_class_create_with_id(self._ptr, id) | |
73 | ||
78668ecd | 74 | event_class = bt2.event_class._EventClass._create_from_ptr(ec_ptr) |
a728f6c3 SM |
75 | |
76 | if name is not None: | |
77 | event_class._name = name | |
78 | ||
79 | if log_level is not None: | |
80 | event_class._log_level = log_level | |
81 | ||
82 | if emf_uri is not None: | |
83 | event_class._emf_uri = emf_uri | |
84 | ||
85 | if specific_context_field_class is not None: | |
86 | event_class._specific_context_field_class = specific_context_field_class | |
87 | ||
88 | if payload_field_class is not None: | |
89 | event_class._payload_field_class = payload_field_class | |
90 | ||
91 | return event_class | |
81447b5b PP |
92 | |
93 | @property | |
060aee52 SM |
94 | def trace_class(self): |
95 | tc_ptr = native_bt.stream_class_borrow_trace_class_const(self._ptr) | |
81447b5b PP |
96 | |
97 | if tc_ptr is not None: | |
78668ecd | 98 | return bt2._TraceClass._create_from_ptr_and_get_ref(tc_ptr) |
81447b5b PP |
99 | |
100 | @property | |
101 | def name(self): | |
839d52a5 | 102 | return native_bt.stream_class_get_name(self._ptr) |
81447b5b | 103 | |
060aee52 | 104 | def _name(self, name): |
81447b5b | 105 | utils._check_str(name) |
fb25b9e3 PP |
106 | status = native_bt.stream_class_set_name(self._ptr, name) |
107 | utils._handle_func_status(status, | |
108 | "cannot set stream class object's name") | |
81447b5b | 109 | |
060aee52 SM |
110 | _name = property(fset=_name) |
111 | ||
112 | @property | |
113 | def assigns_automatic_event_class_id(self): | |
114 | return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr) | |
115 | ||
116 | def _assigns_automatic_event_class_id(self, auto_id): | |
117 | utils._check_bool(auto_id) | |
118 | return native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id) | |
119 | ||
120 | _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id) | |
121 | ||
f377a958 SM |
122 | @property |
123 | def assigns_automatic_stream_id(self): | |
124 | return native_bt.stream_class_assigns_automatic_stream_id(self._ptr) | |
125 | ||
126 | def _assigns_automatic_stream_id(self, auto_id): | |
127 | utils._check_bool(auto_id) | |
128 | return native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id) | |
129 | ||
130 | _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id) | |
131 | ||
37a93d41 PP |
132 | @property |
133 | def supports_packets(self): | |
134 | return native_bt.stream_class_supports_packets(self._ptr) | |
135 | ||
060aee52 | 136 | @property |
5ef34326 PP |
137 | def packets_have_beginning_default_clock_snapshot(self): |
138 | return native_bt.stream_class_packets_have_beginning_default_clock_snapshot(self._ptr) | |
060aee52 | 139 | |
060aee52 | 140 | @property |
5ef34326 PP |
141 | def packets_have_end_default_clock_snapshot(self): |
142 | return native_bt.stream_class_packets_have_end_default_clock_snapshot(self._ptr) | |
060aee52 | 143 | |
37a93d41 PP |
144 | def _set_supports_packets(self, supports, with_begin_cs=False, with_end_cs=False): |
145 | utils._check_bool(supports) | |
146 | utils._check_bool(with_begin_cs) | |
147 | utils._check_bool(with_end_cs) | |
148 | ||
149 | if not supports and (with_begin_cs or with_end_cs): | |
150 | raise ValueError('cannot not support packets, but have default clock snapshots') | |
151 | ||
152 | if not supports and self.packet_context_field_class is not None: | |
153 | raise ValueError('stream class already has a packet context field class') | |
060aee52 | 154 | |
37a93d41 | 155 | native_bt.stream_class_set_supports_packets(self._ptr, supports, with_begin_cs, with_end_cs) |
060aee52 | 156 | |
77037b2b PP |
157 | @property |
158 | def supports_discarded_events(self): | |
159 | return native_bt.stream_class_supports_discarded_events(self._ptr) | |
160 | ||
161 | def _set_supports_discarded_events(self, supports, with_cs=False): | |
162 | utils._check_bool(supports) | |
163 | utils._check_bool(with_cs) | |
164 | ||
165 | if not supports and with_cs: | |
166 | raise ValueError('cannot not support discarded events, but have default clock snapshots') | |
167 | ||
168 | native_bt.stream_class_set_supports_discarded_events(self._ptr, supports, with_cs) | |
169 | ||
170 | @property | |
171 | def discarded_events_have_default_clock_snapshots(self): | |
172 | return native_bt.stream_class_discarded_events_have_default_clock_snapshots(self._ptr) | |
173 | ||
174 | @property | |
175 | def supports_discarded_packets(self): | |
176 | return native_bt.stream_class_supports_discarded_packets(self._ptr) | |
177 | ||
178 | def _set_supports_discarded_packets(self, supports, with_cs): | |
179 | utils._check_bool(supports) | |
180 | utils._check_bool(with_cs) | |
181 | ||
37a93d41 PP |
182 | if supports and not self.supports_packets: |
183 | raise ValueError('cannot support discarded packets, but not support packets') | |
184 | ||
77037b2b PP |
185 | if not supports and with_cs: |
186 | raise ValueError('cannot not support discarded packets, but have default clock snapshots') | |
187 | ||
188 | native_bt.stream_class_set_supports_discarded_packets(self._ptr, supports, with_cs) | |
189 | ||
190 | @property | |
191 | def discarded_packets_have_default_clock_snapshots(self): | |
192 | return native_bt.stream_class_discarded_packets_have_default_clock_snapshots(self._ptr) | |
193 | ||
81447b5b PP |
194 | @property |
195 | def id(self): | |
839d52a5 | 196 | id = native_bt.stream_class_get_id(self._ptr) |
81447b5b | 197 | |
f6a5e476 PP |
198 | if id < 0: |
199 | return | |
81447b5b PP |
200 | |
201 | return id | |
202 | ||
203 | @id.setter | |
204 | def id(self, id): | |
205 | utils._check_int64(id) | |
fb25b9e3 PP |
206 | status = native_bt.stream_class_set_id(self._ptr, id) |
207 | utils._handle_func_status(status, "cannot set stream class object's ID") | |
81447b5b | 208 | |
81447b5b | 209 | @property |
32656995 | 210 | def packet_context_field_class(self): |
060aee52 | 211 | fc_ptr = native_bt.stream_class_borrow_packet_context_field_class_const(self._ptr) |
81447b5b | 212 | |
32656995 | 213 | if fc_ptr is None: |
81447b5b PP |
214 | return |
215 | ||
060aee52 | 216 | return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) |
81447b5b | 217 | |
060aee52 | 218 | def _packet_context_field_class(self, packet_context_field_class): |
32656995 | 219 | if packet_context_field_class is not None: |
060aee52 SM |
220 | utils._check_type(packet_context_field_class, |
221 | bt2.field_class._StructureFieldClass) | |
37a93d41 PP |
222 | |
223 | if not self.supports_packets: | |
224 | raise ValueError('stream class does not support packets') | |
225 | ||
fb25b9e3 PP |
226 | status = native_bt.stream_class_set_packet_context_field_class(self._ptr, |
227 | packet_context_field_class._ptr) | |
228 | utils._handle_func_status(status, | |
229 | "cannot set stream class' packet context field class") | |
81447b5b | 230 | |
060aee52 | 231 | _packet_context_field_class = property(fset=_packet_context_field_class) |
81447b5b PP |
232 | |
233 | @property | |
060aee52 SM |
234 | def event_common_context_field_class(self): |
235 | fc_ptr = native_bt.stream_class_borrow_event_common_context_field_class_const(self._ptr) | |
81447b5b | 236 | |
32656995 | 237 | if fc_ptr is None: |
81447b5b PP |
238 | return |
239 | ||
060aee52 | 240 | return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) |
81447b5b | 241 | |
060aee52 SM |
242 | def _event_common_context_field_class(self, event_common_context_field_class): |
243 | if event_common_context_field_class is not None: | |
244 | utils._check_type(event_common_context_field_class, | |
245 | bt2.field_class._StructureFieldClass) | |
81447b5b | 246 | |
060aee52 | 247 | set_context_fn = native_bt.stream_class_set_event_common_context_field_class |
fb25b9e3 PP |
248 | status = set_context_fn(self._ptr, event_common_context_field_class._ptr) |
249 | utils._handle_func_status(status, | |
250 | "cannot set stream class' event context field type") | |
81447b5b | 251 | |
060aee52 | 252 | _event_common_context_field_class = property(fset=_event_common_context_field_class) |
81447b5b | 253 | |
060aee52 SM |
254 | @property |
255 | def default_clock_class(self): | |
256 | cc_ptr = native_bt.stream_class_borrow_default_clock_class(self._ptr) | |
257 | if cc_ptr is None: | |
81447b5b PP |
258 | return |
259 | ||
4a5ca968 | 260 | return bt2.clock_class._ClockClass._create_from_ptr_and_get_ref(cc_ptr) |
81447b5b | 261 | |
060aee52 | 262 | def _default_clock_class(self, clock_class): |
4a5ca968 | 263 | utils._check_type(clock_class, bt2.clock_class._ClockClass) |
060aee52 SM |
264 | native_bt.stream_class_set_default_clock_class( |
265 | self._ptr, clock_class._ptr) | |
81447b5b | 266 | |
060aee52 | 267 | _default_clock_class = property(fset=_default_clock_class) |