Commit | Line | Data |
---|---|---|
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 | ||
23 | from bt2 import native_bt, object, utils | |
3fb99a22 PP |
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 | |
81447b5b PP |
28 | |
29 | ||
f0a42b33 FD |
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 | ||
81447b5b | 47 | @property |
e8ac1aae | 48 | def cls(self): |
f0a42b33 | 49 | event_class_ptr = self._borrow_class_ptr(self._ptr) |
2ae9f48c | 50 | assert event_class_ptr is not None |
f0a42b33 | 51 | return self._event_class_pycls._create_from_ptr_and_get_ref(event_class_ptr) |
81447b5b PP |
52 | |
53 | @property | |
54 | def name(self): | |
e8ac1aae | 55 | return self.cls.name |
81447b5b PP |
56 | |
57 | @property | |
58 | def id(self): | |
e8ac1aae | 59 | return self.cls.id |
81447b5b PP |
60 | |
61 | @property | |
62 | def packet(self): | |
f0a42b33 | 63 | packet_ptr = self._borrow_packet_ptr(self._ptr) |
26fc5aed PP |
64 | |
65 | if packet_ptr is None: | |
66 | return | |
67 | ||
f0a42b33 | 68 | return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr) |
81447b5b PP |
69 | |
70 | @property | |
71 | def stream(self): | |
f0a42b33 | 72 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
2ae9f48c | 73 | assert stream_ptr is not None |
f0a42b33 | 74 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
75 | |
76 | @property | |
2ae9f48c | 77 | def common_context_field(self): |
f0a42b33 | 78 | field_ptr = self._borrow_common_context_field_ptr(self._ptr) |
81447b5b PP |
79 | |
80 | if field_ptr is None: | |
81 | return | |
82 | ||
f0a42b33 | 83 | return self._create_field_from_ptr( |
cfbd7cf3 FD |
84 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref |
85 | ) | |
81447b5b PP |
86 | |
87 | @property | |
2ae9f48c | 88 | def specific_context_field(self): |
f0a42b33 | 89 | field_ptr = self._borrow_specific_context_field_ptr(self._ptr) |
81447b5b PP |
90 | |
91 | if field_ptr is None: | |
92 | return | |
93 | ||
f0a42b33 | 94 | return self._create_field_from_ptr( |
cfbd7cf3 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): | |
f0a42b33 | 100 | field_ptr = self._borrow_payload_field_ptr(self._ptr) |
81447b5b PP |
101 | |
102 | if field_ptr is None: | |
103 | return | |
104 | ||
f0a42b33 | 105 | return self._create_field_from_ptr( |
cfbd7cf3 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) | |
111 | payload_field = self.payload_field | |
112 | ||
113 | if payload_field is not None and key in payload_field: | |
114 | return payload_field[key] | |
115 | ||
2ae9f48c | 116 | specific_context_field = self.specific_context_field |
81447b5b | 117 | |
2ae9f48c SM |
118 | if specific_context_field is not None and key in specific_context_field: |
119 | return specific_context_field[key] | |
81447b5b | 120 | |
2ae9f48c | 121 | common_context_field = self.common_context_field |
81447b5b | 122 | |
2ae9f48c SM |
123 | if common_context_field is not None and key in common_context_field: |
124 | return common_context_field[key] | |
81447b5b | 125 | |
2ae9f48c | 126 | packet_context_field = self.packet.context_field |
81447b5b | 127 | |
2ae9f48c SM |
128 | if packet_context_field is not None and key in packet_context_field: |
129 | return packet_context_field[key] | |
81447b5b PP |
130 | |
131 | raise KeyError(key) | |
f0a42b33 FD |
132 | |
133 | ||
134 | class _Event(_EventConst): | |
135 | _borrow_class_ptr = staticmethod(native_bt.event_borrow_class) | |
136 | _borrow_packet_ptr = staticmethod(native_bt.event_borrow_packet) | |
137 | _borrow_stream_ptr = staticmethod(native_bt.event_borrow_stream) | |
138 | _borrow_common_context_field_ptr = staticmethod( | |
139 | native_bt.event_borrow_common_context_field | |
140 | ) | |
141 | _borrow_specific_context_field_ptr = staticmethod( | |
142 | native_bt.event_borrow_specific_context_field | |
143 | ) | |
144 | _borrow_payload_field_ptr = staticmethod(native_bt.event_borrow_payload_field) | |
145 | _create_field_from_ptr = staticmethod(bt2_field._create_field_from_ptr) | |
146 | ||
147 | _event_class_pycls = property(lambda _: bt2_event_class._EventClass) | |
148 | _packet_pycls = property(lambda _: bt2_packet._Packet) | |
149 | _stream_pycls = property(lambda _: bt2_stream._Stream) |