Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / event.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com>
4
5 from bt2 import native_bt, object, utils
6 from bt2 import event_class as bt2_event_class
7 from bt2 import packet as bt2_packet
8 from bt2 import stream as bt2_stream
9 from bt2 import field as bt2_field
10 import collections.abc
11
12
13 class _EventConst(object._UniqueObject, collections.abc.Mapping):
14 _borrow_class_ptr = staticmethod(native_bt.event_borrow_class_const)
15 _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet_const)
16 _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream_const)
17 _borrow_common_context_field_ptr = staticmethod(
18 native_bt.event_borrow_common_context_field_const
19 )
20 _borrow_specific_context_field_ptr = staticmethod(
21 native_bt.event_borrow_specific_context_field_const
22 )
23 _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field_const)
24 _create_field_from_ptr = staticmethod(bt2_field._create_field_from_const_ptr)
25
26 _event_class_pycls = property(lambda _: bt2_event_class._EventClassConst)
27 _packet_pycls = property(lambda _: bt2_packet._PacketConst)
28 _stream_pycls = property(lambda _: bt2_stream._StreamConst)
29
30 @property
31 def cls(self):
32 event_class_ptr = self._borrow_class_ptr(self._ptr)
33 assert event_class_ptr is not None
34 return self._event_class_pycls._create_from_ptr_and_get_ref(event_class_ptr)
35
36 @property
37 def name(self):
38 return self.cls.name
39
40 @property
41 def id(self):
42 return self.cls.id
43
44 @property
45 def packet(self):
46 packet_ptr = self._borrow_packet_ptr(self._ptr)
47
48 if packet_ptr is None:
49 return
50
51 return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr)
52
53 @property
54 def stream(self):
55 stream_ptr = self._borrow_stream_ptr(self._ptr)
56 assert stream_ptr is not None
57 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
58
59 @property
60 def common_context_field(self):
61 field_ptr = self._borrow_common_context_field_ptr(self._ptr)
62
63 if field_ptr is None:
64 return
65
66 return self._create_field_from_ptr(
67 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
68 )
69
70 @property
71 def specific_context_field(self):
72 field_ptr = self._borrow_specific_context_field_ptr(self._ptr)
73
74 if field_ptr is None:
75 return
76
77 return self._create_field_from_ptr(
78 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
79 )
80
81 @property
82 def payload_field(self):
83 field_ptr = self._borrow_payload_field_ptr(self._ptr)
84
85 if field_ptr is None:
86 return
87
88 return self._create_field_from_ptr(
89 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
90 )
91
92 def __getitem__(self, key):
93 utils._check_str(key)
94 payload_field = self.payload_field
95
96 if payload_field is not None and key in payload_field:
97 return payload_field[key]
98
99 specific_context_field = self.specific_context_field
100
101 if specific_context_field is not None and key in specific_context_field:
102 return specific_context_field[key]
103
104 common_context_field = self.common_context_field
105
106 if common_context_field is not None and key in common_context_field:
107 return common_context_field[key]
108
109 if self.packet:
110 packet_context_field = self.packet.context_field
111
112 if packet_context_field is not None and key in packet_context_field:
113 return packet_context_field[key]
114
115 raise KeyError(key)
116
117 def __iter__(self):
118 # To only yield unique keys, keep a set of member names that are
119 # already yielded. Two root structure fields (for example,
120 # payload and common context) can contain immediate members
121 # which share the same name.
122 member_names = set()
123
124 if self.payload_field is not None:
125 for field_name in self.payload_field:
126 yield field_name
127 member_names.add(field_name)
128
129 if self.specific_context_field is not None:
130 for field_name in self.specific_context_field:
131 if field_name not in member_names:
132 yield field_name
133 member_names.add(field_name)
134
135 if self.common_context_field is not None:
136 for field_name in self.common_context_field:
137 if field_name not in member_names:
138 yield field_name
139 member_names.add(field_name)
140
141 if self.packet and self.packet.context_field is not None:
142 for field_name in self.packet.context_field:
143 if field_name not in member_names:
144 yield field_name
145 member_names.add(field_name)
146
147 def __len__(self):
148 return sum(1 for _ in self)
149
150
151 class _Event(_EventConst):
152 _borrow_class_ptr = staticmethod(native_bt.event_borrow_class)
153 _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet)
154 _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream)
155 _borrow_common_context_field_ptr = staticmethod(
156 native_bt.event_borrow_common_context_field
157 )
158 _borrow_specific_context_field_ptr = staticmethod(
159 native_bt.event_borrow_specific_context_field
160 )
161 _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field)
162 _create_field_from_ptr = staticmethod(bt2_field._create_field_from_ptr)
163
164 _event_class_pycls = property(lambda _: bt2_event_class._EventClass)
165 _packet_pycls = property(lambda _: bt2_packet._Packet)
166 _stream_pycls = property(lambda _: bt2_stream._Stream)
This page took 0.031731 seconds and 4 git commands to generate.