5cbe82c64d6eda6f1e996a2b2071ede0bdab4cd8
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4
5 from bt2 import native_bt
6 from bt2 import object as bt2_object
7 from bt2 import utils as bt2_utils
8 import collections.abc
9 from bt2 import value as bt2_value
10 from bt2 import stream as bt2_stream
11 from bt2 import stream_class as bt2_stream_class
12 import bt2
13 import functools
14 import uuid as uuidp
15
16
17 def _bt2_trace_class():
18 from bt2 import trace_class as bt2_trace_class
19
20 return bt2_trace_class
21
22
23 class _TraceEnvironmentConst(collections.abc.Mapping):
24 _create_value_from_ptr_and_get_ref = staticmethod(
25 bt2_value._create_from_const_ptr_and_get_ref
26 )
27
28 def __init__(self, trace):
29 self._trace = trace
30
31 def __getitem__(self, key):
32 bt2_utils._check_str(key)
33
34 borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const
35 value_ptr = borrow_entry_fn(self._trace._ptr, key)
36
37 if value_ptr is None:
38 raise KeyError(key)
39
40 return self._create_value_from_ptr_and_get_ref(value_ptr)
41
42 def __len__(self):
43 count = native_bt.trace_get_environment_entry_count(self._trace._ptr)
44 assert count >= 0
45 return count
46
47 def __iter__(self):
48 trace_ptr = self._trace._ptr
49
50 for idx in range(len(self)):
51 borrow_entry_fn = native_bt.trace_borrow_environment_entry_by_index_const
52 entry_name, _ = borrow_entry_fn(trace_ptr, idx)
53 assert entry_name is not None
54 yield entry_name
55
56
57 class _TraceEnvironment(_TraceEnvironmentConst, collections.abc.MutableMapping):
58 _create_value_from_ptr_and_get_ref = staticmethod(
59 bt2_value._create_from_ptr_and_get_ref
60 )
61
62 def __setitem__(self, key, value):
63 if isinstance(value, str):
64 set_env_entry_fn = native_bt.trace_set_environment_entry_string
65 elif isinstance(value, int):
66 set_env_entry_fn = native_bt.trace_set_environment_entry_integer
67 else:
68 raise TypeError("expected str or int, got {}".format(type(value)))
69
70 status = set_env_entry_fn(self._trace._ptr, key, value)
71 bt2_utils._handle_func_status(
72 status, "cannot set trace object's environment entry"
73 )
74
75 def __delitem__(self, key):
76 raise NotImplementedError
77
78
79 class _TraceConst(bt2_object._SharedObject, collections.abc.Mapping):
80 @staticmethod
81 def _get_ref(ptr):
82 native_bt.trace_get_ref(ptr)
83
84 @staticmethod
85 def _put_ref(ptr):
86 native_bt.trace_put_ref(ptr)
87
88 _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id_const)
89 _borrow_stream_ptr_by_index = staticmethod(
90 native_bt.trace_borrow_stream_by_index_const
91 )
92 _borrow_class_ptr = staticmethod(native_bt.trace_borrow_class_const)
93 _borrow_user_attributes_ptr = staticmethod(
94 native_bt.trace_borrow_user_attributes_const
95 )
96 _create_value_from_ptr_and_get_ref = staticmethod(
97 bt2_value._create_from_const_ptr_and_get_ref
98 )
99 _stream_pycls = property(lambda _: bt2_stream._StreamConst)
100 _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClassConst)
101 _trace_env_pycls = property(lambda _: _TraceEnvironmentConst)
102
103 def __len__(self):
104 count = native_bt.trace_get_stream_count(self._ptr)
105 assert count >= 0
106 return count
107
108 def __getitem__(self, id):
109 bt2_utils._check_uint64(id)
110
111 stream_ptr = self._borrow_stream_ptr_by_id(self._ptr, id)
112
113 if stream_ptr is None:
114 raise KeyError(id)
115
116 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
117
118 def __iter__(self):
119 for idx in range(len(self)):
120 stream_ptr = self._borrow_stream_ptr_by_index(self._ptr, idx)
121 assert stream_ptr is not None
122
123 id = native_bt.stream_get_id(stream_ptr)
124 assert id >= 0
125
126 yield id
127
128 @property
129 def cls(self):
130 trace_class_ptr = self._borrow_class_ptr(self._ptr)
131 assert trace_class_ptr is not None
132 return self._trace_class_pycls._create_from_ptr_and_get_ref(trace_class_ptr)
133
134 @property
135 def user_attributes(self):
136 ptr = self._borrow_user_attributes_ptr(self._ptr)
137 assert ptr is not None
138 return self._create_value_from_ptr_and_get_ref(ptr)
139
140 @property
141 def name(self):
142 return native_bt.trace_get_name(self._ptr)
143
144 @property
145 def uuid(self):
146 uuid_bytes = native_bt.trace_get_uuid(self._ptr)
147 if uuid_bytes is None:
148 return
149
150 return uuidp.UUID(bytes=uuid_bytes)
151
152 @property
153 def environment(self):
154 return self._trace_env_pycls(self)
155
156 def add_destruction_listener(self, listener):
157 """Add a listener to be called when the trace is destroyed."""
158 if not callable(listener):
159 raise TypeError("'listener' parameter is not callable")
160
161 handle = bt2_utils._ListenerHandle(self.addr)
162
163 fn = native_bt.bt2_trace_add_destruction_listener
164 listener_from_native = functools.partial(
165 _trace_destruction_listener_from_native, listener, handle
166 )
167
168 status, listener_id = fn(self._ptr, listener_from_native)
169 bt2_utils._handle_func_status(
170 status, "cannot add destruction listener to trace object"
171 )
172
173 handle._set_listener_id(listener_id)
174
175 return handle
176
177 def remove_destruction_listener(self, listener_handle):
178 bt2_utils._check_type(listener_handle, bt2_utils._ListenerHandle)
179
180 if listener_handle._addr != self.addr:
181 raise ValueError(
182 "This trace destruction listener does not match the trace object."
183 )
184
185 if listener_handle._listener_id is None:
186 raise ValueError("This trace destruction listener was already removed.")
187
188 status = native_bt.trace_remove_destruction_listener(
189 self._ptr, listener_handle._listener_id
190 )
191 bt2_utils._handle_func_status(status)
192 listener_handle._invalidate()
193
194
195 class _Trace(_TraceConst):
196 _borrow_stream_ptr_by_id = staticmethod(native_bt.trace_borrow_stream_by_id)
197 _borrow_stream_ptr_by_index = staticmethod(native_bt.trace_borrow_stream_by_index)
198 _borrow_class_ptr = staticmethod(native_bt.trace_borrow_class)
199 _borrow_user_attributes_ptr = staticmethod(native_bt.trace_borrow_user_attributes)
200 _create_value_from_ptr_and_get_ref = staticmethod(
201 bt2_value._create_from_ptr_and_get_ref
202 )
203 _stream_pycls = property(lambda _: bt2_stream._Stream)
204 _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClass)
205 _trace_env_pycls = property(lambda _: _TraceEnvironment)
206
207 def _name(self, name):
208 bt2_utils._check_str(name)
209 status = native_bt.trace_set_name(self._ptr, name)
210 bt2_utils._handle_func_status(status, "cannot set trace class object's name")
211
212 _name = property(fset=_name)
213
214 def _user_attributes(self, user_attributes):
215 value = bt2_value.create_value(user_attributes)
216 bt2_utils._check_type(value, bt2_value.MapValue)
217 native_bt.trace_set_user_attributes(self._ptr, value._ptr)
218
219 _user_attributes = property(fset=_user_attributes)
220
221 def _uuid(self, uuid):
222 bt2_utils._check_type(uuid, uuidp.UUID)
223 native_bt.trace_set_uuid(self._ptr, uuid.bytes)
224
225 _uuid = property(fset=_uuid)
226
227 def create_stream(self, stream_class, id=None, name=None, user_attributes=None):
228 bt2_utils._check_type(stream_class, bt2_stream_class._StreamClass)
229
230 if stream_class.assigns_automatic_stream_id:
231 if id is not None:
232 raise ValueError(
233 "id provided, but stream class assigns automatic stream ids"
234 )
235
236 stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr)
237 else:
238 if id is None:
239 raise ValueError(
240 "id not provided, but stream class does not assign automatic stream ids"
241 )
242
243 bt2_utils._check_uint64(id)
244 stream_ptr = native_bt.stream_create_with_id(
245 stream_class._ptr, self._ptr, id
246 )
247
248 if stream_ptr is None:
249 raise bt2._MemoryError("cannot create stream object")
250
251 stream = bt2_stream._Stream._create_from_ptr(stream_ptr)
252
253 if name is not None:
254 stream._name = name
255
256 if user_attributes is not None:
257 stream._user_attributes = user_attributes
258
259 return stream
260
261
262 def _trace_destruction_listener_from_native(user_listener, handle, trace_ptr):
263 trace = _TraceConst._create_from_ptr_and_get_ref(trace_ptr)
264 user_listener(trace)
265 handle._invalidate()
This page took 0.052848 seconds and 3 git commands to generate.