1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 from bt2
import field_class
as bt2_field_class
25 from bt2
import event_class
as bt2_event_class
26 from bt2
import clock_class
as bt2_clock_class
27 from bt2
import value
as bt2_value
28 import collections
.abc
31 def _bt2_trace_class():
32 from bt2
import trace_class
as bt2_trace_class
34 return bt2_trace_class
37 class _StreamClassConst(object._SharedObject
, collections
.abc
.Mapping
):
38 _get_ref
= staticmethod(native_bt
.stream_class_get_ref
)
39 _put_ref
= staticmethod(native_bt
.stream_class_put_ref
)
40 _borrow_event_class_ptr_by_id
= staticmethod(
41 native_bt
.stream_class_borrow_event_class_by_id_const
43 _borrow_event_class_ptr_by_index
= staticmethod(
44 native_bt
.stream_class_borrow_event_class_by_index_const
46 _borrow_trace_class_ptr
= staticmethod(
47 native_bt
.stream_class_borrow_trace_class_const
49 _borrow_packet_context_field_class_ptr
= staticmethod(
50 native_bt
.stream_class_borrow_packet_context_field_class_const
52 _borrow_event_common_context_field_class_ptr
= staticmethod(
53 native_bt
.stream_class_borrow_event_common_context_field_class_const
55 _borrow_default_clock_class_ptr
= staticmethod(
56 native_bt
.stream_class_borrow_default_clock_class_const
58 _borrow_user_attributes_ptr
= staticmethod(
59 native_bt
.stream_class_borrow_user_attributes_const
62 _event_class_cls
= property(lambda _
: bt2_event_class
._EventClassConst
)
63 _trace_class_cls
= property(lambda _
: _bt2_trace_class()._TraceClassConst
)
64 _clock_class_cls
= property(lambda _
: bt2_clock_class
._ClockClassConst
)
66 def __getitem__(self
, key
):
67 utils
._check
_int
64(key
)
68 ec_ptr
= self
._borrow
_event
_class
_ptr
_by
_id
(self
._ptr
, key
)
73 return self
._event
_class
_cls
._create
_from
_ptr
_and
_get
_ref
(ec_ptr
)
76 count
= native_bt
.stream_class_get_event_class_count(self
._ptr
)
81 for idx
in range(len(self
)):
82 ec_ptr
= self
._borrow
_event
_class
_ptr
_by
_index
(self
._ptr
, idx
)
83 assert ec_ptr
is not None
85 id = native_bt
.event_class_get_id(ec_ptr
)
91 def trace_class(self
):
92 tc_ptr
= self
._borrow
_trace
_class
_ptr
(self
._ptr
)
94 if tc_ptr
is not None:
95 return self
._trace
_class
_cls
._create
_from
_ptr
_and
_get
_ref
(tc_ptr
)
98 def user_attributes(self
):
99 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
100 assert ptr
is not None
101 return bt2_value
._create
_from
_ptr
_and
_get
_ref
(ptr
)
105 return native_bt
.stream_class_get_name(self
._ptr
)
108 def assigns_automatic_event_class_id(self
):
109 return native_bt
.stream_class_assigns_automatic_event_class_id(self
._ptr
)
112 def assigns_automatic_stream_id(self
):
113 return native_bt
.stream_class_assigns_automatic_stream_id(self
._ptr
)
116 def supports_packets(self
):
117 return native_bt
.stream_class_supports_packets(self
._ptr
)
120 def packets_have_beginning_default_clock_snapshot(self
):
121 return native_bt
.stream_class_packets_have_beginning_default_clock_snapshot(
126 def packets_have_end_default_clock_snapshot(self
):
127 return native_bt
.stream_class_packets_have_end_default_clock_snapshot(self
._ptr
)
130 def supports_discarded_events(self
):
131 return native_bt
.stream_class_supports_discarded_events(self
._ptr
)
134 def discarded_events_have_default_clock_snapshots(self
):
135 return native_bt
.stream_class_discarded_events_have_default_clock_snapshots(
140 def supports_discarded_packets(self
):
141 return native_bt
.stream_class_supports_discarded_packets(self
._ptr
)
144 def discarded_packets_have_default_clock_snapshots(self
):
145 return native_bt
.stream_class_discarded_packets_have_default_clock_snapshots(
151 id = native_bt
.stream_class_get_id(self
._ptr
)
159 def packet_context_field_class(self
):
160 fc_ptr
= self
._borrow
_packet
_context
_field
_class
_ptr
(self
._ptr
)
165 return bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
168 def event_common_context_field_class(self
):
169 fc_ptr
= self
._borrow
_event
_common
_context
_field
_class
_ptr
(self
._ptr
)
174 return bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
177 def default_clock_class(self
):
178 cc_ptr
= self
._borrow
_default
_clock
_class
_ptr
(self
._ptr
)
182 return self
._clock
_class
_cls
._create
_from
_ptr
_and
_get
_ref
(cc_ptr
)
185 class _StreamClass(_StreamClassConst
):
186 _get_ref
= staticmethod(native_bt
.stream_class_get_ref
)
187 _put_ref
= staticmethod(native_bt
.stream_class_put_ref
)
188 _borrow_event_class_ptr_by_id
= staticmethod(
189 native_bt
.stream_class_borrow_event_class_by_id
191 _borrow_event_class_ptr_by_index
= staticmethod(
192 native_bt
.stream_class_borrow_event_class_by_index
194 _borrow_trace_class_ptr
= staticmethod(native_bt
.stream_class_borrow_trace_class
)
195 _borrow_packet_context_field_class_ptr
= staticmethod(
196 native_bt
.stream_class_borrow_packet_context_field_class
198 _borrow_event_common_context_field_class_ptr
= staticmethod(
199 native_bt
.stream_class_borrow_event_common_context_field_class
201 _borrow_default_clock_class_ptr
= staticmethod(
202 native_bt
.stream_class_borrow_default_clock_class
204 _borrow_user_attributes_ptr
= staticmethod(
205 native_bt
.stream_class_borrow_user_attributes
208 _event_class_cls
= property(lambda s
: bt2_event_class
._EventClass
)
209 _trace_class_cls
= property(lambda s
: _bt2_trace_class()._TraceClass
)
210 _clock_class_cls
= property(lambda s
: bt2_clock_class
._ClockClass
)
212 def create_event_class(
216 user_attributes
=None,
219 specific_context_field_class
=None,
220 payload_field_class
=None,
222 if self
.assigns_automatic_event_class_id
:
225 'id provided, but stream class assigns automatic event class ids'
228 ec_ptr
= native_bt
.event_class_create(self
._ptr
)
232 'id not provided, but stream class does not assign automatic event class ids'
235 utils
._check
_uint
64(id)
236 ec_ptr
= native_bt
.event_class_create_with_id(self
._ptr
, id)
238 event_class
= bt2_event_class
._EventClass
._create
_from
_ptr
(ec_ptr
)
241 event_class
._name
= name
243 if user_attributes
is not None:
244 event_class
._user
_attributes
= user_attributes
246 if log_level
is not None:
247 event_class
._log
_level
= log_level
249 if emf_uri
is not None:
250 event_class
._emf
_uri
= emf_uri
252 if specific_context_field_class
is not None:
253 event_class
._specific
_context
_field
_class
= specific_context_field_class
255 if payload_field_class
is not None:
256 event_class
._payload
_field
_class
= payload_field_class
260 def _user_attributes(self
, user_attributes
):
261 value
= bt2_value
.create_value(user_attributes
)
262 native_bt
.stream_class_set_user_attributes(self
._ptr
, value
._ptr
)
264 _user_attributes
= property(fset
=_user_attributes
)
266 def _name(self
, name
):
267 status
= native_bt
.stream_class_set_name(self
._ptr
, name
)
268 utils
._handle
_func
_status
(status
, "cannot set stream class object's name")
270 _name
= property(fset
=_name
)
272 def _assigns_automatic_event_class_id(self
, auto_id
):
273 native_bt
.stream_class_set_assigns_automatic_event_class_id(self
._ptr
, auto_id
)
275 _assigns_automatic_event_class_id
= property(fset
=_assigns_automatic_event_class_id
)
277 def _assigns_automatic_stream_id(self
, auto_id
):
278 native_bt
.stream_class_set_assigns_automatic_stream_id(self
._ptr
, auto_id
)
280 _assigns_automatic_stream_id
= property(fset
=_assigns_automatic_stream_id
)
282 def _set_supports_packets(self
, supports
, with_begin_cs
=False, with_end_cs
=False):
283 native_bt
.stream_class_set_supports_packets(
284 self
._ptr
, supports
, with_begin_cs
, with_end_cs
287 def _set_supports_discarded_events(self
, supports
, with_cs
=False):
288 native_bt
.stream_class_set_supports_discarded_events(
289 self
._ptr
, supports
, with_cs
292 _supports_discarded_events
= property(fset
=_set_supports_discarded_events
)
294 def _set_supports_discarded_packets(self
, supports
, with_cs
):
295 native_bt
.stream_class_set_supports_discarded_packets(
296 self
._ptr
, supports
, with_cs
299 _supports_discarded_packets
= property(fset
=_set_supports_discarded_packets
)
301 def _packet_context_field_class(self
, packet_context_field_class
):
302 status
= native_bt
.stream_class_set_packet_context_field_class(
303 self
._ptr
, packet_context_field_class
._ptr
305 utils
._handle
_func
_status
(
306 status
, "cannot set stream class' packet context field class"
309 _packet_context_field_class
= property(fset
=_packet_context_field_class
)
311 def _event_common_context_field_class(self
, event_common_context_field_class
):
312 set_context_fn
= native_bt
.stream_class_set_event_common_context_field_class
313 status
= set_context_fn(self
._ptr
, event_common_context_field_class
._ptr
)
314 utils
._handle
_func
_status
(
315 status
, "cannot set stream class' event context field type"
318 _event_common_context_field_class
= property(fset
=_event_common_context_field_class
)
320 def _default_clock_class(self
, clock_class
):
321 native_bt
.stream_class_set_default_clock_class(self
._ptr
, clock_class
._ptr
)
323 _default_clock_class
= property(fset
=_default_clock_class
)
326 def _validate_create_params(
330 packet_context_field_class
,
331 event_common_context_field_class
,
333 assigns_automatic_event_class_id
,
334 assigns_automatic_stream_id
,
336 packets_have_beginning_default_clock_snapshot
,
337 packets_have_end_default_clock_snapshot
,
338 supports_discarded_events
,
339 discarded_events_have_default_clock_snapshots
,
340 supports_discarded_packets
,
341 discarded_packets_have_default_clock_snapshots
,
345 utils
._check
_str
(name
)
348 if user_attributes
is not None:
349 value
= bt2_value
.create_value(user_attributes
)
350 utils
._check
_type
(value
, bt2_value
.MapValue
)
352 # Packet context field class
353 if packet_context_field_class
is not None:
354 if not supports_packets
:
356 'cannot have a packet context field class without supporting packets'
360 packet_context_field_class
, bt2_field_class
._StructureFieldClass
363 # Event common context field class
364 if event_common_context_field_class
is not None:
366 event_common_context_field_class
, bt2_field_class
._StructureFieldClass
369 # Default clock class
370 if default_clock_class
is not None:
371 utils
._check
_type
(default_clock_class
, bt2_clock_class
._ClockClass
)
373 # Assigns automatic event class id
374 utils
._check
_bool
(assigns_automatic_event_class_id
)
376 # Assigns automatic stream id
377 utils
._check
_bool
(assigns_automatic_stream_id
)
380 utils
._check
_bool
(supports_packets
)
381 utils
._check
_bool
(packets_have_beginning_default_clock_snapshot
)
382 utils
._check
_bool
(packets_have_end_default_clock_snapshot
)
384 if not supports_packets
:
385 if packets_have_beginning_default_clock_snapshot
:
387 'cannot not support packets, but have packet beginning default clock snapshot'
389 if packets_have_end_default_clock_snapshot
:
391 'cannot not support packets, but have packet end default clock snapshots'
395 utils
._check
_bool
(supports_discarded_events
)
396 utils
._check
_bool
(discarded_events_have_default_clock_snapshots
)
399 not supports_discarded_events
400 and discarded_events_have_default_clock_snapshots
403 'cannot not support discarded events, but have default clock snapshots for discarded event messages'
407 utils
._check
_bool
(supports_discarded_packets
)
408 utils
._check
_bool
(discarded_packets_have_default_clock_snapshots
)
410 if supports_discarded_packets
and not supports_packets
:
412 'cannot support discarded packets, but not support packets'
416 not supports_discarded_packets
417 and discarded_packets_have_default_clock_snapshots
420 'cannot not support discarded packets, but have default clock snapshots for discarded packet messages'