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 collections
.abc
25 from bt2
import value
as bt2_value
26 from bt2
import stream
as bt2_stream
27 from bt2
import trace_class
as bt2_trace_class
28 from bt2
import stream_class
as bt2_stream_class
34 class _TraceEnvironmentConst(collections
.abc
.Mapping
):
35 _create_value_from_ptr_and_get_ref
= staticmethod(
36 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
39 def __init__(self
, trace
):
42 def __getitem__(self
, key
):
45 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_value_by_name_const
46 value_ptr
= borrow_entry_fn(self
._trace
._ptr
, key
)
51 return self
._create
_value
_from
_ptr
_and
_get
_ref
(value_ptr
)
54 count
= native_bt
.trace_get_environment_entry_count(self
._trace
._ptr
)
59 trace_ptr
= self
._trace
._ptr
61 for idx
in range(len(self
)):
62 borrow_entry_fn
= native_bt
.trace_borrow_environment_entry_by_index_const
63 entry_name
, _
= borrow_entry_fn(trace_ptr
, idx
)
64 assert entry_name
is not None
68 class _TraceEnvironment(_TraceEnvironmentConst
, collections
.abc
.MutableMapping
):
69 _create_value_from_ptr_and_get_ref
= staticmethod(
70 bt2_value
._create
_from
_ptr
_and
_get
_ref
73 def __setitem__(self
, key
, value
):
74 if isinstance(value
, str):
75 set_env_entry_fn
= native_bt
.trace_set_environment_entry_string
76 elif isinstance(value
, int):
77 set_env_entry_fn
= native_bt
.trace_set_environment_entry_integer
79 raise TypeError('expected str or int, got {}'.format(type(value
)))
81 status
= set_env_entry_fn(self
._trace
._ptr
, key
, value
)
82 utils
._handle
_func
_status
(status
, "cannot set trace object's environment entry")
84 def __delitem__(self
, key
):
85 raise NotImplementedError
88 class _TraceConst(object._SharedObject
, collections
.abc
.Mapping
):
89 _get_ref
= staticmethod(native_bt
.trace_get_ref
)
90 _put_ref
= staticmethod(native_bt
.trace_put_ref
)
91 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id_const
)
92 _borrow_stream_ptr_by_index
= staticmethod(
93 native_bt
.trace_borrow_stream_by_index_const
95 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class_const
)
96 _borrow_user_attributes_ptr
= staticmethod(
97 native_bt
.trace_borrow_user_attributes_const
99 _create_value_from_ptr_and_get_ref
= staticmethod(
100 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
102 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
103 _trace_class_pycls
= property(lambda _
: bt2_trace_class
._TraceClassConst
)
104 _trace_env_pycls
= property(lambda _
: _TraceEnvironmentConst
)
107 count
= native_bt
.trace_get_stream_count(self
._ptr
)
111 def __getitem__(self
, id):
112 utils
._check
_uint
64(id)
114 stream_ptr
= self
._borrow
_stream
_ptr
_by
_id
(self
._ptr
, id)
116 if stream_ptr
is None:
119 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
122 for idx
in range(len(self
)):
123 stream_ptr
= self
._borrow
_stream
_ptr
_by
_index
(self
._ptr
, idx
)
124 assert stream_ptr
is not None
126 id = native_bt
.stream_get_id(stream_ptr
)
133 trace_class_ptr
= self
._borrow
_class
_ptr
(self
._ptr
)
134 assert trace_class_ptr
is not None
135 return self
._trace
_class
_pycls
._create
_from
_ptr
_and
_get
_ref
(trace_class_ptr
)
138 def user_attributes(self
):
139 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
140 assert ptr
is not None
141 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
145 return native_bt
.trace_get_name(self
._ptr
)
149 uuid_bytes
= native_bt
.trace_get_uuid(self
._ptr
)
150 if uuid_bytes
is None:
153 return uuidp
.UUID(bytes
=uuid_bytes
)
156 def environment(self
):
157 return self
._trace
_env
_pycls
(self
)
159 def add_destruction_listener(self
, listener
):
160 '''Add a listener to be called when the trace is destroyed.'''
161 if not callable(listener
):
162 raise TypeError("'listener' parameter is not callable")
164 fn
= native_bt
.bt2_trace_add_destruction_listener
165 listener_from_native
= functools
.partial(
166 _trace_destruction_listener_from_native
, listener
169 status
, listener_id
= fn(self
._ptr
, listener_from_native
)
170 utils
._handle
_func
_status
(
171 status
, 'cannot add destruction listener to trace object'
174 return utils
._ListenerHandle
(listener_id
, self
)
177 class _Trace(_TraceConst
):
178 _borrow_stream_ptr_by_id
= staticmethod(native_bt
.trace_borrow_stream_by_id
)
179 _borrow_stream_ptr_by_index
= staticmethod(native_bt
.trace_borrow_stream_by_index
)
180 _borrow_class_ptr
= staticmethod(native_bt
.trace_borrow_class
)
181 _borrow_user_attributes_ptr
= staticmethod(native_bt
.trace_borrow_user_attributes
)
182 _create_value_from_ptr_and_get_ref
= staticmethod(
183 bt2_value
._create
_from
_ptr
_and
_get
_ref
185 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
186 _trace_class_pycls
= property(lambda _
: bt2_trace_class
._TraceClass
)
187 _trace_env_pycls
= property(lambda _
: _TraceEnvironment
)
189 def _name(self
, name
):
190 utils
._check
_str
(name
)
191 status
= native_bt
.trace_set_name(self
._ptr
, name
)
192 utils
._handle
_func
_status
(status
, "cannot set trace class object's name")
194 _name
= property(fset
=_name
)
196 def _user_attributes(self
, user_attributes
):
197 value
= bt2_value
.create_value(user_attributes
)
198 utils
._check
_type
(value
, bt2_value
.MapValue
)
199 native_bt
.trace_set_user_attributes(self
._ptr
, value
._ptr
)
201 _user_attributes
= property(fset
=_user_attributes
)
203 def _uuid(self
, uuid
):
204 utils
._check
_type
(uuid
, uuidp
.UUID
)
205 native_bt
.trace_set_uuid(self
._ptr
, uuid
.bytes
)
207 _uuid
= property(fset
=_uuid
)
209 def create_stream(self
, stream_class
, id=None, name
=None, user_attributes
=None):
210 utils
._check
_type
(stream_class
, bt2_stream_class
._StreamClass
)
212 if stream_class
.assigns_automatic_stream_id
:
215 "id provided, but stream class assigns automatic stream ids"
218 stream_ptr
= native_bt
.stream_create(stream_class
._ptr
, self
._ptr
)
222 "id not provided, but stream class does not assign automatic stream ids"
225 utils
._check
_uint
64(id)
226 stream_ptr
= native_bt
.stream_create_with_id(
227 stream_class
._ptr
, self
._ptr
, id
230 if stream_ptr
is None:
231 raise bt2
._MemoryError('cannot create stream object')
233 stream
= bt2_stream
._Stream
._create
_from
_ptr
(stream_ptr
)
238 if user_attributes
is not None:
239 stream
._user
_attributes
= user_attributes
244 def _trace_destruction_listener_from_native(user_listener
, trace_ptr
):
245 trace
= _TraceConst
._create
_from
_ptr
_and
_get
_ref
(trace_ptr
)