| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 5 | # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com> |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the "Software"), to deal |
| 9 | # in the Software without restriction, including without limitation the rights |
| 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | # copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | # THE SOFTWARE. |
| 24 | |
| 25 | __all__ = ['TraceClass'] |
| 26 | |
| 27 | import bt2 |
| 28 | from bt2 import native_bt, utils, object |
| 29 | import uuid as uuidp |
| 30 | import collections.abc |
| 31 | import functools |
| 32 | |
| 33 | |
| 34 | class _TraceClassEnv(collections.abc.MutableMapping): |
| 35 | def __init__(self, trace_class): |
| 36 | self._trace_class = trace_class |
| 37 | |
| 38 | def __getitem__(self, key): |
| 39 | utils._check_str(key) |
| 40 | |
| 41 | borrow_entry_fn = native_bt.trace_class_borrow_environment_entry_value_by_name_const |
| 42 | value_ptr = borrow_entry_fn(self._trace_class._ptr, key) |
| 43 | |
| 44 | if value_ptr is None: |
| 45 | raise KeyError(key) |
| 46 | |
| 47 | return bt2.value._create_from_ptr_and_get_ref(value_ptr) |
| 48 | |
| 49 | def __setitem__(self, key, value): |
| 50 | if isinstance(value, str): |
| 51 | set_env_entry_fn = native_bt.trace_class_set_environment_entry_string |
| 52 | elif isinstance(value, int): |
| 53 | set_env_entry_fn = native_bt.trace_class_set_environment_entry_integer |
| 54 | else: |
| 55 | raise TypeError('expected str or int, got {}'.format(type(value))) |
| 56 | |
| 57 | ret = set_env_entry_fn(self._trace_class._ptr, key, value) |
| 58 | |
| 59 | utils._handle_ret(ret, "cannot set trace class object's environment entry") |
| 60 | |
| 61 | def __delitem__(self, key): |
| 62 | raise NotImplementedError |
| 63 | |
| 64 | def __len__(self): |
| 65 | count = native_bt.trace_class_get_environment_entry_count(self._trace_class._ptr) |
| 66 | assert count >= 0 |
| 67 | return count |
| 68 | |
| 69 | def __iter__(self): |
| 70 | trace_class_ptr = self._trace_class_env._trace_class._ptr |
| 71 | |
| 72 | for idx in range(len(self)): |
| 73 | borrow_entry_fn = native_bt.trace_class_borrow_environment_entry_by_index_const |
| 74 | entry_name, _ = borrow_entry_fn(trace_class_ptr, idx) |
| 75 | assert entry_name is not None |
| 76 | yield entry_name |
| 77 | |
| 78 | |
| 79 | class _StreamClassIterator(collections.abc.Iterator): |
| 80 | def __init__(self, trace_class): |
| 81 | self._trace_class = trace_class |
| 82 | self._at = 0 |
| 83 | |
| 84 | def __next__(self): |
| 85 | if self._at == len(self._trace_class): |
| 86 | raise StopIteration |
| 87 | |
| 88 | borrow_stream_class_fn = native_bt.trace_class_borrow_stream_class_by_index_const |
| 89 | sc_ptr = borrow_stream_class_fn(self._trace_class._ptr, self._at) |
| 90 | assert sc_ptr |
| 91 | id = native_bt.stream_class_get_id(sc_ptr) |
| 92 | assert id >= 0 |
| 93 | self._at += 1 |
| 94 | return id |
| 95 | |
| 96 | |
| 97 | def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr): |
| 98 | trace_class = bt2.trace_class.TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) |
| 99 | user_listener(trace_class) |
| 100 | |
| 101 | |
| 102 | class TraceClass(object._SharedObject, collections.abc.Mapping): |
| 103 | _get_ref = staticmethod(native_bt.trace_class_get_ref) |
| 104 | _put_ref = staticmethod(native_bt.trace_class_put_ref) |
| 105 | |
| 106 | @property |
| 107 | def uuid(self): |
| 108 | uuid_bytes = native_bt.trace_class_get_uuid(self._ptr) |
| 109 | if uuid_bytes is None: |
| 110 | return |
| 111 | |
| 112 | return uuidp.UUID(bytes=uuid_bytes) |
| 113 | |
| 114 | def _uuid(self, uuid): |
| 115 | utils._check_type(uuid, uuidp.UUID) |
| 116 | native_bt.trace_class_set_uuid(self._ptr, uuid.bytes) |
| 117 | |
| 118 | _uuid = property(fset=_uuid) |
| 119 | |
| 120 | # Number of stream classes in this trace class. |
| 121 | |
| 122 | def __len__(self): |
| 123 | count = native_bt.trace_class_get_stream_class_count(self._ptr) |
| 124 | assert count >= 0 |
| 125 | return count |
| 126 | |
| 127 | # Get a stream class by stream id. |
| 128 | |
| 129 | def __getitem__(self, key): |
| 130 | utils._check_uint64(key) |
| 131 | |
| 132 | sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key) |
| 133 | if sc_ptr is None: |
| 134 | raise KeyError(key) |
| 135 | |
| 136 | return bt2.StreamClass._create_from_ptr_and_get_ref(sc_ptr) |
| 137 | |
| 138 | def __iter__(self): |
| 139 | for idx in range(len(self)): |
| 140 | sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const(self._ptr, idx) |
| 141 | assert sc_ptr is not None |
| 142 | |
| 143 | id = native_bt.stream_class_get_id(sc_ptr) |
| 144 | assert id >= 0 |
| 145 | |
| 146 | yield id |
| 147 | |
| 148 | @property |
| 149 | def env(self): |
| 150 | return _TraceClassEnv(self) |
| 151 | |
| 152 | def create_stream_class(self, id=None): |
| 153 | |
| 154 | if self.assigns_automatic_stream_class_id: |
| 155 | if id is not None: |
| 156 | raise bt2.CreationError('id provided, but trace class assigns automatic stream class ids') |
| 157 | |
| 158 | sc_ptr = native_bt.stream_class_create(self._ptr) |
| 159 | else: |
| 160 | if id is None: |
| 161 | raise bt2.CreationError('id not provided, but trace class does not assign automatic stream class ids') |
| 162 | |
| 163 | utils._check_uint64(id) |
| 164 | sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id) |
| 165 | |
| 166 | return bt2.StreamClass._create_from_ptr(sc_ptr) |
| 167 | |
| 168 | @property |
| 169 | def assigns_automatic_stream_class_id(self): |
| 170 | return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr) |
| 171 | |
| 172 | def _assigns_automatic_stream_class_id(self, auto_id): |
| 173 | utils._check_bool(auto_id) |
| 174 | return native_bt.trace_class_set_assigns_automatic_stream_class_id(self._ptr, auto_id) |
| 175 | |
| 176 | _assigns_automatic_stream_class_id = property(fset=_assigns_automatic_stream_class_id) |
| 177 | |
| 178 | # Add a listener to be called when the trace class is destroyed. |
| 179 | |
| 180 | def add_destruction_listener(self, listener): |
| 181 | |
| 182 | if not callable(listener): |
| 183 | raise TypeError("'listener' parameter is not callable") |
| 184 | |
| 185 | fn = native_bt.py3_trace_class_add_destruction_listener |
| 186 | listener_from_native = functools.partial(_trace_class_destruction_listener_from_native, |
| 187 | listener) |
| 188 | |
| 189 | listener_id = fn(self._ptr, listener_from_native) |
| 190 | if listener_id is None: |
| 191 | utils._raise_bt2_error('cannot add destruction listener to trace class object') |
| 192 | |
| 193 | return bt2._ListenerHandle(listener_id, self) |