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 | |
81447b5b | 24 | import collections.abc |
3fb99a22 PP |
25 | from bt2 import value as bt2_value |
26 | from bt2 import stream as bt2_stream | |
27 | from bt2 import trace_class as bt2_trace_class | |
28 | from bt2 import stream_class as bt2_stream_class | |
81447b5b | 29 | import bt2 |
8c2367b8 | 30 | import functools |
335a2da5 PP |
31 | import uuid as uuidp |
32 | ||
33 | ||
f0a42b33 FD |
34 | class _TraceEnvironmentConst(collections.abc.Mapping): |
35 | _create_value_from_ptr_and_get_ref = staticmethod( | |
36 | bt2_value._create_from_const_ptr_and_get_ref | |
37 | ) | |
38 | ||
335a2da5 PP |
39 | def __init__(self, trace): |
40 | self._trace = trace | |
41 | ||
42 | def __getitem__(self, key): | |
43 | utils._check_str(key) | |
44 | ||
45 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const | |
46 | value_ptr = borrow_entry_fn(self._trace._ptr, key) | |
47 | ||
48 | if value_ptr is None: | |
49 | raise KeyError(key) | |
50 | ||
f0a42b33 | 51 | return self._create_value_from_ptr_and_get_ref(value_ptr) |
335a2da5 PP |
52 | |
53 | def __len__(self): | |
54 | count = native_bt.trace_get_environment_entry_count(self._trace._ptr) | |
55 | assert count >= 0 | |
56 | return count | |
57 | ||
58 | def __iter__(self): | |
f0a42b33 | 59 | trace_ptr = self._trace._ptr |
335a2da5 PP |
60 | |
61 | for idx in range(len(self)): | |
62 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_by_index_const | |
63 | entry_name, _ = borrow_entry_fn(trace_ptr, idx) | |
64 | assert entry_name is not None | |
65 | yield entry_name | |
81447b5b PP |
66 | |
67 | ||
f0a42b33 FD |
68 | class _TraceEnvironment(_TraceEnvironmentConst, collections.abc.MutableMapping): |
69 | _create_value_from_ptr_and_get_ref = staticmethod( | |
70 | bt2_value._create_from_ptr_and_get_ref | |
71 | ) | |
72 | ||
73 | def __setitem__(self, key, value): | |
74 | if isinstance(value, str): | |
75 | set_env_entry_fn = native_bt.trace_set_environment_entry_string | |
76 | elif isinstance(value, int): | |
77 | set_env_entry_fn = native_bt.trace_set_environment_entry_integer | |
78 | else: | |
79 | raise TypeError('expected str or int, got {}'.format(type(value))) | |
81447b5b | 80 | |
f0a42b33 FD |
81 | status = set_env_entry_fn(self._trace._ptr, key, value) |
82 | utils._handle_func_status(status, "cannot set trace object's environment entry") | |
81447b5b | 83 | |
f0a42b33 FD |
84 | def __delitem__(self, key): |
85 | raise NotImplementedError | |
86 | ||
87 | ||
88 | class _TraceConst(object._SharedObject, collections.abc.Mapping): | |
8c2367b8 SM |
89 | _get_ref = staticmethod(native_bt.trace_get_ref) |
90 | _put_ref = staticmethod(native_bt.trace_put_ref) | |
f0a42b33 FD |
91 | _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id_const) |
92 | _borrow_stream_ptr_by_index = staticmethod( | |
93 | native_bt.trace_borrow_stream_by_index_const | |
94 | ) | |
95 | _borrow_class_ptr = staticmethod(native_bt.trace_borrow_class_const) | |
96 | _borrow_user_attributes_ptr = staticmethod( | |
97 | native_bt.trace_borrow_user_attributes_const | |
98 | ) | |
99 | _create_value_from_ptr_and_get_ref = staticmethod( | |
100 | bt2_value._create_from_const_ptr_and_get_ref | |
101 | ) | |
102 | _stream_pycls = property(lambda _: bt2_stream._StreamConst) | |
103 | _trace_class_pycls = property(lambda _: bt2_trace_class._TraceClassConst) | |
104 | _trace_env_pycls = property(lambda _: _TraceEnvironmentConst) | |
811644b8 PP |
105 | |
106 | def __len__(self): | |
8c2367b8 SM |
107 | count = native_bt.trace_get_stream_count(self._ptr) |
108 | assert count >= 0 | |
811644b8 PP |
109 | return count |
110 | ||
8c2367b8 SM |
111 | def __getitem__(self, id): |
112 | utils._check_uint64(id) | |
81447b5b | 113 | |
f0a42b33 | 114 | stream_ptr = self._borrow_stream_ptr_by_id(self._ptr, id) |
81447b5b | 115 | |
8c2367b8 SM |
116 | if stream_ptr is None: |
117 | raise KeyError(id) | |
81447b5b | 118 | |
f0a42b33 | 119 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
120 | |
121 | def __iter__(self): | |
8c2367b8 | 122 | for idx in range(len(self)): |
f0a42b33 | 123 | stream_ptr = self._borrow_stream_ptr_by_index(self._ptr, idx) |
8c2367b8 | 124 | assert stream_ptr is not None |
81447b5b | 125 | |
8c2367b8 SM |
126 | id = native_bt.stream_get_id(stream_ptr) |
127 | assert id >= 0 | |
81447b5b | 128 | |
8c2367b8 | 129 | yield id |
81447b5b | 130 | |
05abc762 PP |
131 | @property |
132 | def cls(self): | |
f0a42b33 | 133 | trace_class_ptr = self._borrow_class_ptr(self._ptr) |
05abc762 | 134 | assert trace_class_ptr is not None |
f0a42b33 | 135 | return self._trace_class_pycls._create_from_ptr_and_get_ref(trace_class_ptr) |
05abc762 | 136 | |
5783664e PP |
137 | @property |
138 | def user_attributes(self): | |
f0a42b33 | 139 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
5783664e | 140 | assert ptr is not None |
f0a42b33 | 141 | return self._create_value_from_ptr_and_get_ref(ptr) |
5783664e | 142 | |
81447b5b PP |
143 | @property |
144 | def name(self): | |
50842bdc | 145 | return native_bt.trace_get_name(self._ptr) |
81447b5b | 146 | |
f0a42b33 FD |
147 | @property |
148 | def uuid(self): | |
149 | uuid_bytes = native_bt.trace_get_uuid(self._ptr) | |
150 | if uuid_bytes is None: | |
151 | return | |
152 | ||
153 | return uuidp.UUID(bytes=uuid_bytes) | |
154 | ||
155 | @property | |
156 | def environment(self): | |
157 | return self._trace_env_pycls(self) | |
158 | ||
159 | def add_destruction_listener(self, listener): | |
160 | '''Add a listener to be called when the trace is destroyed.''' | |
161 | if not callable(listener): | |
162 | raise TypeError("'listener' parameter is not callable") | |
163 | ||
164 | fn = native_bt.bt2_trace_add_destruction_listener | |
165 | listener_from_native = functools.partial( | |
166 | _trace_destruction_listener_from_native, listener | |
167 | ) | |
168 | ||
169 | status, listener_id = fn(self._ptr, listener_from_native) | |
170 | utils._handle_func_status( | |
171 | status, 'cannot add destruction listener to trace object' | |
172 | ) | |
173 | ||
174 | return utils._ListenerHandle(listener_id, self) | |
175 | ||
176 | ||
177 | class _Trace(_TraceConst): | |
178 | _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id) | |
179 | _borrow_stream_ptr_by_index = staticmethod(native_bt.trace_borrow_stream_by_index) | |
180 | _borrow_class_ptr = staticmethod(native_bt.trace_borrow_class) | |
181 | _borrow_user_attributes_ptr = staticmethod(native_bt.trace_borrow_user_attributes) | |
182 | _create_value_from_ptr_and_get_ref = staticmethod( | |
183 | bt2_value._create_from_ptr_and_get_ref | |
184 | ) | |
185 | _stream_pycls = property(lambda _: bt2_stream._Stream) | |
186 | _trace_class_pycls = property(lambda _: bt2_trace_class._TraceClass) | |
187 | _trace_env_pycls = property(lambda _: _TraceEnvironment) | |
188 | ||
8c2367b8 | 189 | def _name(self, name): |
81447b5b | 190 | utils._check_str(name) |
d24d5663 | 191 | status = native_bt.trace_set_name(self._ptr, name) |
cfbd7cf3 | 192 | utils._handle_func_status(status, "cannot set trace class object's name") |
81447b5b | 193 | |
8c2367b8 | 194 | _name = property(fset=_name) |
811644b8 | 195 | |
f0a42b33 FD |
196 | def _user_attributes(self, user_attributes): |
197 | value = bt2_value.create_value(user_attributes) | |
198 | utils._check_type(value, bt2_value.MapValue) | |
199 | native_bt.trace_set_user_attributes(self._ptr, value._ptr) | |
335a2da5 | 200 | |
f0a42b33 | 201 | _user_attributes = property(fset=_user_attributes) |
335a2da5 PP |
202 | |
203 | def _uuid(self, uuid): | |
204 | utils._check_type(uuid, uuidp.UUID) | |
205 | native_bt.trace_set_uuid(self._ptr, uuid.bytes) | |
206 | ||
207 | _uuid = property(fset=_uuid) | |
208 | ||
5783664e | 209 | def create_stream(self, stream_class, id=None, name=None, user_attributes=None): |
3fb99a22 | 210 | utils._check_type(stream_class, bt2_stream_class._StreamClass) |
81447b5b | 211 | |
8c2367b8 SM |
212 | if stream_class.assigns_automatic_stream_id: |
213 | if id is not None: | |
cfbd7cf3 FD |
214 | raise ValueError( |
215 | "id provided, but stream class assigns automatic stream ids" | |
216 | ) | |
81447b5b | 217 | |
8c2367b8 SM |
218 | stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) |
219 | else: | |
220 | if id is None: | |
cfbd7cf3 FD |
221 | raise ValueError( |
222 | "id not provided, but stream class does not assign automatic stream ids" | |
223 | ) | |
81447b5b | 224 | |
8c2367b8 | 225 | utils._check_uint64(id) |
cfbd7cf3 FD |
226 | stream_ptr = native_bt.stream_create_with_id( |
227 | stream_class._ptr, self._ptr, id | |
228 | ) | |
81447b5b | 229 | |
8c2367b8 | 230 | if stream_ptr is None: |
694c792b | 231 | raise bt2._MemoryError('cannot create stream object') |
81447b5b | 232 | |
3fb99a22 | 233 | stream = bt2_stream._Stream._create_from_ptr(stream_ptr) |
81447b5b | 234 | |
8c2367b8 SM |
235 | if name is not None: |
236 | stream._name = name | |
81447b5b | 237 | |
5783664e PP |
238 | if user_attributes is not None: |
239 | stream._user_attributes = user_attributes | |
240 | ||
8c2367b8 | 241 | return stream |
81447b5b | 242 | |
81447b5b | 243 | |
f0a42b33 FD |
244 | def _trace_destruction_listener_from_native(user_listener, trace_ptr): |
245 | trace = _TraceConst._create_from_ptr_and_get_ref(trace_ptr) | |
246 | user_listener(trace) |