-# The MIT License (MIT)
+# SPDX-License-Identifier: MIT
#
# Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com>
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
from bt2 import native_bt, object, utils
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)
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)