python: make all _get_ref/_put_ref proper static methods
[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, object, utils
6 import collections.abc
7 from bt2 import value as bt2_value
8 from bt2 import stream as bt2_stream
9 from bt2 import stream_class as bt2_stream_class
10 import bt2
11 import functools
12 import uuid as uuidp
13
14
15 def _bt2_trace_class():
16 from bt2 import trace_class as bt2_trace_class
17
18 return bt2_trace_class
19
20
21 class _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
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
38 return self._create_value_from_ptr_and_get_ref(value_ptr)
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):
46 trace_ptr = self._trace._ptr
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
53
54
55 class _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:
66 raise TypeError("expected str or int, got {}".format(type(value)))
67
68 status = set_env_entry_fn(self._trace._ptr, key, value)
69 utils._handle_func_status(status, "cannot set trace object's environment entry")
70
71 def __delitem__(self, key):
72 raise NotImplementedError
73
74
75 class _TraceConst(object._SharedObject, collections.abc.Mapping):
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
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)
96 _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClassConst)
97 _trace_env_pycls = property(lambda _: _TraceEnvironmentConst)
98
99 def __len__(self):
100 count = native_bt.trace_get_stream_count(self._ptr)
101 assert count >= 0
102 return count
103
104 def __getitem__(self, id):
105 utils._check_uint64(id)
106
107 stream_ptr = self._borrow_stream_ptr_by_id(self._ptr, id)
108
109 if stream_ptr is None:
110 raise KeyError(id)
111
112 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
113
114 def __iter__(self):
115 for idx in range(len(self)):
116 stream_ptr = self._borrow_stream_ptr_by_index(self._ptr, idx)
117 assert stream_ptr is not None
118
119 id = native_bt.stream_get_id(stream_ptr)
120 assert id >= 0
121
122 yield id
123
124 @property
125 def cls(self):
126 trace_class_ptr = self._borrow_class_ptr(self._ptr)
127 assert trace_class_ptr is not None
128 return self._trace_class_pycls._create_from_ptr_and_get_ref(trace_class_ptr)
129
130 @property
131 def user_attributes(self):
132 ptr = self._borrow_user_attributes_ptr(self._ptr)
133 assert ptr is not None
134 return self._create_value_from_ptr_and_get_ref(ptr)
135
136 @property
137 def name(self):
138 return native_bt.trace_get_name(self._ptr)
139
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):
153 """Add a listener to be called when the trace is destroyed."""
154 if not callable(listener):
155 raise TypeError("'listener' parameter is not callable")
156
157 handle = utils._ListenerHandle(self.addr)
158
159 fn = native_bt.bt2_trace_add_destruction_listener
160 listener_from_native = functools.partial(
161 _trace_destruction_listener_from_native, listener, handle
162 )
163
164 status, listener_id = fn(self._ptr, listener_from_native)
165 utils._handle_func_status(
166 status, "cannot add destruction listener to trace object"
167 )
168
169 handle._set_listener_id(listener_id)
170
171 return handle
172
173 def remove_destruction_listener(self, listener_handle):
174 utils._check_type(listener_handle, utils._ListenerHandle)
175
176 if listener_handle._addr != self.addr:
177 raise ValueError(
178 "This trace destruction listener does not match the trace object."
179 )
180
181 if listener_handle._listener_id is None:
182 raise ValueError("This trace destruction listener was already removed.")
183
184 status = native_bt.trace_remove_destruction_listener(
185 self._ptr, listener_handle._listener_id
186 )
187 utils._handle_func_status(status)
188 listener_handle._invalidate()
189
190
191 class _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)
200 _trace_class_pycls = property(lambda _: _bt2_trace_class()._TraceClass)
201 _trace_env_pycls = property(lambda _: _TraceEnvironment)
202
203 def _name(self, name):
204 utils._check_str(name)
205 status = native_bt.trace_set_name(self._ptr, name)
206 utils._handle_func_status(status, "cannot set trace class object's name")
207
208 _name = property(fset=_name)
209
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)
214
215 _user_attributes = property(fset=_user_attributes)
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
223 def create_stream(self, stream_class, id=None, name=None, user_attributes=None):
224 utils._check_type(stream_class, bt2_stream_class._StreamClass)
225
226 if stream_class.assigns_automatic_stream_id:
227 if id is not None:
228 raise ValueError(
229 "id provided, but stream class assigns automatic stream ids"
230 )
231
232 stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr)
233 else:
234 if id is None:
235 raise ValueError(
236 "id not provided, but stream class does not assign automatic stream ids"
237 )
238
239 utils._check_uint64(id)
240 stream_ptr = native_bt.stream_create_with_id(
241 stream_class._ptr, self._ptr, id
242 )
243
244 if stream_ptr is None:
245 raise bt2._MemoryError("cannot create stream object")
246
247 stream = bt2_stream._Stream._create_from_ptr(stream_ptr)
248
249 if name is not None:
250 stream._name = name
251
252 if user_attributes is not None:
253 stream._user_attributes = user_attributes
254
255 return stream
256
257
258 def _trace_destruction_listener_from_native(user_listener, handle, trace_ptr):
259 trace = _TraceConst._create_from_ptr_and_get_ref(trace_ptr)
260 user_listener(trace)
261 handle._invalidate()
This page took 0.034117 seconds and 4 git commands to generate.