bt2: change some bt2.CreationError usages to ValueError
[babeltrace.git] / bindings / python / bt2 / bt2 / trace_class.py
CommitLineData
fbbe9302
SM
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
27import bt2
28from bt2 import native_bt, utils, object
29import uuid as uuidp
30import collections.abc
31import functools
32
33
34class _TraceClassEnv(collections.abc.MutableMapping):
35 def __init__(self, trace_class):
36 self._trace_class = trace_class
37
38 def __getitem__(self, key):
39 utils._check_str(key)
40
41 borrow_entry_fn = native_bt.trace_class_borrow_environment_entry_value_by_name_const
42 value_ptr = borrow_entry_fn(self._trace_class._ptr, key)
43
44 if value_ptr is None:
45 raise KeyError(key)
46
47 return bt2.value._create_from_ptr_and_get_ref(value_ptr)
48
49 def __setitem__(self, key, value):
50 if isinstance(value, str):
51 set_env_entry_fn = native_bt.trace_class_set_environment_entry_string
52 elif isinstance(value, int):
53 set_env_entry_fn = native_bt.trace_class_set_environment_entry_integer
54 else:
55 raise TypeError('expected str or int, got {}'.format(type(value)))
56
57 ret = set_env_entry_fn(self._trace_class._ptr, key, value)
58
59 utils._handle_ret(ret, "cannot set trace class object's environment entry")
60
61 def __delitem__(self, key):
62 raise NotImplementedError
63
64 def __len__(self):
65 count = native_bt.trace_class_get_environment_entry_count(self._trace_class._ptr)
66 assert count >= 0
67 return count
68
69 def __iter__(self):
70 trace_class_ptr = self._trace_class_env._trace_class._ptr
71
72 for idx in range(len(self)):
73 borrow_entry_fn = native_bt.trace_class_borrow_environment_entry_by_index_const
74 entry_name, _ = borrow_entry_fn(trace_class_ptr, idx)
75 assert entry_name is not None
76 yield entry_name
77
78
79class _StreamClassIterator(collections.abc.Iterator):
80 def __init__(self, trace_class):
81 self._trace_class = trace_class
82 self._at = 0
83
84 def __next__(self):
85 if self._at == len(self._trace_class):
86 raise StopIteration
87
88 borrow_stream_class_fn = native_bt.trace_class_borrow_stream_class_by_index_const
89 sc_ptr = borrow_stream_class_fn(self._trace_class._ptr, self._at)
90 assert sc_ptr
91 id = native_bt.stream_class_get_id(sc_ptr)
92 assert id >= 0
93 self._at += 1
94 return id
95
96
97def _trace_class_destruction_listener_from_native(user_listener, trace_class_ptr):
98 trace_class = bt2.trace_class.TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
99 user_listener(trace_class)
100
101
102class TraceClass(object._SharedObject, collections.abc.Mapping):
103 _get_ref = staticmethod(native_bt.trace_class_get_ref)
104 _put_ref = staticmethod(native_bt.trace_class_put_ref)
105
106 @property
107 def uuid(self):
108 uuid_bytes = native_bt.trace_class_get_uuid(self._ptr)
109 if uuid_bytes is None:
110 return
111
112 return uuidp.UUID(bytes=uuid_bytes)
113
114 def _uuid(self, uuid):
115 utils._check_type(uuid, uuidp.UUID)
116 native_bt.trace_class_set_uuid(self._ptr, uuid.bytes)
117
118 _uuid = property(fset=_uuid)
119
8c2367b8
SM
120 # Instantiate a trace of this class.
121
122 def __call__(self, name=None):
123 trace_ptr = native_bt.trace_create(self._ptr)
124
125 if trace_ptr is None:
126 raise bt2.CreationError('cannot create trace class object')
127
128 trace = bt2.trace.Trace._create_from_ptr(trace_ptr)
129
130 if name is not None:
131 trace._name = name
132
133 return trace
134
fbbe9302
SM
135 # Number of stream classes in this trace class.
136
137 def __len__(self):
138 count = native_bt.trace_class_get_stream_class_count(self._ptr)
139 assert count >= 0
140 return count
141
142 # Get a stream class by stream id.
143
144 def __getitem__(self, key):
145 utils._check_uint64(key)
146
147 sc_ptr = native_bt.trace_class_borrow_stream_class_by_id_const(self._ptr, key)
148 if sc_ptr is None:
149 raise KeyError(key)
150
151 return bt2.StreamClass._create_from_ptr_and_get_ref(sc_ptr)
152
153 def __iter__(self):
154 for idx in range(len(self)):
155 sc_ptr = native_bt.trace_class_borrow_stream_class_by_index_const(self._ptr, idx)
156 assert sc_ptr is not None
157
158 id = native_bt.stream_class_get_id(sc_ptr)
159 assert id >= 0
160
161 yield id
162
163 @property
164 def env(self):
165 return _TraceClassEnv(self)
166
8c2367b8 167 def create_stream_class(self, id=None,
3cdfbaea
SM
168 name=None,
169 packet_context_field_class=None,
170 event_common_context_field_class=None,
171 default_clock_class=None,
172 assigns_automatic_event_class_id=True,
173 assigns_automatic_stream_id=True,
174 packets_have_default_beginning_clock_snapshot=False,
175 packets_have_default_end_clock_snapshot=False):
fbbe9302
SM
176
177 if self.assigns_automatic_stream_class_id:
178 if id is not None:
4430bc80 179 raise ValueError('id provided, but trace class assigns automatic stream class ids')
fbbe9302
SM
180
181 sc_ptr = native_bt.stream_class_create(self._ptr)
182 else:
183 if id is None:
4430bc80 184 raise ValueError('id not provided, but trace class does not assign automatic stream class ids')
fbbe9302
SM
185
186 utils._check_uint64(id)
187 sc_ptr = native_bt.stream_class_create_with_id(self._ptr, id)
188
8c2367b8
SM
189 sc = bt2.stream_class.StreamClass._create_from_ptr(sc_ptr)
190
3cdfbaea
SM
191 if name is not None:
192 sc._name = name
193
194 if packet_context_field_class is not None:
195 sc._packet_context_field_class = packet_context_field_class
196
197 if event_common_context_field_class is not None:
198 sc._event_common_context_field_class = event_common_context_field_class
199
200 if default_clock_class is not None:
201 sc._default_clock_class = default_clock_class
202
203 sc._assigns_automatic_event_class_id = assigns_automatic_event_class_id
8c2367b8 204 sc._assigns_automatic_stream_id = assigns_automatic_stream_id
3cdfbaea
SM
205 sc._packets_have_default_beginning_clock_snapshot = packets_have_default_beginning_clock_snapshot
206 sc._packets_have_default_end_clock_snapshot = packets_have_default_end_clock_snapshot
8c2367b8
SM
207
208 return sc
fbbe9302
SM
209
210 @property
211 def assigns_automatic_stream_class_id(self):
212 return native_bt.trace_class_assigns_automatic_stream_class_id(self._ptr)
213
214 def _assigns_automatic_stream_class_id(self, auto_id):
215 utils._check_bool(auto_id)
216 return native_bt.trace_class_set_assigns_automatic_stream_class_id(self._ptr, auto_id)
217
218 _assigns_automatic_stream_class_id = property(fset=_assigns_automatic_stream_class_id)
219
3cdfbaea
SM
220 # Field class creation methods.
221
222 def _check_create_status(self, ptr, type_name):
223 if ptr is None:
224 raise bt2.CreationError(
225 'cannot create {} field class'.format(type_name))
226
d47b87ac 227 def _create_integer_field_class(self, create_func, py_cls, type_name, field_value_range, preferred_display_base):
af4bbfc7
SM
228 field_class_ptr = create_func(self._ptr)
229 self._check_create_status(field_class_ptr, type_name)
230
231 field_class = py_cls._create_from_ptr(field_class_ptr)
232
d47b87ac
SM
233 if field_value_range is not None:
234 field_class._field_value_range = field_value_range
af4bbfc7 235
d47b87ac
SM
236 if preferred_display_base is not None:
237 field_class._preferred_display_base = preferred_display_base
af4bbfc7
SM
238
239 return field_class
240
d47b87ac 241 def create_signed_integer_field_class(self, field_value_range=None, preferred_display_base=None):
af4bbfc7 242 return self._create_integer_field_class(native_bt.field_class_signed_integer_create,
d47b87ac
SM
243 bt2.field_class._SignedIntegerFieldClass,
244 'signed integer', field_value_range, preferred_display_base)
af4bbfc7 245
d47b87ac 246 def create_unsigned_integer_field_class(self, field_value_range=None, preferred_display_base=None):
2ae9f48c 247 return self._create_integer_field_class(native_bt.field_class_unsigned_integer_create,
d47b87ac
SM
248 bt2.field_class._UnsignedIntegerFieldClass,
249 'unsigned integer', field_value_range, preferred_display_base)
250
251 def create_signed_enumeration_field_class(self, field_value_range=None, preferred_display_base=None):
252 return self._create_integer_field_class(native_bt.field_class_signed_enumeration_create,
253 bt2.field_class._SignedEnumerationFieldClass,
254 'signed enumeration', field_value_range, preferred_display_base)
255
256 def create_unsigned_enumeration_field_class(self, field_value_range=None, preferred_display_base=None):
257 return self._create_integer_field_class(native_bt.field_class_unsigned_enumeration_create,
258 bt2.field_class._UnsignedEnumerationFieldClass,
259 'unsigned enumeration', field_value_range, preferred_display_base)
2ae9f48c
SM
260
261 def create_real_field_class(self, is_single_precision=False):
262 field_class_ptr = native_bt.field_class_real_create(self._ptr)
263 self._check_create_status(field_class_ptr, 'real')
264
d47b87ac 265 field_class = bt2.field_class._RealFieldClass._create_from_ptr(field_class_ptr)
2ae9f48c 266
d47b87ac 267 field_class._is_single_precision = is_single_precision
2ae9f48c
SM
268
269 return field_class
270
3cdfbaea
SM
271 def create_structure_field_class(self):
272 field_class_ptr = native_bt.field_class_structure_create(self._ptr)
273 self._check_create_status(field_class_ptr, 'structure')
274
275 return bt2.field_class._StructureFieldClass._create_from_ptr(field_class_ptr)
276
277 def create_string_field_class(self):
278 field_class_ptr = native_bt.field_class_string_create(self._ptr)
279 self._check_create_status(field_class_ptr, 'string')
280
d47b87ac
SM
281 return bt2.field_class._StringFieldClass._create_from_ptr(field_class_ptr)
282
283 def create_static_array_field_class(self, elem_fc, length):
284 utils._check_type(elem_fc, bt2.field_class._FieldClass)
285 utils._check_uint64(length)
286 ptr = native_bt.field_class_static_array_create(self._ptr, elem_fc._ptr, length)
287 self._check_create_status(ptr, 'static array')
288
289 return bt2.field_class._StaticArrayFieldClass._create_from_ptr_and_get_ref(ptr)
290
291 def create_dynamic_array_field_class(self, elem_fc, length_fc=None):
292 utils._check_type(elem_fc, bt2.field_class._FieldClass)
293 ptr = native_bt.field_class_dynamic_array_create(self._ptr, elem_fc._ptr)
294 self._check_create_status(ptr, 'dynamic array')
295 obj = bt2.field_class._DynamicArrayFieldClass._create_from_ptr(ptr)
296
297 if length_fc is not None:
298 obj._length_field_class = length_fc
299
300 return obj
301
302 def create_variant_field_class(self, selector_fc=None):
303 ptr = native_bt.field_class_variant_create(self._ptr)
304 self._check_create_status(ptr, 'variant')
305 obj = bt2.field_class._VariantFieldClass._create_from_ptr(ptr)
306
307 if selector_fc is not None:
308 obj._selector_field_class = selector_fc
309
310 return obj
3cdfbaea 311
fbbe9302
SM
312 # Add a listener to be called when the trace class is destroyed.
313
314 def add_destruction_listener(self, listener):
315
316 if not callable(listener):
317 raise TypeError("'listener' parameter is not callable")
318
319 fn = native_bt.py3_trace_class_add_destruction_listener
320 listener_from_native = functools.partial(_trace_class_destruction_listener_from_native,
321 listener)
322
323 listener_id = fn(self._ptr, listener_from_native)
324 if listener_id is None:
325 utils._raise_bt2_error('cannot add destruction listener to trace class object')
326
327 return bt2._ListenerHandle(listener_id, self)
This page took 0.035102 seconds and 4 git commands to generate.