Add Babeltrace 2 Python bindings
[babeltrace.git] / bindings / python / bt2 / notification.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
3# Copyright (c) 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
24import bt2.packet
25import bt2.stream
26import bt2.event
27import bt2
28
29
30def _create_from_ptr(ptr):
31 notif_type = native_bt.notification_get_type(ptr)
32 cls = None
33
34 if notif_type not in _NOTIF_TYPE_TO_CLS:
35 raise bt2.Error('unknown notification type: {}'.format(notif_type))
36
37 return _NOTIF_TYPE_TO_CLS[notif_type]._create_from_ptr(ptr)
38
39
40class _Notification(object._Object):
41 pass
42
43
44class TraceEventNotification(_Notification):
45 def __init__(self, event):
46 utils._check_type(event, bt2.event._Event)
47 ptr = native_bt.notification_event_create(event._ptr)
48
49 if ptr is None:
50 raise bt2.CreationError('cannot create trace event notification object')
51
52 super().__init__(ptr)
53
54 @property
55 def event(self):
56 event_ptr = native_bt.notification_event_get_event(self._ptr)
57 utils._handle_ptr(event_ptr, "cannot get trace event notification object's event object")
58 return bt2.event._create_from_ptr(event_ptr)
59
60
61class BeginningOfPacketNotification(_Notification):
62 def __init__(self, packet):
63 utils._check_type(packet, bt2.packet._Packet)
64 ptr = native_bt.notification_packet_begin_create(packet._ptr)
65
66 if ptr is None:
67 raise bt2.CreationError('cannot create beginning of packet notification object')
68
69 super().__init__(ptr)
70
71 @property
72 def packet(self):
73 packet_ptr = native_bt.notification_packet_begin_get_packet(self._ptr)
74 utils._handle_ptr(packet_ptr, "cannot get beginning of packet notification object's packet object")
75 return bt2.packet._Packet._create_from_ptr(packet_ptr)
76
77
78class EndOfPacketNotification(_Notification):
79 def __init__(self, packet):
80 utils._check_type(packet, bt2.packet._Packet)
81 ptr = native_bt.notification_packet_end_create(packet._ptr)
82
83 if ptr is None:
84 raise bt2.CreationError('cannot create end of packet notification object')
85
86 super().__init__(ptr)
87
88 @property
89 def packet(self):
90 packet_ptr = native_bt.notification_packet_end_get_packet(self._ptr)
91 utils._handle_ptr(packet_ptr, "cannot get end of packet notification object's packet object")
92 return bt2.packet._Packet._create_from_ptr(packet_ptr)
93
94
95class EndOfStreamNotification(_Notification):
96 def __init__(self, stream):
97 utils._check_type(stream, bt2.stream._Stream)
98 ptr = native_bt.notification_stream_end_create(stream._ptr)
99
100 if ptr is None:
101 raise bt2.CreationError('cannot create end of stream notification object')
102
103 super().__init__(ptr)
104
105 @property
106 def stream(self):
107 stream_ptr = native_bt.notification_stream_end_get_stream(self._ptr)
108 utils._handle_ptr(stream_ptr, "cannot get end of stream notification object's stream object")
109 return bt2.stream._create_from_ptr(stream_ptr)
110
111
112class NewTraceNotification(_Notification):
113 def __init__(self, trace):
114 utils._check_type(trace, bt2.Trace)
115 ptr = native_bt.notification_new_trace_create(trace._ptr)
116
117 if ptr is None:
118 raise bt2.CreationError('cannot create new trace notification object')
119
120 super().__init__(ptr)
121
122 @property
123 def trace(self):
124 trace_ptr = native_bt.notification_new_trace_get_trace(self._ptr)
125 utils._handle_ptr(trace_ptr, "cannot get new trace notification object's trace object")
126 return bt2.trace._create_from_ptr(trace_ptr)
127
128
129class NewStreamClassNotification(_Notification):
130 def __init__(self, stream_class):
131 utils._check_type(stream_class, bt2.StreamClass)
132 ptr = native_bt.notification_new_stream_class_create(stream_class._ptr)
133
134 if ptr is None:
135 raise bt2.CreationError('cannot create new stream class notification object')
136
137 super().__init__(ptr)
138
139 @property
140 def stream_class(self):
141 stream_class_ptr = native_bt.notification_new_stream_class_get_stream_class(self._ptr)
142 utils._handle_ptr(stream_class_ptr, "cannot get new stream class notification object's stream class object")
143 return bt2.stream_class.StreamClass._create_from_ptr(stream_class_ptr)
144
145
146_NOTIF_TYPE_TO_CLS = {
147 native_bt.NOTIFICATION_TYPE_EVENT: TraceEventNotification,
148 native_bt.NOTIFICATION_TYPE_PACKET_BEGIN: BeginningOfPacketNotification,
149 native_bt.NOTIFICATION_TYPE_PACKET_END: EndOfPacketNotification,
150 native_bt.NOTIFICATION_TYPE_STREAM_END: EndOfStreamNotification,
151 native_bt.NOTIFICATION_TYPE_NEW_TRACE: NewTraceNotification,
152 native_bt.NOTIFICATION_TYPE_NEW_STREAM_CLASS: NewStreamClassNotification,
153}
This page took 0.029828 seconds and 4 git commands to generate.