Commit | Line | Data |
---|---|---|
be5a4e67 PP |
1 | # writer.py |
2 | # | |
3 | # Babeltrace writer interface Python module | |
4 | # | |
5 | # Copyright 2012-2015 EfficiOS Inc. | |
6 | # | |
7 | # Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
8 | # | |
9 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | # of this software and associated documentation files (the "Software"), to deal | |
11 | # in the Software without restriction, including without limitation the rights | |
12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | # copies of the Software, and to permit persons to whom the Software is | |
14 | # furnished to do so, subject to the following conditions: | |
15 | # | |
16 | # The above copyright notice and this permission notice shall be included in | |
17 | # all copies or substantial portions of the Software. | |
18 | # | |
19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | # SOFTWARE. | |
26 | ||
27 | import babeltrace.nativebt as nbt | |
28 | import babeltrace.common as common | |
29 | from uuid import UUID | |
30 | ||
31 | ||
32 | # Used to compare to -1ULL in error checks | |
33 | _MAX_UINT64 = 0xFFFFFFFFFFFFFFFF | |
34 | ||
35 | ||
36 | class EnumerationMapping: | |
37 | """ | |
1759e132 | 38 | Mapping from an enumeration label to a range of integers. |
be5a4e67 PP |
39 | """ |
40 | ||
41 | def __init__(self, name, start, end): | |
1759e132 PP |
42 | """ |
43 | Creates an enumeration mapping, where label *name* is mapped to | |
44 | the [*start*, *end*] range of integers (*end* is included). | |
45 | ||
46 | Set *start* and *end* to the same value to create an enumeration | |
47 | mapping to a single value. | |
48 | """ | |
49 | ||
be5a4e67 PP |
50 | self.name = name |
51 | self.start = start | |
52 | self.end = end | |
53 | ||
54 | ||
55 | class Clock: | |
56 | def __init__(self, name): | |
57 | self._c = nbt._bt_ctf_clock_create(name) | |
58 | ||
59 | if self._c is None: | |
60 | raise ValueError("Invalid clock name.") | |
61 | ||
62 | def __del__(self): | |
63 | nbt._bt_ctf_clock_put(self._c) | |
64 | ||
65 | @property | |
66 | def name(self): | |
67 | """ | |
68 | Get the clock's name. | |
69 | """ | |
70 | ||
71 | name = nbt._bt_ctf_clock_get_name(self._c) | |
72 | ||
73 | if name is None: | |
74 | raise ValueError("Invalid clock instance.") | |
75 | ||
76 | return name | |
77 | ||
78 | @property | |
79 | def description(self): | |
80 | """ | |
81 | Get the clock's description. None if unset. | |
82 | """ | |
83 | ||
84 | return nbt._bt_ctf_clock_get_description(self._c) | |
85 | ||
86 | @description.setter | |
87 | def description(self, desc): | |
88 | """ | |
89 | Set the clock's description. The description appears in the clock's TSDL | |
90 | meta-data. | |
91 | """ | |
92 | ||
93 | ret = nbt._bt_ctf_clock_set_description(self._c, str(desc)) | |
94 | ||
95 | if ret < 0: | |
96 | raise ValueError("Invalid clock description.") | |
97 | ||
98 | @property | |
99 | def frequency(self): | |
100 | """ | |
101 | Get the clock's frequency (Hz). | |
102 | """ | |
103 | ||
104 | freq = nbt._bt_ctf_clock_get_frequency(self._c) | |
105 | ||
106 | if freq == _MAX_UINT64: | |
107 | raise ValueError("Invalid clock instance") | |
108 | ||
109 | return freq | |
110 | ||
111 | @frequency.setter | |
112 | def frequency(self, freq): | |
113 | """ | |
114 | Set the clock's frequency (Hz). | |
115 | """ | |
116 | ||
117 | ret = nbt._bt_ctf_clock_set_frequency(self._c, freq) | |
118 | ||
119 | if ret < 0: | |
120 | raise ValueError("Invalid frequency value.") | |
121 | ||
122 | @property | |
123 | def precision(self): | |
124 | """ | |
125 | Get the clock's precision (in clock ticks). | |
126 | """ | |
127 | ||
128 | precision = nbt._bt_ctf_clock_get_precision(self._c) | |
129 | ||
130 | if precision == _MAX_UINT64: | |
131 | raise ValueError("Invalid clock instance") | |
132 | ||
133 | return precision | |
134 | ||
135 | @precision.setter | |
136 | def precision(self, precision): | |
137 | """ | |
138 | Set the clock's precision (in clock ticks). | |
139 | """ | |
140 | ||
141 | ret = nbt._bt_ctf_clock_set_precision(self._c, precision) | |
142 | ||
143 | @property | |
144 | def offset_seconds(self): | |
145 | """ | |
146 | Get the clock's offset in seconds from POSIX.1 Epoch. | |
147 | """ | |
148 | ||
149 | offset_s = nbt._bt_ctf_clock_get_offset_s(self._c) | |
150 | ||
151 | if offset_s == _MAX_UINT64: | |
152 | raise ValueError("Invalid clock instance") | |
153 | ||
154 | return offset_s | |
155 | ||
156 | @offset_seconds.setter | |
157 | def offset_seconds(self, offset_s): | |
158 | """ | |
159 | Set the clock's offset in seconds from POSIX.1 Epoch. | |
160 | """ | |
161 | ||
162 | ret = nbt._bt_ctf_clock_set_offset_s(self._c, offset_s) | |
163 | ||
164 | if ret < 0: | |
165 | raise ValueError("Invalid offset value.") | |
166 | ||
167 | @property | |
168 | def offset(self): | |
169 | """ | |
170 | Get the clock's offset in ticks from POSIX.1 Epoch + offset in seconds. | |
171 | """ | |
172 | ||
173 | offset = nbt._bt_ctf_clock_get_offset(self._c) | |
174 | ||
175 | if offset == _MAX_UINT64: | |
176 | raise ValueError("Invalid clock instance") | |
177 | ||
178 | return offset | |
179 | ||
180 | @offset.setter | |
181 | def offset(self, offset): | |
182 | """ | |
183 | Set the clock's offset in ticks from POSIX.1 Epoch + offset in seconds. | |
184 | """ | |
185 | ||
186 | ret = nbt._bt_ctf_clock_set_offset(self._c, offset) | |
187 | ||
188 | if ret < 0: | |
189 | raise ValueError("Invalid offset value.") | |
190 | ||
191 | @property | |
192 | def absolute(self): | |
193 | """ | |
194 | Get a clock's absolute attribute. A clock is absolute if the clock | |
195 | is a global reference across the trace's other clocks. | |
196 | """ | |
197 | ||
198 | is_absolute = nbt._bt_ctf_clock_get_is_absolute(self._c) | |
199 | ||
200 | if is_absolute == -1: | |
201 | raise ValueError("Invalid clock instance") | |
202 | ||
203 | return False if is_absolute == 0 else True | |
204 | ||
205 | @absolute.setter | |
206 | def absolute(self, is_absolute): | |
207 | """ | |
208 | Set a clock's absolute attribute. A clock is absolute if the clock | |
209 | is a global reference across the trace's other clocks. | |
210 | """ | |
211 | ||
212 | ret = nbt._bt_ctf_clock_set_is_absolute(self._c, int(is_absolute)) | |
213 | ||
214 | if ret < 0: | |
215 | raise ValueError("Could not set the clock's absolute attribute.") | |
216 | ||
217 | @property | |
218 | def uuid(self): | |
219 | """ | |
220 | Get a clock's UUID (an object of type UUID). | |
221 | """ | |
222 | ||
223 | uuid_list = [] | |
224 | ||
225 | for i in range(16): | |
226 | ret, value = nbt._bt_python_ctf_clock_get_uuid_index(self._c, i) | |
227 | ||
228 | if ret < 0: | |
229 | raise ValueError("Invalid clock instance") | |
230 | ||
231 | uuid_list.append(value) | |
232 | ||
233 | return UUID(bytes=bytes(uuid_list)) | |
234 | ||
235 | @uuid.setter | |
236 | def uuid(self, uuid): | |
237 | """ | |
238 | Set a clock's UUID (an object of type UUID). | |
239 | """ | |
240 | ||
241 | uuid_bytes = uuid.bytes | |
242 | ||
243 | if len(uuid_bytes) != 16: | |
244 | raise ValueError("Invalid UUID provided. UUID length must be 16 bytes") | |
245 | ||
246 | for i in range(len(uuid_bytes)): | |
247 | ret = nbt._bt_python_ctf_clock_set_uuid_index(self._c, i, | |
248 | uuid_bytes[i]) | |
249 | ||
250 | if ret < 0: | |
251 | raise ValueError("Invalid clock instance") | |
252 | ||
253 | @property | |
254 | def time(self): | |
255 | """ | |
256 | Get the current time in nanoseconds since the clock's origin (offset and | |
257 | offset_s attributes). | |
258 | """ | |
259 | ||
260 | time = nbt._bt_ctf_clock_get_time(self._c) | |
261 | ||
262 | if time == _MAX_UINT64: | |
263 | raise ValueError("Invalid clock instance") | |
264 | ||
265 | return time | |
266 | ||
267 | @time.setter | |
268 | def time(self, time): | |
269 | """ | |
270 | Set the current time in nanoseconds since the clock's origin (offset and | |
271 | offset_s attributes). The clock's value will be sampled as events are | |
272 | appended to a stream. | |
273 | """ | |
274 | ||
275 | ret = nbt._bt_ctf_clock_set_time(self._c, time) | |
276 | ||
277 | if ret < 0: | |
278 | raise ValueError("Invalid time value.") | |
279 | ||
280 | ||
281 | class FieldDeclaration: | |
282 | """ | |
283 | FieldDeclaration should not be instantiated directly. Instantiate | |
284 | one of the concrete FieldDeclaration classes. | |
285 | """ | |
286 | ||
287 | class IntegerBase: | |
288 | # These values are based on the bt_ctf_integer_base enum | |
289 | # declared in event-types.h. | |
290 | INTEGER_BASE_UNKNOWN = -1 | |
291 | INTEGER_BASE_BINARY = 2 | |
292 | INTEGER_BASE_OCTAL = 8 | |
293 | INTEGER_BASE_DECIMAL = 10 | |
294 | INTEGER_BASE_HEXADECIMAL = 16 | |
295 | ||
296 | def __init__(self): | |
297 | if self._ft is None: | |
298 | raise ValueError("FieldDeclaration creation failed.") | |
299 | ||
300 | def __del__(self): | |
301 | nbt._bt_ctf_field_type_put(self._ft) | |
302 | ||
303 | @staticmethod | |
304 | def _create_field_declaration_from_native_instance( | |
305 | native_field_declaration): | |
306 | type_dict = { | |
307 | common.CTFTypeId.INTEGER: IntegerFieldDeclaration, | |
308 | common.CTFTypeId.FLOAT: FloatFieldDeclaration, | |
309 | common.CTFTypeId.ENUM: EnumerationFieldDeclaration, | |
310 | common.CTFTypeId.STRING: StringFieldDeclaration, | |
311 | common.CTFTypeId.STRUCT: StructureFieldDeclaration, | |
312 | common.CTFTypeId.VARIANT: VariantFieldDeclaration, | |
313 | common.CTFTypeId.ARRAY: ArrayFieldDeclaration, | |
314 | common.CTFTypeId.SEQUENCE: SequenceFieldDeclaration | |
315 | } | |
316 | ||
317 | field_type_id = nbt._bt_ctf_field_type_get_type_id(native_field_declaration) | |
318 | ||
319 | if field_type_id == common.CTFTypeId.UNKNOWN: | |
320 | raise TypeError("Invalid field instance") | |
321 | ||
322 | declaration = Field.__new__(Field) | |
323 | declaration._ft = native_field_declaration | |
324 | declaration.__class__ = type_dict[field_type_id] | |
325 | ||
326 | return declaration | |
327 | ||
328 | @property | |
329 | def alignment(self): | |
330 | """ | |
331 | Get the field declaration's alignment. Returns -1 on error. | |
332 | """ | |
333 | ||
334 | return nbt._bt_ctf_field_type_get_alignment(self._ft) | |
335 | ||
336 | @alignment.setter | |
337 | def alignment(self, alignment): | |
338 | """ | |
339 | Set the field declaration's alignment. Defaults to 1 (bit-aligned). However, | |
340 | some types, such as structures and string, may impose other alignment | |
341 | constraints. | |
342 | """ | |
343 | ||
344 | ret = nbt._bt_ctf_field_type_set_alignment(self._ft, alignment) | |
345 | ||
346 | if ret < 0: | |
347 | raise ValueError("Invalid alignment value.") | |
348 | ||
349 | @property | |
350 | def byte_order(self): | |
351 | """ | |
352 | Get the field declaration's byte order. One of the ByteOrder's constant. | |
353 | """ | |
354 | ||
355 | return nbt._bt_ctf_field_type_get_byte_order(self._ft) | |
356 | ||
357 | @byte_order.setter | |
358 | def byte_order(self, byte_order): | |
359 | """ | |
360 | Set the field declaration's byte order. Use constants defined in the ByteOrder | |
361 | class. | |
362 | """ | |
363 | ||
364 | ret = nbt._bt_ctf_field_type_set_byte_order(self._ft, byte_order) | |
365 | ||
366 | if ret < 0: | |
367 | raise ValueError("Could not set byte order value.") | |
368 | ||
369 | ||
370 | class IntegerFieldDeclaration(FieldDeclaration): | |
371 | def __init__(self, size): | |
372 | """ | |
373 | Create a new integer field declaration of the given size. | |
374 | """ | |
375 | self._ft = nbt._bt_ctf_field_type_integer_create(size) | |
376 | super().__init__() | |
377 | ||
378 | @property | |
379 | def size(self): | |
380 | """ | |
381 | Get an integer's size. | |
382 | """ | |
383 | ||
384 | ret = nbt._bt_ctf_field_type_integer_get_size(self._ft) | |
385 | ||
386 | if ret < 0: | |
387 | raise ValueError("Could not get Integer's size attribute.") | |
388 | else: | |
389 | return ret | |
390 | ||
391 | @property | |
392 | def signed(self): | |
393 | """ | |
394 | Get an integer's signedness attribute. | |
395 | """ | |
396 | ||
397 | ret = nbt._bt_ctf_field_type_integer_get_signed(self._ft) | |
398 | ||
399 | if ret < 0: | |
400 | raise ValueError("Could not get Integer's signed attribute.") | |
401 | elif ret > 0: | |
402 | return True | |
403 | else: | |
404 | return False | |
405 | ||
406 | @signed.setter | |
407 | def signed(self, signed): | |
408 | """ | |
409 | Set an integer's signedness attribute. | |
410 | """ | |
411 | ||
412 | ret = nbt._bt_ctf_field_type_integer_set_signed(self._ft, signed) | |
413 | ||
414 | if ret < 0: | |
415 | raise ValueError("Could not set Integer's signed attribute.") | |
416 | ||
417 | @property | |
418 | def base(self): | |
419 | """ | |
420 | Get the integer's base used to pretty-print the resulting trace. | |
421 | Returns a constant from the FieldDeclaration.IntegerBase class. | |
422 | """ | |
423 | ||
424 | return nbt._bt_ctf_field_type_integer_get_base(self._ft) | |
425 | ||
426 | @base.setter | |
427 | def base(self, base): | |
428 | """ | |
429 | Set the integer's base used to pretty-print the resulting trace. | |
430 | The base must be a constant of the FieldDeclarationIntegerBase class. | |
431 | """ | |
432 | ||
433 | ret = nbt._bt_ctf_field_type_integer_set_base(self._ft, base) | |
434 | ||
435 | if ret < 0: | |
436 | raise ValueError("Could not set Integer's base.") | |
437 | ||
438 | @property | |
439 | def encoding(self): | |
440 | """ | |
441 | Get the integer's encoding (one of the constants of the | |
442 | CTFStringEncoding class). | |
443 | Returns a constant from the CTFStringEncoding class. | |
444 | """ | |
445 | ||
446 | return nbt._bt_ctf_field_type_integer_get_encoding(self._ft) | |
447 | ||
448 | @encoding.setter | |
449 | def encoding(self, encoding): | |
450 | """ | |
451 | An integer encoding may be set to signal that the integer must be printed | |
452 | as a text character. Must be a constant from the CTFStringEncoding class. | |
453 | """ | |
454 | ||
455 | ret = nbt._bt_ctf_field_type_integer_set_encoding(self._ft, encoding) | |
456 | ||
457 | if ret < 0: | |
458 | raise ValueError("Could not set Integer's encoding.") | |
459 | ||
460 | ||
461 | class EnumerationFieldDeclaration(FieldDeclaration): | |
462 | def __init__(self, integer_type): | |
463 | """ | |
464 | Create a new enumeration field declaration with the given underlying container type. | |
465 | """ | |
466 | isinst = isinstance(integer_type, IntegerFieldDeclaration) | |
467 | ||
468 | if integer_type is None or not isinst: | |
469 | raise TypeError("Invalid integer container.") | |
470 | ||
471 | self._ft = nbt._bt_ctf_field_type_enumeration_create(integer_type._ft) | |
472 | super().__init__() | |
473 | ||
474 | @property | |
475 | def container(self): | |
476 | """ | |
477 | Get the enumeration's underlying container type. | |
478 | """ | |
479 | ||
480 | ret = nbt._bt_ctf_field_type_enumeration_get_container_type(self._ft) | |
481 | ||
482 | if ret is None: | |
483 | raise TypeError("Invalid enumeration declaration") | |
484 | ||
485 | return FieldDeclaration._create_field_declaration_from_native_instance(ret) | |
486 | ||
487 | def add_mapping(self, name, range_start, range_end): | |
488 | """ | |
489 | Add a mapping to the enumeration. The range's values are inclusive. | |
490 | """ | |
491 | ||
492 | if range_start < 0 or range_end < 0: | |
493 | ret = nbt._bt_ctf_field_type_enumeration_add_mapping(self._ft, | |
494 | str(name), | |
495 | range_start, | |
496 | range_end) | |
497 | else: | |
498 | ret = nbt._bt_ctf_field_type_enumeration_add_mapping_unsigned(self._ft, | |
499 | str(name), | |
500 | range_start, | |
501 | range_end) | |
502 | ||
503 | if ret < 0: | |
504 | raise ValueError("Could not add mapping to enumeration declaration.") | |
505 | ||
506 | @property | |
507 | def mappings(self): | |
508 | """ | |
509 | Generator returning instances of EnumerationMapping. | |
510 | """ | |
511 | ||
512 | signed = self.container.signed | |
513 | ||
514 | count = nbt._bt_ctf_field_type_enumeration_get_mapping_count(self._ft) | |
515 | ||
516 | for i in range(count): | |
517 | if signed: | |
518 | ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, i) | |
519 | else: | |
520 | ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, i) | |
521 | ||
522 | if len(ret) != 3: | |
523 | msg = "Could not get Enumeration mapping at index {}".format(i) | |
524 | raise TypeError(msg) | |
525 | ||
526 | name, range_start, range_end = ret | |
527 | yield EnumerationMapping(name, range_start, range_end) | |
528 | ||
529 | def get_mapping_by_name(self, name): | |
530 | """ | |
531 | Get a mapping by name (EnumerationMapping). | |
532 | """ | |
533 | ||
534 | index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_name(self._ft, name) | |
535 | ||
536 | if index < 0: | |
537 | return None | |
538 | ||
539 | if self.container.signed: | |
540 | ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index) | |
541 | else: | |
542 | ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index) | |
543 | ||
544 | if len(ret) != 3: | |
545 | msg = "Could not get Enumeration mapping at index {}".format(i) | |
546 | raise TypeError(msg) | |
547 | ||
548 | name, range_start, range_end = ret | |
549 | ||
550 | return EnumerationMapping(name, range_start, range_end) | |
551 | ||
552 | def get_mapping_by_value(self, value): | |
553 | """ | |
554 | Get a mapping by value (EnumerationMapping). | |
555 | """ | |
556 | ||
557 | if value < 0: | |
558 | index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_value(self._ft, value) | |
559 | else: | |
560 | index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(self._ft, value) | |
561 | ||
562 | if index < 0: | |
563 | return None | |
564 | ||
565 | if self.container.signed: | |
566 | ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index) | |
567 | else: | |
568 | ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index) | |
569 | ||
570 | if len(ret) != 3: | |
571 | msg = "Could not get Enumeration mapping at index {}".format(i) | |
572 | raise TypeError(msg) | |
573 | ||
574 | name, range_start, range_end = ret | |
575 | ||
576 | return EnumerationMapping(name, range_start, range_end) | |
577 | ||
578 | ||
579 | class FloatFieldDeclaration(FieldDeclaration): | |
580 | FLT_EXP_DIG = 8 | |
581 | DBL_EXP_DIG = 11 | |
582 | FLT_MANT_DIG = 24 | |
583 | DBL_MANT_DIG = 53 | |
584 | ||
585 | def __init__(self): | |
586 | """ | |
587 | Create a new floating point field declaration. | |
588 | """ | |
589 | ||
590 | self._ft = nbt._bt_ctf_field_type_floating_point_create() | |
591 | super().__init__() | |
592 | ||
593 | @property | |
594 | def exponent_digits(self): | |
595 | """ | |
596 | Get the number of exponent digits used to store the floating point field. | |
597 | """ | |
598 | ||
599 | ret = nbt._bt_ctf_field_type_floating_point_get_exponent_digits(self._ft) | |
600 | ||
601 | if ret < 0: | |
602 | raise TypeError( | |
603 | "Could not get Floating point exponent digit count") | |
604 | ||
605 | return ret | |
606 | ||
607 | @exponent_digits.setter | |
608 | def exponent_digits(self, exponent_digits): | |
609 | """ | |
610 | Set the number of exponent digits to use to store the floating point field. | |
611 | The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which | |
612 | are defined as constants of this class. | |
613 | """ | |
614 | ||
615 | ret = nbt._bt_ctf_field_type_floating_point_set_exponent_digits(self._ft, | |
616 | exponent_digits) | |
617 | ||
618 | if ret < 0: | |
619 | raise ValueError("Could not set exponent digit count.") | |
620 | ||
621 | @property | |
622 | def mantissa_digits(self): | |
623 | """ | |
624 | Get the number of mantissa digits used to store the floating point field. | |
625 | """ | |
626 | ||
627 | ret = nbt._bt_ctf_field_type_floating_point_get_mantissa_digits(self._ft) | |
628 | ||
629 | if ret < 0: | |
630 | raise TypeError("Could not get Floating point mantissa digit count") | |
631 | ||
632 | return ret | |
633 | ||
634 | @mantissa_digits.setter | |
635 | def mantissa_digits(self, mantissa_digits): | |
636 | """ | |
637 | Set the number of mantissa digits to use to store the floating point field. | |
638 | The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which | |
639 | are defined as constants of this class. | |
640 | """ | |
641 | ||
642 | ret = nbt._bt_ctf_field_type_floating_point_set_mantissa_digits(self._ft, | |
643 | mantissa_digits) | |
644 | ||
645 | if ret < 0: | |
646 | raise ValueError("Could not set mantissa digit count.") | |
647 | ||
648 | ||
649 | class FloatingPointFieldDeclaration(FloatFieldDeclaration): | |
650 | pass | |
651 | ||
652 | ||
653 | class StructureFieldDeclaration(FieldDeclaration): | |
654 | def __init__(self): | |
655 | """ | |
656 | Create a new structure field declaration. | |
657 | """ | |
658 | ||
659 | self._ft = nbt._bt_ctf_field_type_structure_create() | |
660 | super().__init__() | |
661 | ||
662 | def add_field(self, field_type, field_name): | |
663 | """ | |
664 | Add a field of type "field_type" to the structure. | |
665 | """ | |
666 | ||
667 | ret = nbt._bt_ctf_field_type_structure_add_field(self._ft, | |
668 | field_type._ft, | |
669 | str(field_name)) | |
670 | ||
671 | if ret < 0: | |
672 | raise ValueError("Could not add field to structure.") | |
673 | ||
674 | @property | |
675 | def fields(self): | |
676 | """ | |
677 | Generator returning the structure's field as tuples of (field name, field declaration). | |
678 | """ | |
679 | ||
680 | count = nbt._bt_ctf_field_type_structure_get_field_count(self._ft) | |
681 | ||
682 | if count < 0: | |
683 | raise TypeError("Could not get Structure field count") | |
684 | ||
685 | for i in range(count): | |
686 | field_name = nbt._bt_python_ctf_field_type_structure_get_field_name(self._ft, i) | |
687 | ||
688 | if field_name is None: | |
689 | msg = "Could not get Structure field name at index {}".format(i) | |
690 | raise TypeError(msg) | |
691 | ||
692 | field_type_native = nbt._bt_python_ctf_field_type_structure_get_field_type(self._ft, i) | |
693 | ||
694 | if field_type_native is None: | |
695 | msg = "Could not get Structure field type at index {}".format(i) | |
696 | raise TypeError(msg) | |
697 | ||
698 | field_type = FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
699 | yield (field_name, field_type) | |
700 | ||
701 | def get_field_by_name(self, name): | |
702 | """ | |
703 | Get a field declaration by name (FieldDeclaration). | |
704 | """ | |
705 | ||
706 | field_type_native = nbt._bt_ctf_field_type_structure_get_field_type_by_name(self._ft, name) | |
707 | ||
708 | if field_type_native is None: | |
709 | msg = "Could not find Structure field with name {}".format(name) | |
710 | raise TypeError(msg) | |
711 | ||
712 | return FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
713 | ||
714 | ||
715 | class VariantFieldDeclaration(FieldDeclaration): | |
716 | def __init__(self, enum_tag, tag_name): | |
717 | """ | |
718 | Create a new variant field declaration. | |
719 | """ | |
720 | ||
721 | isinst = isinstance(enum_tag, EnumerationFieldDeclaration) | |
722 | if enum_tag is None or not isinst: | |
723 | raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.") | |
724 | ||
725 | self._ft = nbt._bt_ctf_field_type_variant_create(enum_tag._ft, | |
726 | str(tag_name)) | |
727 | super().__init__() | |
728 | ||
729 | @property | |
730 | def tag_name(self): | |
731 | """ | |
732 | Get the variant's tag name. | |
733 | """ | |
734 | ||
735 | ret = nbt._bt_ctf_field_type_variant_get_tag_name(self._ft) | |
736 | ||
737 | if ret is None: | |
738 | raise TypeError("Could not get Variant tag name") | |
739 | ||
740 | return ret | |
741 | ||
742 | @property | |
743 | def tag_type(self): | |
744 | """ | |
745 | Get the variant's tag type. | |
746 | """ | |
747 | ||
748 | ret = nbt._bt_ctf_field_type_variant_get_tag_type(self._ft) | |
749 | ||
750 | if ret is None: | |
751 | raise TypeError("Could not get Variant tag type") | |
752 | ||
753 | return FieldDeclaration._create_field_declaration_from_native_instance(ret) | |
754 | ||
755 | def add_field(self, field_type, field_name): | |
756 | """ | |
757 | Add a field of type "field_type" to the variant. | |
758 | """ | |
759 | ||
760 | ret = nbt._bt_ctf_field_type_variant_add_field(self._ft, | |
761 | field_type._ft, | |
762 | str(field_name)) | |
763 | ||
764 | if ret < 0: | |
765 | raise ValueError("Could not add field to variant.") | |
766 | ||
767 | @property | |
768 | def fields(self): | |
769 | """ | |
770 | Generator returning the variant's field as tuples of (field name, field declaration). | |
771 | """ | |
772 | ||
773 | count = nbt._bt_ctf_field_type_variant_get_field_count(self._ft) | |
774 | ||
775 | if count < 0: | |
776 | raise TypeError("Could not get Variant field count") | |
777 | ||
778 | for i in range(count): | |
779 | field_name = nbt._bt_python_ctf_field_type_variant_get_field_name(self._ft, i) | |
780 | ||
781 | if field_name is None: | |
782 | msg = "Could not get Variant field name at index {}".format(i) | |
783 | raise TypeError(msg) | |
784 | ||
785 | field_type_native = nbt._bt_python_ctf_field_type_variant_get_field_type(self._ft, i) | |
786 | ||
787 | if field_type_native is None: | |
788 | msg = "Could not get Variant field type at index {}".format(i) | |
789 | raise TypeError(msg) | |
790 | ||
791 | field_type = FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
792 | yield (field_name, field_type) | |
793 | ||
794 | def get_field_by_name(self, name): | |
795 | """ | |
796 | Get a field declaration by name (FieldDeclaration). | |
797 | """ | |
798 | ||
799 | field_type_native = nbt._bt_ctf_field_type_variant_get_field_type_by_name(self._ft, | |
800 | name) | |
801 | ||
802 | if field_type_native is None: | |
803 | msg = "Could not find Variant field with name {}".format(name) | |
804 | raise TypeError(msg) | |
805 | ||
806 | return FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
807 | ||
808 | def get_field_from_tag(self, tag): | |
809 | """ | |
810 | Get a field declaration from tag (EnumerationField). | |
811 | """ | |
812 | ||
813 | field_type_native = nbt._bt_ctf_field_type_variant_get_field_type_from_tag(self._ft, tag._f) | |
814 | ||
815 | if field_type_native is None: | |
816 | msg = "Could not find Variant field with tag value {}".format(tag.value) | |
817 | raise TypeError(msg) | |
818 | ||
819 | return FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
820 | ||
821 | ||
822 | class ArrayFieldDeclaration(FieldDeclaration): | |
823 | def __init__(self, element_type, length): | |
824 | """ | |
825 | Create a new array field declaration. | |
826 | """ | |
827 | ||
828 | self._ft = nbt._bt_ctf_field_type_array_create(element_type._ft, | |
829 | length) | |
830 | super().__init__() | |
831 | ||
832 | @property | |
833 | def element_type(self): | |
834 | """ | |
835 | Get the array's element type. | |
836 | """ | |
837 | ||
838 | ret = nbt._bt_ctf_field_type_array_get_element_type(self._ft) | |
839 | ||
840 | if ret is None: | |
841 | raise TypeError("Could not get Array element type") | |
842 | ||
843 | return FieldDeclaration._create_field_declaration_from_native_instance(ret) | |
844 | ||
845 | @property | |
846 | def length(self): | |
847 | """ | |
848 | Get the array's length. | |
849 | """ | |
850 | ||
851 | ret = nbt._bt_ctf_field_type_array_get_length(self._ft) | |
852 | ||
853 | if ret < 0: | |
854 | raise TypeError("Could not get Array length") | |
855 | ||
856 | return ret | |
857 | ||
858 | ||
859 | class SequenceFieldDeclaration(FieldDeclaration): | |
860 | def __init__(self, element_type, length_field_name): | |
861 | """ | |
862 | Create a new sequence field declaration. | |
863 | """ | |
864 | ||
865 | self._ft = nbt._bt_ctf_field_type_sequence_create(element_type._ft, | |
866 | str(length_field_name)) | |
867 | super().__init__() | |
868 | ||
869 | @property | |
870 | def element_type(self): | |
871 | """ | |
872 | Get the sequence's element type. | |
873 | """ | |
874 | ||
875 | ret = nbt._bt_ctf_field_type_sequence_get_element_type(self._ft) | |
876 | ||
877 | if ret is None: | |
878 | raise TypeError("Could not get Sequence element type") | |
879 | ||
880 | return FieldDeclaration._create_field_declaration_from_native_instance(ret) | |
881 | ||
882 | @property | |
883 | def length_field_name(self): | |
884 | """ | |
885 | Get the sequence's length field name. | |
886 | """ | |
887 | ||
888 | ret = nbt._bt_ctf_field_type_sequence_get_length_field_name(self._ft) | |
889 | ||
890 | if ret is None: | |
891 | raise TypeError("Could not get Sequence length field name") | |
892 | ||
893 | return ret | |
894 | ||
895 | ||
896 | class StringFieldDeclaration(FieldDeclaration): | |
897 | def __init__(self): | |
898 | """ | |
899 | Create a new string field declaration. | |
900 | """ | |
901 | ||
902 | self._ft = nbt._bt_ctf_field_type_string_create() | |
903 | super().__init__() | |
904 | ||
905 | @property | |
906 | def encoding(self): | |
907 | """ | |
908 | Get a string declaration's encoding (a constant from the CTFStringEncoding class). | |
909 | """ | |
910 | ||
911 | return nbt._bt_ctf_field_type_string_get_encoding(self._ft) | |
912 | ||
913 | @encoding.setter | |
914 | def encoding(self, encoding): | |
915 | """ | |
916 | Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class. | |
917 | """ | |
918 | ||
919 | ret = nbt._bt_ctf_field_type_string_set_encoding(self._ft, encoding) | |
920 | if ret < 0: | |
921 | raise ValueError("Could not set string encoding.") | |
922 | ||
923 | ||
924 | @staticmethod | |
925 | def create_field(field_type): | |
926 | """ | |
927 | Create an instance of a field. | |
928 | """ | |
929 | isinst = isinstance(field_type, FieldDeclaration) | |
930 | ||
931 | if field_type is None or not isinst: | |
932 | raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.") | |
933 | ||
934 | if isinstance(field_type, IntegerFieldDeclaration): | |
935 | return IntegerField(field_type) | |
936 | elif isinstance(field_type, EnumerationFieldDeclaration): | |
937 | return EnumerationField(field_type) | |
938 | elif isinstance(field_type, FloatFieldDeclaration): | |
939 | return FloatingPointField(field_type) | |
940 | elif isinstance(field_type, StructureFieldDeclaration): | |
941 | return StructureField(field_type) | |
942 | elif isinstance(field_type, VariantFieldDeclaration): | |
943 | return VariantField(field_type) | |
944 | elif isinstance(field_type, ArrayFieldDeclaration): | |
945 | return ArrayField(field_type) | |
946 | elif isinstance(field_type, SequenceFieldDeclaration): | |
947 | return SequenceField(field_type) | |
948 | elif isinstance(field_type, StringFieldDeclaration): | |
949 | return StringField(field_type) | |
950 | ||
951 | ||
952 | class Field: | |
953 | """ | |
954 | Base class, do not instantiate. | |
955 | """ | |
956 | ||
957 | def __init__(self, field_type): | |
958 | if not isinstance(field_type, FieldDeclaration): | |
959 | raise TypeError("Invalid field_type argument.") | |
960 | ||
961 | self._f = nbt._bt_ctf_field_create(field_type._ft) | |
962 | ||
963 | if self._f is None: | |
964 | raise ValueError("Field creation failed.") | |
965 | ||
966 | def __del__(self): | |
967 | nbt._bt_ctf_field_put(self._f) | |
968 | ||
969 | @staticmethod | |
970 | def _create_field_from_native_instance(native_field_instance): | |
971 | type_dict = { | |
972 | common.CTFTypeId.INTEGER: IntegerField, | |
973 | common.CTFTypeId.FLOAT: FloatingPointField, | |
974 | common.CTFTypeId.ENUM: EnumerationField, | |
975 | common.CTFTypeId.STRING: StringField, | |
976 | common.CTFTypeId.STRUCT: StructureField, | |
977 | common.CTFTypeId.VARIANT: VariantField, | |
978 | common.CTFTypeId.ARRAY: ArrayField, | |
979 | common.CTFTypeId.SEQUENCE: SequenceField | |
980 | } | |
981 | ||
982 | field_type = nbt._bt_python_get_field_type(native_field_instance) | |
983 | ||
984 | if field_type == common.CTFTypeId.UNKNOWN: | |
985 | raise TypeError("Invalid field instance") | |
986 | ||
987 | field = Field.__new__(Field) | |
988 | field._f = native_field_instance | |
989 | field.__class__ = type_dict[field_type] | |
990 | ||
991 | return field | |
992 | ||
993 | @property | |
994 | def declaration(self): | |
995 | native_field_type = nbt._bt_ctf_field_get_type(self._f) | |
996 | ||
997 | if native_field_type is None: | |
998 | raise TypeError("Invalid field instance") | |
999 | return FieldDeclaration._create_field_declaration_from_native_instance( | |
1000 | native_field_type) | |
1001 | ||
1002 | ||
1003 | class IntegerField(Field): | |
1004 | @property | |
1005 | def value(self): | |
1006 | """ | |
1007 | Get an integer field's value. | |
1008 | """ | |
1009 | ||
1010 | signedness = nbt._bt_python_field_integer_get_signedness(self._f) | |
1011 | ||
1012 | if signedness < 0: | |
1013 | raise TypeError("Invalid integer instance.") | |
1014 | ||
1015 | if signedness == 0: | |
1016 | ret, value = nbt._bt_ctf_field_unsigned_integer_get_value(self._f) | |
1017 | else: | |
1018 | ret, value = nbt._bt_ctf_field_signed_integer_get_value(self._f) | |
1019 | ||
1020 | if ret < 0: | |
1021 | raise ValueError("Could not get integer field value.") | |
1022 | ||
1023 | return value | |
1024 | ||
1025 | @value.setter | |
1026 | def value(self, value): | |
1027 | """ | |
1028 | Set an integer field's value. | |
1029 | """ | |
1030 | ||
1031 | if not isinstance(value, int): | |
1032 | raise TypeError("IntegerField's value must be an int") | |
1033 | ||
1034 | signedness = nbt._bt_python_field_integer_get_signedness(self._f) | |
1035 | if signedness < 0: | |
1036 | raise TypeError("Invalid integer instance.") | |
1037 | ||
1038 | if signedness == 0: | |
1039 | ret = nbt._bt_ctf_field_unsigned_integer_set_value(self._f, value) | |
1040 | else: | |
1041 | ret = nbt._bt_ctf_field_signed_integer_set_value(self._f, value) | |
1042 | ||
1043 | if ret < 0: | |
1044 | raise ValueError("Could not set integer field value.") | |
1045 | ||
1046 | ||
1047 | class EnumerationField(Field): | |
1048 | @property | |
1049 | def container(self): | |
1050 | """ | |
1051 | Return the enumeration's underlying container field (an integer field). | |
1052 | """ | |
1053 | ||
1054 | container = IntegerField.__new__(IntegerField) | |
1055 | container._f = nbt._bt_ctf_field_enumeration_get_container(self._f) | |
1056 | ||
1057 | if container._f is None: | |
1058 | raise TypeError("Invalid enumeration field type.") | |
1059 | ||
1060 | return container | |
1061 | ||
1062 | @property | |
1063 | def value(self): | |
1064 | """ | |
1065 | Get the enumeration field's mapping name. | |
1066 | """ | |
1067 | ||
1068 | value = nbt._bt_ctf_field_enumeration_get_mapping_name(self._f) | |
1069 | ||
1070 | if value is None: | |
1071 | raise ValueError("Could not get enumeration's mapping name.") | |
1072 | ||
1073 | return value | |
1074 | ||
1075 | @value.setter | |
1076 | def value(self, value): | |
1077 | """ | |
1078 | Set the enumeration field's value. Must be an integer as mapping names | |
1079 | may be ambiguous. | |
1080 | """ | |
1081 | ||
1082 | if not isinstance(value, int): | |
1083 | raise TypeError("EnumerationField value must be an int") | |
1084 | ||
1085 | self.container.value = value | |
1086 | ||
1087 | ||
1088 | class FloatingPointField(Field): | |
1089 | @property | |
1090 | def value(self): | |
1091 | """ | |
1092 | Get a floating point field's value. | |
1093 | """ | |
1094 | ||
1095 | ret, value = nbt._bt_ctf_field_floating_point_get_value(self._f) | |
1096 | ||
1097 | if ret < 0: | |
1098 | raise ValueError("Could not get floating point field value.") | |
1099 | ||
1100 | return value | |
1101 | ||
1102 | @value.setter | |
1103 | def value(self, value): | |
1104 | """ | |
1105 | Set a floating point field's value. | |
1106 | """ | |
1107 | ||
1108 | if not isinstance(value, int) and not isinstance(value, float): | |
1109 | raise TypeError("Value must be either a float or an int") | |
1110 | ||
1111 | ret = nbt._bt_ctf_field_floating_point_set_value(self._f, float(value)) | |
1112 | ||
1113 | if ret < 0: | |
1114 | raise ValueError("Could not set floating point field value.") | |
1115 | ||
1116 | ||
1117 | # oops!! This class is provided to ensure backward-compatibility since | |
1118 | # a stable release publicly exposed this abomination. | |
1119 | class FloatFieldingPoint(FloatingPointField): | |
1120 | pass | |
1121 | ||
1122 | ||
1123 | class StructureField(Field): | |
1124 | def field(self, field_name): | |
1125 | """ | |
1126 | Get the structure's field corresponding to the provided field name. | |
1127 | """ | |
1128 | ||
1129 | native_instance = nbt._bt_ctf_field_structure_get_field(self._f, | |
1130 | str(field_name)) | |
1131 | ||
1132 | if native_instance is None: | |
1133 | raise ValueError("Invalid field_name provided.") | |
1134 | ||
1135 | return Field._create_field_from_native_instance(native_instance) | |
1136 | ||
1137 | ||
1138 | class VariantField(Field): | |
1139 | def field(self, tag): | |
1140 | """ | |
1141 | Return the variant's selected field. The "tag" field is the selector enum field. | |
1142 | """ | |
1143 | ||
1144 | native_instance = nbt._bt_ctf_field_variant_get_field(self._f, tag._f) | |
1145 | ||
1146 | if native_instance is None: | |
1147 | raise ValueError("Invalid tag provided.") | |
1148 | ||
1149 | return Field._create_field_from_native_instance(native_instance) | |
1150 | ||
1151 | ||
1152 | class ArrayField(Field): | |
1153 | def field(self, index): | |
1154 | """ | |
1155 | Return the array's field at position "index". | |
1156 | """ | |
1157 | ||
1158 | native_instance = nbt._bt_ctf_field_array_get_field(self._f, index) | |
1159 | ||
1160 | if native_instance is None: | |
1161 | raise IndexError("Invalid index provided.") | |
1162 | ||
1163 | return Field._create_field_from_native_instance(native_instance) | |
1164 | ||
1165 | ||
1166 | class SequenceField(Field): | |
1167 | @property | |
1168 | def length(self): | |
1169 | """ | |
1170 | Get the sequence's length field (IntegerField). | |
1171 | """ | |
1172 | ||
1173 | native_instance = nbt._bt_ctf_field_sequence_get_length(self._f) | |
1174 | ||
1175 | if native_instance is None: | |
1176 | length = -1 | |
1177 | ||
1178 | return Field._create_field_from_native_instance(native_instance) | |
1179 | ||
1180 | @length.setter | |
1181 | def length(self, length_field): | |
1182 | """ | |
1183 | Set the sequence's length field (IntegerField). | |
1184 | """ | |
1185 | ||
1186 | if not isinstance(length_field, IntegerField): | |
1187 | raise TypeError("Invalid length field.") | |
1188 | ||
1189 | if length_field.declaration.signed: | |
1190 | raise TypeError("Sequence field length must be unsigned") | |
1191 | ||
1192 | ret = nbt._bt_ctf_field_sequence_set_length(self._f, length_field._f) | |
1193 | ||
1194 | if ret < 0: | |
1195 | raise ValueError("Could not set sequence length.") | |
1196 | ||
1197 | def field(self, index): | |
1198 | """ | |
1199 | Return the sequence's field at position "index". | |
1200 | """ | |
1201 | ||
1202 | native_instance = nbt._bt_ctf_field_sequence_get_field(self._f, index) | |
1203 | ||
1204 | if native_instance is None: | |
1205 | raise ValueError("Could not get sequence element at index.") | |
1206 | ||
1207 | return Field._create_field_from_native_instance(native_instance) | |
1208 | ||
1209 | ||
1210 | class StringField(Field): | |
1211 | @property | |
1212 | def value(self): | |
1213 | """ | |
1214 | Get a string field's value. | |
1215 | """ | |
1216 | ||
1217 | return nbt._bt_ctf_field_string_get_value(self._f) | |
1218 | ||
1219 | @value.setter | |
1220 | def value(self, value): | |
1221 | """ | |
1222 | Set a string field's value. | |
1223 | """ | |
1224 | ||
1225 | ret = nbt._bt_ctf_field_string_set_value(self._f, str(value)) | |
1226 | ||
1227 | if ret < 0: | |
1228 | raise ValueError("Could not set string field value.") | |
1229 | ||
1230 | ||
1231 | class EventClass: | |
1232 | def __init__(self, name): | |
1233 | """ | |
1234 | Create a new event class of the given name. | |
1235 | """ | |
1236 | ||
1237 | self._ec = nbt._bt_ctf_event_class_create(name) | |
1238 | ||
1239 | if self._ec is None: | |
1240 | raise ValueError("Event class creation failed.") | |
1241 | ||
1242 | def __del__(self): | |
1243 | nbt._bt_ctf_event_class_put(self._ec) | |
1244 | ||
1245 | def add_field(self, field_type, field_name): | |
1246 | """ | |
1247 | Add a field of type "field_type" to the event class. | |
1248 | """ | |
1249 | ||
1250 | ret = nbt._bt_ctf_event_class_add_field(self._ec, field_type._ft, | |
1251 | str(field_name)) | |
1252 | ||
1253 | if ret < 0: | |
1254 | raise ValueError("Could not add field to event class.") | |
1255 | ||
1256 | @property | |
1257 | def name(self): | |
1258 | """ | |
1259 | Get the event class' name. | |
1260 | """ | |
1261 | ||
1262 | name = nbt._bt_ctf_event_class_get_name(self._ec) | |
1263 | ||
1264 | if name is None: | |
1265 | raise TypeError("Could not get EventClass name") | |
1266 | ||
1267 | return name | |
1268 | ||
1269 | @property | |
1270 | def id(self): | |
1271 | """ | |
1272 | Get the event class' id. Returns a negative value if unset. | |
1273 | """ | |
1274 | ||
1275 | id = nbt._bt_ctf_event_class_get_id(self._ec) | |
1276 | ||
1277 | if id < 0: | |
1278 | raise TypeError("Could not get EventClass id") | |
1279 | ||
1280 | return id | |
1281 | ||
1282 | @id.setter | |
1283 | def id(self, id): | |
1284 | """ | |
1285 | Set the event class' id. Throws a TypeError if the event class | |
1286 | is already registered to a stream class. | |
1287 | """ | |
1288 | ||
1289 | ret = nbt._bt_ctf_event_class_set_id(self._ec, id) | |
1290 | ||
1291 | if ret < 0: | |
1292 | raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class") | |
1293 | ||
1294 | @property | |
1295 | def stream_class(self): | |
1296 | """ | |
1297 | Get the event class' stream class. Returns None if unset. | |
1298 | """ | |
1299 | stream_class_native = nbt._bt_ctf_event_class_get_stream_class(self._ec) | |
1300 | ||
1301 | if stream_class_native is None: | |
1302 | return None | |
1303 | ||
1304 | stream_class = StreamClass.__new__(StreamClass) | |
1305 | stream_class._sc = stream_class_native | |
1306 | ||
1307 | return stream_class | |
1308 | ||
1309 | @property | |
1310 | def fields(self): | |
1311 | """ | |
1312 | Generator returning the event class' fields as tuples of (field name, field declaration). | |
1313 | """ | |
1314 | ||
1315 | count = nbt._bt_ctf_event_class_get_field_count(self._ec) | |
1316 | ||
1317 | if count < 0: | |
1318 | raise TypeError("Could not get EventClass' field count") | |
1319 | ||
1320 | for i in range(count): | |
1321 | field_name = nbt._bt_python_ctf_event_class_get_field_name(self._ec, i) | |
1322 | ||
1323 | if field_name is None: | |
1324 | msg = "Could not get EventClass' field name at index {}".format(i) | |
1325 | raise TypeError(msg) | |
1326 | ||
1327 | field_type_native = nbt._bt_python_ctf_event_class_get_field_type(self._ec, i) | |
1328 | ||
1329 | if field_type_native is None: | |
1330 | msg = "Could not get EventClass' field type at index {}".format(i) | |
1331 | raise TypeError(msg) | |
1332 | ||
1333 | field_type = FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
1334 | yield (field_name, field_type) | |
1335 | ||
1336 | def get_field_by_name(self, name): | |
1337 | """ | |
1338 | Get a field declaration by name (FieldDeclaration). | |
1339 | """ | |
1340 | ||
1341 | field_type_native = nbt._bt_ctf_event_class_get_field_by_name(self._ec, name) | |
1342 | ||
1343 | if field_type_native is None: | |
1344 | msg = "Could not find EventClass field with name {}".format(name) | |
1345 | raise TypeError(msg) | |
1346 | ||
1347 | return FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
1348 | ||
1349 | ||
1350 | class Event: | |
1351 | def __init__(self, event_class): | |
1352 | """ | |
1353 | Create a new event of the given event class. | |
1354 | """ | |
1355 | ||
1356 | if not isinstance(event_class, EventClass): | |
1357 | raise TypeError("Invalid event_class argument.") | |
1358 | ||
1359 | self._e = nbt._bt_ctf_event_create(event_class._ec) | |
1360 | ||
1361 | if self._e is None: | |
1362 | raise ValueError("Event creation failed.") | |
1363 | ||
1364 | def __del__(self): | |
1365 | nbt._bt_ctf_event_put(self._e) | |
1366 | ||
1367 | @property | |
1368 | def event_class(self): | |
1369 | """ | |
1370 | Get the event's class. | |
1371 | """ | |
1372 | ||
1373 | event_class_native = nbt._bt_ctf_event_get_class(self._e) | |
1374 | ||
1375 | if event_class_native is None: | |
1376 | return None | |
1377 | ||
1378 | event_class = EventClass.__new__(EventClass) | |
1379 | event_class._ec = event_class_native | |
1380 | ||
1381 | return event_class | |
1382 | ||
1383 | def clock(self): | |
1384 | """ | |
1385 | Get a clock from event. Returns None if the event's class | |
1386 | is not registered to a stream class. | |
1387 | """ | |
1388 | ||
1389 | clock_instance = nbt._bt_ctf_event_get_clock(self._e) | |
1390 | ||
1391 | if clock_instance is None: | |
1392 | return None | |
1393 | ||
1394 | clock = Clock.__new__(Clock) | |
1395 | clock._c = clock_instance | |
1396 | ||
1397 | return clock | |
1398 | ||
1399 | def payload(self, field_name): | |
1400 | """ | |
1401 | Get a field from event. | |
1402 | """ | |
1403 | ||
1404 | native_instance = nbt._bt_ctf_event_get_payload(self._e, | |
1405 | str(field_name)) | |
1406 | ||
1407 | if native_instance is None: | |
1408 | raise ValueError("Could not get event payload.") | |
1409 | ||
1410 | return Field._create_field_from_native_instance(native_instance) | |
1411 | ||
1412 | def set_payload(self, field_name, value_field): | |
1413 | """ | |
1414 | Set a manually created field as an event's payload. | |
1415 | """ | |
1416 | ||
1417 | if not isinstance(value, Field): | |
1418 | raise TypeError("Invalid value type.") | |
1419 | ||
1420 | ret = nbt._bt_ctf_event_set_payload(self._e, str(field_name), | |
1421 | value_field._f) | |
1422 | ||
1423 | if ret < 0: | |
1424 | raise ValueError("Could not set event field payload.") | |
1425 | ||
1426 | ||
1427 | class StreamClass: | |
1428 | def __init__(self, name): | |
1429 | """ | |
1430 | Create a new stream class of the given name. | |
1431 | """ | |
1432 | ||
1433 | self._sc = nbt._bt_ctf_stream_class_create(name) | |
1434 | ||
1435 | if self._sc is None: | |
1436 | raise ValueError("Stream class creation failed.") | |
1437 | ||
1438 | def __del__(self): | |
1439 | nbt._bt_ctf_stream_class_put(self._sc) | |
1440 | ||
1441 | @property | |
1442 | def name(self): | |
1443 | """ | |
1444 | Get a stream class' name. | |
1445 | """ | |
1446 | ||
1447 | name = nbt._bt_ctf_stream_class_get_name(self._sc) | |
1448 | ||
1449 | if name is None: | |
1450 | raise TypeError("Could not get StreamClass name") | |
1451 | ||
1452 | return name | |
1453 | ||
1454 | @property | |
1455 | def clock(self): | |
1456 | """ | |
1457 | Get a stream class' clock. | |
1458 | """ | |
1459 | ||
1460 | clock_instance = nbt._bt_ctf_stream_class_get_clock(self._sc) | |
1461 | ||
1462 | if clock_instance is None: | |
1463 | return None | |
1464 | ||
1465 | clock = Clock.__new__(Clock) | |
1466 | clock._c = clock_instance | |
1467 | ||
1468 | return clock | |
1469 | ||
1470 | @clock.setter | |
1471 | def clock(self, clock): | |
1472 | """ | |
1473 | Assign a clock to a stream class. | |
1474 | """ | |
1475 | ||
1476 | if not isinstance(clock, Clock): | |
1477 | raise TypeError("Invalid clock type.") | |
1478 | ||
1479 | ret = nbt._bt_ctf_stream_class_set_clock(self._sc, clock._c) | |
1480 | ||
1481 | if ret < 0: | |
1482 | raise ValueError("Could not set stream class clock.") | |
1483 | ||
1484 | @property | |
1485 | def id(self): | |
1486 | """ | |
1487 | Get a stream class' id. | |
1488 | """ | |
1489 | ||
1490 | ret = nbt._bt_ctf_stream_class_get_id(self._sc) | |
1491 | ||
1492 | if ret < 0: | |
1493 | raise TypeError("Could not get StreamClass id") | |
1494 | ||
1495 | return ret | |
1496 | ||
1497 | @id.setter | |
1498 | def id(self, id): | |
1499 | """ | |
1500 | Assign an id to a stream class. | |
1501 | """ | |
1502 | ||
1503 | ret = nbt._bt_ctf_stream_class_set_id(self._sc, id) | |
1504 | ||
1505 | if ret < 0: | |
1506 | raise TypeError("Could not set stream class id.") | |
1507 | ||
1508 | @property | |
1509 | def event_classes(self): | |
1510 | """ | |
1511 | Generator returning the stream class' event classes. | |
1512 | """ | |
1513 | ||
1514 | count = nbt._bt_ctf_stream_class_get_event_class_count(self._sc) | |
1515 | ||
1516 | if count < 0: | |
1517 | raise TypeError("Could not get StreamClass' event class count") | |
1518 | ||
1519 | for i in range(count): | |
1520 | event_class_native = nbt._bt_ctf_stream_class_get_event_class(self._sc, i) | |
1521 | ||
1522 | if event_class_native is None: | |
1523 | msg = "Could not get StreamClass' event class at index {}".format(i) | |
1524 | raise TypeError(msg) | |
1525 | ||
1526 | event_class = EventClass.__new__(EventClass) | |
1527 | event_class._ec = event_class_native | |
1528 | yield event_class | |
1529 | ||
1530 | def add_event_class(self, event_class): | |
1531 | """ | |
1532 | Add an event class to a stream class. New events can be added even after a | |
1533 | stream has been instantiated and events have been appended. However, a stream | |
1534 | will not accept events of a class that has not been added to the stream | |
1535 | class beforehand. | |
1536 | """ | |
1537 | ||
1538 | if not isinstance(event_class, EventClass): | |
1539 | raise TypeError("Invalid event_class type.") | |
1540 | ||
1541 | ret = nbt._bt_ctf_stream_class_add_event_class(self._sc, | |
1542 | event_class._ec) | |
1543 | ||
1544 | if ret < 0: | |
1545 | raise ValueError("Could not add event class.") | |
1546 | ||
1547 | @property | |
1548 | def packet_context_type(self): | |
1549 | """ | |
1550 | Get the StreamClass' packet context type (StructureFieldDeclaration) | |
1551 | """ | |
1552 | ||
1553 | field_type_native = nbt._bt_ctf_stream_class_get_packet_context_type(self._sc) | |
1554 | ||
1555 | if field_type_native is None: | |
1556 | raise ValueError("Invalid StreamClass") | |
1557 | ||
1558 | field_type = FieldDeclaration._create_field_declaration_from_native_instance(field_type_native) | |
1559 | ||
1560 | return field_type | |
1561 | ||
1562 | @packet_context_type.setter | |
1563 | def packet_context_type(self, field_type): | |
1564 | """ | |
1565 | Set a StreamClass' packet context type. Must be of type | |
1566 | StructureFieldDeclaration. | |
1567 | """ | |
1568 | ||
1569 | if not isinstance(field_type, StructureFieldDeclaration): | |
1570 | raise TypeError("field_type argument must be of type StructureFieldDeclaration.") | |
1571 | ||
1572 | ret = nbt._bt_ctf_stream_class_set_packet_context_type(self._sc, | |
1573 | field_type._ft) | |
1574 | ||
1575 | if ret < 0: | |
1576 | raise ValueError("Failed to set packet context type.") | |
1577 | ||
1578 | ||
1579 | class Stream: | |
1580 | def __init__(self): | |
1581 | raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()") | |
1582 | ||
1583 | def __del__(self): | |
1584 | nbt._bt_ctf_stream_put(self._s) | |
1585 | ||
1586 | @property | |
1587 | def discarded_events(self): | |
1588 | """ | |
1589 | Get a stream's discarded event count. | |
1590 | """ | |
1591 | ||
1592 | ret, count = nbt._bt_ctf_stream_get_discarded_events_count(self._s) | |
1593 | ||
1594 | if ret < 0: | |
1595 | raise ValueError("Could not get the stream's discarded events count") | |
1596 | ||
1597 | return count | |
1598 | ||
1599 | def append_discarded_events(self, event_count): | |
1600 | """ | |
1601 | Increase the current packet's discarded event count. | |
1602 | """ | |
1603 | ||
1604 | nbt._bt_ctf_stream_append_discarded_events(self._s, event_count) | |
1605 | ||
1606 | def append_event(self, event): | |
1607 | """ | |
1608 | Append "event" to the stream's current packet. The stream's associated clock | |
1609 | will be sampled during this call. The event shall not be modified after | |
1610 | being appended to a stream. | |
1611 | """ | |
1612 | ||
1613 | ret = nbt._bt_ctf_stream_append_event(self._s, event._e) | |
1614 | ||
1615 | if ret < 0: | |
1616 | raise ValueError("Could not append event to stream.") | |
1617 | ||
1618 | @property | |
1619 | def packet_context(self): | |
1620 | """ | |
1621 | Get a Stream's packet context field (a StructureField). | |
1622 | """ | |
1623 | ||
1624 | native_field = nbt._bt_ctf_stream_get_packet_context(self._s) | |
1625 | ||
1626 | if native_field is None: | |
1627 | raise ValueError("Invalid Stream.") | |
1628 | ||
1629 | return Field._create_field_from_native_instance(native_field) | |
1630 | ||
1631 | @packet_context.setter | |
1632 | def packet_context(self, field): | |
1633 | """ | |
1634 | Set a Stream's packet context field (must be a StructureField). | |
1635 | """ | |
1636 | ||
1637 | if not isinstance(field, StructureField): | |
1638 | raise TypeError("Argument field must be of type StructureField") | |
1639 | ||
1640 | ret = nbt._bt_ctf_stream_set_packet_context(self._s, field._f) | |
1641 | ||
1642 | if ret < 0: | |
1643 | raise ValueError("Invalid packet context field.") | |
1644 | ||
1645 | def flush(self): | |
1646 | """ | |
1647 | The stream's current packet's events will be flushed to disk. Events | |
1648 | subsequently appended to the stream will be added to a new packet. | |
1649 | """ | |
1650 | ||
1651 | ret = nbt._bt_ctf_stream_flush(self._s) | |
1652 | ||
1653 | if ret < 0: | |
1654 | raise ValueError("Could not flush stream.") | |
1655 | ||
1656 | ||
1657 | class Writer: | |
1658 | def __init__(self, path): | |
1659 | """ | |
1660 | Create a new writer that will produce a trace in the given path. | |
1661 | """ | |
1662 | ||
1663 | self._w = nbt._bt_ctf_writer_create(path) | |
1664 | ||
1665 | if self._w is None: | |
1666 | raise ValueError("Writer creation failed.") | |
1667 | ||
1668 | def __del__(self): | |
1669 | nbt._bt_ctf_writer_put(self._w) | |
1670 | ||
1671 | def create_stream(self, stream_class): | |
1672 | """ | |
1673 | Create a new stream instance and register it to the writer. | |
1674 | """ | |
1675 | ||
1676 | if not isinstance(stream_class, StreamClass): | |
1677 | raise TypeError("Invalid stream_class type.") | |
1678 | ||
1679 | stream = Stream.__new__(Stream) | |
1680 | stream._s = nbt._bt_ctf_writer_create_stream(self._w, stream_class._sc) | |
1681 | ||
1682 | return stream | |
1683 | ||
1684 | def add_environment_field(self, name, value): | |
1685 | """ | |
1686 | Add an environment field to the trace. | |
1687 | """ | |
1688 | ||
1689 | ret = nbt._bt_ctf_writer_add_environment_field(self._w, str(name), | |
1690 | str(value)) | |
1691 | ||
1692 | if ret < 0: | |
1693 | raise ValueError("Could not add environment field to trace.") | |
1694 | ||
1695 | def add_clock(self, clock): | |
1696 | """ | |
1697 | Add a clock to the trace. Clocks assigned to stream classes must be | |
1698 | registered to the writer. | |
1699 | """ | |
1700 | ||
1701 | ret = nbt._bt_ctf_writer_add_clock(self._w, clock._c) | |
1702 | ||
1703 | if ret < 0: | |
1704 | raise ValueError("Could not add clock to Writer.") | |
1705 | ||
1706 | @property | |
1707 | def metadata(self): | |
1708 | """ | |
1709 | Get the trace's TSDL meta-data. | |
1710 | """ | |
1711 | ||
1712 | return nbt._bt_ctf_writer_get_metadata_string(self._w) | |
1713 | ||
1714 | def flush_metadata(self): | |
1715 | """ | |
1716 | Flush the trace's metadata to the metadata file. | |
1717 | """ | |
1718 | ||
1719 | nbt._bt_ctf_writer_flush_metadata(self._w) | |
1720 | ||
1721 | @property | |
1722 | def byte_order(self): | |
1723 | """ | |
1724 | Get the trace's byte order. Must be a constant from the ByteOrder | |
1725 | class. | |
1726 | """ | |
1727 | ||
1728 | raise NotImplementedError("Getter not implemented.") | |
1729 | ||
1730 | @byte_order.setter | |
1731 | def byte_order(self, byte_order): | |
1732 | """ | |
1733 | Set the trace's byte order. Must be a constant from the ByteOrder | |
1734 | class. Defaults to the host machine's endianness | |
1735 | """ | |
1736 | ||
1737 | ret = nbt._bt_ctf_writer_set_byte_order(self._w, byte_order) | |
1738 | ||
1739 | if ret < 0: | |
1740 | raise ValueError("Could not set trace's byte order.") |