Python: document ArrayFieldDeclaration
[babeltrace.git] / bindings / python / bt.py
1 # nativebt.i.in
2 #
3 # Babeltrace native interface Python module
4 #
5 # Copyright 2012-2015 EfficiOS Inc.
6 #
7 # Author: Danny Serres <danny.serres@efficios.com>
8 # Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 # SOFTWARE.
27
28 import babeltrace.nativebt as nbt
29 import collections
30 import os
31 from datetime import datetime
32 from uuid import UUID
33
34
35 class TraceCollection:
36 """
37 A :class:`TraceCollection` is a collection of opened traces.
38
39 In general, once a trace collection is created, you add one to many
40 independent traces to it using :meth:`add_trace` or
41 :meth:`add_traces_recursive`, and then iterate the ordered events
42 of all traces merged together using :attr:`events`.
43
44 You may use :meth:`remove_trace` to close and remove a specific
45 trace from a trace collection, although all the traces of a given
46 trace collection will be automatically removed when it is garbage
47 collected.
48 """
49
50 def __init__(self):
51 """
52 Creates an empty trace collection.
53 """
54
55 self._tc = nbt._bt_context_create()
56
57 def __del__(self):
58 nbt._bt_context_put(self._tc)
59
60 def add_trace(self, path, format_str):
61 """
62 Adds a trace to the trace collection.
63
64 The trace is located at the file system path *path*. This
65 function **does not** recurse directories to find the trace:
66 *path* must point to the exact trace location (see
67 :meth:`add_traces_recursive` for a recursive version of this
68 function).
69
70 *format_str* is a string indicating the Babeltrace type of the
71 trace to add. ``ctf`` is the only currently supported trace
72 format.
73
74 Once added, the trace is opened.
75
76 Returns the corresponding :class:`TraceHandle` instance for
77 this opened trace on success, or ``None`` on error.
78 """
79
80 ret = nbt._bt_context_add_trace(self._tc, path, format_str,
81 None, None, None)
82
83 if ret < 0:
84 return None
85
86 th = TraceHandle.__new__(TraceHandle)
87 th._id = ret
88 th._trace_collection = self
89
90 return th
91
92 def add_traces_recursive(self, path, format_str):
93 """
94 Adds traces to this trace collection by recursively searching
95 in the *path* directory.
96
97 *format_str* is a string indicating the Babeltrace type of the
98 traces to find and add. ``ctf`` is the only currently supported
99 trace format.
100
101 See also :meth:`add_trace`.
102
103 Returns a :class:`dict` object mapping full paths to trace
104 handles for each trace found, or ``None`` on error.
105 """
106
107 trace_handles = {}
108 noTrace = True
109 error = False
110
111 for fullpath, dirs, files in os.walk(path):
112 if "metadata" in files:
113 trace_handle = self.add_trace(fullpath, format_str)
114
115 if trace_handle is None:
116 error = True
117 continue
118
119 trace_handles[fullpath] = trace_handle
120 noTrace = False
121
122 if noTrace and error:
123 return None
124
125 return trace_handles
126
127 def remove_trace(self, trace_handle):
128 """
129 Removes a trace from the trace collection using its trace
130 handle *trace_handle*.
131
132 :class:`TraceHandle` objects are returned by :meth:`add_trace`
133 and :meth:`add_traces_recursive`.
134
135 The trace is closed before being removed.
136 """
137
138 try:
139 nbt._bt_context_remove_trace(self._tc, trace_handle._id)
140 except AttributeError:
141 raise TypeError("in remove_trace, argument 2 must be a TraceHandle instance")
142
143 @property
144 def events(self):
145 """
146 Generates the ordered :class:`Event` objects of all the opened
147 traces contained in this trace collection. Iterate this function
148 to iterate actual events.
149
150 Due to limitations of the native Babeltrace API, only one event
151 may be "alive" at a given time, i.e. a user **should never**
152 store a copy of the events returned by this function for
153 ulterior use. Users shall make sure to copy the information
154 they need *from* an event before accessing the next one.
155
156 Furthermore, :class:`Event` objects become invalid when the
157 generator goes out of scope as the underlying iterator will be
158 reclaimed. Using an event after the the generator has gone out
159 of scope may result in a crash or data corruption.
160 """
161
162 begin_pos_ptr = nbt._bt_iter_pos()
163 end_pos_ptr = nbt._bt_iter_pos()
164 begin_pos_ptr.type = nbt.SEEK_BEGIN
165 end_pos_ptr.type = nbt.SEEK_LAST
166
167 for event in self._events(begin_pos_ptr, end_pos_ptr):
168 yield event
169
170 def events_timestamps(self, timestamp_begin, timestamp_end):
171 """
172 Generates the ordered :class:`Event` objects of all the opened
173 traces contained in this trace collection from *timestamp_begin*
174 to *timestamp_end*.
175
176 See :attr:`events` for notes and limitations.
177 """
178
179 begin_pos_ptr = nbt._bt_iter_pos()
180 end_pos_ptr = nbt._bt_iter_pos()
181 begin_pos_ptr.type = end_pos_ptr.type = nbt.SEEK_TIME
182 begin_pos_ptr.u.seek_time = timestamp_begin
183 end_pos_ptr.u.seek_time = timestamp_end
184
185 for event in self._events(begin_pos_ptr, end_pos_ptr):
186 yield event
187
188 @property
189 def timestamp_begin(self):
190 """
191 Trace collection's begin timestamp.
192 """
193
194 pos_ptr = nbt._bt_iter_pos()
195 pos_ptr.type = nbt.SEEK_BEGIN
196
197 return self._timestamp_at_pos(pos_ptr)
198
199 @property
200 def timestamp_end(self):
201 """
202 Trace collection's end timestamp.
203 """
204
205 pos_ptr = nbt._bt_iter_pos()
206 pos_ptr.type = nbt.SEEK_LAST
207
208 return self._timestamp_at_pos(pos_ptr)
209
210 def _timestamp_at_pos(self, pos_ptr):
211 ctf_it_ptr = nbt._bt_ctf_iter_create(self._tc, pos_ptr, pos_ptr)
212
213 if ctf_it_ptr is None:
214 raise NotImplementedError("Creation of multiple iterators is unsupported.")
215
216 ev_ptr = nbt._bt_ctf_iter_read_event(ctf_it_ptr)
217 nbt._bt_ctf_iter_destroy(ctf_it_ptr)
218
219 def _events(self, begin_pos_ptr, end_pos_ptr):
220 ctf_it_ptr = nbt._bt_ctf_iter_create(self._tc, begin_pos_ptr, end_pos_ptr)
221
222 if ctf_it_ptr is None:
223 raise NotImplementedError("Creation of multiple iterators is unsupported.")
224
225 while True:
226 ev_ptr = nbt._bt_ctf_iter_read_event(ctf_it_ptr)
227
228 if ev_ptr is None:
229 break
230
231 ev = Event.__new__(Event)
232 ev._e = ev_ptr
233
234 try:
235 yield ev
236 except GeneratorExit:
237 break
238
239 ret = nbt._bt_iter_next(nbt._bt_ctf_get_iter(ctf_it_ptr))
240
241 if ret != 0:
242 break
243
244 nbt._bt_ctf_iter_destroy(ctf_it_ptr)
245
246
247 # Based on enum bt_clock_type in clock-type.h
248 class _ClockType:
249 CLOCK_CYCLES = 0
250 CLOCK_REAL = 1
251
252
253 class TraceHandle:
254 """
255 A :class:`TraceHandle` is a handle allowing the user to manipulate
256 a specific trace directly. It is a unique identifier representing a
257 trace, and is not meant to be instantiated by the user.
258 """
259
260 def __init__(self):
261 raise NotImplementedError("TraceHandle cannot be instantiated")
262
263 def __repr__(self):
264 return "Babeltrace TraceHandle: trace_id('{0}')".format(self._id)
265
266 @property
267 def id(self):
268 """
269 Trace handle's numeric ID.
270 """
271
272 return self._id
273
274 @property
275 def path(self):
276 """
277 Path of the underlying trace.
278 """
279
280 return nbt._bt_trace_handle_get_path(self._trace_collection._tc,
281 self._id)
282
283 @property
284 def timestamp_begin(self):
285 """
286 Buffers creation timestamp (nanoseconds since Epoch) of the
287 underlying trace.
288 """
289
290 return nbt._bt_trace_handle_get_timestamp_begin(self._trace_collection._tc,
291 self._id,
292 _ClockType.CLOCK_REAL)
293
294 @property
295 def timestamp_end(self):
296 """
297 Buffers destruction timestamp (nanoseconds since Epoch) of the
298 underlying trace.
299 """
300
301 return nbt._bt_trace_handle_get_timestamp_end(self._trace_collection._tc,
302 self._id,
303 _ClockType.CLOCK_REAL)
304
305 @property
306 def events(self):
307 """
308 Generates all the :class:`EventDeclaration` objects of the
309 underlying trace.
310
311 Note that this doesn't generate actual trace *events*, but
312 rather their declarations, i.e. their layouts and metadata.
313 """
314
315 ret = nbt._bt_python_event_decl_listcaller(self.id,
316 self._trace_collection._tc)
317
318 if not isinstance(ret, list):
319 return
320
321 ptr_list, count = ret
322
323 for i in range(count):
324 tmp = EventDeclaration.__new__(EventDeclaration)
325 tmp._ed = nbt._bt_python_decl_one_from_list(ptr_list, i)
326 yield tmp
327
328
329 class CTFStringEncoding:
330 """
331 CTF string encodings.
332 """
333
334 #: None
335 NONE = 0
336
337 #: UTF-8
338 UTF8 = 1
339
340 #: ASCII
341 ASCII = 2
342
343 #: Unknown
344 UNKNOWN = 3
345
346
347 # Based on the enum in ctf-writer/writer.h
348 class ByteOrder:
349 """
350 Byte orders.
351 """
352
353 #: Native byte order
354 BYTE_ORDER_NATIVE = 0
355
356 #: Little-endian
357 BYTE_ORDER_LITTLE_ENDIAN = 1
358
359 #: Big-endian
360 BYTE_ORDER_BIG_ENDIAN = 2
361
362 #: Network byte order (big-endian)
363 BYTE_ORDER_NETWORK = 3
364
365 #: Unknown byte order
366 BYTE_ORDER_UNKNOWN = 4 # Python-specific entry
367
368
369 # enum equivalent, accessible constants
370 # These are taken directly from ctf/events.h
371 # All changes to enums must also be made here
372 class CTFTypeId:
373 """
374 CTF numeric type identifiers.
375 """
376
377 #: Unknown type
378 UNKNOWN = 0
379
380 #: Integer
381 INTEGER = 1
382
383 #: Floating point number
384 FLOAT = 2
385
386 #: Enumeration
387 ENUM = 3
388
389 #: String
390 STRING = 4
391
392 #: Structure
393 STRUCT = 5
394
395 #: Untagged variant
396 UNTAGGED_VARIANT = 6
397
398 #: Variant
399 VARIANT = 7
400
401 #: Array
402 ARRAY = 8
403
404 #: Sequence
405 SEQUENCE = 9
406
407 NR_CTF_TYPES = 10
408
409 def type_name(id):
410 """
411 Returns the name of the CTF numeric type identifier *id*.
412 """
413
414 name = "UNKNOWN_TYPE"
415 constants = [
416 attr for attr in dir(CTFTypeId) if not callable(
417 getattr(
418 CTFTypeId,
419 attr)) and not attr.startswith("__")]
420
421 for attr in constants:
422 if getattr(CTFTypeId, attr) == id:
423 name = attr
424 break
425
426 return name
427
428
429 class CTFScope:
430 """
431 CTF scopes.
432 """
433
434 #: Packet header
435 TRACE_PACKET_HEADER = 0
436
437 #: Packet context
438 STREAM_PACKET_CONTEXT = 1
439
440 #: Event header
441 STREAM_EVENT_HEADER = 2
442
443 #: Stream event context
444 STREAM_EVENT_CONTEXT = 3
445
446 #: Event context
447 EVENT_CONTEXT = 4
448
449 #: Event fields
450 EVENT_FIELDS = 5
451
452 def scope_name(scope):
453 """
454 Returns the name of the CTF scope *scope*.
455 """
456
457 name = "UNKNOWN_SCOPE"
458 constants = [
459 attr for attr in dir(CTFScope) if not callable(
460 getattr(
461 CTFScope,
462 attr)) and not attr.startswith("__")]
463
464 for attr in constants:
465 if getattr(CTFScope, attr) == scope:
466 name = attr
467 break
468
469 return name
470
471
472 # Priority of the scopes when searching for event fields
473 _scopes = [
474 CTFScope.EVENT_FIELDS,
475 CTFScope.EVENT_CONTEXT,
476 CTFScope.STREAM_EVENT_CONTEXT,
477 CTFScope.STREAM_EVENT_HEADER,
478 CTFScope.STREAM_PACKET_CONTEXT,
479 CTFScope.TRACE_PACKET_HEADER
480 ]
481
482
483 class Event(collections.Mapping):
484 """
485 An :class:`Event` object represents a trace event. :class:`Event`
486 objects are returned by :attr:`TraceCollection.events` and are
487 not meant to be instantiated by the user.
488
489 :class:`Event` has a :class:`dict`-like interface for accessing
490 an event's field value by field name:
491
492 .. code-block:: python
493
494 event['my_field']
495
496 If a field name exists in multiple scopes, the value of the first
497 field found is returned. The scopes are searched in the following
498 order:
499
500 1. Event fields (:attr:`CTFScope.EVENT_FIELDS`)
501 2. Event context (:attr:`CTFScope.EVENT_CONTEXT`)
502 3. Stream event context (:attr:`CTFScope.STREAM_EVENT_CONTEXT`)
503 4. Event header (:attr:`CTFScope.STREAM_EVENT_HEADER`)
504 5. Packet context (:attr:`CTFScope.STREAM_PACKET_CONTEXT`)
505 6. Packet header (:attr:`CTFScope.TRACE_PACKET_HEADER`)
506
507 It is still possible to obtain a field's value from a specific
508 scope using :meth:`field_with_scope`.
509
510 Field values are returned as native Python types, that is:
511
512 +-----------------------+----------------------------------+
513 | Field type | Python type |
514 +=======================+==================================+
515 | Integer | :class:`int` |
516 +-----------------------+----------------------------------+
517 | Floating point number | :class:`float` |
518 +-----------------------+----------------------------------+
519 | Enumeration | :class:`str` (enumeration label) |
520 +-----------------------+----------------------------------+
521 | String | :class:`str` |
522 +-----------------------+----------------------------------+
523 | Array | :class:`list` of native Python |
524 | | objects |
525 +-----------------------+----------------------------------+
526 | Sequence | :class:`list` of native Python |
527 | | objects |
528 +-----------------------+----------------------------------+
529 | Structure | :class:`dict` mapping field |
530 | | names to native Python objects |
531 +-----------------------+----------------------------------+
532
533 For example, printing the third element of a sequence named ``seq``
534 in a structure named ``my_struct`` of the ``event``'s field named
535 ``my_field`` is done this way:
536
537 .. code-block:: python
538
539 print(event['my_field']['my_struct']['seq'][2])
540 """
541
542 def __init__(self):
543 raise NotImplementedError("Event cannot be instantiated")
544
545 @property
546 def name(self):
547 """
548 Event's name or ``None`` on error.
549 """
550
551 return nbt._bt_ctf_event_name(self._e)
552
553 @property
554 def cycles(self):
555 """
556 Event's timestamp in cycles or -1 on error.
557 """
558
559 return nbt._bt_ctf_get_cycles(self._e)
560
561 @property
562 def timestamp(self):
563 """
564 Event's timestamp (nanoseconds since Epoch) or -1 on error.
565 """
566
567 return nbt._bt_ctf_get_timestamp(self._e)
568
569 @property
570 def datetime(self):
571 """
572 Event's timestamp as a standard :class:`datetime.datetime`
573 object.
574
575 Note that the :class:`datetime.datetime` class' precision
576 is limited to microseconds, whereas :attr:`timestamp` provides
577 the event's timestamp with a nanosecond resolution.
578 """
579
580 return datetime.fromtimestamp(self.timestamp / 1E9)
581
582 def field_with_scope(self, field_name, scope):
583 """
584 Returns the value of a field named *field_name* within the
585 scope *scope*, or ``None`` if the field cannot be found.
586
587 *scope* must be one of :class:`CTFScope` constants.
588 """
589
590 if scope not in _scopes:
591 raise ValueError("Invalid scope provided")
592
593 field = self._field_with_scope(field_name, scope)
594
595 if field is not None:
596 return field.value
597
598 def field_list_with_scope(self, scope):
599 """
600 Returns a list of field names in the scope *scope*.
601 """
602
603 if scope not in _scopes:
604 raise ValueError("Invalid scope provided")
605
606 field_names = []
607
608 for field in self._field_list_with_scope(scope):
609 field_names.append(field.name)
610
611 return field_names
612
613 @property
614 def handle(self):
615 """
616 :class:`TraceHandle` object containing this event, or ``None``
617 on error.
618 """
619
620 ret = nbt._bt_ctf_event_get_handle_id(self._e)
621
622 if ret < 0:
623 return None
624
625 th = TraceHandle.__new__(TraceHandle)
626 th._id = ret
627 th._trace_collection = self.get_trace_collection()
628
629 return th
630
631 @property
632 def trace_collection(self):
633 """
634 :class:`TraceCollection` object containing this event, or
635 ``None`` on error.
636 """
637
638 trace_collection = TraceCollection()
639 trace_collection._tc = nbt._bt_ctf_event_get_context(self._e)
640
641 if trace_collection._tc is not None:
642 return trace_collection
643
644 def __getitem__(self, field_name):
645 field = self._field(field_name)
646
647 if field is not None:
648 return field.value
649
650 raise KeyError(field_name)
651
652 def __iter__(self):
653 for key in self.keys():
654 yield key
655
656 def __len__(self):
657 count = 0
658
659 for scope in _scopes:
660 scope_ptr = nbt._bt_ctf_get_top_level_scope(self._e, scope)
661 ret = nbt._bt_python_field_listcaller(self._e, scope_ptr)
662
663 if isinstance(ret, list):
664 count += ret[1]
665
666 return count
667
668 def __contains__(self, field_name):
669 return self._field(field_name) is not None
670
671 def keys(self):
672 """
673 Returns the list of field names.
674
675 Note: field names are unique within the returned list, although
676 a field name could exist in multiple scopes. Use
677 :meth:`field_list_with_scope` to obtain the list of field names
678 of a given scope.
679 """
680
681 field_names = set()
682
683 for scope in _scopes:
684 for name in self.field_list_with_scope(scope):
685 field_names.add(name)
686
687 return list(field_names)
688
689 def get(self, field_name, default=None):
690 """
691 Returns the value of the field named *field_name*, or *default*
692 when not found.
693
694 See :class:`Event` note about how fields are retrieved by
695 name when multiple fields share the same name in different
696 scopes.
697 """
698
699 field = self._field(field_name)
700
701 if field is None:
702 return default
703
704 return field.value
705
706 def items(self):
707 """
708 Generates pairs of (field name, field value).
709
710 This method iterates :meth:`keys` to find field names, which
711 means some fields could be unavailable if other fields share
712 their names in scopes with higher priorities.
713 """
714
715 for field in self.keys():
716 yield (field, self[field])
717
718 def _field_with_scope(self, field_name, scope):
719 scope_ptr = nbt._bt_ctf_get_top_level_scope(self._e, scope)
720
721 if scope_ptr is None:
722 return None
723
724 definition_ptr = nbt._bt_ctf_get_field(self._e, scope_ptr, field_name)
725
726 if definition_ptr is None:
727 return None
728
729 field = _Definition(definition_ptr, scope)
730
731 return field
732
733 def _field(self, field_name):
734 field = None
735
736 for scope in _scopes:
737 field = self._field_with_scope(field_name, scope)
738
739 if field is not None:
740 break
741
742 return field
743
744 def _field_list_with_scope(self, scope):
745 fields = []
746 scope_ptr = nbt._bt_ctf_get_top_level_scope(self._e, scope)
747
748 # Returns a list [list_ptr, count]. If list_ptr is NULL, SWIG will only
749 # provide the "count" return value
750 count = 0
751 list_ptr = None
752 ret = nbt._bt_python_field_listcaller(self._e, scope_ptr)
753
754 if isinstance(ret, list):
755 list_ptr, count = ret
756
757 for i in range(count):
758 definition_ptr = nbt._bt_python_field_one_from_list(list_ptr, i)
759
760 if definition_ptr is not None:
761 definition = _Definition(definition_ptr, scope)
762 fields.append(definition)
763
764 return fields
765
766
767 class FieldError(Exception):
768 """
769 Field error, raised when a field's value cannot be accessed.
770 """
771
772 def __init__(self, value):
773 self.value = value
774
775 def __str__(self):
776 return repr(self.value)
777
778
779 class EventDeclaration:
780 """
781 An event declaration contains the properties of a class of events,
782 that is, the common properties and fields layout of all the actual
783 recorded events associated with this declaration.
784
785 This class is not meant to be instantiated by the user. It is
786 returned by :attr:`TraceHandle.events`.
787 """
788
789 MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
790
791 def __init__(self):
792 raise NotImplementedError("EventDeclaration cannot be instantiated")
793
794 @property
795 def name(self):
796 """
797 Event's name, or ``None`` on error.
798 """
799
800 return nbt._bt_ctf_get_decl_event_name(self._ed)
801
802 @property
803 def id(self):
804 """
805 Event's numeric ID, or -1 on error.
806 """
807
808 id = nbt._bt_ctf_get_decl_event_id(self._ed)
809
810 if id == self.MAX_UINT64:
811 id = -1
812
813 return id
814
815 @property
816 def fields(self):
817 """
818 Generates all the event's field declarations, going through
819 each scope in the following order:
820
821 1. Event fields (:attr:`CTFScope.EVENT_FIELDS`)
822 2. Event context (:attr:`CTFScope.EVENT_CONTEXT`)
823 3. Stream event context (:attr:`CTFScope.STREAM_EVENT_CONTEXT`)
824 4. Event header (:attr:`CTFScope.STREAM_EVENT_HEADER`)
825 5. Packet context (:attr:`CTFScope.STREAM_PACKET_CONTEXT`)
826 6. Packet header (:attr:`CTFScope.TRACE_PACKET_HEADER`)
827
828 All the generated field declarations inherit
829 :class:`FieldDeclaration`, and are among:
830
831 * :class:`IntegerFieldDeclaration`
832 * :class:`FloatFieldDeclaration`
833 * :class:`EnumerationFieldDeclaration`
834 * :class:`StringFieldDeclaration`
835 * :class:`ArrayFieldDeclaration`
836 * :class:`SequenceFieldDeclaration`
837 * :class:`StructureFieldDeclaration`
838 * :class:`VariantFieldDeclaration`
839 """
840
841 for scope in _scopes:
842 for declaration in self.fields_scope(scope):
843 yield declaration
844
845 def fields_scope(self, scope):
846 """
847 Generates all the field declarations of the event's scope
848 *scope*.
849
850 *scope* must be one of :class:`CTFScope` constants.
851
852 All the generated field declarations inherit
853 :class:`FieldDeclaration`, and are among:
854
855 * :class:`IntegerFieldDeclaration`
856 * :class:`FloatFieldDeclaration`
857 * :class:`EnumerationFieldDeclaration`
858 * :class:`StringFieldDeclaration`
859 * :class:`ArrayFieldDeclaration`
860 * :class:`SequenceFieldDeclaration`
861 * :class:`StructureFieldDeclaration`
862 * :class:`VariantFieldDeclaration`
863 """
864 ret = nbt._by_python_field_decl_listcaller(self._ed, scope)
865
866 if not isinstance(ret, list):
867 return
868
869 list_ptr, count = ret
870
871 for i in range(count):
872 field_decl_ptr = nbt._bt_python_field_decl_one_from_list(list_ptr, i)
873
874 if field_decl_ptr is not None:
875 decl_ptr = nbt._bt_ctf_get_decl_from_field_decl(field_decl_ptr)
876 name = nbt._bt_ctf_get_decl_field_name(field_decl_ptr)
877 field_declaration = _create_field_declaration(decl_ptr, name,
878 scope)
879 yield field_declaration
880
881
882 class FieldDeclaration:
883 """
884 Base class for concrete field declarations.
885
886 This class is not meant to be instantiated by the user.
887 """
888
889 def __init__(self):
890 raise NotImplementedError("FieldDeclaration cannot be instantiated")
891
892 def __repr__(self):
893 return "({0}) {1} {2}".format(CTFScope.scope_name(self.scope),
894 CTFTypeId.type_name(self.type),
895 self.name)
896
897 @property
898 def name(self):
899 """
900 Field's name, or ``None`` on error.
901 """
902
903 return self._name
904
905 @property
906 def type(self):
907 """
908 Field's type (one of :class:`CTFTypeId` constants).
909 """
910
911 return nbt._bt_ctf_field_type(self._fd)
912
913 @property
914 def scope(self):
915 """
916 Field's scope (one of :class:`CTFScope` constants).
917 """
918
919 return self._s
920
921
922 class IntegerFieldDeclaration(FieldDeclaration):
923 """Do not instantiate."""
924
925 def __init__(self):
926 raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated")
927
928 @property
929 def signedness(self):
930 """
931 Return the signedness of an integer:
932 0 if unsigned; 1 if signed; -1 on error.
933 """
934
935 return nbt._bt_ctf_get_int_signedness(self._fd)
936
937 @property
938 def base(self):
939 """Return the base of an int or a negative value on error."""
940
941 return nbt._bt_ctf_get_int_base(self._fd)
942
943 @property
944 def byte_order(self):
945 """
946 Return the byte order. One of class ByteOrder's entries.
947 """
948
949 ret = nbt._bt_ctf_get_int_byte_order(self._fd)
950
951 if ret == 1234:
952 return ByteOrder.BYTE_ORDER_LITTLE_ENDIAN
953 elif ret == 4321:
954 return ByteOrder.BYTE_ORDER_BIG_ENDIAN
955 else:
956 return ByteOrder.BYTE_ORDER_UNKNOWN
957
958 @property
959 def length(self):
960 """
961 Return the size, in bits, of an int or a negative
962 value on error.
963 """
964
965 return nbt._bt_ctf_get_int_len(self._fd)
966
967 @property
968 def encoding(self):
969 """
970 Return the encoding. One of class CTFStringEncoding's entries.
971 Return a negative value on error.
972 """
973
974 return nbt._bt_ctf_get_encoding(self._fd)
975
976
977 class EnumerationFieldDeclaration(FieldDeclaration):
978 """
979 Enumeration field declaration.
980
981 .. note::
982
983 As of this version, this class is missing some properties.
984 """
985
986 def __init__(self):
987 raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated")
988
989
990 class ArrayFieldDeclaration(FieldDeclaration):
991 """
992 Static array field declaration.
993 """
994
995 def __init__(self):
996 raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated")
997
998 @property
999 def length(self):
1000 """
1001 Static array's fixed length (number of contained elements), or
1002 a negative value on error.
1003 """
1004
1005 return nbt._bt_ctf_get_array_len(self._fd)
1006
1007 @property
1008 def element_declaration(self):
1009 """
1010 Underlying element's field declaration.
1011 """
1012
1013 field_decl_ptr = nbt._bt_python_get_array_element_declaration(self._fd)
1014
1015 return _create_field_declaration(field_decl_ptr, "", self.scope)
1016
1017
1018 class SequenceFieldDeclaration(FieldDeclaration):
1019 """Do not instantiate."""
1020
1021 def __init__(self):
1022 raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated")
1023
1024 @property
1025 def element_declaration(self):
1026 """
1027 Return element declaration.
1028 """
1029
1030 field_decl_ptr = nbt._bt_python_get_sequence_element_declaration(self._fd)
1031
1032 return _create_field_declaration(field_decl_ptr, "", self.scope)
1033
1034
1035 class FloatFieldDeclaration(FieldDeclaration):
1036 """Do not instantiate."""
1037
1038 def __init__(self):
1039 raise NotImplementedError("FloatFieldDeclaration cannot be instantiated")
1040
1041
1042 class StructureFieldDeclaration(FieldDeclaration):
1043 """Do not instantiate."""
1044
1045 def __init__(self):
1046 raise NotImplementedError("StructureFieldDeclaration cannot be instantiated")
1047
1048
1049 class StringFieldDeclaration(FieldDeclaration):
1050 """Do not instantiate."""
1051
1052 def __init__(self):
1053 raise NotImplementedError("StringFieldDeclaration cannot be instantiated")
1054
1055
1056 class VariantFieldDeclaration(FieldDeclaration):
1057 """Do not instantiate."""
1058
1059 def __init__(self):
1060 raise NotImplementedError("VariantFieldDeclaration cannot be instantiated")
1061
1062
1063 def field_error():
1064 """
1065 Return the last error code encountered while
1066 accessing a field and reset the error flag.
1067 Return 0 if no error, a negative value otherwise.
1068 """
1069
1070 return nbt._bt_ctf_field_get_error()
1071
1072
1073 def _create_field_declaration(declaration_ptr, name, scope):
1074 """
1075 Private field declaration factory.
1076 """
1077
1078 if declaration_ptr is None:
1079 raise ValueError("declaration_ptr must be valid")
1080 if scope not in _scopes:
1081 raise ValueError("Invalid scope provided")
1082
1083 type = nbt._bt_ctf_field_type(declaration_ptr)
1084 declaration = None
1085
1086 if type == CTFTypeId.INTEGER:
1087 declaration = IntegerFieldDeclaration.__new__(IntegerFieldDeclaration)
1088 elif type == CTFTypeId.ENUM:
1089 declaration = EnumerationFieldDeclaration.__new__(EnumerationFieldDeclaration)
1090 elif type == CTFTypeId.ARRAY:
1091 declaration = ArrayFieldDeclaration.__new__(ArrayFieldDeclaration)
1092 elif type == CTFTypeId.SEQUENCE:
1093 declaration = SequenceFieldDeclaration.__new__(SequenceFieldDeclaration)
1094 elif type == CTFTypeId.FLOAT:
1095 declaration = FloatFieldDeclaration.__new__(FloatFieldDeclaration)
1096 elif type == CTFTypeId.STRUCT:
1097 declaration = StructureFieldDeclaration.__new__(StructureFieldDeclaration)
1098 elif type == CTFTypeId.STRING:
1099 declaration = StringFieldDeclaration.__new__(StringFieldDeclaration)
1100 elif type == CTFTypeId.VARIANT:
1101 declaration = VariantFieldDeclaration.__new__(VariantFieldDeclaration)
1102 else:
1103 return declaration
1104
1105 declaration._fd = declaration_ptr
1106 declaration._s = scope
1107 declaration._name = name
1108
1109 return declaration
1110
1111
1112 class _Definition:
1113 def __init__(self, definition_ptr, scope):
1114 self._d = definition_ptr
1115 self._s = scope
1116
1117 if scope not in _scopes:
1118 ValueError("Invalid scope provided")
1119
1120 @property
1121 def name(self):
1122 """Return the name of a field or None on error."""
1123
1124 return nbt._bt_ctf_field_name(self._d)
1125
1126 @property
1127 def type(self):
1128 """Return the type of a field or -1 if unknown."""
1129
1130 return nbt._bt_ctf_field_type(nbt._bt_ctf_get_decl_from_def(self._d))
1131
1132 @property
1133 def declaration(self):
1134 """Return the associated Definition object."""
1135
1136 return _create_field_declaration(
1137 nbt._bt_ctf_get_decl_from_def(self._d), self.name, self.scope)
1138
1139 def _get_enum_str(self):
1140 """
1141 Return the string matching the current enumeration.
1142 Return None on error.
1143 """
1144
1145 return nbt._bt_ctf_get_enum_str(self._d)
1146
1147 def _get_array_element_at(self, index):
1148 """
1149 Return the array's element at position index.
1150 Return None on error
1151 """
1152
1153 array_ptr = nbt._bt_python_get_array_from_def(self._d)
1154
1155 if array_ptr is None:
1156 return None
1157
1158 definition_ptr = nbt._bt_array_index(array_ptr, index)
1159
1160 if definition_ptr is None:
1161 return None
1162
1163 return _Definition(definition_ptr, self.scope)
1164
1165 def _get_sequence_len(self):
1166 """
1167 Return the len of a sequence or a negative
1168 value on error.
1169 """
1170
1171 seq = nbt._bt_python_get_sequence_from_def(self._d)
1172
1173 return nbt._bt_sequence_len(seq)
1174
1175 def _get_sequence_element_at(self, index):
1176 """
1177 Return the sequence's element at position index,
1178 otherwise return None
1179 """
1180
1181 seq = nbt._bt_python_get_sequence_from_def(self._d)
1182
1183 if seq is not None:
1184 definition_ptr = nbt._bt_sequence_index(seq, index)
1185
1186 if definition_ptr is not None:
1187 return _Definition(definition_ptr, self.scope)
1188
1189 def _get_uint64(self):
1190 """
1191 Return the value associated with the field.
1192 If the field does not exist or is not of the type requested,
1193 the value returned is undefined. To check if an error occured,
1194 use the field_error() function after accessing a field.
1195 """
1196
1197 return nbt._bt_ctf_get_uint64(self._d)
1198
1199 def _get_int64(self):
1200 """
1201 Return the value associated with the field.
1202 If the field does not exist or is not of the type requested,
1203 the value returned is undefined. To check if an error occured,
1204 use the field_error() function after accessing a field.
1205 """
1206
1207 return nbt._bt_ctf_get_int64(self._d)
1208
1209 def _get_char_array(self):
1210 """
1211 Return the value associated with the field.
1212 If the field does not exist or is not of the type requested,
1213 the value returned is undefined. To check if an error occurred,
1214 use the field_error() function after accessing a field.
1215 """
1216
1217 return nbt._bt_ctf_get_char_array(self._d)
1218
1219 def _get_str(self):
1220 """
1221 Return the value associated with the field.
1222 If the field does not exist or is not of the type requested,
1223 the value returned is undefined. To check if an error occurred,
1224 use the field_error() function after accessing a field.
1225 """
1226
1227 return nbt._bt_ctf_get_string(self._d)
1228
1229 def _get_float(self):
1230 """
1231 Return the value associated with the field.
1232 If the field does not exist or is not of the type requested,
1233 the value returned is undefined. To check if an error occurred,
1234 use the field_error() function after accessing a field.
1235 """
1236
1237 return nbt._bt_ctf_get_float(self._d)
1238
1239 def _get_variant(self):
1240 """
1241 Return the variant's selected field.
1242 If the field does not exist or is not of the type requested,
1243 the value returned is undefined. To check if an error occurred,
1244 use the field_error() function after accessing a field.
1245 """
1246
1247 return nbt._bt_ctf_get_variant(self._d)
1248
1249 def _get_struct_field_count(self):
1250 """
1251 Return the number of fields contained in the structure.
1252 If the field does not exist or is not of the type requested,
1253 the value returned is undefined.
1254 """
1255
1256 return nbt._bt_ctf_get_struct_field_count(self._d)
1257
1258 def _get_struct_field_at(self, i):
1259 """
1260 Return the structure's field at position i.
1261 If the field does not exist or is not of the type requested,
1262 the value returned is undefined. To check if an error occurred,
1263 use the field_error() function after accessing a field.
1264 """
1265
1266 return nbt._bt_ctf_get_struct_field_index(self._d, i)
1267
1268 @property
1269 def value(self):
1270 """
1271 Return the value associated with the field according to its type.
1272 Return None on error.
1273 """
1274
1275 id = self.type
1276 value = None
1277
1278 if id == CTFTypeId.STRING:
1279 value = self._get_str()
1280 elif id == CTFTypeId.ARRAY:
1281 element_decl = self.declaration.element_declaration
1282
1283 if ((element_decl.type == CTFTypeId.INTEGER
1284 and element_decl.length == 8)
1285 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1286 value = nbt._bt_python_get_array_string(self._d)
1287 else:
1288 value = []
1289
1290 for i in range(self.declaration.length):
1291 element = self._get_array_element_at(i)
1292 value.append(element.value)
1293 elif id == CTFTypeId.INTEGER:
1294 if self.declaration.signedness == 0:
1295 value = self._get_uint64()
1296 else:
1297 value = self._get_int64()
1298 elif id == CTFTypeId.ENUM:
1299 value = self._get_enum_str()
1300 elif id == CTFTypeId.SEQUENCE:
1301 element_decl = self.declaration.element_declaration
1302
1303 if ((element_decl.type == CTFTypeId.INTEGER
1304 and element_decl.length == 8)
1305 and (element_decl.encoding == CTFStringEncoding.ASCII or element_decl.encoding == CTFStringEncoding.UTF8)):
1306 value = nbt._bt_python_get_sequence_string(self._d)
1307 else:
1308 seq_len = self._get_sequence_len()
1309 value = []
1310
1311 for i in range(seq_len):
1312 evDef = self._get_sequence_element_at(i)
1313 value.append(evDef.value)
1314 elif id == CTFTypeId.FLOAT:
1315 value = self._get_float()
1316 elif id == CTFTypeId.VARIANT:
1317 variant = _Definition.__new__(_Definition)
1318 variant._d = self._get_variant()
1319 value = variant.value
1320 elif id == CTFTypeId.STRUCT:
1321 value = {}
1322
1323 for i in range(self._get_struct_field_count()):
1324 member = _Definition(self._get_struct_field_at(i), self.scope)
1325 value[member.name] = member.value
1326
1327 if field_error():
1328 raise FieldError(
1329 "Error occurred while accessing field {} of type {}".format(
1330 self.name,
1331 CTFTypeId.type_name(id)))
1332
1333 return value
1334
1335 @property
1336 def scope(self):
1337 """Return the scope of a field or None on error."""
1338
1339 return self._s
1340
1341
1342 class CTFWriter:
1343 # Used to compare to -1ULL in error checks
1344 _MAX_UINT64 = 0xFFFFFFFFFFFFFFFF
1345
1346 class EnumerationMapping:
1347 """
1348 Enumeration mapping class. start and end values are inclusive.
1349 """
1350
1351 def __init__(self, name, start, end):
1352 self.name = name
1353 self.start = start
1354 self.end = end
1355
1356 class Clock:
1357 def __init__(self, name):
1358 self._c = nbt._bt_ctf_clock_create(name)
1359
1360 if self._c is None:
1361 raise ValueError("Invalid clock name.")
1362
1363 def __del__(self):
1364 nbt._bt_ctf_clock_put(self._c)
1365
1366 @property
1367 def name(self):
1368 """
1369 Get the clock's name.
1370 """
1371
1372 name = nbt._bt_ctf_clock_get_name(self._c)
1373
1374 if name is None:
1375 raise ValueError("Invalid clock instance.")
1376
1377 return name
1378
1379 @property
1380 def description(self):
1381 """
1382 Get the clock's description. None if unset.
1383 """
1384
1385 return nbt._bt_ctf_clock_get_description(self._c)
1386
1387 @description.setter
1388 def description(self, desc):
1389 """
1390 Set the clock's description. The description appears in the clock's TSDL
1391 meta-data.
1392 """
1393
1394 ret = nbt._bt_ctf_clock_set_description(self._c, str(desc))
1395
1396 if ret < 0:
1397 raise ValueError("Invalid clock description.")
1398
1399 @property
1400 def frequency(self):
1401 """
1402 Get the clock's frequency (Hz).
1403 """
1404
1405 freq = nbt._bt_ctf_clock_get_frequency(self._c)
1406
1407 if freq == CTFWriter._MAX_UINT64:
1408 raise ValueError("Invalid clock instance")
1409
1410 return freq
1411
1412 @frequency.setter
1413 def frequency(self, freq):
1414 """
1415 Set the clock's frequency (Hz).
1416 """
1417
1418 ret = nbt._bt_ctf_clock_set_frequency(self._c, freq)
1419
1420 if ret < 0:
1421 raise ValueError("Invalid frequency value.")
1422
1423 @property
1424 def precision(self):
1425 """
1426 Get the clock's precision (in clock ticks).
1427 """
1428
1429 precision = nbt._bt_ctf_clock_get_precision(self._c)
1430
1431 if precision == CTFWriter._MAX_UINT64:
1432 raise ValueError("Invalid clock instance")
1433
1434 return precision
1435
1436 @precision.setter
1437 def precision(self, precision):
1438 """
1439 Set the clock's precision (in clock ticks).
1440 """
1441
1442 ret = nbt._bt_ctf_clock_set_precision(self._c, precision)
1443
1444 @property
1445 def offset_seconds(self):
1446 """
1447 Get the clock's offset in seconds from POSIX.1 Epoch.
1448 """
1449
1450 offset_s = nbt._bt_ctf_clock_get_offset_s(self._c)
1451
1452 if offset_s == CTFWriter._MAX_UINT64:
1453 raise ValueError("Invalid clock instance")
1454
1455 return offset_s
1456
1457 @offset_seconds.setter
1458 def offset_seconds(self, offset_s):
1459 """
1460 Set the clock's offset in seconds from POSIX.1 Epoch.
1461 """
1462
1463 ret = nbt._bt_ctf_clock_set_offset_s(self._c, offset_s)
1464
1465 if ret < 0:
1466 raise ValueError("Invalid offset value.")
1467
1468 @property
1469 def offset(self):
1470 """
1471 Get the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1472 """
1473
1474 offset = nbt._bt_ctf_clock_get_offset(self._c)
1475
1476 if offset == CTFWriter._MAX_UINT64:
1477 raise ValueError("Invalid clock instance")
1478
1479 return offset
1480
1481 @offset.setter
1482 def offset(self, offset):
1483 """
1484 Set the clock's offset in ticks from POSIX.1 Epoch + offset in seconds.
1485 """
1486
1487 ret = nbt._bt_ctf_clock_set_offset(self._c, offset)
1488
1489 if ret < 0:
1490 raise ValueError("Invalid offset value.")
1491
1492 @property
1493 def absolute(self):
1494 """
1495 Get a clock's absolute attribute. A clock is absolute if the clock
1496 is a global reference across the trace's other clocks.
1497 """
1498
1499 is_absolute = nbt._bt_ctf_clock_get_is_absolute(self._c)
1500
1501 if is_absolute == -1:
1502 raise ValueError("Invalid clock instance")
1503
1504 return False if is_absolute == 0 else True
1505
1506 @absolute.setter
1507 def absolute(self, is_absolute):
1508 """
1509 Set a clock's absolute attribute. A clock is absolute if the clock
1510 is a global reference across the trace's other clocks.
1511 """
1512
1513 ret = nbt._bt_ctf_clock_set_is_absolute(self._c, int(is_absolute))
1514
1515 if ret < 0:
1516 raise ValueError("Could not set the clock's absolute attribute.")
1517
1518 @property
1519 def uuid(self):
1520 """
1521 Get a clock's UUID (an object of type UUID).
1522 """
1523
1524 uuid_list = []
1525
1526 for i in range(16):
1527 ret, value = nbt._bt_python_ctf_clock_get_uuid_index(self._c, i)
1528
1529 if ret < 0:
1530 raise ValueError("Invalid clock instance")
1531
1532 uuid_list.append(value)
1533
1534 return UUID(bytes=bytes(uuid_list))
1535
1536 @uuid.setter
1537 def uuid(self, uuid):
1538 """
1539 Set a clock's UUID (an object of type UUID).
1540 """
1541
1542 uuid_bytes = uuid.bytes
1543
1544 if len(uuid_bytes) != 16:
1545 raise ValueError("Invalid UUID provided. UUID length must be 16 bytes")
1546
1547 for i in range(len(uuid_bytes)):
1548 ret = nbt._bt_python_ctf_clock_set_uuid_index(self._c, i,
1549 uuid_bytes[i])
1550
1551 if ret < 0:
1552 raise ValueError("Invalid clock instance")
1553
1554 @property
1555 def time(self):
1556 """
1557 Get the current time in nanoseconds since the clock's origin (offset and
1558 offset_s attributes).
1559 """
1560
1561 time = nbt._bt_ctf_clock_get_time(self._c)
1562
1563 if time == CTFWriter._MAX_UINT64:
1564 raise ValueError("Invalid clock instance")
1565
1566 return time
1567
1568 @time.setter
1569 def time(self, time):
1570 """
1571 Set the current time in nanoseconds since the clock's origin (offset and
1572 offset_s attributes). The clock's value will be sampled as events are
1573 appended to a stream.
1574 """
1575
1576 ret = nbt._bt_ctf_clock_set_time(self._c, time)
1577
1578 if ret < 0:
1579 raise ValueError("Invalid time value.")
1580
1581 class FieldDeclaration:
1582 """
1583 FieldDeclaration should not be instantiated directly. Instantiate
1584 one of the concrete FieldDeclaration classes.
1585 """
1586
1587 class IntegerBase:
1588 # These values are based on the bt_ctf_integer_base enum
1589 # declared in event-types.h.
1590 INTEGER_BASE_UNKNOWN = -1
1591 INTEGER_BASE_BINARY = 2
1592 INTEGER_BASE_OCTAL = 8
1593 INTEGER_BASE_DECIMAL = 10
1594 INTEGER_BASE_HEXADECIMAL = 16
1595
1596 def __init__(self):
1597 if self._ft is None:
1598 raise ValueError("FieldDeclaration creation failed.")
1599
1600 def __del__(self):
1601 nbt._bt_ctf_field_type_put(self._ft)
1602
1603 @staticmethod
1604 def _create_field_declaration_from_native_instance(
1605 native_field_declaration):
1606 type_dict = {
1607 CTFTypeId.INTEGER: CTFWriter.IntegerFieldDeclaration,
1608 CTFTypeId.FLOAT: CTFWriter.FloatFieldDeclaration,
1609 CTFTypeId.ENUM: CTFWriter.EnumerationFieldDeclaration,
1610 CTFTypeId.STRING: CTFWriter.StringFieldDeclaration,
1611 CTFTypeId.STRUCT: CTFWriter.StructureFieldDeclaration,
1612 CTFTypeId.VARIANT: CTFWriter.VariantFieldDeclaration,
1613 CTFTypeId.ARRAY: CTFWriter.ArrayFieldDeclaration,
1614 CTFTypeId.SEQUENCE: CTFWriter.SequenceFieldDeclaration
1615 }
1616
1617 field_type_id = nbt._bt_ctf_field_type_get_type_id(native_field_declaration)
1618
1619 if field_type_id == CTFTypeId.UNKNOWN:
1620 raise TypeError("Invalid field instance")
1621
1622 declaration = CTFWriter.Field.__new__(CTFWriter.Field)
1623 declaration._ft = native_field_declaration
1624 declaration.__class__ = type_dict[field_type_id]
1625
1626 return declaration
1627
1628 @property
1629 def alignment(self):
1630 """
1631 Get the field declaration's alignment. Returns -1 on error.
1632 """
1633
1634 return nbt._bt_ctf_field_type_get_alignment(self._ft)
1635
1636 @alignment.setter
1637 def alignment(self, alignment):
1638 """
1639 Set the field declaration's alignment. Defaults to 1 (bit-aligned). However,
1640 some types, such as structures and string, may impose other alignment
1641 constraints.
1642 """
1643
1644 ret = nbt._bt_ctf_field_type_set_alignment(self._ft, alignment)
1645
1646 if ret < 0:
1647 raise ValueError("Invalid alignment value.")
1648
1649 @property
1650 def byte_order(self):
1651 """
1652 Get the field declaration's byte order. One of the ByteOrder's constant.
1653 """
1654
1655 return nbt._bt_ctf_field_type_get_byte_order(self._ft)
1656
1657 @byte_order.setter
1658 def byte_order(self, byte_order):
1659 """
1660 Set the field declaration's byte order. Use constants defined in the ByteOrder
1661 class.
1662 """
1663
1664 ret = nbt._bt_ctf_field_type_set_byte_order(self._ft, byte_order)
1665
1666 if ret < 0:
1667 raise ValueError("Could not set byte order value.")
1668
1669 class IntegerFieldDeclaration(FieldDeclaration):
1670 def __init__(self, size):
1671 """
1672 Create a new integer field declaration of the given size.
1673 """
1674 self._ft = nbt._bt_ctf_field_type_integer_create(size)
1675 super().__init__()
1676
1677 @property
1678 def size(self):
1679 """
1680 Get an integer's size.
1681 """
1682
1683 ret = nbt._bt_ctf_field_type_integer_get_size(self._ft)
1684
1685 if ret < 0:
1686 raise ValueError("Could not get Integer's size attribute.")
1687 else:
1688 return ret
1689
1690 @property
1691 def signed(self):
1692 """
1693 Get an integer's signedness attribute.
1694 """
1695
1696 ret = nbt._bt_ctf_field_type_integer_get_signed(self._ft)
1697
1698 if ret < 0:
1699 raise ValueError("Could not get Integer's signed attribute.")
1700 elif ret > 0:
1701 return True
1702 else:
1703 return False
1704
1705 @signed.setter
1706 def signed(self, signed):
1707 """
1708 Set an integer's signedness attribute.
1709 """
1710
1711 ret = nbt._bt_ctf_field_type_integer_set_signed(self._ft, signed)
1712
1713 if ret < 0:
1714 raise ValueError("Could not set Integer's signed attribute.")
1715
1716 @property
1717 def base(self):
1718 """
1719 Get the integer's base used to pretty-print the resulting trace.
1720 Returns a constant from the FieldDeclaration.IntegerBase class.
1721 """
1722
1723 return nbt._bt_ctf_field_type_integer_get_base(self._ft)
1724
1725 @base.setter
1726 def base(self, base):
1727 """
1728 Set the integer's base used to pretty-print the resulting trace.
1729 The base must be a constant of the FieldDeclarationIntegerBase class.
1730 """
1731
1732 ret = nbt._bt_ctf_field_type_integer_set_base(self._ft, base)
1733
1734 if ret < 0:
1735 raise ValueError("Could not set Integer's base.")
1736
1737 @property
1738 def encoding(self):
1739 """
1740 Get the integer's encoding (one of the constants of the
1741 CTFStringEncoding class).
1742 Returns a constant from the CTFStringEncoding class.
1743 """
1744
1745 return nbt._bt_ctf_field_type_integer_get_encoding(self._ft)
1746
1747 @encoding.setter
1748 def encoding(self, encoding):
1749 """
1750 An integer encoding may be set to signal that the integer must be printed
1751 as a text character. Must be a constant from the CTFStringEncoding class.
1752 """
1753
1754 ret = nbt._bt_ctf_field_type_integer_set_encoding(self._ft, encoding)
1755
1756 if ret < 0:
1757 raise ValueError("Could not set Integer's encoding.")
1758
1759 class EnumerationFieldDeclaration(FieldDeclaration):
1760 def __init__(self, integer_type):
1761 """
1762 Create a new enumeration field declaration with the given underlying container type.
1763 """
1764 isinst = isinstance(integer_type, CTFWriter.IntegerFieldDeclaration)
1765
1766 if integer_type is None or not isinst:
1767 raise TypeError("Invalid integer container.")
1768
1769 self._ft = nbt._bt_ctf_field_type_enumeration_create(integer_type._ft)
1770 super().__init__()
1771
1772 @property
1773 def container(self):
1774 """
1775 Get the enumeration's underlying container type.
1776 """
1777
1778 ret = nbt._bt_ctf_field_type_enumeration_get_container_type(self._ft)
1779
1780 if ret is None:
1781 raise TypeError("Invalid enumeration declaration")
1782
1783 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
1784
1785 def add_mapping(self, name, range_start, range_end):
1786 """
1787 Add a mapping to the enumeration. The range's values are inclusive.
1788 """
1789
1790 if range_start < 0 or range_end < 0:
1791 ret = nbt._bt_ctf_field_type_enumeration_add_mapping(self._ft,
1792 str(name),
1793 range_start,
1794 range_end)
1795 else:
1796 ret = nbt._bt_ctf_field_type_enumeration_add_mapping_unsigned(self._ft,
1797 str(name),
1798 range_start,
1799 range_end)
1800
1801 if ret < 0:
1802 raise ValueError("Could not add mapping to enumeration declaration.")
1803
1804 @property
1805 def mappings(self):
1806 """
1807 Generator returning instances of EnumerationMapping.
1808 """
1809
1810 signed = self.container.signed
1811
1812 count = nbt._bt_ctf_field_type_enumeration_get_mapping_count(self._ft)
1813
1814 for i in range(count):
1815 if signed:
1816 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, i)
1817 else:
1818 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, i)
1819
1820 if len(ret) != 3:
1821 msg = "Could not get Enumeration mapping at index {}".format(i)
1822 raise TypeError(msg)
1823
1824 name, range_start, range_end = ret
1825 yield CTFWriter.EnumerationMapping(name, range_start, range_end)
1826
1827 def get_mapping_by_name(self, name):
1828 """
1829 Get a mapping by name (EnumerationMapping).
1830 """
1831
1832 index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_name(self._ft, name)
1833
1834 if index < 0:
1835 return None
1836
1837 if self.container.signed:
1838 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index)
1839 else:
1840 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index)
1841
1842 if len(ret) != 3:
1843 msg = "Could not get Enumeration mapping at index {}".format(i)
1844 raise TypeError(msg)
1845
1846 name, range_start, range_end = ret
1847
1848 return CTFWriter.EnumerationMapping(name, range_start, range_end)
1849
1850 def get_mapping_by_value(self, value):
1851 """
1852 Get a mapping by value (EnumerationMapping).
1853 """
1854
1855 if value < 0:
1856 index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_value(self._ft, value)
1857 else:
1858 index = nbt._bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(self._ft, value)
1859
1860 if index < 0:
1861 return None
1862
1863 if self.container.signed:
1864 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping(self._ft, index)
1865 else:
1866 ret = nbt._bt_python_ctf_field_type_enumeration_get_mapping_unsigned(self._ft, index)
1867
1868 if len(ret) != 3:
1869 msg = "Could not get Enumeration mapping at index {}".format(i)
1870 raise TypeError(msg)
1871
1872 name, range_start, range_end = ret
1873
1874 return CTFWriter.EnumerationMapping(name, range_start, range_end)
1875
1876 class FloatFieldDeclaration(FieldDeclaration):
1877 FLT_EXP_DIG = 8
1878 DBL_EXP_DIG = 11
1879 FLT_MANT_DIG = 24
1880 DBL_MANT_DIG = 53
1881
1882 def __init__(self):
1883 """
1884 Create a new floating point field declaration.
1885 """
1886
1887 self._ft = nbt._bt_ctf_field_type_floating_point_create()
1888 super().__init__()
1889
1890 @property
1891 def exponent_digits(self):
1892 """
1893 Get the number of exponent digits used to store the floating point field.
1894 """
1895
1896 ret = nbt._bt_ctf_field_type_floating_point_get_exponent_digits(self._ft)
1897
1898 if ret < 0:
1899 raise TypeError(
1900 "Could not get Floating point exponent digit count")
1901
1902 return ret
1903
1904 @exponent_digits.setter
1905 def exponent_digits(self, exponent_digits):
1906 """
1907 Set the number of exponent digits to use to store the floating point field.
1908 The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG which
1909 are defined as constants of this class.
1910 """
1911
1912 ret = nbt._bt_ctf_field_type_floating_point_set_exponent_digits(self._ft,
1913 exponent_digits)
1914
1915 if ret < 0:
1916 raise ValueError("Could not set exponent digit count.")
1917
1918 @property
1919 def mantissa_digits(self):
1920 """
1921 Get the number of mantissa digits used to store the floating point field.
1922 """
1923
1924 ret = nbt._bt_ctf_field_type_floating_point_get_mantissa_digits(self._ft)
1925
1926 if ret < 0:
1927 raise TypeError("Could not get Floating point mantissa digit count")
1928
1929 return ret
1930
1931 @mantissa_digits.setter
1932 def mantissa_digits(self, mantissa_digits):
1933 """
1934 Set the number of mantissa digits to use to store the floating point field.
1935 The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG which
1936 are defined as constants of this class.
1937 """
1938
1939 ret = nbt._bt_ctf_field_type_floating_point_set_mantissa_digits(self._ft,
1940 mantissa_digits)
1941
1942 if ret < 0:
1943 raise ValueError("Could not set mantissa digit count.")
1944
1945 class FloatingPointFieldDeclaration(FloatFieldDeclaration):
1946 pass
1947
1948 class StructureFieldDeclaration(FieldDeclaration):
1949 def __init__(self):
1950 """
1951 Create a new structure field declaration.
1952 """
1953
1954 self._ft = nbt._bt_ctf_field_type_structure_create()
1955 super().__init__()
1956
1957 def add_field(self, field_type, field_name):
1958 """
1959 Add a field of type "field_type" to the structure.
1960 """
1961
1962 ret = nbt._bt_ctf_field_type_structure_add_field(self._ft,
1963 field_type._ft,
1964 str(field_name))
1965
1966 if ret < 0:
1967 raise ValueError("Could not add field to structure.")
1968
1969 @property
1970 def fields(self):
1971 """
1972 Generator returning the structure's field as tuples of (field name, field declaration).
1973 """
1974
1975 count = nbt._bt_ctf_field_type_structure_get_field_count(self._ft)
1976
1977 if count < 0:
1978 raise TypeError("Could not get Structure field count")
1979
1980 for i in range(count):
1981 field_name = nbt._bt_python_ctf_field_type_structure_get_field_name(self._ft, i)
1982
1983 if field_name is None:
1984 msg = "Could not get Structure field name at index {}".format(i)
1985 raise TypeError(msg)
1986
1987 field_type_native = nbt._bt_python_ctf_field_type_structure_get_field_type(self._ft, i)
1988
1989 if field_type_native is None:
1990 msg = "Could not get Structure field type at index {}".format(i)
1991 raise TypeError(msg)
1992
1993 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
1994 yield (field_name, field_type)
1995
1996 def get_field_by_name(self, name):
1997 """
1998 Get a field declaration by name (FieldDeclaration).
1999 """
2000
2001 field_type_native = nbt._bt_ctf_field_type_structure_get_field_type_by_name(self._ft, name)
2002
2003 if field_type_native is None:
2004 msg = "Could not find Structure field with name {}".format(name)
2005 raise TypeError(msg)
2006
2007 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2008
2009 class VariantFieldDeclaration(FieldDeclaration):
2010 def __init__(self, enum_tag, tag_name):
2011 """
2012 Create a new variant field declaration.
2013 """
2014
2015 isinst = isinstance(enum_tag, CTFWriter.EnumerationFieldDeclaration)
2016 if enum_tag is None or not isinst:
2017 raise TypeError("Invalid tag type; must be of type EnumerationFieldDeclaration.")
2018
2019 self._ft = nbt._bt_ctf_field_type_variant_create(enum_tag._ft,
2020 str(tag_name))
2021 super().__init__()
2022
2023 @property
2024 def tag_name(self):
2025 """
2026 Get the variant's tag name.
2027 """
2028
2029 ret = nbt._bt_ctf_field_type_variant_get_tag_name(self._ft)
2030
2031 if ret is None:
2032 raise TypeError("Could not get Variant tag name")
2033
2034 return ret
2035
2036 @property
2037 def tag_type(self):
2038 """
2039 Get the variant's tag type.
2040 """
2041
2042 ret = nbt._bt_ctf_field_type_variant_get_tag_type(self._ft)
2043
2044 if ret is None:
2045 raise TypeError("Could not get Variant tag type")
2046
2047 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2048
2049 def add_field(self, field_type, field_name):
2050 """
2051 Add a field of type "field_type" to the variant.
2052 """
2053
2054 ret = nbt._bt_ctf_field_type_variant_add_field(self._ft,
2055 field_type._ft,
2056 str(field_name))
2057
2058 if ret < 0:
2059 raise ValueError("Could not add field to variant.")
2060
2061 @property
2062 def fields(self):
2063 """
2064 Generator returning the variant's field as tuples of (field name, field declaration).
2065 """
2066
2067 count = nbt._bt_ctf_field_type_variant_get_field_count(self._ft)
2068
2069 if count < 0:
2070 raise TypeError("Could not get Variant field count")
2071
2072 for i in range(count):
2073 field_name = nbt._bt_python_ctf_field_type_variant_get_field_name(self._ft, i)
2074
2075 if field_name is None:
2076 msg = "Could not get Variant field name at index {}".format(i)
2077 raise TypeError(msg)
2078
2079 field_type_native = nbt._bt_python_ctf_field_type_variant_get_field_type(self._ft, i)
2080
2081 if field_type_native is None:
2082 msg = "Could not get Variant field type at index {}".format(i)
2083 raise TypeError(msg)
2084
2085 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2086 yield (field_name, field_type)
2087
2088 def get_field_by_name(self, name):
2089 """
2090 Get a field declaration by name (FieldDeclaration).
2091 """
2092
2093 field_type_native = nbt._bt_ctf_field_type_variant_get_field_type_by_name(self._ft,
2094 name)
2095
2096 if field_type_native is None:
2097 msg = "Could not find Variant field with name {}".format(name)
2098 raise TypeError(msg)
2099
2100 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2101
2102 def get_field_from_tag(self, tag):
2103 """
2104 Get a field declaration from tag (EnumerationField).
2105 """
2106
2107 field_type_native = nbt._bt_ctf_field_type_variant_get_field_type_from_tag(self._ft, tag._f)
2108
2109 if field_type_native is None:
2110 msg = "Could not find Variant field with tag value {}".format(tag.value)
2111 raise TypeError(msg)
2112
2113 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2114
2115 class ArrayFieldDeclaration(FieldDeclaration):
2116 def __init__(self, element_type, length):
2117 """
2118 Create a new array field declaration.
2119 """
2120
2121 self._ft = nbt._bt_ctf_field_type_array_create(element_type._ft,
2122 length)
2123 super().__init__()
2124
2125 @property
2126 def element_type(self):
2127 """
2128 Get the array's element type.
2129 """
2130
2131 ret = nbt._bt_ctf_field_type_array_get_element_type(self._ft)
2132
2133 if ret is None:
2134 raise TypeError("Could not get Array element type")
2135
2136 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2137
2138 @property
2139 def length(self):
2140 """
2141 Get the array's length.
2142 """
2143
2144 ret = nbt._bt_ctf_field_type_array_get_length(self._ft)
2145
2146 if ret < 0:
2147 raise TypeError("Could not get Array length")
2148
2149 return ret
2150
2151 class SequenceFieldDeclaration(FieldDeclaration):
2152 def __init__(self, element_type, length_field_name):
2153 """
2154 Create a new sequence field declaration.
2155 """
2156
2157 self._ft = nbt._bt_ctf_field_type_sequence_create(element_type._ft,
2158 str(length_field_name))
2159 super().__init__()
2160
2161 @property
2162 def element_type(self):
2163 """
2164 Get the sequence's element type.
2165 """
2166
2167 ret = nbt._bt_ctf_field_type_sequence_get_element_type(self._ft)
2168
2169 if ret is None:
2170 raise TypeError("Could not get Sequence element type")
2171
2172 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(ret)
2173
2174 @property
2175 def length_field_name(self):
2176 """
2177 Get the sequence's length field name.
2178 """
2179
2180 ret = nbt._bt_ctf_field_type_sequence_get_length_field_name(self._ft)
2181
2182 if ret is None:
2183 raise TypeError("Could not get Sequence length field name")
2184
2185 return ret
2186
2187 class StringFieldDeclaration(FieldDeclaration):
2188 def __init__(self):
2189 """
2190 Create a new string field declaration.
2191 """
2192
2193 self._ft = nbt._bt_ctf_field_type_string_create()
2194 super().__init__()
2195
2196 @property
2197 def encoding(self):
2198 """
2199 Get a string declaration's encoding (a constant from the CTFStringEncoding class).
2200 """
2201
2202 return nbt._bt_ctf_field_type_string_get_encoding(self._ft)
2203
2204 @encoding.setter
2205 def encoding(self, encoding):
2206 """
2207 Set a string declaration's encoding. Must be a constant from the CTFStringEncoding class.
2208 """
2209
2210 ret = nbt._bt_ctf_field_type_string_set_encoding(self._ft, encoding)
2211 if ret < 0:
2212 raise ValueError("Could not set string encoding.")
2213
2214 @staticmethod
2215 def create_field(field_type):
2216 """
2217 Create an instance of a field.
2218 """
2219 isinst = isinstance(field_type, CTFWriter.FieldDeclaration)
2220
2221 if field_type is None or not isinst:
2222 raise TypeError("Invalid field_type. Type must be a FieldDeclaration-derived class.")
2223
2224 if isinstance(field_type, CTFWriter.IntegerFieldDeclaration):
2225 return CTFWriter.IntegerField(field_type)
2226 elif isinstance(field_type, CTFWriter.EnumerationFieldDeclaration):
2227 return CTFWriter.EnumerationField(field_type)
2228 elif isinstance(field_type, CTFWriter.FloatFieldDeclaration):
2229 return CTFWriter.FloatingPointField(field_type)
2230 elif isinstance(field_type, CTFWriter.StructureFieldDeclaration):
2231 return CTFWriter.StructureField(field_type)
2232 elif isinstance(field_type, CTFWriter.VariantFieldDeclaration):
2233 return CTFWriter.VariantField(field_type)
2234 elif isinstance(field_type, CTFWriter.ArrayFieldDeclaration):
2235 return CTFWriter.ArrayField(field_type)
2236 elif isinstance(field_type, CTFWriter.SequenceFieldDeclaration):
2237 return CTFWriter.SequenceField(field_type)
2238 elif isinstance(field_type, CTFWriter.StringFieldDeclaration):
2239 return CTFWriter.StringField(field_type)
2240
2241 class Field:
2242 """
2243 Base class, do not instantiate.
2244 """
2245
2246 def __init__(self, field_type):
2247 if not isinstance(field_type, CTFWriter.FieldDeclaration):
2248 raise TypeError("Invalid field_type argument.")
2249
2250 self._f = nbt._bt_ctf_field_create(field_type._ft)
2251
2252 if self._f is None:
2253 raise ValueError("Field creation failed.")
2254
2255 def __del__(self):
2256 nbt._bt_ctf_field_put(self._f)
2257
2258 @staticmethod
2259 def _create_field_from_native_instance(native_field_instance):
2260 type_dict = {
2261 CTFTypeId.INTEGER: CTFWriter.IntegerField,
2262 CTFTypeId.FLOAT: CTFWriter.FloatingPointField,
2263 CTFTypeId.ENUM: CTFWriter.EnumerationField,
2264 CTFTypeId.STRING: CTFWriter.StringField,
2265 CTFTypeId.STRUCT: CTFWriter.StructureField,
2266 CTFTypeId.VARIANT: CTFWriter.VariantField,
2267 CTFTypeId.ARRAY: CTFWriter.ArrayField,
2268 CTFTypeId.SEQUENCE: CTFWriter.SequenceField
2269 }
2270
2271 field_type = nbt._bt_python_get_field_type(native_field_instance)
2272
2273 if field_type == CTFTypeId.UNKNOWN:
2274 raise TypeError("Invalid field instance")
2275
2276 field = CTFWriter.Field.__new__(CTFWriter.Field)
2277 field._f = native_field_instance
2278 field.__class__ = type_dict[field_type]
2279
2280 return field
2281
2282 @property
2283 def declaration(self):
2284 native_field_type = nbt._bt_ctf_field_get_type(self._f)
2285
2286 if native_field_type is None:
2287 raise TypeError("Invalid field instance")
2288 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(
2289 native_field_type)
2290
2291 class IntegerField(Field):
2292 @property
2293 def value(self):
2294 """
2295 Get an integer field's value.
2296 """
2297
2298 signedness = nbt._bt_python_field_integer_get_signedness(self._f)
2299
2300 if signedness < 0:
2301 raise TypeError("Invalid integer instance.")
2302
2303 if signedness == 0:
2304 ret, value = nbt._bt_ctf_field_unsigned_integer_get_value(self._f)
2305 else:
2306 ret, value = nbt._bt_ctf_field_signed_integer_get_value(self._f)
2307
2308 if ret < 0:
2309 raise ValueError("Could not get integer field value.")
2310
2311 return value
2312
2313 @value.setter
2314 def value(self, value):
2315 """
2316 Set an integer field's value.
2317 """
2318
2319 if not isinstance(value, int):
2320 raise TypeError("IntegerField's value must be an int")
2321
2322 signedness = nbt._bt_python_field_integer_get_signedness(self._f)
2323 if signedness < 0:
2324 raise TypeError("Invalid integer instance.")
2325
2326 if signedness == 0:
2327 ret = nbt._bt_ctf_field_unsigned_integer_set_value(self._f, value)
2328 else:
2329 ret = nbt._bt_ctf_field_signed_integer_set_value(self._f, value)
2330
2331 if ret < 0:
2332 raise ValueError("Could not set integer field value.")
2333
2334 class EnumerationField(Field):
2335 @property
2336 def container(self):
2337 """
2338 Return the enumeration's underlying container field (an integer field).
2339 """
2340
2341 container = CTFWriter.IntegerField.__new__(CTFWriter.IntegerField)
2342 container._f = nbt._bt_ctf_field_enumeration_get_container(self._f)
2343
2344 if container._f is None:
2345 raise TypeError("Invalid enumeration field type.")
2346
2347 return container
2348
2349 @property
2350 def value(self):
2351 """
2352 Get the enumeration field's mapping name.
2353 """
2354
2355 value = nbt._bt_ctf_field_enumeration_get_mapping_name(self._f)
2356
2357 if value is None:
2358 raise ValueError("Could not get enumeration's mapping name.")
2359
2360 return value
2361
2362 @value.setter
2363 def value(self, value):
2364 """
2365 Set the enumeration field's value. Must be an integer as mapping names
2366 may be ambiguous.
2367 """
2368
2369 if not isinstance(value, int):
2370 raise TypeError("EnumerationField value must be an int")
2371
2372 self.container.value = value
2373
2374 class FloatingPointField(Field):
2375 @property
2376 def value(self):
2377 """
2378 Get a floating point field's value.
2379 """
2380
2381 ret, value = nbt._bt_ctf_field_floating_point_get_value(self._f)
2382
2383 if ret < 0:
2384 raise ValueError("Could not get floating point field value.")
2385
2386 return value
2387
2388 @value.setter
2389 def value(self, value):
2390 """
2391 Set a floating point field's value.
2392 """
2393
2394 if not isinstance(value, int) and not isinstance(value, float):
2395 raise TypeError("Value must be either a float or an int")
2396
2397 ret = nbt._bt_ctf_field_floating_point_set_value(self._f, float(value))
2398
2399 if ret < 0:
2400 raise ValueError("Could not set floating point field value.")
2401
2402 # oops!! This class is provided to ensure backward-compatibility since
2403 # a stable release publicly exposed this abomination.
2404 class FloatFieldingPoint(FloatingPointField):
2405 pass
2406
2407 class StructureField(Field):
2408 def field(self, field_name):
2409 """
2410 Get the structure's field corresponding to the provided field name.
2411 """
2412
2413 native_instance = nbt._bt_ctf_field_structure_get_field(self._f,
2414 str(field_name))
2415
2416 if native_instance is None:
2417 raise ValueError("Invalid field_name provided.")
2418
2419 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2420
2421 class VariantField(Field):
2422 def field(self, tag):
2423 """
2424 Return the variant's selected field. The "tag" field is the selector enum field.
2425 """
2426
2427 native_instance = nbt._bt_ctf_field_variant_get_field(self._f, tag._f)
2428
2429 if native_instance is None:
2430 raise ValueError("Invalid tag provided.")
2431
2432 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2433
2434 class ArrayField(Field):
2435 def field(self, index):
2436 """
2437 Return the array's field at position "index".
2438 """
2439
2440 native_instance = nbt._bt_ctf_field_array_get_field(self._f, index)
2441
2442 if native_instance is None:
2443 raise IndexError("Invalid index provided.")
2444
2445 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2446
2447 class SequenceField(Field):
2448 @property
2449 def length(self):
2450 """
2451 Get the sequence's length field (IntegerField).
2452 """
2453
2454 native_instance = nbt._bt_ctf_field_sequence_get_length(self._f)
2455
2456 if native_instance is None:
2457 length = -1
2458
2459 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2460
2461 @length.setter
2462 def length(self, length_field):
2463 """
2464 Set the sequence's length field (IntegerField).
2465 """
2466
2467 if not isinstance(length_field, CTFWriter.IntegerField):
2468 raise TypeError("Invalid length field.")
2469
2470 if length_field.declaration.signed:
2471 raise TypeError("Sequence field length must be unsigned")
2472
2473 ret = nbt._bt_ctf_field_sequence_set_length(self._f, length_field._f)
2474
2475 if ret < 0:
2476 raise ValueError("Could not set sequence length.")
2477
2478 def field(self, index):
2479 """
2480 Return the sequence's field at position "index".
2481 """
2482
2483 native_instance = nbt._bt_ctf_field_sequence_get_field(self._f, index)
2484
2485 if native_instance is None:
2486 raise ValueError("Could not get sequence element at index.")
2487
2488 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2489
2490 class StringField(Field):
2491 @property
2492 def value(self):
2493 """
2494 Get a string field's value.
2495 """
2496
2497 return nbt._bt_ctf_field_string_get_value(self._f)
2498
2499 @value.setter
2500 def value(self, value):
2501 """
2502 Set a string field's value.
2503 """
2504
2505 ret = nbt._bt_ctf_field_string_set_value(self._f, str(value))
2506
2507 if ret < 0:
2508 raise ValueError("Could not set string field value.")
2509
2510 class EventClass:
2511 def __init__(self, name):
2512 """
2513 Create a new event class of the given name.
2514 """
2515
2516 self._ec = nbt._bt_ctf_event_class_create(name)
2517
2518 if self._ec is None:
2519 raise ValueError("Event class creation failed.")
2520
2521 def __del__(self):
2522 nbt._bt_ctf_event_class_put(self._ec)
2523
2524 def add_field(self, field_type, field_name):
2525 """
2526 Add a field of type "field_type" to the event class.
2527 """
2528
2529 ret = nbt._bt_ctf_event_class_add_field(self._ec, field_type._ft,
2530 str(field_name))
2531
2532 if ret < 0:
2533 raise ValueError("Could not add field to event class.")
2534
2535 @property
2536 def name(self):
2537 """
2538 Get the event class' name.
2539 """
2540
2541 name = nbt._bt_ctf_event_class_get_name(self._ec)
2542
2543 if name is None:
2544 raise TypeError("Could not get EventClass name")
2545
2546 return name
2547
2548 @property
2549 def id(self):
2550 """
2551 Get the event class' id. Returns a negative value if unset.
2552 """
2553
2554 id = nbt._bt_ctf_event_class_get_id(self._ec)
2555
2556 if id < 0:
2557 raise TypeError("Could not get EventClass id")
2558
2559 return id
2560
2561 @id.setter
2562 def id(self, id):
2563 """
2564 Set the event class' id. Throws a TypeError if the event class
2565 is already registered to a stream class.
2566 """
2567
2568 ret = nbt._bt_ctf_event_class_set_id(self._ec, id)
2569
2570 if ret < 0:
2571 raise TypeError("Can't change an Event Class's id after it has been assigned to a stream class")
2572
2573 @property
2574 def stream_class(self):
2575 """
2576 Get the event class' stream class. Returns None if unset.
2577 """
2578 stream_class_native = nbt._bt_ctf_event_class_get_stream_class(self._ec)
2579
2580 if stream_class_native is None:
2581 return None
2582
2583 stream_class = CTFWriter.StreamClass.__new__(CTFWriter.StreamClass)
2584 stream_class._sc = stream_class_native
2585
2586 return stream_class
2587
2588 @property
2589 def fields(self):
2590 """
2591 Generator returning the event class' fields as tuples of (field name, field declaration).
2592 """
2593
2594 count = nbt._bt_ctf_event_class_get_field_count(self._ec)
2595
2596 if count < 0:
2597 raise TypeError("Could not get EventClass' field count")
2598
2599 for i in range(count):
2600 field_name = nbt._bt_python_ctf_event_class_get_field_name(self._ec, i)
2601
2602 if field_name is None:
2603 msg = "Could not get EventClass' field name at index {}".format(i)
2604 raise TypeError(msg)
2605
2606 field_type_native = nbt._bt_python_ctf_event_class_get_field_type(self._ec, i)
2607
2608 if field_type_native is None:
2609 msg = "Could not get EventClass' field type at index {}".format(i)
2610 raise TypeError(msg)
2611
2612 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2613 yield (field_name, field_type)
2614
2615 def get_field_by_name(self, name):
2616 """
2617 Get a field declaration by name (FieldDeclaration).
2618 """
2619
2620 field_type_native = nbt._bt_ctf_event_class_get_field_by_name(self._ec, name)
2621
2622 if field_type_native is None:
2623 msg = "Could not find EventClass field with name {}".format(name)
2624 raise TypeError(msg)
2625
2626 return CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2627
2628 class Event:
2629 def __init__(self, event_class):
2630 """
2631 Create a new event of the given event class.
2632 """
2633
2634 if not isinstance(event_class, CTFWriter.EventClass):
2635 raise TypeError("Invalid event_class argument.")
2636
2637 self._e = nbt._bt_ctf_event_create(event_class._ec)
2638
2639 if self._e is None:
2640 raise ValueError("Event creation failed.")
2641
2642 def __del__(self):
2643 nbt._bt_ctf_event_put(self._e)
2644
2645 @property
2646 def event_class(self):
2647 """
2648 Get the event's class.
2649 """
2650
2651 event_class_native = nbt._bt_ctf_event_get_class(self._e)
2652
2653 if event_class_native is None:
2654 return None
2655
2656 event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
2657 event_class._ec = event_class_native
2658
2659 return event_class
2660
2661 def clock(self):
2662 """
2663 Get a clock from event. Returns None if the event's class
2664 is not registered to a stream class.
2665 """
2666
2667 clock_instance = nbt._bt_ctf_event_get_clock(self._e)
2668
2669 if clock_instance is None:
2670 return None
2671
2672 clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
2673 clock._c = clock_instance
2674
2675 return clock
2676
2677 def payload(self, field_name):
2678 """
2679 Get a field from event.
2680 """
2681
2682 native_instance = nbt._bt_ctf_event_get_payload(self._e,
2683 str(field_name))
2684
2685 if native_instance is None:
2686 raise ValueError("Could not get event payload.")
2687
2688 return CTFWriter.Field._create_field_from_native_instance(native_instance)
2689
2690 def set_payload(self, field_name, value_field):
2691 """
2692 Set a manually created field as an event's payload.
2693 """
2694
2695 if not isinstance(value, CTFWriter.Field):
2696 raise TypeError("Invalid value type.")
2697
2698 ret = nbt._bt_ctf_event_set_payload(self._e, str(field_name),
2699 value_field._f)
2700
2701 if ret < 0:
2702 raise ValueError("Could not set event field payload.")
2703
2704 class StreamClass:
2705 def __init__(self, name):
2706 """
2707 Create a new stream class of the given name.
2708 """
2709
2710 self._sc = nbt._bt_ctf_stream_class_create(name)
2711
2712 if self._sc is None:
2713 raise ValueError("Stream class creation failed.")
2714
2715 def __del__(self):
2716 nbt._bt_ctf_stream_class_put(self._sc)
2717
2718 @property
2719 def name(self):
2720 """
2721 Get a stream class' name.
2722 """
2723
2724 name = nbt._bt_ctf_stream_class_get_name(self._sc)
2725
2726 if name is None:
2727 raise TypeError("Could not get StreamClass name")
2728
2729 return name
2730
2731 @property
2732 def clock(self):
2733 """
2734 Get a stream class' clock.
2735 """
2736
2737 clock_instance = nbt._bt_ctf_stream_class_get_clock(self._sc)
2738
2739 if clock_instance is None:
2740 return None
2741
2742 clock = CTFWriter.Clock.__new__(CTFWriter.Clock)
2743 clock._c = clock_instance
2744
2745 return clock
2746
2747 @clock.setter
2748 def clock(self, clock):
2749 """
2750 Assign a clock to a stream class.
2751 """
2752
2753 if not isinstance(clock, CTFWriter.Clock):
2754 raise TypeError("Invalid clock type.")
2755
2756 ret = nbt._bt_ctf_stream_class_set_clock(self._sc, clock._c)
2757
2758 if ret < 0:
2759 raise ValueError("Could not set stream class clock.")
2760
2761 @property
2762 def id(self):
2763 """
2764 Get a stream class' id.
2765 """
2766
2767 ret = nbt._bt_ctf_stream_class_get_id(self._sc)
2768
2769 if ret < 0:
2770 raise TypeError("Could not get StreamClass id")
2771
2772 return ret
2773
2774 @id.setter
2775 def id(self, id):
2776 """
2777 Assign an id to a stream class.
2778 """
2779
2780 ret = nbt._bt_ctf_stream_class_set_id(self._sc, id)
2781
2782 if ret < 0:
2783 raise TypeError("Could not set stream class id.")
2784
2785 @property
2786 def event_classes(self):
2787 """
2788 Generator returning the stream class' event classes.
2789 """
2790
2791 count = nbt._bt_ctf_stream_class_get_event_class_count(self._sc)
2792
2793 if count < 0:
2794 raise TypeError("Could not get StreamClass' event class count")
2795
2796 for i in range(count):
2797 event_class_native = nbt._bt_ctf_stream_class_get_event_class(self._sc, i)
2798
2799 if event_class_native is None:
2800 msg = "Could not get StreamClass' event class at index {}".format(i)
2801 raise TypeError(msg)
2802
2803 event_class = CTFWriter.EventClass.__new__(CTFWriter.EventClass)
2804 event_class._ec = event_class_native
2805 yield event_class
2806
2807 def add_event_class(self, event_class):
2808 """
2809 Add an event class to a stream class. New events can be added even after a
2810 stream has been instantiated and events have been appended. However, a stream
2811 will not accept events of a class that has not been added to the stream
2812 class beforehand.
2813 """
2814
2815 if not isinstance(event_class, CTFWriter.EventClass):
2816 raise TypeError("Invalid event_class type.")
2817
2818 ret = nbt._bt_ctf_stream_class_add_event_class(self._sc,
2819 event_class._ec)
2820
2821 if ret < 0:
2822 raise ValueError("Could not add event class.")
2823
2824 @property
2825 def packet_context_type(self):
2826 """
2827 Get the StreamClass' packet context type (StructureFieldDeclaration)
2828 """
2829
2830 field_type_native = nbt._bt_ctf_stream_class_get_packet_context_type(self._sc)
2831
2832 if field_type_native is None:
2833 raise ValueError("Invalid StreamClass")
2834
2835 field_type = CTFWriter.FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
2836
2837 return field_type
2838
2839 @packet_context_type.setter
2840 def packet_context_type(self, field_type):
2841 """
2842 Set a StreamClass' packet context type. Must be of type
2843 StructureFieldDeclaration.
2844 """
2845
2846 if not isinstance(field_type, CTFWriter.StructureFieldDeclaration):
2847 raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
2848
2849 ret = nbt._bt_ctf_stream_class_set_packet_context_type(self._sc,
2850 field_type._ft)
2851
2852 if ret < 0:
2853 raise ValueError("Failed to set packet context type.")
2854
2855 class Stream:
2856 def __init__(self):
2857 raise NotImplementedError("Stream cannot be instantiated; use Writer.create_stream()")
2858
2859 def __del__(self):
2860 nbt._bt_ctf_stream_put(self._s)
2861
2862 @property
2863 def discarded_events(self):
2864 """
2865 Get a stream's discarded event count.
2866 """
2867
2868 ret, count = nbt._bt_ctf_stream_get_discarded_events_count(self._s)
2869
2870 if ret < 0:
2871 raise ValueError("Could not get the stream's discarded events count")
2872
2873 return count
2874
2875 def append_discarded_events(self, event_count):
2876 """
2877 Increase the current packet's discarded event count.
2878 """
2879
2880 nbt._bt_ctf_stream_append_discarded_events(self._s, event_count)
2881
2882 def append_event(self, event):
2883 """
2884 Append "event" to the stream's current packet. The stream's associated clock
2885 will be sampled during this call. The event shall not be modified after
2886 being appended to a stream.
2887 """
2888
2889 ret = nbt._bt_ctf_stream_append_event(self._s, event._e)
2890
2891 if ret < 0:
2892 raise ValueError("Could not append event to stream.")
2893
2894 @property
2895 def packet_context(self):
2896 """
2897 Get a Stream's packet context field (a StructureField).
2898 """
2899
2900 native_field = nbt._bt_ctf_stream_get_packet_context(self._s)
2901
2902 if native_field is None:
2903 raise ValueError("Invalid Stream.")
2904
2905 return CTFWriter.Field._create_field_from_native_instance(native_field)
2906
2907 @packet_context.setter
2908 def packet_context(self, field):
2909 """
2910 Set a Stream's packet context field (must be a StructureField).
2911 """
2912
2913 if not isinstance(field, CTFWriter.StructureField):
2914 raise TypeError("Argument field must be of type StructureField")
2915
2916 ret = nbt._bt_ctf_stream_set_packet_context(self._s, field._f)
2917
2918 if ret < 0:
2919 raise ValueError("Invalid packet context field.")
2920
2921 def flush(self):
2922 """
2923 The stream's current packet's events will be flushed to disk. Events
2924 subsequently appended to the stream will be added to a new packet.
2925 """
2926
2927 ret = nbt._bt_ctf_stream_flush(self._s)
2928
2929 if ret < 0:
2930 raise ValueError("Could not flush stream.")
2931
2932 class Writer:
2933 def __init__(self, path):
2934 """
2935 Create a new writer that will produce a trace in the given path.
2936 """
2937
2938 self._w = nbt._bt_ctf_writer_create(path)
2939
2940 if self._w is None:
2941 raise ValueError("Writer creation failed.")
2942
2943 def __del__(self):
2944 nbt._bt_ctf_writer_put(self._w)
2945
2946 def create_stream(self, stream_class):
2947 """
2948 Create a new stream instance and register it to the writer.
2949 """
2950
2951 if not isinstance(stream_class, CTFWriter.StreamClass):
2952 raise TypeError("Invalid stream_class type.")
2953
2954 stream = CTFWriter.Stream.__new__(CTFWriter.Stream)
2955 stream._s = nbt._bt_ctf_writer_create_stream(self._w, stream_class._sc)
2956
2957 return stream
2958
2959 def add_environment_field(self, name, value):
2960 """
2961 Add an environment field to the trace.
2962 """
2963
2964 ret = nbt._bt_ctf_writer_add_environment_field(self._w, str(name),
2965 str(value))
2966
2967 if ret < 0:
2968 raise ValueError("Could not add environment field to trace.")
2969
2970 def add_clock(self, clock):
2971 """
2972 Add a clock to the trace. Clocks assigned to stream classes must be
2973 registered to the writer.
2974 """
2975
2976 ret = nbt._bt_ctf_writer_add_clock(self._w, clock._c)
2977
2978 if ret < 0:
2979 raise ValueError("Could not add clock to Writer.")
2980
2981 @property
2982 def metadata(self):
2983 """
2984 Get the trace's TSDL meta-data.
2985 """
2986
2987 return nbt._bt_ctf_writer_get_metadata_string(self._w)
2988
2989 def flush_metadata(self):
2990 """
2991 Flush the trace's metadata to the metadata file.
2992 """
2993
2994 nbt._bt_ctf_writer_flush_metadata(self._w)
2995
2996 @property
2997 def byte_order(self):
2998 """
2999 Get the trace's byte order. Must be a constant from the ByteOrder
3000 class.
3001 """
3002
3003 raise NotImplementedError("Getter not implemented.")
3004
3005 @byte_order.setter
3006 def byte_order(self, byte_order):
3007 """
3008 Set the trace's byte order. Must be a constant from the ByteOrder
3009 class. Defaults to the host machine's endianness
3010 """
3011
3012 ret = nbt._bt_ctf_writer_set_byte_order(self._w, byte_order)
3013
3014 if ret < 0:
3015 raise ValueError("Could not set trace's byte order.")
This page took 0.105637 seconds and 4 git commands to generate.