1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
6 from bt2
import object as bt2_object
7 from bt2
import utils
as bt2_utils
8 from bt2
import event_class
as bt2_event_class
9 from bt2
import packet
as bt2_packet
10 from bt2
import stream
as bt2_stream
11 from bt2
import field
as bt2_field
12 import collections
.abc
15 class _EventConst(bt2_object
._UniqueObject
, collections
.abc
.Mapping
):
16 _borrow_class_ptr
= staticmethod(native_bt
.event_borrow_class_const
)
17 _borrow_packet_ptr
= staticmethod(native_bt
.event_borrow_packet_const
)
18 _borrow_stream_ptr
= staticmethod(native_bt
.event_borrow_stream_const
)
19 _borrow_common_context_field_ptr
= staticmethod(
20 native_bt
.event_borrow_common_context_field_const
22 _borrow_specific_context_field_ptr
= staticmethod(
23 native_bt
.event_borrow_specific_context_field_const
25 _borrow_payload_field_ptr
= staticmethod(native_bt
.event_borrow_payload_field_const
)
26 _create_field_from_ptr
= staticmethod(bt2_field
._create
_field
_from
_const
_ptr
)
28 _event_class_pycls
= property(lambda _
: bt2_event_class
._EventClassConst
)
29 _packet_pycls
= property(lambda _
: bt2_packet
._PacketConst
)
30 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
34 event_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
35 assert event_class_ptr
is not None
36 return self
._event
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(event_class_ptr
)
48 packet_ptr
= self
._borrow
_packet
_ptr
(self
._ptr
)
50 if packet_ptr
is None:
53 return self
._packet
_pycls
._create
_from
_ptr
_and
_get
_ref
(packet_ptr
)
57 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
58 assert stream_ptr
is not None
59 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
62 def common_context_field(self
):
63 field_ptr
= self
._borrow
_common
_context
_field
_ptr
(self
._ptr
)
68 return self
._create
_field
_from
_ptr
(
69 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
73 def specific_context_field(self
):
74 field_ptr
= self
._borrow
_specific
_context
_field
_ptr
(self
._ptr
)
79 return self
._create
_field
_from
_ptr
(
80 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
84 def payload_field(self
):
85 field_ptr
= self
._borrow
_payload
_field
_ptr
(self
._ptr
)
90 return self
._create
_field
_from
_ptr
(
91 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
94 def __getitem__(self
, key
):
95 bt2_utils
._check
_str
(key
)
96 payload_field
= self
.payload_field
98 if payload_field
is not None and key
in payload_field
:
99 return payload_field
[key
]
101 specific_context_field
= self
.specific_context_field
103 if specific_context_field
is not None and key
in specific_context_field
:
104 return specific_context_field
[key
]
106 common_context_field
= self
.common_context_field
108 if common_context_field
is not None and key
in common_context_field
:
109 return common_context_field
[key
]
112 packet_context_field
= self
.packet
.context_field
114 if packet_context_field
is not None and key
in packet_context_field
:
115 return packet_context_field
[key
]
120 # To only yield unique keys, keep a set of member names that are
121 # already yielded. Two root structure fields (for example,
122 # payload and common context) can contain immediate members
123 # which share the same name.
126 if self
.payload_field
is not None:
127 for field_name
in self
.payload_field
:
129 member_names
.add(field_name
)
131 if self
.specific_context_field
is not None:
132 for field_name
in self
.specific_context_field
:
133 if field_name
not in member_names
:
135 member_names
.add(field_name
)
137 if self
.common_context_field
is not None:
138 for field_name
in self
.common_context_field
:
139 if field_name
not in member_names
:
141 member_names
.add(field_name
)
143 if self
.packet
and self
.packet
.context_field
is not None:
144 for field_name
in self
.packet
.context_field
:
145 if field_name
not in member_names
:
147 member_names
.add(field_name
)
150 return sum(1 for _
in self
)
153 class _Event(_EventConst
):
154 _borrow_class_ptr
= staticmethod(native_bt
.event_borrow_class
)
155 _borrow_packet_ptr
= staticmethod(native_bt
.event_borrow_packet
)
156 _borrow_stream_ptr
= staticmethod(native_bt
.event_borrow_stream
)
157 _borrow_common_context_field_ptr
= staticmethod(
158 native_bt
.event_borrow_common_context_field
160 _borrow_specific_context_field_ptr
= staticmethod(
161 native_bt
.event_borrow_specific_context_field
163 _borrow_payload_field_ptr
= staticmethod(native_bt
.event_borrow_payload_field
)
164 _create_field_from_ptr
= staticmethod(bt2_field
._create
_field
_from
_ptr
)
166 _event_class_pycls
= property(lambda _
: bt2_event_class
._EventClass
)
167 _packet_pycls
= property(lambda _
: bt2_packet
._Packet
)
168 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)