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