bt2: Adapt test_stream_class.py and make it pass
[babeltrace.git] / bindings / python / bt2 / bt2 / stream_class.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
PP
25import collections.abc
26import bt2.ctf_writer
27import bt2.stream
81447b5b
PP
28import bt2
29
30
78288f58 31class StreamClass(object._SharedObject, collections.abc.Mapping):
fbbe9302
SM
32 _get_ref = staticmethod(native_bt.stream_class_get_ref)
33 _put_ref = staticmethod(native_bt.stream_class_put_ref)
34
81447b5b 35 def __getitem__(self, key):
811644b8 36 utils._check_int64(key)
3cdfbaea 37 ec_ptr = native_bt.stream_class_borrow_event_class_by_id(self._ptr, key)
81447b5b
PP
38
39 if ec_ptr is None:
40 raise KeyError(key)
41
3cdfbaea 42 return bt2.EventClass._create_from_ptr_and_get_ref(ec_ptr)
81447b5b
PP
43
44 def __len__(self):
50842bdc 45 count = native_bt.stream_class_get_event_class_count(self._ptr)
3cdfbaea 46 assert count >= 0
81447b5b
PP
47 return count
48
49 def __iter__(self):
3cdfbaea
SM
50 for idx in range(len(self)):
51 ec_ptr = native_bt.stream_class_borrow_event_class_by_index_const(self._ptr, idx)
52 assert ec_ptr is not None
53
54 id = native_bt.event_class_get_id(ec_ptr)
55 assert id >= 0
56
57 yield id
81447b5b 58
3cdfbaea
SM
59 def create_event_class(self, id=None):
60 if self.assigns_automatic_event_class_id:
61 if id is not None:
62 raise bt2.CreationError('id provided, but stream class assigns automatic event class ids')
63
64 ec_ptr = native_bt.event_class_create(self._ptr)
65 else:
66 if id is None:
67 raise bt2.CreationError('id not provided, but stream class does not assign automatic event class ids')
68
69 utils._check_uint64(id)
70 ec_ptr = native_bt.event_class_create_with_id(self._ptr, id)
71
72 return bt2.event_class.EventClass._create_from_ptr(ec_ptr)
81447b5b
PP
73
74 @property
3cdfbaea
SM
75 def trace_class(self):
76 tc_ptr = native_bt.stream_class_borrow_trace_class_const(self._ptr)
81447b5b
PP
77
78 if tc_ptr is not None:
3cdfbaea 79 return bt2.TraceClass._create_from_ptr_and_get_ref(tc_ptr)
81447b5b
PP
80
81 @property
82 def name(self):
50842bdc 83 return native_bt.stream_class_get_name(self._ptr)
81447b5b 84
3cdfbaea 85 def _name(self, name):
81447b5b 86 utils._check_str(name)
50842bdc 87 ret = native_bt.stream_class_set_name(self._ptr, name)
81447b5b
PP
88 utils._handle_ret(ret, "cannot set stream class object's name")
89
3cdfbaea
SM
90 _name = property(fset=_name)
91
92 @property
93 def assigns_automatic_event_class_id(self):
94 return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr)
95
96 def _assigns_automatic_event_class_id(self, auto_id):
97 utils._check_bool(auto_id)
98 return native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id)
99
100 _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id)
101
8c2367b8
SM
102 @property
103 def assigns_automatic_stream_id(self):
104 return native_bt.stream_class_assigns_automatic_stream_id(self._ptr)
105
106 def _assigns_automatic_stream_id(self, auto_id):
107 utils._check_bool(auto_id)
108 return native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id)
109
110 _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id)
111
3cdfbaea
SM
112 @property
113 def packets_have_default_beginning_clock_snapshot(self):
114 return native_bt.stream_class_packets_have_default_beginning_clock_snapshot(self._ptr)
115
116 def _packets_have_default_beginning_clock_snapshot(self, value):
117 utils._check_bool(value)
118 native_bt.stream_class_set_packets_have_default_beginning_clock_snapshot(self._ptr, value)
119
120 _packets_have_default_beginning_clock_snapshot = property(fset=_packets_have_default_beginning_clock_snapshot)
121
122 @property
123 def packets_have_default_end_clock_snapshot(self):
124 return native_bt.stream_class_packets_have_default_end_clock_snapshot(self._ptr)
125
126 def _packets_have_default_end_clock_snapshot(self, value):
127 utils._check_bool(value)
128 native_bt.stream_class_set_packets_have_default_end_clock_snapshot(self._ptr, value)
129
130 _packets_have_default_end_clock_snapshot = property(fset=_packets_have_default_end_clock_snapshot)
131
81447b5b
PP
132 @property
133 def id(self):
50842bdc 134 id = native_bt.stream_class_get_id(self._ptr)
81447b5b 135
811644b8
PP
136 if id < 0:
137 return
81447b5b
PP
138
139 return id
140
141 @id.setter
142 def id(self, id):
143 utils._check_int64(id)
50842bdc 144 ret = native_bt.stream_class_set_id(self._ptr, id)
81447b5b
PP
145 utils._handle_ret(ret, "cannot set stream class object's ID")
146
147 @property
148 def clock(self):
50842bdc 149 clock_ptr = native_bt.stream_class_get_clock(self._ptr)
81447b5b
PP
150
151 if clock_ptr is None:
152 return
153
154 return bt2.ctf_writer.CtfWriterClock._create_from_ptr(clock_ptr)
155
156 @clock.setter
157 def clock(self, clock):
158 utils._check_type(clock, bt2.ctf_writer.CtfWriterClock)
50842bdc 159 ret = native_bt.stream_class_set_clock(self._ptr, clock._ptr)
81447b5b
PP
160 utils._handle_ret(ret, "cannot set stream class object's CTF writer clock object")
161
162 @property
b4f45851 163 def packet_context_field_class(self):
3cdfbaea 164 fc_ptr = native_bt.stream_class_borrow_packet_context_field_class_const(self._ptr)
81447b5b 165
b4f45851 166 if fc_ptr is None:
81447b5b
PP
167 return
168
3cdfbaea 169 return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)
81447b5b 170
3cdfbaea 171 def _packet_context_field_class(self, packet_context_field_class):
b4f45851 172 if packet_context_field_class is not None:
3cdfbaea
SM
173 utils._check_type(packet_context_field_class,
174 bt2.field_class._StructureFieldClass)
175 ret = native_bt.stream_class_set_packet_context_field_class(self._ptr,
176 packet_context_field_class._ptr)
177 utils._handle_ret(ret, "cannot set stream class' packet context field class")
81447b5b 178
3cdfbaea 179 _packet_context_field_class = property(fset=_packet_context_field_class)
81447b5b
PP
180
181 @property
3cdfbaea
SM
182 def event_common_context_field_class(self):
183 fc_ptr = native_bt.stream_class_borrow_event_common_context_field_class_const(self._ptr)
81447b5b 184
b4f45851 185 if fc_ptr is None:
81447b5b
PP
186 return
187
3cdfbaea 188 return bt2.field_class._create_field_class_from_ptr_and_get_ref(fc_ptr)
81447b5b 189
3cdfbaea
SM
190 def _event_common_context_field_class(self, event_common_context_field_class):
191 if event_common_context_field_class is not None:
192 utils._check_type(event_common_context_field_class,
193 bt2.field_class._StructureFieldClass)
81447b5b 194
3cdfbaea
SM
195 set_context_fn = native_bt.stream_class_set_event_common_context_field_class
196 ret = set_context_fn(self._ptr, event_common_context_field_class._ptr)
81447b5b 197
3cdfbaea 198 utils._handle_ret(ret, "cannot set stream class' event context field type")
81447b5b 199
3cdfbaea 200 _event_common_context_field_class = property(fset=_event_common_context_field_class)
81447b5b 201
3cdfbaea
SM
202 @property
203 def default_clock_class(self):
204 cc_ptr = native_bt.stream_class_borrow_default_clock_class(self._ptr)
205 if cc_ptr is None:
81447b5b
PP
206 return
207
3cdfbaea 208 return bt2.clock_class.ClockClass._create_from_ptr_and_get_ref(cc_ptr)
81447b5b 209
3cdfbaea
SM
210 def _default_clock_class(self, clock_class):
211 utils._check_type(clock_class, bt2.clock_class.ClockClass)
212 native_bt.stream_class_set_default_clock_class(
213 self._ptr, clock_class._ptr)
81447b5b 214
3cdfbaea 215 _default_clock_class = property(fset=_default_clock_class)
This page took 0.046092 seconds and 4 git commands to generate.