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