bt2: _EventConst.__getitem__(): use a single temporary variable
[babeltrace.git] / src / bindings / python / bt2 / bt2 / event.py
CommitLineData
81447b5b
PP
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
23from bt2 import native_bt, object, utils
c946c9de
PP
24from bt2 import event_class as bt2_event_class
25from bt2 import packet as bt2_packet
26from bt2 import stream as bt2_stream
27from bt2 import field as bt2_field
81447b5b
PP
28
29
9cbe0c59
FD
30class _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
81447b5b 47 @property
c88be1c8 48 def cls(self):
9cbe0c59 49 event_class_ptr = self._borrow_class_ptr(self._ptr)
27d97a3f 50 assert event_class_ptr is not None
9cbe0c59 51 return self._event_class_pycls._create_from_ptr_and_get_ref(event_class_ptr)
81447b5b
PP
52
53 @property
54 def name(self):
c88be1c8 55 return self.cls.name
81447b5b
PP
56
57 @property
58 def id(self):
c88be1c8 59 return self.cls.id
81447b5b
PP
60
61 @property
62 def packet(self):
9cbe0c59 63 packet_ptr = self._borrow_packet_ptr(self._ptr)
37a93d41
PP
64
65 if packet_ptr is None:
66 return
67
9cbe0c59 68 return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr)
81447b5b
PP
69
70 @property
71 def stream(self):
9cbe0c59 72 stream_ptr = self._borrow_stream_ptr(self._ptr)
27d97a3f 73 assert stream_ptr is not None
9cbe0c59 74 return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr)
81447b5b
PP
75
76 @property
27d97a3f 77 def common_context_field(self):
9cbe0c59 78 field_ptr = self._borrow_common_context_field_ptr(self._ptr)
81447b5b
PP
79
80 if field_ptr is None:
81 return
82
9cbe0c59 83 return self._create_field_from_ptr(
61d96b89
FD
84 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
85 )
81447b5b
PP
86
87 @property
27d97a3f 88 def specific_context_field(self):
9cbe0c59 89 field_ptr = self._borrow_specific_context_field_ptr(self._ptr)
81447b5b
PP
90
91 if field_ptr is None:
92 return
93
9cbe0c59 94 return self._create_field_from_ptr(
61d96b89
FD
95 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
96 )
81447b5b
PP
97
98 @property
99 def payload_field(self):
9cbe0c59 100 field_ptr = self._borrow_payload_field_ptr(self._ptr)
81447b5b
PP
101
102 if field_ptr is None:
103 return
104
9cbe0c59 105 return self._create_field_from_ptr(
61d96b89
FD
106 field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref
107 )
81447b5b
PP
108
109 def __getitem__(self, key):
110 utils._check_str(key)
57eb9829 111 root_field = self.payload_field
81447b5b 112
57eb9829
PP
113 if root_field is not None and key in root_field:
114 return root_field[key]
81447b5b 115
57eb9829 116 root_field = self.specific_context_field
81447b5b 117
57eb9829
PP
118 if root_field is not None and key in root_field:
119 return root_field[key]
81447b5b 120
57eb9829 121 root_field = self.common_context_field
81447b5b 122
57eb9829
PP
123 if root_field is not None and key in root_field:
124 return root_field[key]
81447b5b 125
37b9f25d 126 if self.packet:
57eb9829 127 root_field = self.packet.context_field
81447b5b 128
57eb9829
PP
129 if root_field is not None and key in root_field:
130 return root_field[key]
81447b5b
PP
131
132 raise KeyError(key)
9cbe0c59
FD
133
134
135class _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.065407 seconds and 4 git commands to generate.