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 |
81447b5b | 28 | import bt2 |
8c2367b8 | 29 | import functools |
81447b5b PP |
30 | |
31 | ||
8c2367b8 SM |
32 | def _trace_destruction_listener_from_native(user_listener, trace_ptr): |
33 | trace = bt2.trace.Trace._create_from_ptr_and_get_ref(trace_ptr) | |
34 | user_listener(trace) | |
81447b5b | 35 | |
81447b5b | 36 | |
8c2367b8 SM |
37 | class Trace(object._SharedObject, collections.abc.Mapping): |
38 | _get_ref = staticmethod(native_bt.trace_get_ref) | |
39 | _put_ref = staticmethod(native_bt.trace_put_ref) | |
811644b8 PP |
40 | |
41 | def __len__(self): | |
8c2367b8 SM |
42 | count = native_bt.trace_get_stream_count(self._ptr) |
43 | assert count >= 0 | |
811644b8 PP |
44 | return count |
45 | ||
8c2367b8 SM |
46 | def __getitem__(self, id): |
47 | utils._check_uint64(id) | |
81447b5b | 48 | |
8c2367b8 | 49 | stream_ptr = native_bt.trace_borrow_stream_by_id_const(self._ptr, id) |
81447b5b | 50 | |
8c2367b8 SM |
51 | if stream_ptr is None: |
52 | raise KeyError(id) | |
81447b5b | 53 | |
8c2367b8 | 54 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) |
81447b5b PP |
55 | |
56 | def __iter__(self): | |
8c2367b8 SM |
57 | for idx in range(len(self)): |
58 | stream_ptr = native_bt.trace_borrow_stream_by_index_const(self._ptr, idx) | |
59 | assert stream_ptr is not None | |
81447b5b | 60 | |
8c2367b8 SM |
61 | id = native_bt.stream_get_id(stream_ptr) |
62 | assert id >= 0 | |
81447b5b | 63 | |
8c2367b8 | 64 | yield id |
81447b5b PP |
65 | |
66 | @property | |
67 | def name(self): | |
50842bdc | 68 | return native_bt.trace_get_name(self._ptr) |
81447b5b | 69 | |
8c2367b8 | 70 | def _name(self, name): |
81447b5b | 71 | utils._check_str(name) |
50842bdc | 72 | ret = native_bt.trace_set_name(self._ptr, name) |
81447b5b PP |
73 | utils._handle_ret(ret, "cannot set trace class object's name") |
74 | ||
8c2367b8 | 75 | _name = property(fset=_name) |
811644b8 | 76 | |
8c2367b8 SM |
77 | def create_stream(self, stream_class, id=None, name=None): |
78 | utils._check_type(stream_class, bt2.stream_class.StreamClass) | |
81447b5b | 79 | |
8c2367b8 SM |
80 | if stream_class.assigns_automatic_stream_id: |
81 | if id is not None: | |
82 | raise bt2.CreationError("id provided, but stream class assigns automatic stream ids") | |
81447b5b | 83 | |
8c2367b8 SM |
84 | stream_ptr = native_bt.stream_create(stream_class._ptr, self._ptr) |
85 | else: | |
86 | if id is None: | |
87 | raise bt2.CreationError("id not provided, but stream class does not assign automatic stream ids") | |
81447b5b | 88 | |
8c2367b8 SM |
89 | utils._check_uint64(id) |
90 | stream_ptr = native_bt.stream_create_with_id(stream_class._ptr, self._ptr, id) | |
81447b5b | 91 | |
8c2367b8 SM |
92 | if stream_ptr is None: |
93 | raise bt2.CreationError('cannot create stream object') | |
81447b5b | 94 | |
8c2367b8 | 95 | stream = bt2.stream._Stream._create_from_ptr(stream_ptr) |
81447b5b | 96 | |
8c2367b8 SM |
97 | if name is not None: |
98 | stream._name = name | |
81447b5b | 99 | |
8c2367b8 | 100 | return stream |
81447b5b | 101 | |
8c2367b8 SM |
102 | def add_destruction_listener(self, listener): |
103 | '''Add a listener to be called when the trace is destroyed.''' | |
104 | if not callable(listener): | |
105 | raise TypeError("'listener' parameter is not callable") | |
81447b5b | 106 | |
8c2367b8 SM |
107 | fn = native_bt.py3_trace_add_destruction_listener |
108 | listener_from_native = functools.partial(_trace_destruction_listener_from_native, | |
109 | listener) | |
81447b5b | 110 | |
8c2367b8 SM |
111 | listener_id = fn(self._ptr, listener_from_native) |
112 | if listener_id is None: | |
113 | utils._raise_bt2_error('cannot add destruction listener to trace object') | |
81447b5b | 114 | |
8c2367b8 | 115 | return bt2._ListenerHandle(listener_id, self) |