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 | |
24 | import bt2.clock_class | |
78668ecd | 25 | import bt2.event_class |
81447b5b PP |
26 | import bt2.packet |
27 | import bt2.stream | |
ae0bfae8 | 28 | import bt2.field |
f192fc47 | 29 | import bt2.clock_snapshot |
81447b5b PP |
30 | import bt2 |
31 | ||
32 | ||
c3044a97 | 33 | class _Event(object._UniqueObject): |
81447b5b | 34 | @property |
c88be1c8 | 35 | def cls(self): |
27d97a3f SM |
36 | event_class_ptr = native_bt.event_borrow_class(self._ptr) |
37 | assert event_class_ptr is not None | |
78668ecd | 38 | return bt2.event_class._EventClass._create_from_ptr_and_get_ref(event_class_ptr) |
81447b5b PP |
39 | |
40 | @property | |
41 | def name(self): | |
c88be1c8 | 42 | return self.cls.name |
81447b5b PP |
43 | |
44 | @property | |
45 | def id(self): | |
c88be1c8 | 46 | return self.cls.id |
81447b5b PP |
47 | |
48 | @property | |
49 | def packet(self): | |
27d97a3f | 50 | packet_ptr = native_bt.event_borrow_packet(self._ptr) |
37a93d41 PP |
51 | |
52 | if packet_ptr is None: | |
53 | return | |
54 | ||
27d97a3f | 55 | return bt2.packet._Packet._create_from_ptr_and_get_ref(packet_ptr) |
81447b5b PP |
56 | |
57 | @property | |
58 | def stream(self): | |
27d97a3f SM |
59 | stream_ptr = native_bt.event_borrow_stream(self._ptr) |
60 | assert stream_ptr is not None | |
d016011d | 61 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
62 | |
63 | @property | |
27d97a3f SM |
64 | def common_context_field(self): |
65 | field_ptr = native_bt.event_borrow_common_context_field(self._ptr) | |
81447b5b PP |
66 | |
67 | if field_ptr is None: | |
68 | return | |
69 | ||
61d96b89 FD |
70 | return bt2.field._create_field_from_ptr( |
71 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref | |
72 | ) | |
81447b5b PP |
73 | |
74 | @property | |
27d97a3f SM |
75 | def specific_context_field(self): |
76 | field_ptr = native_bt.event_borrow_specific_context_field(self._ptr) | |
81447b5b PP |
77 | |
78 | if field_ptr is None: | |
79 | return | |
80 | ||
61d96b89 FD |
81 | return bt2.field._create_field_from_ptr( |
82 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref | |
83 | ) | |
81447b5b PP |
84 | |
85 | @property | |
86 | def payload_field(self): | |
27d97a3f | 87 | field_ptr = native_bt.event_borrow_payload_field(self._ptr) |
81447b5b PP |
88 | |
89 | if field_ptr is None: | |
90 | return | |
91 | ||
61d96b89 FD |
92 | return bt2.field._create_field_from_ptr( |
93 | field_ptr, self._owner_ptr, self._owner_get_ref, self._owner_put_ref | |
94 | ) | |
81447b5b PP |
95 | |
96 | def __getitem__(self, key): | |
97 | utils._check_str(key) | |
98 | payload_field = self.payload_field | |
99 | ||
100 | if payload_field is not None and key in payload_field: | |
101 | return payload_field[key] | |
102 | ||
27d97a3f | 103 | specific_context_field = self.specific_context_field |
81447b5b | 104 | |
27d97a3f SM |
105 | if specific_context_field is not None and key in specific_context_field: |
106 | return specific_context_field[key] | |
81447b5b | 107 | |
27d97a3f | 108 | common_context_field = self.common_context_field |
81447b5b | 109 | |
27d97a3f SM |
110 | if common_context_field is not None and key in common_context_field: |
111 | return common_context_field[key] | |
81447b5b | 112 | |
27d97a3f | 113 | packet_context_field = self.packet.context_field |
81447b5b | 114 | |
27d97a3f SM |
115 | if packet_context_field is not None and key in packet_context_field: |
116 | return packet_context_field[key] | |
81447b5b PP |
117 | |
118 | raise KeyError(key) |