bt2: _EventConst.__getitem__(): use a single temporary variable
[babeltrace.git] / src / bindings / python / bt2 / bt2 / event.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2016-2017 Philippe Proulx <pproulx@efficios.com>
4 #
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:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
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
21 # THE SOFTWARE.
22
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
29
30 class _EventConst(object._UniqueObject):
31 _borrow_class_ptr = staticmethod(native_bt.event_borrow_class_const)
32 _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet_const)
33 _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream_const)
34 _borrow_common_context_field_ptr = staticmethod(
35 native_bt.event_borrow_common_context_field_const
36 )
37 _borrow_specific_context_field_ptr = staticmethod(
38 native_bt.event_borrow_specific_context_field_const
39 )
40 _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field_const)
41 _create_field_from_ptr = staticmethod(bt2_field._create_field_from_const_ptr)
42
43 _event_class_pycls = property(lambda _: bt2_event_class._EventClassConst)
44 _packet_pycls = property(lambda _: bt2_packet._PacketConst)
45 _stream_pycls = property(lambda _: bt2_stream._StreamConst)
46
47 @property
48 def cls(self):
49 event_class_ptr = self._borrow_class_ptr(self._ptr)
50 assert event_class_ptr is not None
51 return self._event_class_pycls._create_from_ptr_and_get_ref(event_class_ptr)
52
53 @property
54 def name(self):
55 return self.cls.name
56
57 @property
58 def id(self):
59 return self.cls.id
60
61 @property
62 def packet(self):
63 packet_ptr = self._borrow_packet_ptr(self._ptr)
64
65 if packet_ptr is None:
66 return
67
68 return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr)
69
70 @property
71 def stream(self):
72 stream_ptr = self._borrow_stream_ptr(self._ptr)
73 assert stream_ptr is not None
74 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
75
76 @property
77 def common_context_field(self):
78 field_ptr = self._borrow_common_context_field_ptr(self._ptr)
79
80 if field_ptr is None:
81 return
82
83 return self._create_field_from_ptr(
84 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
85 )
86
87 @property
88 def specific_context_field(self):
89 field_ptr = self._borrow_specific_context_field_ptr(self._ptr)
90
91 if field_ptr is None:
92 return
93
94 return self._create_field_from_ptr(
95 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
96 )
97
98 @property
99 def payload_field(self):
100 field_ptr = self._borrow_payload_field_ptr(self._ptr)
101
102 if field_ptr is None:
103 return
104
105 return self._create_field_from_ptr(
106 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
107 )
108
109 def __getitem__(self, key):
110 utils._check_str(key)
111 root_field = self.payload_field
112
113 if root_field is not None and key in root_field:
114 return root_field[key]
115
116 root_field = self.specific_context_field
117
118 if root_field is not None and key in root_field:
119 return root_field[key]
120
121 root_field = self.common_context_field
122
123 if root_field is not None and key in root_field:
124 return root_field[key]
125
126 if self.packet:
127 root_field = self.packet.context_field
128
129 if root_field is not None and key in root_field:
130 return root_field[key]
131
132 raise KeyError(key)
133
134
135 class _Event(_EventConst):
136 _borrow_class_ptr = staticmethod(native_bt.event_borrow_class)
137 _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet)
138 _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream)
139 _borrow_common_context_field_ptr = staticmethod(
140 native_bt.event_borrow_common_context_field
141 )
142 _borrow_specific_context_field_ptr = staticmethod(
143 native_bt.event_borrow_specific_context_field
144 )
145 _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field)
146 _create_field_from_ptr = staticmethod(bt2_field._create_field_from_ptr)
147
148 _event_class_pycls = property(lambda _: bt2_event_class._EventClass)
149 _packet_pycls = property(lambda _: bt2_packet._Packet)
150 _stream_pycls = property(lambda _: bt2_stream._Stream)
This page took 0.032106 seconds and 4 git commands to generate.