| 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 | payload_field = self.payload_field |
| 112 | |
| 113 | if payload_field is not None and key in payload_field: |
| 114 | return payload_field[key] |
| 115 | |
| 116 | specific_context_field = self.specific_context_field |
| 117 | |
| 118 | if specific_context_field is not None and key in specific_context_field: |
| 119 | return specific_context_field[key] |
| 120 | |
| 121 | common_context_field = self.common_context_field |
| 122 | |
| 123 | if common_context_field is not None and key in common_context_field: |
| 124 | return common_context_field[key] |
| 125 | |
| 126 | packet_context_field = self.packet.context_field |
| 127 | |
| 128 | if packet_context_field is not None and key in packet_context_field: |
| 129 | return packet_context_field[key] |
| 130 | |
| 131 | raise KeyError(key) |
| 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) |