bt2: clean available `bt2` package names
[babeltrace.git] / src / bindings / python / bt2 / bt2 / trace.py
CommitLineData
81447b5b
PP
1# The MIT License (MIT)
2#
811644b8 3# Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
81447b5b
PP
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
23from bt2 import native_bt, object, utils
3fb99a22 24from bt2 import field_class as bt2_field_class
81447b5b 25import collections.abc
3fb99a22
PP
26from bt2 import value as bt2_value
27from bt2 import stream as bt2_stream
28from bt2 import trace_class as bt2_trace_class
29from bt2 import stream_class as bt2_stream_class
81447b5b 30import bt2
8c2367b8 31import functools
335a2da5
PP
32import uuid as uuidp
33
34
35class _TraceEnv(collections.abc.MutableMapping):
36 def __init__(self, trace):
37 self._trace = trace
38
39 def __getitem__(self, key):
40 utils._check_str(key)
41
42 borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const
43 value_ptr = borrow_entry_fn(self._trace._ptr, key)
44
45 if value_ptr is None:
46 raise KeyError(key)
47
3fb99a22 48 return bt2_value._create_from_ptr_and_get_ref(value_ptr)
335a2da5
PP
49
50 def __setitem__(self, key, value):
51 if isinstance(value, str):
52 set_env_entry_fn = native_bt.trace_set_environment_entry_string
53 elif isinstance(value, int):
54 set_env_entry_fn = native_bt.trace_set_environment_entry_integer
55 else:
56 raise TypeError('expected str or int, got {}'.format(type(value)))
57
58 status = set_env_entry_fn(self._trace._ptr, key, value)
cfbd7cf3 59 utils._handle_func_status(status, "cannot set trace object's environment entry")
335a2da5
PP
60
61 def __delitem__(self, key):
62 raise NotImplementedError
63
64 def __len__(self):
65 count = native_bt.trace_get_environment_entry_count(self._trace._ptr)
66 assert count >= 0
67 return count
68
69 def __iter__(self):
70 trace_ptr = self._trace_env._trace._ptr
71
72 for idx in range(len(self)):
73 borrow_entry_fn = native_bt.trace_borrow_environment_entry_by_index_const
74 entry_name, _ = borrow_entry_fn(trace_ptr, idx)
75 assert entry_name is not None
76 yield entry_name
81447b5b
PP
77
78
8c2367b8 79def _trace_destruction_listener_from_native(user_listener, trace_ptr):
3fb99a22 80 trace = _Trace._create_from_ptr_and_get_ref(trace_ptr)
8c2367b8 81 user_listener(trace)
81447b5b 82
81447b5b 83
2c6f8520 84class _Trace(object._SharedObject, collections.abc.Mapping):
8c2367b8
SM
85 _get_ref = staticmethod(native_bt.trace_get_ref)
86 _put_ref = staticmethod(native_bt.trace_put_ref)
811644b8
PP
87
88 def __len__(self):
8c2367b8
SM
89 count = native_bt.trace_get_stream_count(self._ptr)
90 assert count >= 0
811644b8
PP
91 return count
92
8c2367b8
SM
93 def __getitem__(self, id):
94 utils._check_uint64(id)
81447b5b 95
8c2367b8 96 stream_ptr = native_bt.trace_borrow_stream_by_id_const(self._ptr, id)
81447b5b 97
8c2367b8
SM
98 if stream_ptr is None:
99 raise KeyError(id)
81447b5b 100
3fb99a22 101 return bt2_stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
81447b5b
PP
102
103 def __iter__(self):
8c2367b8
SM
104 for idx in range(len(self)):
105 stream_ptr = native_bt.trace_borrow_stream_by_index_const(self._ptr, idx)
106 assert stream_ptr is not None
81447b5b 107
8c2367b8
SM
108 id = native_bt.stream_get_id(stream_ptr)
109 assert id >= 0
81447b5b 110
8c2367b8 111 yield id
81447b5b 112
05abc762
PP
113 @property
114 def cls(self):
115 trace_class_ptr = native_bt.trace_borrow_class(self._ptr)
116 assert trace_class_ptr is not None
3fb99a22 117 return bt2_trace_class._TraceClass._create_from_ptr_and_get_ref(trace_class_ptr)
05abc762 118
81447b5b
PP
119 @property
120 def name(self):
50842bdc 121 return native_bt.trace_get_name(self._ptr)
81447b5b 122
8c2367b8 123 def _name(self, name):
81447b5b 124 utils._check_str(name)
d24d5663 125 status = native_bt.trace_set_name(self._ptr, name)
cfbd7cf3 126 utils._handle_func_status(status, "cannot set trace class object's name")
81447b5b 127
8c2367b8 128 _name = property(fset=_name)
811644b8 129
335a2da5
PP
130 @property
131 def uuid(self):
132 uuid_bytes = native_bt.trace_get_uuid(self._ptr)
133 if uuid_bytes is None:
134 return
135
136 return uuidp.UUID(bytes=uuid_bytes)
137
138 def _uuid(self, uuid):
139 utils._check_type(uuid, uuidp.UUID)
140 native_bt.trace_set_uuid(self._ptr, uuid.bytes)
141
142 _uuid = property(fset=_uuid)
143
144 @property
145 def env(self):
146 return _TraceEnv(self)
147
8c2367b8 148 def create_stream(self, stream_class, id=None, name=None):
3fb99a22 149 utils._check_type(stream_class, bt2_stream_class._StreamClass)
81447b5b 150
8c2367b8
SM
151 if stream_class.assigns_automatic_stream_id:
152 if id is not None:
cfbd7cf3
FD
153 raise ValueError(
154 "id provided, but stream class assigns automatic stream ids"
155 )
81447b5b 156
8c2367b8
SM
157 stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr)
158 else:
159 if id is None:
cfbd7cf3
FD
160 raise ValueError(
161 "id not provided, but stream class does not assign automatic stream ids"
162 )
81447b5b 163
8c2367b8 164 utils._check_uint64(id)
cfbd7cf3
FD
165 stream_ptr = native_bt.stream_create_with_id(
166 stream_class._ptr, self._ptr, id
167 )
81447b5b 168
8c2367b8 169 if stream_ptr is None:
694c792b 170 raise bt2._MemoryError('cannot create stream object')
81447b5b 171
3fb99a22 172 stream = bt2_stream._Stream._create_from_ptr(stream_ptr)
81447b5b 173
8c2367b8
SM
174 if name is not None:
175 stream._name = name
81447b5b 176
8c2367b8 177 return stream
81447b5b 178
8c2367b8
SM
179 def add_destruction_listener(self, listener):
180 '''Add a listener to be called when the trace is destroyed.'''
181 if not callable(listener):
182 raise TypeError("'listener' parameter is not callable")
81447b5b 183
d24d5663 184 fn = native_bt.bt2_trace_add_destruction_listener
cfbd7cf3
FD
185 listener_from_native = functools.partial(
186 _trace_destruction_listener_from_native, listener
187 )
81447b5b 188
ee2cad25
SM
189 status, listener_id = fn(self._ptr, listener_from_native)
190 utils._handle_func_status(
191 status, 'cannot add destruction listener to trace object'
192 )
81447b5b 193
3fb99a22 194 return utils._ListenerHandle(listener_id, self)
This page took 0.053274 seconds and 4 git commands to generate.