Commit | Line | Data |
---|---|---|
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 | ||
23 | from bt2 import native_bt, object, utils | |
b4f45851 | 24 | import bt2.field_class |
81447b5b | 25 | import collections.abc |
c4239792 | 26 | import bt2.value |
811644b8 | 27 | import bt2.stream |
05abc762 | 28 | import bt2.trace_class |
81447b5b | 29 | import bt2 |
8c2367b8 | 30 | import functools |
335a2da5 PP |
31 | import uuid as uuidp |
32 | ||
33 | ||
34 | class _TraceEnv(collections.abc.MutableMapping): | |
35 | def __init__(self, trace): | |
36 | self._trace = trace | |
37 | ||
38 | def __getitem__(self, key): | |
39 | utils._check_str(key) | |
40 | ||
41 | borrow_entry_fn = native_bt.trace_borrow_environment_entry_value_by_name_const | |
42 | value_ptr = borrow_entry_fn(self._trace._ptr, key) | |
43 | ||
44 | if value_ptr is None: | |
45 | raise KeyError(key) | |
46 | ||
47 | return bt2.value._create_from_ptr_and_get_ref(value_ptr) | |
48 | ||
49 | def __setitem__(self, key, value): | |
50 | if isinstance(value, str): | |
51 | set_env_entry_fn = native_bt.trace_set_environment_entry_string | |
52 | elif isinstance(value, int): | |
53 | set_env_entry_fn = native_bt.trace_set_environment_entry_integer | |
54 | else: | |
55 | raise TypeError('expected str or int, got {}'.format(type(value))) | |
56 | ||
57 | status = set_env_entry_fn(self._trace._ptr, key, value) | |
58 | utils._handle_func_status(status, | |
59 | "cannot set trace object's environment entry") | |
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 | 79 | def _trace_destruction_listener_from_native(user_listener, trace_ptr): |
2c6f8520 | 80 | trace = bt2.trace._Trace._create_from_ptr_and_get_ref(trace_ptr) |
8c2367b8 | 81 | user_listener(trace) |
81447b5b | 82 | |
81447b5b | 83 | |
2c6f8520 | 84 | class _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 | |
8c2367b8 | 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 | |
117 | return bt2.trace_class._TraceClass._create_from_ptr_and_get_ref(trace_class_ptr) | |
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 PP |
125 | status = native_bt.trace_set_name(self._ptr, name) |
126 | utils._handle_func_status(status, | |
127 | "cannot set trace class object's name") | |
81447b5b | 128 | |
8c2367b8 | 129 | _name = property(fset=_name) |
811644b8 | 130 | |
335a2da5 PP |
131 | @property |
132 | def uuid(self): | |
133 | uuid_bytes = native_bt.trace_get_uuid(self._ptr) | |
134 | if uuid_bytes is None: | |
135 | return | |
136 | ||
137 | return uuidp.UUID(bytes=uuid_bytes) | |
138 | ||
139 | def _uuid(self, uuid): | |
140 | utils._check_type(uuid, uuidp.UUID) | |
141 | native_bt.trace_set_uuid(self._ptr, uuid.bytes) | |
142 | ||
143 | _uuid = property(fset=_uuid) | |
144 | ||
145 | @property | |
146 | def env(self): | |
147 | return _TraceEnv(self) | |
148 | ||
8c2367b8 | 149 | def create_stream(self, stream_class, id=None, name=None): |
2c6f8520 | 150 | utils._check_type(stream_class, bt2.stream_class._StreamClass) |
81447b5b | 151 | |
8c2367b8 SM |
152 | if stream_class.assigns_automatic_stream_id: |
153 | if id is not None: | |
4430bc80 | 154 | raise ValueError("id provided, but stream class assigns automatic stream ids") |
81447b5b | 155 | |
8c2367b8 SM |
156 | stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) |
157 | else: | |
158 | if id is None: | |
4430bc80 | 159 | raise ValueError("id not provided, but stream class does not assign automatic stream ids") |
81447b5b | 160 | |
8c2367b8 SM |
161 | utils._check_uint64(id) |
162 | stream_ptr = native_bt.stream_create_with_id(stream_class._ptr, self._ptr, id) | |
81447b5b | 163 | |
8c2367b8 SM |
164 | if stream_ptr is None: |
165 | raise bt2.CreationError('cannot create stream object') | |
81447b5b | 166 | |
8c2367b8 | 167 | stream = bt2.stream._Stream._create_from_ptr(stream_ptr) |
81447b5b | 168 | |
8c2367b8 SM |
169 | if name is not None: |
170 | stream._name = name | |
81447b5b | 171 | |
8c2367b8 | 172 | return stream |
81447b5b | 173 | |
8c2367b8 SM |
174 | def add_destruction_listener(self, listener): |
175 | '''Add a listener to be called when the trace is destroyed.''' | |
176 | if not callable(listener): | |
177 | raise TypeError("'listener' parameter is not callable") | |
81447b5b | 178 | |
d24d5663 | 179 | fn = native_bt.bt2_trace_add_destruction_listener |
8c2367b8 SM |
180 | listener_from_native = functools.partial(_trace_destruction_listener_from_native, |
181 | listener) | |
81447b5b | 182 | |
8c2367b8 SM |
183 | listener_id = fn(self._ptr, listener_from_native) |
184 | if listener_id is None: | |
185 | utils._raise_bt2_error('cannot add destruction listener to trace object') | |
81447b5b | 186 | |
8c2367b8 | 187 | return bt2._ListenerHandle(listener_id, self) |