bt2: Adapt test_event.py and make it pass
[babeltrace.git] / bindings / python / bt2 / bt2 / clock_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
24import uuid as uuidp
811644b8 25import numbers
81447b5b 26import bt2
4b552f8b 27import bt2.clock_snapshot as clock_snapshot
81447b5b 28
0b03f63e 29
81447b5b
PP
30class ClockClassOffset:
31 def __init__(self, seconds=0, cycles=0):
32 utils._check_int64(seconds)
33 utils._check_int64(cycles)
34 self._seconds = seconds
35 self._cycles = cycles
36
37 @property
38 def seconds(self):
39 return self._seconds
40
41 @property
42 def cycles(self):
43 return self._cycles
44
811644b8
PP
45 def __hash__(self):
46 return hash((self.seconds, self.cycles))
47
81447b5b
PP
48 def __eq__(self, other):
49 if not isinstance(other, self.__class__):
50 # not comparing apples to apples
51 return False
52
53 return (self.seconds, self.cycles) == (other.seconds, other.cycles)
54
55
78288f58 56class ClockClass(object._SharedObject):
3cdfbaea
SM
57 _get_ref = staticmethod(native_bt.clock_class_get_ref)
58 _put_ref = staticmethod(native_bt.clock_class_put_ref)
59
811644b8 60 def __init__(self, name, frequency, description=None, precision=None,
81447b5b
PP
61 offset=None, is_absolute=None, uuid=None):
62 utils._check_str(name)
811644b8 63 utils._check_uint64(frequency)
50842bdc 64 ptr = native_bt.clock_class_create(name, frequency)
81447b5b
PP
65
66 if ptr is None:
67 raise bt2.CreationError('cannot create clock class object')
68
69 super().__init__(ptr)
70
71 if description is not None:
72 self.description = description
73
74 if frequency is not None:
75 self.frequency = frequency
76
77 if precision is not None:
78 self.precision = precision
79
80 if offset is not None:
81 self.offset = offset
82
83 if is_absolute is not None:
84 self.is_absolute = is_absolute
85
86 if uuid is not None:
87 self.uuid = uuid
88
89 def __eq__(self, other):
90 if type(self) is not type(other):
91 # not comparing apples to apples
92 return False
93
94 self_props = (
95 self.name,
96 self.description,
97 self.frequency,
98 self.precision,
99 self.offset,
100 self.is_absolute,
101 self.uuid
102 )
103 other_props = (
104 other.name,
105 other.description,
106 other.frequency,
107 other.precision,
108 other.offset,
109 other.is_absolute,
110 other.uuid
111 )
112 return self_props == other_props
113
114 def __copy__(self):
115 return ClockClass(name=self.name, description=self.description,
116 frequency=self.frequency, precision=self.precision,
117 offset=self.offset, is_absolute=self.is_absolute,
118 uuid=self.uuid)
119
120 def __deepcopy__(self, memo):
121 cpy = self.__copy__()
122 memo[id(self)] = cpy
123 return cpy
124
811644b8
PP
125 def __hash__(self):
126 return hash((
127 self.name,
128 self.description,
129 self.frequency,
130 self.precision,
131 self.offset.seconds,
132 self.offset.cycles,
133 self.is_absolute,
134 self.uuid))
135
81447b5b
PP
136 @property
137 def name(self):
50842bdc 138 name = native_bt.clock_class_get_name(self._ptr)
811644b8 139 assert(name is not None)
81447b5b
PP
140 return name
141
142 @name.setter
143 def name(self, name):
144 utils._check_str(name)
50842bdc 145 ret = native_bt.clock_class_set_name(self._ptr, name)
81447b5b
PP
146 utils._handle_ret(ret, "cannot set clock class object's name")
147
148 @property
149 def description(self):
50842bdc 150 return native_bt.clock_class_get_description(self._ptr)
81447b5b
PP
151
152 @description.setter
153 def description(self, description):
154 utils._check_str(description)
50842bdc 155 ret = native_bt.clock_class_set_description(self._ptr, description)
81447b5b
PP
156 utils._handle_ret(ret, "cannot set clock class object's description")
157
158 @property
159 def frequency(self):
50842bdc 160 frequency = native_bt.clock_class_get_frequency(self._ptr)
811644b8 161 assert(frequency >= 1)
81447b5b
PP
162 return frequency
163
164 @frequency.setter
165 def frequency(self, frequency):
166 utils._check_uint64(frequency)
50842bdc 167 ret = native_bt.clock_class_set_frequency(self._ptr, frequency)
81447b5b
PP
168 utils._handle_ret(ret, "cannot set clock class object's frequency")
169
170 @property
171 def precision(self):
50842bdc 172 precision = native_bt.clock_class_get_precision(self._ptr)
811644b8 173 assert(precision >= 0)
81447b5b
PP
174 return precision
175
176 @precision.setter
177 def precision(self, precision):
178 utils._check_uint64(precision)
50842bdc 179 ret = native_bt.clock_class_set_precision(self._ptr, precision)
81447b5b
PP
180 utils._handle_ret(ret, "cannot set clock class object's precision")
181
182 @property
183 def offset(self):
50842bdc 184 ret, offset_s = native_bt.clock_class_get_offset_s(self._ptr)
811644b8 185 assert(ret == 0)
50842bdc 186 ret, offset_cycles = native_bt.clock_class_get_offset_cycles(self._ptr)
811644b8 187 assert(ret == 0)
81447b5b
PP
188 return ClockClassOffset(offset_s, offset_cycles)
189
190 @offset.setter
191 def offset(self, offset):
192 utils._check_type(offset, ClockClassOffset)
50842bdc 193 ret = native_bt.clock_class_set_offset_s(self._ptr, offset.seconds)
81447b5b 194 utils._handle_ret(ret, "cannot set clock class object's offset (seconds)")
50842bdc 195 ret = native_bt.clock_class_set_offset_cycles(self._ptr, offset.cycles)
81447b5b
PP
196 utils._handle_ret(ret, "cannot set clock class object's offset (cycles)")
197
198 @property
199 def is_absolute(self):
50842bdc 200 is_absolute = native_bt.clock_class_is_absolute(self._ptr)
811644b8 201 assert(is_absolute >= 0)
81447b5b
PP
202 return is_absolute > 0
203
204 @is_absolute.setter
205 def is_absolute(self, is_absolute):
206 utils._check_bool(is_absolute)
50842bdc 207 ret = native_bt.clock_class_set_is_absolute(self._ptr, int(is_absolute))
81447b5b
PP
208 utils._handle_ret(ret, "cannot set clock class object's absoluteness")
209
210 @property
211 def uuid(self):
50842bdc 212 uuid_bytes = native_bt.clock_class_get_uuid(self._ptr)
81447b5b
PP
213
214 if uuid_bytes is None:
811644b8 215 return
81447b5b
PP
216
217 return uuidp.UUID(bytes=uuid_bytes)
218
219 @uuid.setter
220 def uuid(self, uuid):
221 utils._check_type(uuid, uuidp.UUID)
50842bdc 222 ret = native_bt.clock_class_set_uuid(self._ptr, uuid.bytes)
81447b5b
PP
223 utils._handle_ret(ret, "cannot set clock class object's UUID")
224
811644b8 225 def __call__(self, cycles):
4b552f8b 226 return clock_snapshot._ClockSnapshot(self._ptr, cycles)
This page took 0.045391 seconds and 4 git commands to generate.