bt2: clean available `bt2` package names
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace_class.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4 # Copyright (c) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
5 # Copyright (c) 2019 Simon Marchi <simon.marchi@efficios.com>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24
25 __all__ = ['_TraceClass']
26
27 import bt2
28 from bt2 import native_bt, utils, object
29 from bt2 import stream_class as bt2_stream_class
30 from bt2 import field_class as bt2_field_class
31 from bt2 import trace as bt2_trace
32 from bt2 import trace_class as bt2_trace_class
33 import collections.abc
34 import functools
35
36
37 class _StreamClassIterator(collections.abc.Iterator):
38 def __init__(self, trace_class):
39 self._trace_class = trace_class
40 self._at = 0
41
42 def __next__(self):
43 if self._at == len(self._trace_class):
44 raise StopIteration
45
46 borrow_stream_class_fn = (
47 native_bt.trace_class_borrow_stream_class_by_index_const
48 )
49 sc_ptr = borrow_stream_class_fn(self._trace_class._ptr, self._at)
50 assert sc_ptr
51 id = native_bt.stream_class_get_id(sc_ptr)
52 assert id >= 0
53 self._at += 1
54 return id
55
56
57 def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
58 trace_class = bt2_trace_class._TraceClass._create_from_ptr_and_get_ref(
59 trace_class_ptr
60 )
61 user_listener(trace_class)
62
63
64 class _TraceClass(object._SharedObject, collections.abc.Mapping):
65 _get_ref = staticmethod(native_bt.trace_class_get_ref)
66 _put_ref = staticmethod(native_bt.trace_class_put_ref)
67
68 # Instantiate a trace of this class.
69
70 def __call__(self, name=None, uuid=None, env=None):
71 trace_ptr = native_bt.trace_create(self._ptr)
72
73 if trace_ptr is None:
74 raise bt2._MemoryError('cannot create trace class object')
75
76 trace = bt2_trace._Trace._create_from_ptr(trace_ptr)
77
78 if name is not None:
79 trace._name = name
80
81 if uuid is not None:
82 trace._uuid = uuid
83
84 if env is not None:
85 for key, value in env.items():
86 trace.env[key] = value
87
88 return trace
89
90 # Number of stream classes in this trace class.
91
92 def __len__(self):
93 count = native_bt.trace_class_get_stream_class_count(self._ptr)
94 assert count >= 0
95 return count
96
97 # Get a stream class by stream id.
98
99 def __getitem__(self, key):
100 utils._check_uint64(key)
101
102 sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key)
103 if sc_ptr is None:
104 raise KeyError(key)
105
106 return bt2_stream_class._StreamClass._create_from_ptr_and_get_ref(sc_ptr)
107
108 def __iter__(self):
109 for idx in range(len(self)):
110 sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const(
111 self._ptr, idx
112 )
113 assert sc_ptr is not None
114
115 id = native_bt.stream_class_get_id(sc_ptr)
116 assert id >= 0
117
118 yield id
119
120 def create_stream_class(
121 self,
122 id=None,
123 name=None,
124 packet_context_field_class=None,
125 event_common_context_field_class=None,
126 default_clock_class=None,
127 assigns_automatic_event_class_id=True,
128 assigns_automatic_stream_id=True,
129 supports_packets=False,
130 packets_have_beginning_default_clock_snapshot=False,
131 packets_have_end_default_clock_snapshot=False,
132 supports_discarded_events=False,
133 discarded_events_have_default_clock_snapshots=False,
134 supports_discarded_packets=False,
135 discarded_packets_have_default_clock_snapshots=False,
136 ):
137
138 if self.assigns_automatic_stream_class_id:
139 if id is not None:
140 raise ValueError(
141 'id provided, but trace class assigns automatic stream class ids'
142 )
143
144 sc_ptr = native_bt.stream_class_create(self._ptr)
145 else:
146 if id is None:
147 raise ValueError(
148 'id not provided, but trace class does not assign automatic stream class ids'
149 )
150
151 utils._check_uint64(id)
152 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
153
154 sc = bt2_stream_class._StreamClass._create_from_ptr(sc_ptr)
155
156 if name is not None:
157 sc._name = name
158
159 if event_common_context_field_class is not None:
160 sc._event_common_context_field_class = event_common_context_field_class
161
162 if default_clock_class is not None:
163 sc._default_clock_class = default_clock_class
164
165 # call after `sc._default_clock_class` because, if
166 # `packets_have_beginning_default_clock_snapshot` or
167 # `packets_have_end_default_clock_snapshot` is true, then this
168 # stream class needs a default clock class already.
169 sc._set_supports_packets(
170 supports_packets,
171 packets_have_beginning_default_clock_snapshot,
172 packets_have_end_default_clock_snapshot,
173 )
174
175 # call after sc._set_supports_packets() because, if
176 # `packet_context_field_class` is not `None`, then this stream
177 # class needs to support packets already.
178 if packet_context_field_class is not None:
179 sc._packet_context_field_class = packet_context_field_class
180
181 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
182 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
183 sc._set_supports_discarded_events(
184 supports_discarded_events, discarded_events_have_default_clock_snapshots
185 )
186 sc._set_supports_discarded_packets(
187 supports_discarded_packets, discarded_packets_have_default_clock_snapshots
188 )
189 return sc
190
191 @property
192 def assigns_automatic_stream_class_id(self):
193 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
194
195 def _assigns_automatic_stream_class_id(self, auto_id):
196 utils._check_bool(auto_id)
197 return native_bt.trace_class_set_assigns_automatic_stream_class_id(
198 self._ptr, auto_id
199 )
200
201 _assigns_automatic_stream_class_id = property(
202 fset=_assigns_automatic_stream_class_id
203 )
204
205 # Field class creation methods.
206
207 def _check_create_status(self, ptr, type_name):
208 if ptr is None:
209 raise bt2._MemoryError('cannot create {} field class'.format(type_name))
210
211 def _create_integer_field_class(
212 self, create_func, py_cls, type_name, field_value_range, preferred_display_base
213 ):
214 field_class_ptr = create_func(self._ptr)
215 self._check_create_status(field_class_ptr, type_name)
216
217 field_class = py_cls._create_from_ptr(field_class_ptr)
218
219 if field_value_range is not None:
220 field_class._field_value_range = field_value_range
221
222 if preferred_display_base is not None:
223 field_class._preferred_display_base = preferred_display_base
224
225 return field_class
226
227 def create_signed_integer_field_class(
228 self, field_value_range=None, preferred_display_base=None
229 ):
230 return self._create_integer_field_class(
231 native_bt.field_class_integer_signed_create,
232 bt2_field_class._SignedIntegerFieldClass,
233 'signed integer',
234 field_value_range,
235 preferred_display_base,
236 )
237
238 def create_unsigned_integer_field_class(
239 self, field_value_range=None, preferred_display_base=None
240 ):
241 return self._create_integer_field_class(
242 native_bt.field_class_integer_unsigned_create,
243 bt2_field_class._UnsignedIntegerFieldClass,
244 'unsigned integer',
245 field_value_range,
246 preferred_display_base,
247 )
248
249 def create_signed_enumeration_field_class(
250 self, field_value_range=None, preferred_display_base=None
251 ):
252 return self._create_integer_field_class(
253 native_bt.field_class_enumeration_signed_create,
254 bt2_field_class._SignedEnumerationFieldClass,
255 'signed enumeration',
256 field_value_range,
257 preferred_display_base,
258 )
259
260 def create_unsigned_enumeration_field_class(
261 self, field_value_range=None, preferred_display_base=None
262 ):
263 return self._create_integer_field_class(
264 native_bt.field_class_enumeration_unsigned_create,
265 bt2_field_class._UnsignedEnumerationFieldClass,
266 'unsigned enumeration',
267 field_value_range,
268 preferred_display_base,
269 )
270
271 def create_real_field_class(self, is_single_precision=False):
272 field_class_ptr = native_bt.field_class_real_create(self._ptr)
273 self._check_create_status(field_class_ptr, 'real')
274
275 field_class = bt2_field_class._RealFieldClass._create_from_ptr(field_class_ptr)
276
277 field_class._is_single_precision = is_single_precision
278
279 return field_class
280
281 def create_structure_field_class(self):
282 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
283 self._check_create_status(field_class_ptr, 'structure')
284
285 return bt2_field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
286
287 def create_string_field_class(self):
288 field_class_ptr = native_bt.field_class_string_create(self._ptr)
289 self._check_create_status(field_class_ptr, 'string')
290
291 return bt2_field_class._StringFieldClass._create_from_ptr(field_class_ptr)
292
293 def create_static_array_field_class(self, elem_fc, length):
294 utils._check_type(elem_fc, bt2_field_class._FieldClass)
295 utils._check_uint64(length)
296 ptr = native_bt.field_class_array_static_create(self._ptr, elem_fc._ptr, length)
297 self._check_create_status(ptr, 'static array')
298
299 return bt2_field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr)
300
301 def create_dynamic_array_field_class(self, elem_fc, length_fc=None):
302 utils._check_type(elem_fc, bt2_field_class._FieldClass)
303 length_fc_ptr = None
304
305 if length_fc is not None:
306 utils._check_type(length_fc, bt2_field_class._UnsignedIntegerFieldClass)
307 length_fc_ptr = length_fc._ptr
308
309 ptr = native_bt.field_class_array_dynamic_create(
310 self._ptr, elem_fc._ptr, length_fc_ptr
311 )
312 self._check_create_status(ptr, 'dynamic array')
313 return bt2_field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
314
315 def create_variant_field_class(self, selector_fc=None):
316 selector_fc_ptr = None
317
318 if selector_fc is not None:
319 utils._check_type(selector_fc, bt2_field_class._IntegerFieldClass)
320 selector_fc_ptr = selector_fc._ptr
321
322 ptr = native_bt.field_class_variant_create(self._ptr, selector_fc_ptr)
323 self._check_create_status(ptr, 'variant')
324 return bt2_field_class._create_field_class_from_ptr_and_get_ref(ptr)
325
326 # Add a listener to be called when the trace class is destroyed.
327
328 def add_destruction_listener(self, listener):
329
330 if not callable(listener):
331 raise TypeError("'listener' parameter is not callable")
332
333 fn = native_bt.bt2_trace_class_add_destruction_listener
334 listener_from_native = functools.partial(
335 _trace_class_destruction_listener_from_native, listener
336 )
337
338 status, listener_id = fn(self._ptr, listener_from_native)
339 utils._handle_func_status(
340 status, 'cannot add destruction listener to trace class object'
341 )
342
343 return utils._ListenerHandle(listener_id, self)
This page took 0.036409 seconds and 4 git commands to generate.