bt2: Sync native_bt_field_class.i with field-class-const.h
[babeltrace.git] / bindings / python / bt2 / bt2 / notification.py
index 2ad8487893dd8a141c3c3610c6cff44bd66f88e7..6ec9f16a139aace1910e87f5d2f769694b55d3b2 100644 (file)
@@ -22,6 +22,8 @@
 
 from bt2 import native_bt, object, utils
 import bt2.clock_class_priority_map
+import bt2.clock_value
+import collections
 import bt2.packet
 import bt2.stream
 import bt2.event
@@ -39,6 +41,19 @@ def _create_from_ptr(ptr):
     return _NOTIF_TYPE_TO_CLS[notif_type]._create_from_ptr(ptr)
 
 
+def _notif_types_from_notif_classes(notification_types):
+    if notification_types is None:
+        notif_types = None
+    else:
+        for notif_cls in notification_types:
+            if notif_cls not in _NOTIF_TYPE_TO_CLS.values():
+                raise ValueError("'{}' is not a notification class".format(notif_cls))
+
+        notif_types = [notif_cls._TYPE for notif_cls in notification_types]
+
+    return notif_types
+
+
 class _Notification(object._Object):
     pass
 
@@ -54,7 +69,7 @@ class _CopyableNotification(_Notification):
 
 
 class EventNotification(_CopyableNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_EVENT
+    _TYPE = native_bt.MESSAGE_TYPE_EVENT
 
     def __init__(self, event, cc_prio_map=None):
         utils._check_type(event, bt2.event._Event)
@@ -110,7 +125,7 @@ class EventNotification(_CopyableNotification):
 
 
 class PacketBeginningNotification(_CopyableNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_PACKET_BEGIN
+    _TYPE = native_bt.MESSAGE_TYPE_PACKET_BEGINNING
 
     def __init__(self, packet):
         utils._check_type(packet, bt2.packet._Packet)
@@ -145,7 +160,7 @@ class PacketBeginningNotification(_CopyableNotification):
 
 
 class PacketEndNotification(_CopyableNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_PACKET_END
+    _TYPE = native_bt.MESSAGE_TYPE_PACKET_END
 
     def __init__(self, packet):
         utils._check_type(packet, bt2.packet._Packet)
@@ -180,7 +195,7 @@ class PacketEndNotification(_CopyableNotification):
 
 
 class StreamBeginningNotification(_CopyableNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_STREAM_BEGIN
+    _TYPE = native_bt.MESSAGE_TYPE_STREAM_BEGINNING
 
     def __init__(self, stream):
         utils._check_type(stream, bt2.stream._Stream)
@@ -215,7 +230,7 @@ class StreamBeginningNotification(_CopyableNotification):
 
 
 class StreamEndNotification(_CopyableNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_STREAM_END
+    _TYPE = native_bt.MESSAGE_TYPE_STREAM_END
 
     def __init__(self, stream):
         utils._check_type(stream, bt2.stream._Stream)
@@ -249,8 +264,50 @@ class StreamEndNotification(_CopyableNotification):
         return StreamEndNotification(self.stream)
 
 
+class _InactivityNotificationClockValuesIterator(collections.abc.Iterator):
+    def __init__(self, notif_clock_values):
+        self._notif_clock_values = notif_clock_values
+        self._clock_classes = list(notif_clock_values._notif.clock_class_priority_map)
+        self._at = 0
+
+    def __next__(self):
+        if self._at == len(self._clock_classes):
+            raise StopIteration
+
+        self._at += 1
+        return self._clock_classes[at]
+
+
+class _InactivityNotificationClockValues(collections.abc.Mapping):
+    def __init__(self, notif):
+        self._notif = notif
+
+    def __getitem__(self, clock_class):
+        utils._check_type(clock_class, bt2.ClockClass)
+        clock_value_ptr = native_bt.notification_inactivity_get_clock_value(self._notif._ptr,
+                                                                            clock_class._ptr)
+
+        if clock_value_ptr is None:
+            return
+
+        clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
+        return clock_value
+
+    def add(self, clock_value):
+        utils._check_type(clock_value, bt2.clock_value._ClockValue)
+        ret = native_bt.notification_inactivity_set_clock_value(self._notif._ptr,
+                                                                clock_value._ptr)
+        utils._handle_ret(ret, "cannot set inactivity notification object's clock value")
+
+    def __len__(self):
+        return len(self._notif.clock_class_priority_map)
+
+    def __iter__(self):
+        return _InactivityNotificationClockValuesIterator(self)
+
+
 class InactivityNotification(_CopyableNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_INACTIVITY
+    _TYPE = native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
 
     def __init__(self, cc_prio_map=None):
         if cc_prio_map is not None:
@@ -272,29 +329,14 @@ class InactivityNotification(_CopyableNotification):
         assert(cc_prio_map_ptr)
         return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr)
 
-    def clock_value(self, clock_class):
-        utils._check_type(clock_class, bt2.ClockClass)
-        clock_value_ptr = native_bt.notification_inactivity_get_clock_value(self._ptr,
-                                                                            clock_class._ptr)
-
-        if clock_value_ptr is None:
-            return
-
-        clock_value = bt2.clock_class._create_clock_value_from_ptr(clock_value_ptr)
-        return clock_value
-
-    def add_clock_value(self, clock_value):
-        utils._check_type(clock_value, bt2.clock_class._ClockValue)
-        ret = native_bt.notification_inactivity_set_clock_value(self._ptr,
-                                                                clock_value._ptr)
-        utils._handle_ret(ret, "cannot set inactivity notification object's clock value")
+    @property
+    def clock_values(self):
+        return _InactivityNotificationClockValues(self)
 
     def _get_clock_values(self):
         clock_values = {}
 
-        for clock_class in self.clock_class_priority_map:
-            clock_value = self.clock_value(clock_class)
-
+        for clock_class, clock_value in self.clock_values.items():
             if clock_value is None:
                 continue
 
@@ -322,13 +364,11 @@ class InactivityNotification(_CopyableNotification):
     def __copy__(self):
         cpy = InactivityNotification(self.clock_class_priority_map)
 
-        for clock_class in self.clock_class_priority_map:
-            clock_value = self.clock_value(clock_class)
-
+        for clock_class, clock_value in self.clock_values.items():
             if clock_value is None:
                 continue
 
-            cpy.add_clock_value(clock_value)
+            cpy.clock_values.add(clock_value)
 
         return cpy
 
@@ -352,7 +392,7 @@ class InactivityNotification(_CopyableNotification):
             clock_value_cpy = cpy_clock_class(orig_clock_value.cycles)
 
             # set copied clock value in notification copy
-            cpy.add_clock_value(clock_value_cpy)
+            cpy.clock_values.add(clock_value_cpy)
 
         memo[id(self)] = cpy
         return cpy
@@ -382,7 +422,7 @@ class _DiscardedElementsNotification(_Notification):
 
 
 class _DiscardedPacketsNotification(_DiscardedElementsNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_DISCARDED_PACKETS
+    _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_PACKETS
 
     @property
     def count(self):
@@ -403,7 +443,7 @@ class _DiscardedPacketsNotification(_DiscardedElementsNotification):
         if clock_value_ptr is None:
             return
 
-        clock_value = bt2.clock_class._create_clock_value_from_ptr(clock_value_ptr)
+        clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
         return clock_value
 
     @property
@@ -413,12 +453,12 @@ class _DiscardedPacketsNotification(_DiscardedElementsNotification):
         if clock_value_ptr is None:
             return
 
-        clock_value = bt2.clock_class._create_clock_value_from_ptr(clock_value_ptr)
+        clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
         return clock_value
 
 
 class _DiscardedEventsNotification(_DiscardedElementsNotification):
-    _TYPE = native_bt.NOTIFICATION_TYPE_DISCARDED_EVENTS
+    _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_EVENTS
 
     @property
     def count(self):
@@ -439,7 +479,7 @@ class _DiscardedEventsNotification(_DiscardedElementsNotification):
         if clock_value_ptr is None:
             return
 
-        clock_value = bt2.clock_class._create_clock_value_from_ptr(clock_value_ptr)
+        clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
         return clock_value
 
     @property
@@ -449,17 +489,17 @@ class _DiscardedEventsNotification(_DiscardedElementsNotification):
         if clock_value_ptr is None:
             return
 
-        clock_value = bt2.clock_class._create_clock_value_from_ptr(clock_value_ptr)
+        clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
         return clock_value
 
 
 _NOTIF_TYPE_TO_CLS = {
-    native_bt.NOTIFICATION_TYPE_EVENT: EventNotification,
-    native_bt.NOTIFICATION_TYPE_PACKET_BEGIN: PacketBeginningNotification,
-    native_bt.NOTIFICATION_TYPE_PACKET_END: PacketEndNotification,
-    native_bt.NOTIFICATION_TYPE_STREAM_BEGIN: StreamBeginningNotification,
-    native_bt.NOTIFICATION_TYPE_STREAM_END: StreamEndNotification,
-    native_bt.NOTIFICATION_TYPE_INACTIVITY: InactivityNotification,
-    native_bt.NOTIFICATION_TYPE_DISCARDED_PACKETS: _DiscardedPacketsNotification,
-    native_bt.NOTIFICATION_TYPE_DISCARDED_EVENTS: _DiscardedEventsNotification,
+    native_bt.MESSAGE_TYPE_EVENT: EventNotification,
+    native_bt.MESSAGE_TYPE_PACKET_BEGINNING: PacketBeginningNotification,
+    native_bt.MESSAGE_TYPE_PACKET_END: PacketEndNotification,
+    native_bt.MESSAGE_TYPE_STREAM_BEGINNING: StreamBeginningNotification,
+    native_bt.MESSAGE_TYPE_STREAM_END: StreamEndNotification,
+    native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: InactivityNotification,
+    native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsNotification,
+    native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsNotification,
 }
This page took 0.026258 seconds and 4 git commands to generate.