1 # The MIT License (MIT)
3 # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from bt2
import native_bt
, object, utils
24 from bt2
import event_class
as bt2_event_class
25 from bt2
import packet
as bt2_packet
26 from bt2
import stream
as bt2_stream
27 from bt2
import field
as bt2_field
28 import collections
.abc
31 class _EventConst(object._UniqueObject
, collections
.abc
.Mapping
):
32 _borrow_class_ptr
= staticmethod(native_bt
.event_borrow_class_const
)
33 _borrow_packet_ptr
= staticmethod(native_bt
.event_borrow_packet_const
)
34 _borrow_stream_ptr
= staticmethod(native_bt
.event_borrow_stream_const
)
35 _borrow_common_context_field_ptr
= staticmethod(
36 native_bt
.event_borrow_common_context_field_const
38 _borrow_specific_context_field_ptr
= staticmethod(
39 native_bt
.event_borrow_specific_context_field_const
41 _borrow_payload_field_ptr
= staticmethod(native_bt
.event_borrow_payload_field_const
)
42 _create_field_from_ptr
= staticmethod(bt2_field
._create
_field
_from
_const
_ptr
)
44 _event_class_pycls
= property(lambda _
: bt2_event_class
._EventClassConst
)
45 _packet_pycls
= property(lambda _
: bt2_packet
._PacketConst
)
46 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
50 event_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
51 assert event_class_ptr
is not None
52 return self
._event
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(event_class_ptr
)
64 packet_ptr
= self
._borrow
_packet
_ptr
(self
._ptr
)
66 if packet_ptr
is None:
69 return self
._packet
_pycls
._create
_from
_ptr
_and
_get
_ref
(packet_ptr
)
73 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
74 assert stream_ptr
is not None
75 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
78 def common_context_field(self
):
79 field_ptr
= self
._borrow
_common
_context
_field
_ptr
(self
._ptr
)
84 return self
._create
_field
_from
_ptr
(
85 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
89 def specific_context_field(self
):
90 field_ptr
= self
._borrow
_specific
_context
_field
_ptr
(self
._ptr
)
95 return self
._create
_field
_from
_ptr
(
96 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
100 def payload_field(self
):
101 field_ptr
= self
._borrow
_payload
_field
_ptr
(self
._ptr
)
103 if field_ptr
is None:
106 return self
._create
_field
_from
_ptr
(
107 field_ptr
, self
._owner
_ptr
, self
._owner
_get
_ref
, self
._owner
_put
_ref
110 def __getitem__(self
, key
):
111 utils
._check
_str
(key
)
112 payload_field
= self
.payload_field
114 if payload_field
is not None and key
in payload_field
:
115 return payload_field
[key
]
117 specific_context_field
= self
.specific_context_field
119 if specific_context_field
is not None and key
in specific_context_field
:
120 return specific_context_field
[key
]
122 common_context_field
= self
.common_context_field
124 if common_context_field
is not None and key
in common_context_field
:
125 return common_context_field
[key
]
128 packet_context_field
= self
.packet
.context_field
130 if packet_context_field
is not None and key
in packet_context_field
:
131 return packet_context_field
[key
]
136 # To only yield unique keys, keep a set of member names that are
137 # already yielded. Two root structure fields (for example,
138 # payload and common context) can contain immediate members
139 # which share the same name.
142 if self
.payload_field
is not None:
143 for field_name
in self
.payload_field
:
145 member_names
.add(field_name
)
147 if self
.specific_context_field
is not None:
148 for field_name
in self
.specific_context_field
:
149 if field_name
not in member_names
:
151 member_names
.add(field_name
)
153 if self
.common_context_field
is not None:
154 for field_name
in self
.common_context_field
:
155 if field_name
not in member_names
:
157 member_names
.add(field_name
)
159 if self
.packet
and self
.packet
.context_field
is not None:
160 for field_name
in self
.packet
.context_field
:
161 if field_name
not in member_names
:
163 member_names
.add(field_name
)
166 return sum(1 for _
in self
)
169 class _Event(_EventConst
):
170 _borrow_class_ptr
= staticmethod(native_bt
.event_borrow_class
)
171 _borrow_packet_ptr
= staticmethod(native_bt
.event_borrow_packet
)
172 _borrow_stream_ptr
= staticmethod(native_bt
.event_borrow_stream
)
173 _borrow_common_context_field_ptr
= staticmethod(
174 native_bt
.event_borrow_common_context_field
176 _borrow_specific_context_field_ptr
= staticmethod(
177 native_bt
.event_borrow_specific_context_field
179 _borrow_payload_field_ptr
= staticmethod(native_bt
.event_borrow_payload_field
)
180 _create_field_from_ptr
= staticmethod(bt2_field
._create
_field
_from
_ptr
)
182 _event_class_pycls
= property(lambda _
: bt2_event_class
._EventClass
)
183 _packet_pycls
= property(lambda _
: bt2_packet
._Packet
)
184 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)