bt2: make `_EventConst` a mapping
[babeltrace.git] / src / bindings / python / bt2 / bt2 / event.py
index 6a6c1185987517c8c930e32b0b545e19b0daf5a8..e7ac59abc162b92b4b5b7482f629f0bbf022f45c 100644 (file)
@@ -25,9 +25,10 @@ from bt2 import event_class as bt2_event_class
 from bt2 import packet as bt2_packet
 from bt2 import stream as bt2_stream
 from bt2 import field as bt2_field
+import collections.abc
 
 
-class _EventConst(object._UniqueObject):
+class _EventConst(object._UniqueObject, collections.abc.Mapping):
     _borrow_class_ptr = staticmethod(native_bt.event_borrow_class_const)
     _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet_const)
     _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream_const)
@@ -131,6 +132,39 @@ class _EventConst(object._UniqueObject):
 
         raise KeyError(key)
 
+    def __iter__(self):
+        # To only yield unique keys, keep a set of member names that are
+        # already yielded. Two root structure fields (for example,
+        # payload and common context) can contain immediate members
+        # which share the same name.
+        member_names = set()
+
+        if self.payload_field is not None:
+            for field_name in self.payload_field:
+                yield field_name
+                member_names.add(field_name)
+
+        if self.specific_context_field is not None:
+            for field_name in self.specific_context_field:
+                if field_name not in member_names:
+                    yield field_name
+                    member_names.add(field_name)
+
+        if self.common_context_field is not None:
+            for field_name in self.common_context_field:
+                if field_name not in member_names:
+                    yield field_name
+                    member_names.add(field_name)
+
+        if self.packet and self.packet.context_field is not None:
+            for field_name in self.packet.context_field:
+                if field_name not in member_names:
+                    yield field_name
+                    member_names.add(field_name)
+
+    def __len__(self):
+        return sum(1 for _ in self)
+
 
 class _Event(_EventConst):
     _borrow_class_ptr = staticmethod(native_bt.event_borrow_class)
This page took 0.024144 seconds and 4 git commands to generate.