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 import bt2
.field_class
25 import collections
.abc
32 class _EventClassIterator(collections
.abc
.Iterator
):
33 def __init__(self
, stream_class
):
34 self
._stream
_class
= stream_class
38 if self
._at
== len(self
._stream
_class
):
41 ec_ptr
= native_bt
.stream_class_get_event_class_by_index(self
._stream
_class
._ptr
,
44 ev_id
= native_bt
.event_class_get_id(ec_ptr
)
46 utils
._handle
_ret
(ev_id
, "cannot get event class object's ID")
51 class StreamClass(object._Object
, collections
.abc
.Mapping
):
52 def __init__(self
, name
=None, id=None, packet_context_field_class
=None,
53 event_header_field_class
=None, event_context_field_class
=None,
55 ptr
= native_bt
.stream_class_create_empty(None)
58 raise bt2
.CreationError('cannot create stream class object')
68 if packet_context_field_class
is not None:
69 self
.packet_context_field_class
= packet_context_field_class
71 if event_header_field_class
is not None:
72 self
.event_header_field_class
= event_header_field_class
74 if event_context_field_class
is not None:
75 self
.event_context_field_class
= event_context_field_class
77 if event_classes
is not None:
78 for event_class
in event_classes
:
79 self
.add_event_class(event_class
)
81 def __getitem__(self
, key
):
82 utils
._check
_int
64(key
)
83 ec_ptr
= native_bt
.stream_class_get_event_class_by_id(self
._ptr
,
89 return bt2
.EventClass
._create
_from
_ptr
(ec_ptr
)
92 count
= native_bt
.stream_class_get_event_class_count(self
._ptr
)
97 return _EventClassIterator(self
)
99 def add_event_class(self
, event_class
):
100 utils
._check
_type
(event_class
, bt2
.EventClass
)
101 ret
= native_bt
.stream_class_add_event_class(self
._ptr
, event_class
._ptr
)
102 utils
._handle
_ret
(ret
, "cannot add event class object to stream class object's")
106 tc_ptr
= native_bt
.stream_class_get_trace(self
._ptr
)
108 if tc_ptr
is not None:
109 return bt2
.Trace
._create
_from
_ptr
(tc_ptr
)
113 return native_bt
.stream_class_get_name(self
._ptr
)
116 def name(self
, name
):
117 utils
._check
_str
(name
)
118 ret
= native_bt
.stream_class_set_name(self
._ptr
, name
)
119 utils
._handle
_ret
(ret
, "cannot set stream class object's name")
123 id = native_bt
.stream_class_get_id(self
._ptr
)
132 utils
._check
_int
64(id)
133 ret
= native_bt
.stream_class_set_id(self
._ptr
, id)
134 utils
._handle
_ret
(ret
, "cannot set stream class object's ID")
138 clock_ptr
= native_bt
.stream_class_get_clock(self
._ptr
)
140 if clock_ptr
is None:
143 return bt2
.ctf_writer
.CtfWriterClock
._create
_from
_ptr
(clock_ptr
)
146 def clock(self
, clock
):
147 utils
._check
_type
(clock
, bt2
.ctf_writer
.CtfWriterClock
)
148 ret
= native_bt
.stream_class_set_clock(self
._ptr
, clock
._ptr
)
149 utils
._handle
_ret
(ret
, "cannot set stream class object's CTF writer clock object")
152 def packet_context_field_class(self
):
153 fc_ptr
= native_bt
.stream_class_get_packet_context_type(self
._ptr
)
158 return bt2
.field_class
._create
_from
_ptr
(fc_ptr
)
160 @packet_context_field_class.setter
161 def packet_context_field_class(self
, packet_context_field_class
):
162 packet_context_field_class_ptr
= None
164 if packet_context_field_class
is not None:
165 utils
._check
_type
(packet_context_field_class
, bt2
.field_class
._FieldClass
)
166 packet_context_field_class_ptr
= packet_context_field_class
._ptr
168 ret
= native_bt
.stream_class_set_packet_context_type(self
._ptr
,
169 packet_context_field_class_ptr
)
170 utils
._handle
_ret
(ret
, "cannot set stream class object's packet context field class")
173 def event_header_field_class(self
):
174 fc_ptr
= native_bt
.stream_class_get_event_header_type(self
._ptr
)
179 return bt2
.field_class
._create
_from
_ptr
(fc_ptr
)
181 @event_header_field_class.setter
182 def event_header_field_class(self
, event_header_field_class
):
183 event_header_field_class_ptr
= None
185 if event_header_field_class
is not None:
186 utils
._check
_type
(event_header_field_class
, bt2
.field_class
._FieldClass
)
187 event_header_field_class_ptr
= event_header_field_class
._ptr
189 ret
= native_bt
.stream_class_set_event_header_type(self
._ptr
,
190 event_header_field_class_ptr
)
191 utils
._handle
_ret
(ret
, "cannot set stream class object's event header field class")
194 def event_context_field_class(self
):
195 fc_ptr
= native_bt
.stream_class_get_event_context_type(self
._ptr
)
200 return bt2
.field_class
._create
_from
_ptr
(fc_ptr
)
202 @event_context_field_class.setter
203 def event_context_field_class(self
, event_context_field_class
):
204 event_context_field_class_ptr
= None
206 if event_context_field_class
is not None:
207 utils
._check
_type
(event_context_field_class
, bt2
.field_class
._FieldClass
)
208 event_context_field_class_ptr
= event_context_field_class
._ptr
210 ret
= native_bt
.stream_class_set_event_context_type(self
._ptr
,
211 event_context_field_class_ptr
)
212 utils
._handle
_ret
(ret
, "cannot set stream class object's event context field class")
214 def __call__(self
, name
=None, id=None):
216 utils
._check
_str
(name
)
219 stream_ptr
= native_bt
.stream_create(self
._ptr
, name
)
221 stream_ptr
= native_bt
.stream_create_with_id(self
._ptr
, name
, id)
223 if stream_ptr
is None:
224 raise bt2
.CreationError('cannot create stream object')
226 return bt2
.stream
._create
_from
_ptr
(stream_ptr
)
228 def __eq__(self
, other
):
229 if type(other
) is not type(self
):
232 if self
.addr
== other
.addr
:
235 self_event_classes
= list(self
.values())
236 other_event_classes
= list(other
.values())
241 self
.packet_context_field_class
,
242 self
.event_header_field_class
,
243 self
.event_context_field_class
,
250 other
.packet_context_field_class
,
251 other
.event_header_field_class
,
252 other
.event_context_field_class
,
256 return self_props
== other_props
258 def _copy(self
, fc_copy_func
, ev_copy_func
):
261 if self
.id is not None:
264 if self
.name
is not None:
267 if self
.clock
is not None:
268 cpy
.clock
= self
.clock
270 cpy
.packet_context_field_class
= fc_copy_func(self
.packet_context_field_class
)
271 cpy
.event_header_field_class
= fc_copy_func(self
.event_header_field_class
)
272 cpy
.event_context_field_class
= fc_copy_func(self
.event_context_field_class
)
274 for event_class
in self
.values():
275 cpy
.add_event_class(ev_copy_func(event_class
))
280 return self
._copy
(lambda fc
: fc
, copy
.copy
)
282 def __deepcopy__(self
, memo
):
283 cpy
= self
._copy
(copy
.deepcopy
, copy
.deepcopy
)