bt2: Add remaining trace-ir `*Const` classes and adapt tests
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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
24 import collections.abc
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
29 import bt2
30 import functools
31 import uuid as uuidp
32
33
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
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
51 return self._create_value_from_ptr_and_get_ref(value_ptr)
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):
59 trace_ptr = self._trace._ptr
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
66
67
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)))
80
81 status = set_env_entry_fn(self._trace._ptr, key, value)
82 utils._handle_func_status(status, "cannot set trace object's environment entry")
83
84 def __delitem__(self, key):
85 raise NotImplementedError
86
87
88 class _TraceConst(object._SharedObject, collections.abc.Mapping):
89 _get_ref = staticmethod(native_bt.trace_get_ref)
90 _put_ref = staticmethod(native_bt.trace_put_ref)
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)
105
106 def __len__(self):
107 count = native_bt.trace_get_stream_count(self._ptr)
108 assert count >= 0
109 return count
110
111 def __getitem__(self, id):
112 utils._check_uint64(id)
113
114 stream_ptr = self._borrow_stream_ptr_by_id(self._ptr, id)
115
116 if stream_ptr is None:
117 raise KeyError(id)
118
119 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
120
121 def __iter__(self):
122 for idx in range(len(self)):
123 stream_ptr = self._borrow_stream_ptr_by_index(self._ptr, idx)
124 assert stream_ptr is not None
125
126 id = native_bt.stream_get_id(stream_ptr)
127 assert id >= 0
128
129 yield id
130
131 @property
132 def cls(self):
133 trace_class_ptr = self._borrow_class_ptr(self._ptr)
134 assert trace_class_ptr is not None
135 return self._trace_class_pycls._create_from_ptr_and_get_ref(trace_class_ptr)
136
137 @property
138 def user_attributes(self):
139 ptr = self._borrow_user_attributes_ptr(self._ptr)
140 assert ptr is not None
141 return self._create_value_from_ptr_and_get_ref(ptr)
142
143 @property
144 def name(self):
145 return native_bt.trace_get_name(self._ptr)
146
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
189 def _name(self, name):
190 utils._check_str(name)
191 status = native_bt.trace_set_name(self._ptr, name)
192 utils._handle_func_status(status, "cannot set trace class object's name")
193
194 _name = property(fset=_name)
195
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)
200
201 _user_attributes = property(fset=_user_attributes)
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
209 def create_stream(self, stream_class, id=None, name=None, user_attributes=None):
210 utils._check_type(stream_class, bt2_stream_class._StreamClass)
211
212 if stream_class.assigns_automatic_stream_id:
213 if id is not None:
214 raise ValueError(
215 "id provided, but stream class assigns automatic stream ids"
216 )
217
218 stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr)
219 else:
220 if id is None:
221 raise ValueError(
222 "id not provided, but stream class does not assign automatic stream ids"
223 )
224
225 utils._check_uint64(id)
226 stream_ptr = native_bt.stream_create_with_id(
227 stream_class._ptr, self._ptr, id
228 )
229
230 if stream_ptr is None:
231 raise bt2._MemoryError('cannot create stream object')
232
233 stream = bt2_stream._Stream._create_from_ptr(stream_ptr)
234
235 if name is not None:
236 stream._name = name
237
238 if user_attributes is not None:
239 stream._user_attributes = user_attributes
240
241 return stream
242
243
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)
This page took 0.03942 seconds and 4 git commands to generate.