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