python: standardize intra-bt2 imports
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace.py
CommitLineData
0235b0db 1# SPDX-License-Identifier: MIT
81447b5b 2#
811644b8 3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b 4
e5914347
SM
5from bt2 import native_bt
6from bt2 import object as bt2_object
7from bt2 import utils as bt2_utils
81447b5b 8import collections.abc
3fb99a22
PP
9from bt2 import value as bt2_value
10from bt2 import stream as bt2_stream
3fb99a22 11from bt2 import stream_class as bt2_stream_class
81447b5b 12import bt2
8c2367b8 13import functools
335a2da5
PP
14import uuid as uuidp
15
16
79935628
SM
17def _bt2_trace_class():
18 from bt2 import trace_class as bt2_trace_class
19
20 return bt2_trace_class
21
22
f0a42b33
FD
23class _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
335a2da5
PP
28 def __init__(self, trace):
29 self._trace = trace
30
31 def __getitem__(self, key):
e5914347 32 bt2_utils._check_str(key)
335a2da5
PP
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
f0a42b33 40 return self._create_value_from_ptr_and_get_ref(value_ptr)
335a2da5
PP
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):
f0a42b33 48 trace_ptr = self._trace._ptr
335a2da5
PP
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
81447b5b
PP
55
56
f0a42b33
FD
57class _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:
f5567ea8 68 raise TypeError("expected str or int, got {}".format(type(value)))
81447b5b 69
f0a42b33 70 status = set_env_entry_fn(self._trace._ptr, key, value)
e5914347
SM
71 bt2_utils._handle_func_status(
72 status, "cannot set trace object's environment entry"
73 )
81447b5b 74
f0a42b33
FD
75 def __delitem__(self, key):
76 raise NotImplementedError
77
78
e5914347 79class _TraceConst(bt2_object._SharedObject, collections.abc.Mapping):
9dee90bd
SM
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
f0a42b33
FD
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)
79935628 100 _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClassConst)
f0a42b33 101 _trace_env_pycls = property(lambda _: _TraceEnvironmentConst)
811644b8
PP
102
103 def __len__(self):
8c2367b8
SM
104 count = native_bt.trace_get_stream_count(self._ptr)
105 assert count >= 0
811644b8
PP
106 return count
107
8c2367b8 108 def __getitem__(self, id):
e5914347 109 bt2_utils._check_uint64(id)
81447b5b 110
f0a42b33 111 stream_ptr = self._borrow_stream_ptr_by_id(self._ptr, id)
81447b5b 112
8c2367b8
SM
113 if stream_ptr is None:
114 raise KeyError(id)
81447b5b 115
f0a42b33 116 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
81447b5b
PP
117
118 def __iter__(self):
8c2367b8 119 for idx in range(len(self)):
f0a42b33 120 stream_ptr = self._borrow_stream_ptr_by_index(self._ptr, idx)
8c2367b8 121 assert stream_ptr is not None
81447b5b 122
8c2367b8
SM
123 id = native_bt.stream_get_id(stream_ptr)
124 assert id >= 0
81447b5b 125
8c2367b8 126 yield id
81447b5b 127
05abc762
PP
128 @property
129 def cls(self):
f0a42b33 130 trace_class_ptr = self._borrow_class_ptr(self._ptr)
05abc762 131 assert trace_class_ptr is not None
f0a42b33 132 return self._trace_class_pycls._create_from_ptr_and_get_ref(trace_class_ptr)
05abc762 133
5783664e
PP
134 @property
135 def user_attributes(self):
f0a42b33 136 ptr = self._borrow_user_attributes_ptr(self._ptr)
5783664e 137 assert ptr is not None
f0a42b33 138 return self._create_value_from_ptr_and_get_ref(ptr)
5783664e 139
81447b5b
PP
140 @property
141 def name(self):
50842bdc 142 return native_bt.trace_get_name(self._ptr)
81447b5b 143
f0a42b33
FD
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):
f5567ea8 157 """Add a listener to be called when the trace is destroyed."""
f0a42b33
FD
158 if not callable(listener):
159 raise TypeError("'listener' parameter is not callable")
160
e5914347 161 handle = bt2_utils._ListenerHandle(self.addr)
67de22ba 162
f0a42b33
FD
163 fn = native_bt.bt2_trace_add_destruction_listener
164 listener_from_native = functools.partial(
67de22ba 165 _trace_destruction_listener_from_native, listener, handle
f0a42b33
FD
166 )
167
168 status, listener_id = fn(self._ptr, listener_from_native)
e5914347 169 bt2_utils._handle_func_status(
f5567ea8 170 status, "cannot add destruction listener to trace object"
f0a42b33
FD
171 )
172
67de22ba
SM
173 handle._set_listener_id(listener_id)
174
175 return handle
f0a42b33 176
1114a7d5 177 def remove_destruction_listener(self, listener_handle):
e5914347 178 bt2_utils._check_type(listener_handle, bt2_utils._ListenerHandle)
1114a7d5 179
67de22ba 180 if listener_handle._addr != self.addr:
1114a7d5 181 raise ValueError(
f5567ea8 182 "This trace destruction listener does not match the trace object."
1114a7d5
SM
183 )
184
185 if listener_handle._listener_id is None:
f5567ea8 186 raise ValueError("This trace destruction listener was already removed.")
1114a7d5
SM
187
188 status = native_bt.trace_remove_destruction_listener(
189 self._ptr, listener_handle._listener_id
190 )
e5914347 191 bt2_utils._handle_func_status(status)
67de22ba 192 listener_handle._invalidate()
1114a7d5 193
f0a42b33
FD
194
195class _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)
79935628 204 _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClass)
f0a42b33
FD
205 _trace_env_pycls = property(lambda _: _TraceEnvironment)
206
8c2367b8 207 def _name(self, name):
e5914347 208 bt2_utils._check_str(name)
d24d5663 209 status = native_bt.trace_set_name(self._ptr, name)
e5914347 210 bt2_utils._handle_func_status(status, "cannot set trace class object's name")
81447b5b 211
8c2367b8 212 _name = property(fset=_name)
811644b8 213
f0a42b33
FD
214 def _user_attributes(self, user_attributes):
215 value = bt2_value.create_value(user_attributes)
e5914347 216 bt2_utils._check_type(value, bt2_value.MapValue)
f0a42b33 217 native_bt.trace_set_user_attributes(self._ptr, value._ptr)
335a2da5 218
f0a42b33 219 _user_attributes = property(fset=_user_attributes)
335a2da5
PP
220
221 def _uuid(self, uuid):
e5914347 222 bt2_utils._check_type(uuid, uuidp.UUID)
335a2da5
PP
223 native_bt.trace_set_uuid(self._ptr, uuid.bytes)
224
225 _uuid = property(fset=_uuid)
226
5783664e 227 def create_stream(self, stream_class, id=None, name=None, user_attributes=None):
e5914347 228 bt2_utils._check_type(stream_class, bt2_stream_class._StreamClass)
81447b5b 229
8c2367b8
SM
230 if stream_class.assigns_automatic_stream_id:
231 if id is not None:
cfbd7cf3
FD
232 raise ValueError(
233 "id provided, but stream class assigns automatic stream ids"
234 )
81447b5b 235
8c2367b8
SM
236 stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr)
237 else:
238 if id is None:
cfbd7cf3
FD
239 raise ValueError(
240 "id not provided, but stream class does not assign automatic stream ids"
241 )
81447b5b 242
e5914347 243 bt2_utils._check_uint64(id)
cfbd7cf3
FD
244 stream_ptr = native_bt.stream_create_with_id(
245 stream_class._ptr, self._ptr, id
246 )
81447b5b 247
8c2367b8 248 if stream_ptr is None:
f5567ea8 249 raise bt2._MemoryError("cannot create stream object")
81447b5b 250
3fb99a22 251 stream = bt2_stream._Stream._create_from_ptr(stream_ptr)
81447b5b 252
8c2367b8
SM
253 if name is not None:
254 stream._name = name
81447b5b 255
5783664e
PP
256 if user_attributes is not None:
257 stream._user_attributes = user_attributes
258
8c2367b8 259 return stream
81447b5b 260
81447b5b 261
67de22ba 262def _trace_destruction_listener_from_native(user_listener, handle, trace_ptr):
f0a42b33
FD
263 trace = _TraceConst._create_from_ptr_and_get_ref(trace_ptr)
264 user_listener(trace)
67de22ba 265 handle._invalidate()
This page took 0.090207 seconds and 4 git commands to generate.