| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 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 |
| 24 | import bt2.field_class |
| 25 | import collections.abc |
| 26 | import bt2.stream |
| 27 | import bt2 |
| 28 | |
| 29 | |
| 30 | class StreamClass(object._SharedObject, collections.abc.Mapping): |
| 31 | _get_ref = staticmethod(native_bt.stream_class_get_ref) |
| 32 | _put_ref = staticmethod(native_bt.stream_class_put_ref) |
| 33 | |
| 34 | def __getitem__(self, key): |
| 35 | utils._check_int64(key) |
| 36 | ec_ptr = native_bt.stream_class_borrow_event_class_by_id(self._ptr, key) |
| 37 | |
| 38 | if ec_ptr is None: |
| 39 | raise KeyError(key) |
| 40 | |
| 41 | return bt2.EventClass._create_from_ptr_and_get_ref(ec_ptr) |
| 42 | |
| 43 | def __len__(self): |
| 44 | count = native_bt.stream_class_get_event_class_count(self._ptr) |
| 45 | assert count >= 0 |
| 46 | return count |
| 47 | |
| 48 | def __iter__(self): |
| 49 | for idx in range(len(self)): |
| 50 | ec_ptr = native_bt.stream_class_borrow_event_class_by_index_const(self._ptr, idx) |
| 51 | assert ec_ptr is not None |
| 52 | |
| 53 | id = native_bt.event_class_get_id(ec_ptr) |
| 54 | assert id >= 0 |
| 55 | |
| 56 | yield id |
| 57 | |
| 58 | def create_event_class(self, id=None, name=None, log_level=None, emf_uri=None, |
| 59 | specific_context_field_class=None, |
| 60 | payload_field_class=None): |
| 61 | if self.assigns_automatic_event_class_id: |
| 62 | if id is not None: |
| 63 | raise ValueError('id provided, but stream class assigns automatic event class ids') |
| 64 | |
| 65 | ec_ptr = native_bt.event_class_create(self._ptr) |
| 66 | else: |
| 67 | if id is None: |
| 68 | raise ValueError('id not provided, but stream class does not assign automatic event class ids') |
| 69 | |
| 70 | utils._check_uint64(id) |
| 71 | ec_ptr = native_bt.event_class_create_with_id(self._ptr, id) |
| 72 | |
| 73 | event_class = bt2.event_class.EventClass._create_from_ptr(ec_ptr) |
| 74 | |
| 75 | if name is not None: |
| 76 | event_class._name = name |
| 77 | |
| 78 | if log_level is not None: |
| 79 | event_class._log_level = log_level |
| 80 | |
| 81 | if emf_uri is not None: |
| 82 | event_class._emf_uri = emf_uri |
| 83 | |
| 84 | if specific_context_field_class is not None: |
| 85 | event_class._specific_context_field_class = specific_context_field_class |
| 86 | |
| 87 | if payload_field_class is not None: |
| 88 | event_class._payload_field_class = payload_field_class |
| 89 | |
| 90 | return event_class |
| 91 | |
| 92 | @property |
| 93 | def trace_class(self): |
| 94 | tc_ptr = native_bt.stream_class_borrow_trace_class_const(self._ptr) |
| 95 | |
| 96 | if tc_ptr is not None: |
| 97 | return bt2.TraceClass._create_from_ptr_and_get_ref(tc_ptr) |
| 98 | |
| 99 | @property |
| 100 | def name(self): |
| 101 | return native_bt.stream_class_get_name(self._ptr) |
| 102 | |
| 103 | def _name(self, name): |
| 104 | utils._check_str(name) |
| 105 | ret = native_bt.stream_class_set_name(self._ptr, name) |
| 106 | utils._handle_ret(ret, "cannot set stream class object's name") |
| 107 | |
| 108 | _name = property(fset=_name) |
| 109 | |
| 110 | @property |
| 111 | def assigns_automatic_event_class_id(self): |
| 112 | return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr) |
| 113 | |
| 114 | def _assigns_automatic_event_class_id(self, auto_id): |
| 115 | utils._check_bool(auto_id) |
| 116 | return native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id) |
| 117 | |
| 118 | _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id) |
| 119 | |
| 120 | @property |
| 121 | def assigns_automatic_stream_id(self): |
| 122 | return native_bt.stream_class_assigns_automatic_stream_id(self._ptr) |
| 123 | |
| 124 | def _assigns_automatic_stream_id(self, auto_id): |
| 125 | utils._check_bool(auto_id) |
| 126 | return native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id) |
| 127 | |
| 128 | _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id) |
| 129 | |
| 130 | @property |
| 131 | def packets_have_beginning_default_clock_snapshot(self): |
| 132 | return native_bt.stream_class_packets_have_beginning_default_clock_snapshot(self._ptr) |
| 133 | |
| 134 | def _packets_have_beginning_default_clock_snapshot(self, value): |
| 135 | utils._check_bool(value) |
| 136 | native_bt.stream_class_set_packets_have_beginning_default_clock_snapshot(self._ptr, value) |
| 137 | |
| 138 | _packets_have_beginning_default_clock_snapshot = property(fset=_packets_have_beginning_default_clock_snapshot) |
| 139 | |
| 140 | @property |
| 141 | def packets_have_end_default_clock_snapshot(self): |
| 142 | return native_bt.stream_class_packets_have_end_default_clock_snapshot(self._ptr) |
| 143 | |
| 144 | def _packets_have_end_default_clock_snapshot(self, value): |
| 145 | utils._check_bool(value) |
| 146 | native_bt.stream_class_set_packets_have_end_default_clock_snapshot(self._ptr, value) |
| 147 | |
| 148 | _packets_have_end_default_clock_snapshot = property(fset=_packets_have_end_default_clock_snapshot) |
| 149 | |
| 150 | @property |
| 151 | def supports_discarded_events(self): |
| 152 | return native_bt.stream_class_supports_discarded_events(self._ptr) |
| 153 | |
| 154 | def _set_supports_discarded_events(self, supports, with_cs=False): |
| 155 | utils._check_bool(supports) |
| 156 | utils._check_bool(with_cs) |
| 157 | |
| 158 | if not supports and with_cs: |
| 159 | raise ValueError('cannot not support discarded events, but have default clock snapshots') |
| 160 | |
| 161 | native_bt.stream_class_set_supports_discarded_events(self._ptr, supports, with_cs) |
| 162 | |
| 163 | @property |
| 164 | def discarded_events_have_default_clock_snapshots(self): |
| 165 | return native_bt.stream_class_discarded_events_have_default_clock_snapshots(self._ptr) |
| 166 | |
| 167 | @property |
| 168 | def supports_discarded_packets(self): |
| 169 | return native_bt.stream_class_supports_discarded_packets(self._ptr) |
| 170 | |
| 171 | def _set_supports_discarded_packets(self, supports, with_cs): |
| 172 | utils._check_bool(supports) |
| 173 | utils._check_bool(with_cs) |
| 174 | |
| 175 | if not supports and with_cs: |
| 176 | raise ValueError('cannot not support discarded packets, but have default clock snapshots') |
| 177 | |
| 178 | native_bt.stream_class_set_supports_discarded_packets(self._ptr, supports, with_cs) |
| 179 | |
| 180 | @property |
| 181 | def discarded_packets_have_default_clock_snapshots(self): |
| 182 | return native_bt.stream_class_discarded_packets_have_default_clock_snapshots(self._ptr) |
| 183 | |
| 184 | @property |
| 185 | def id(self): |
| 186 | id = native_bt.stream_class_get_id(self._ptr) |
| 187 | |
| 188 | if id < 0: |
| 189 | return |
| 190 | |
| 191 | return id |
| 192 | |
| 193 | @id.setter |
| 194 | def id(self, id): |
| 195 | utils._check_int64(id) |
| 196 | ret = native_bt.stream_class_set_id(self._ptr, id) |
| 197 | utils._handle_ret(ret, "cannot set stream class object's ID") |
| 198 | |
| 199 | @property |
| 200 | def packet_context_field_class(self): |
| 201 | fc_ptr = native_bt.stream_class_borrow_packet_context_field_class_const(self._ptr) |
| 202 | |
| 203 | if fc_ptr is None: |
| 204 | return |
| 205 | |
| 206 | return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) |
| 207 | |
| 208 | def _packet_context_field_class(self, packet_context_field_class): |
| 209 | if packet_context_field_class is not None: |
| 210 | utils._check_type(packet_context_field_class, |
| 211 | bt2.field_class._StructureFieldClass) |
| 212 | ret = native_bt.stream_class_set_packet_context_field_class(self._ptr, |
| 213 | packet_context_field_class._ptr) |
| 214 | utils._handle_ret(ret, "cannot set stream class' packet context field class") |
| 215 | |
| 216 | _packet_context_field_class = property(fset=_packet_context_field_class) |
| 217 | |
| 218 | @property |
| 219 | def event_common_context_field_class(self): |
| 220 | fc_ptr = native_bt.stream_class_borrow_event_common_context_field_class_const(self._ptr) |
| 221 | |
| 222 | if fc_ptr is None: |
| 223 | return |
| 224 | |
| 225 | return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) |
| 226 | |
| 227 | def _event_common_context_field_class(self, event_common_context_field_class): |
| 228 | if event_common_context_field_class is not None: |
| 229 | utils._check_type(event_common_context_field_class, |
| 230 | bt2.field_class._StructureFieldClass) |
| 231 | |
| 232 | set_context_fn = native_bt.stream_class_set_event_common_context_field_class |
| 233 | ret = set_context_fn(self._ptr, event_common_context_field_class._ptr) |
| 234 | |
| 235 | utils._handle_ret(ret, "cannot set stream class' event context field type") |
| 236 | |
| 237 | _event_common_context_field_class = property(fset=_event_common_context_field_class) |
| 238 | |
| 239 | @property |
| 240 | def default_clock_class(self): |
| 241 | cc_ptr = native_bt.stream_class_borrow_default_clock_class(self._ptr) |
| 242 | if cc_ptr is None: |
| 243 | return |
| 244 | |
| 245 | return bt2.clock_class._ClockClass._create_from_ptr_and_get_ref(cc_ptr) |
| 246 | |
| 247 | def _default_clock_class(self, clock_class): |
| 248 | utils._check_type(clock_class, bt2.clock_class._ClockClass) |
| 249 | native_bt.stream_class_set_default_clock_class( |
| 250 | self._ptr, clock_class._ptr) |
| 251 | |
| 252 | _default_clock_class = property(fset=_default_clock_class) |