bt2: Add bindings for trace classes
[babeltrace.git] / bindings / python / bt2 / bt2 / stream_class.py
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
23 from bt2 import native_bt, object, utils
24 import bt2.field_class
25 import collections.abc
26 import bt2.ctf_writer
27 import bt2.stream
28 import copy
29 import bt2
30
31
32 class _EventClassIterator(collections.abc.Iterator):
33 def __init__(self, stream_class):
34 self._stream_class = stream_class
35 self._at = 0
36
37 def __next__(self):
38 if self._at == len(self._stream_class):
39 raise StopIteration
40
41 ec_ptr = native_bt.stream_class_get_event_class_by_index(self._stream_class._ptr,
42 self._at)
43 assert(ec_ptr)
44 ev_id = native_bt.event_class_get_id(ec_ptr)
45 native_bt.put(ec_ptr)
46 utils._handle_ret(ev_id, "cannot get event class object's ID")
47 self._at += 1
48 return ev_id
49
50
51 class StreamClass(object._SharedObject, collections.abc.Mapping):
52 _get_ref = staticmethod(native_bt.stream_class_get_ref)
53 _put_ref = staticmethod(native_bt.stream_class_put_ref)
54
55 def __init__(self, name=None, id=None, packet_context_field_class=None,
56 event_header_field_class=None, event_context_field_class=None,
57 event_classes=None):
58 ptr = native_bt.stream_class_create_empty(None)
59
60 if ptr is None:
61 raise bt2.CreationError('cannot create stream class object')
62
63 super().__init__(ptr)
64
65 if name is not None:
66 self.name = name
67
68 if id is not None:
69 self.id = id
70
71 if packet_context_field_class is not None:
72 self.packet_context_field_class = packet_context_field_class
73
74 if event_header_field_class is not None:
75 self.event_header_field_class = event_header_field_class
76
77 if event_context_field_class is not None:
78 self.event_context_field_class = event_context_field_class
79
80 if event_classes is not None:
81 for event_class in event_classes:
82 self.add_event_class(event_class)
83
84 def __getitem__(self, key):
85 utils._check_int64(key)
86 ec_ptr = native_bt.stream_class_get_event_class_by_id(self._ptr,
87 key)
88
89 if ec_ptr is None:
90 raise KeyError(key)
91
92 return bt2.EventClass._create_from_ptr(ec_ptr)
93
94 def __len__(self):
95 count = native_bt.stream_class_get_event_class_count(self._ptr)
96 assert(count >= 0)
97 return count
98
99 def __iter__(self):
100 return _EventClassIterator(self)
101
102 def add_event_class(self, event_class):
103 utils._check_type(event_class, bt2.EventClass)
104 ret = native_bt.stream_class_add_event_class(self._ptr, event_class._ptr)
105 utils._handle_ret(ret, "cannot add event class object to stream class object's")
106
107 @property
108 def trace(self):
109 tc_ptr = native_bt.stream_class_get_trace(self._ptr)
110
111 if tc_ptr is not None:
112 return bt2.Trace._create_from_ptr(tc_ptr)
113
114 @property
115 def name(self):
116 return native_bt.stream_class_get_name(self._ptr)
117
118 @name.setter
119 def name(self, name):
120 utils._check_str(name)
121 ret = native_bt.stream_class_set_name(self._ptr, name)
122 utils._handle_ret(ret, "cannot set stream class object's name")
123
124 @property
125 def id(self):
126 id = native_bt.stream_class_get_id(self._ptr)
127
128 if id < 0:
129 return
130
131 return id
132
133 @id.setter
134 def id(self, id):
135 utils._check_int64(id)
136 ret = native_bt.stream_class_set_id(self._ptr, id)
137 utils._handle_ret(ret, "cannot set stream class object's ID")
138
139 @property
140 def clock(self):
141 clock_ptr = native_bt.stream_class_get_clock(self._ptr)
142
143 if clock_ptr is None:
144 return
145
146 return bt2.ctf_writer.CtfWriterClock._create_from_ptr(clock_ptr)
147
148 @clock.setter
149 def clock(self, clock):
150 utils._check_type(clock, bt2.ctf_writer.CtfWriterClock)
151 ret = native_bt.stream_class_set_clock(self._ptr, clock._ptr)
152 utils._handle_ret(ret, "cannot set stream class object's CTF writer clock object")
153
154 @property
155 def packet_context_field_class(self):
156 fc_ptr = native_bt.stream_class_get_packet_context_type(self._ptr)
157
158 if fc_ptr is None:
159 return
160
161 return bt2.field_class._create_from_ptr(fc_ptr)
162
163 @packet_context_field_class.setter
164 def packet_context_field_class(self, packet_context_field_class):
165 packet_context_field_class_ptr = None
166
167 if packet_context_field_class is not None:
168 utils._check_type(packet_context_field_class, bt2.field_class._FieldClass)
169 packet_context_field_class_ptr = packet_context_field_class._ptr
170
171 ret = native_bt.stream_class_set_packet_context_type(self._ptr,
172 packet_context_field_class_ptr)
173 utils._handle_ret(ret, "cannot set stream class object's packet context field class")
174
175 @property
176 def event_header_field_class(self):
177 fc_ptr = native_bt.stream_class_get_event_header_type(self._ptr)
178
179 if fc_ptr is None:
180 return
181
182 return bt2.field_class._create_from_ptr(fc_ptr)
183
184 @event_header_field_class.setter
185 def event_header_field_class(self, event_header_field_class):
186 event_header_field_class_ptr = None
187
188 if event_header_field_class is not None:
189 utils._check_type(event_header_field_class, bt2.field_class._FieldClass)
190 event_header_field_class_ptr = event_header_field_class._ptr
191
192 ret = native_bt.stream_class_set_event_header_type(self._ptr,
193 event_header_field_class_ptr)
194 utils._handle_ret(ret, "cannot set stream class object's event header field class")
195
196 @property
197 def event_context_field_class(self):
198 fc_ptr = native_bt.stream_class_get_event_context_type(self._ptr)
199
200 if fc_ptr is None:
201 return
202
203 return bt2.field_class._create_from_ptr(fc_ptr)
204
205 @event_context_field_class.setter
206 def event_context_field_class(self, event_context_field_class):
207 event_context_field_class_ptr = None
208
209 if event_context_field_class is not None:
210 utils._check_type(event_context_field_class, bt2.field_class._FieldClass)
211 event_context_field_class_ptr = event_context_field_class._ptr
212
213 ret = native_bt.stream_class_set_event_context_type(self._ptr,
214 event_context_field_class_ptr)
215 utils._handle_ret(ret, "cannot set stream class object's event context field class")
216
217 def __call__(self, name=None, id=None):
218 if name is not None:
219 utils._check_str(name)
220
221 if id is None:
222 stream_ptr = native_bt.stream_create(self._ptr, name)
223 else:
224 stream_ptr = native_bt.stream_create_with_id(self._ptr, name, id)
225
226 if stream_ptr is None:
227 raise bt2.CreationError('cannot create stream object')
228
229 return bt2.stream._create_from_ptr(stream_ptr)
230
231 def __eq__(self, other):
232 if type(other) is not type(self):
233 return False
234
235 if self.addr == other.addr:
236 return True
237
238 self_event_classes = list(self.values())
239 other_event_classes = list(other.values())
240 self_props = (
241 self_event_classes,
242 self.name,
243 self.id,
244 self.packet_context_field_class,
245 self.event_header_field_class,
246 self.event_context_field_class,
247 self.clock,
248 )
249 other_props = (
250 other_event_classes,
251 other.name,
252 other.id,
253 other.packet_context_field_class,
254 other.event_header_field_class,
255 other.event_context_field_class,
256 other.clock,
257 )
258
259 return self_props == other_props
260
261 def _copy(self, fc_copy_func, ev_copy_func):
262 cpy = StreamClass()
263
264 if self.id is not None:
265 cpy.id = self.id
266
267 if self.name is not None:
268 cpy.name = self.name
269
270 if self.clock is not None:
271 cpy.clock = self.clock
272
273 cpy.packet_context_field_class = fc_copy_func(self.packet_context_field_class)
274 cpy.event_header_field_class = fc_copy_func(self.event_header_field_class)
275 cpy.event_context_field_class = fc_copy_func(self.event_context_field_class)
276
277 for event_class in self.values():
278 cpy.add_event_class(ev_copy_func(event_class))
279
280 return cpy
281
282 def __copy__(self):
283 return self._copy(lambda fc: fc, copy.copy)
284
285 def __deepcopy__(self, memo):
286 cpy = self._copy(copy.deepcopy, copy.deepcopy)
287 memo[id(self)] = cpy
288 return cpy
This page took 0.037927 seconds and 4 git commands to generate.