1 # The MIT License (MIT)
3 # Copyright (c) 2016 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_types
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
.ctf_stream_class_get_event_class(self
._stream
_class
._ptr
,
43 utils
._handle
_ptr
(ec_ptr
, "cannot get stream class object's event class object")
44 name
= native_bt
.ctf_event_class_get_name(ec_ptr
)
46 utils
._handle
_ptr
(name
, "cannot get event class object's name")
51 class StreamClass(object._Object
, collections
.abc
.Mapping
):
52 def __init__(self
, name
=None, id=None, packet_context_field_type
=None,
53 event_header_field_type
=None, event_context_field_type
=None,
55 ptr
= native_bt
.ctf_stream_class_create(None)
58 raise bt2
.CreationError('cannot create stream class object')
68 if packet_context_field_type
is not None:
69 self
.packet_context_field_type
= packet_context_field_type
71 if event_header_field_type
is not None:
72 self
.event_header_field_type
= event_header_field_type
74 if event_context_field_type
is not None:
75 self
.event_context_field_type
= event_context_field_type
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
):
83 ec_ptr
= native_bt
.ctf_stream_class_get_event_class_by_name(self
._ptr
,
89 return bt2
.EventClass
._create
_from
_ptr
(ec_ptr
)
92 count
= native_bt
.ctf_stream_class_get_event_class_count(self
._ptr
)
93 utils
._handle
_ret
(count
, "cannot get stream class object's event class count")
97 return _EventClassIterator(self
)
99 def event_class_with_id(self
, id):
100 utils
._check
_int
64(id)
101 ec_ptr
= native_bt
.ctf_stream_class_get_event_class_by_id(self
._ptr
, id)
102 utils
._handle
_ptr
(ec_ptr
, "cannot get stream class object's event class object")
103 return bt2
.EventClass
._create
_from
_ptr
(ec_ptr
)
105 def add_event_class(self
, event_class
):
106 utils
._check
_type
(event_class
, bt2
.EventClass
)
107 ret
= native_bt
.ctf_stream_class_add_event_class(self
._ptr
, event_class
._ptr
)
108 utils
._handle
_ret
(ret
, "cannot add event class object to stream class object's")
112 tc_ptr
= native_bt
.ctf_stream_class_get_trace(self
._ptr
)
114 if tc_ptr
is not None:
115 return bt2
.Trace
._create
_from
_ptr
(tc_ptr
)
119 return native_bt
.ctf_stream_class_get_name(self
._ptr
)
122 def name(self
, name
):
123 utils
._check
_str
(name
)
124 ret
= native_bt
.ctf_stream_class_set_name(self
._ptr
, name
)
125 utils
._handle
_ret
(ret
, "cannot set stream class object's name")
129 id = native_bt
.ctf_stream_class_get_id(self
._ptr
)
131 if utils
._is
_m
1ull(id):
132 raise bt2
.Error("cannot get stream class object's ID")
138 utils
._check
_int
64(id)
139 ret
= native_bt
.ctf_stream_class_set_id(self
._ptr
, id)
140 utils
._handle
_ret
(ret
, "cannot set stream class object's ID")
144 clock_ptr
= native_bt
.ctf_stream_class_get_clock(self
._ptr
)
146 if clock_ptr
is None:
149 return bt2
.ctf_writer
.CtfWriterClock
._create
_from
_ptr
(clock_ptr
)
152 def clock(self
, clock
):
153 utils
._check
_type
(clock
, bt2
.ctf_writer
.CtfWriterClock
)
154 ret
= native_bt
.ctf_stream_class_set_clock(self
._ptr
, clock
._ptr
)
155 utils
._handle
_ret
(ret
, "cannot set stream class object's CTF writer clock object")
158 def packet_context_field_type(self
):
159 ft_ptr
= native_bt
.ctf_stream_class_get_packet_context_type(self
._ptr
)
164 return bt2
.field_types
._create
_from
_ptr
(ft_ptr
)
166 @packet_context_field_type.setter
167 def packet_context_field_type(self
, packet_context_field_type
):
168 packet_context_field_type_ptr
= None
170 if packet_context_field_type
is not None:
171 utils
._check
_type
(packet_context_field_type
, bt2
.field_types
._FieldType
)
172 packet_context_field_type_ptr
= packet_context_field_type
._ptr
174 ret
= native_bt
.ctf_stream_class_set_packet_context_type(self
._ptr
,
175 packet_context_field_type_ptr
)
176 utils
._handle
_ret
(ret
, "cannot set stream class object's packet context field type")
179 def event_header_field_type(self
):
180 ft_ptr
= native_bt
.ctf_stream_class_get_event_header_type(self
._ptr
)
185 return bt2
.field_types
._create
_from
_ptr
(ft_ptr
)
187 @event_header_field_type.setter
188 def event_header_field_type(self
, event_header_field_type
):
189 event_header_field_type_ptr
= None
191 if event_header_field_type
is not None:
192 utils
._check
_type
(event_header_field_type
, bt2
.field_types
._FieldType
)
193 event_header_field_type_ptr
= event_header_field_type
._ptr
195 ret
= native_bt
.ctf_stream_class_set_event_header_type(self
._ptr
,
196 event_header_field_type_ptr
)
197 utils
._handle
_ret
(ret
, "cannot set stream class object's event header field type")
200 def event_context_field_type(self
):
201 ft_ptr
= native_bt
.ctf_stream_class_get_event_context_type(self
._ptr
)
206 return bt2
.field_types
._create
_from
_ptr
(ft_ptr
)
208 @event_context_field_type.setter
209 def event_context_field_type(self
, event_context_field_type
):
210 event_context_field_type_ptr
= None
212 if event_context_field_type
is not None:
213 utils
._check
_type
(event_context_field_type
, bt2
.field_types
._FieldType
)
214 event_context_field_type_ptr
= event_context_field_type
._ptr
216 ret
= native_bt
.ctf_stream_class_set_event_context_type(self
._ptr
,
217 event_context_field_type_ptr
)
218 utils
._handle
_ret
(ret
, "cannot set stream class object's event context field type")
220 def __call__(self
, name
=None):
222 utils
._check
_str
(name
)
224 stream_ptr
= native_bt
.ctf_stream_create(self
._ptr
, name
)
226 if stream_ptr
is None:
227 raise bt2
.CreationError('cannot create stream object')
229 return bt2
.stream
._create
_from
_ptr
(stream_ptr
)
231 def __eq__(self
, other
):
232 if type(other
) is not type(self
):
235 if self
.addr
== other
.addr
:
238 self_event_classes
= list(self
.values())
239 other_event_classes
= list(other
.values())
245 self
.packet_context_field_type
,
246 self
.event_header_field_type
,
247 self
.event_context_field_type
,
254 other
.packet_context_field_type
,
255 other
.event_header_field_type
,
256 other
.event_context_field_type
,
258 return self_props
== other_props
260 def _copy(self
, ft_copy_func
, ev_copy_func
):
264 cpy
.packet_context_field_type
= ft_copy_func(self
.packet_context_field_type
)
265 cpy
.event_header_field_type
= ft_copy_func(self
.event_header_field_type
)
266 cpy
.event_context_field_type
= ft_copy_func(self
.event_context_field_type
)
268 for event_class
in self
.values():
269 cpy
.add_event_class(ev_copy_func(event_class
))
274 return self
._copy
(lambda ft
: ft
, copy
.copy
)
276 def __deepcopy__(self
, memo
):
277 cpy
= self
._copy
(copy
.deepcopy
, copy
.deepcopy
)