Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
f6a5e476 | 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 | |
24 | import uuid as uuidp | |
f6a5e476 | 25 | import numbers |
81447b5b PP |
26 | import bt2 |
27 | ||
28 | ||
29 | class ClockClassOffset: | |
30 | def __init__(self, seconds=0, cycles=0): | |
31 | utils._check_int64(seconds) | |
32 | utils._check_int64(cycles) | |
33 | self._seconds = seconds | |
34 | self._cycles = cycles | |
35 | ||
36 | @property | |
37 | def seconds(self): | |
38 | return self._seconds | |
39 | ||
40 | @property | |
41 | def cycles(self): | |
42 | return self._cycles | |
43 | ||
f6a5e476 PP |
44 | def __hash__(self): |
45 | return hash((self.seconds, self.cycles)) | |
46 | ||
81447b5b PP |
47 | def __eq__(self, other): |
48 | if not isinstance(other, self.__class__): | |
49 | # not comparing apples to apples | |
50 | return False | |
51 | ||
52 | return (self.seconds, self.cycles) == (other.seconds, other.cycles) | |
53 | ||
54 | ||
55 | class ClockClass(object._Object): | |
f6a5e476 | 56 | def __init__(self, name, frequency, description=None, precision=None, |
81447b5b PP |
57 | offset=None, is_absolute=None, uuid=None): |
58 | utils._check_str(name) | |
f6a5e476 PP |
59 | utils._check_uint64(frequency) |
60 | ptr = native_bt.ctf_clock_class_create(name, frequency) | |
81447b5b PP |
61 | |
62 | if ptr is None: | |
63 | raise bt2.CreationError('cannot create clock class object') | |
64 | ||
65 | super().__init__(ptr) | |
66 | ||
67 | if description is not None: | |
68 | self.description = description | |
69 | ||
70 | if frequency is not None: | |
71 | self.frequency = frequency | |
72 | ||
73 | if precision is not None: | |
74 | self.precision = precision | |
75 | ||
76 | if offset is not None: | |
77 | self.offset = offset | |
78 | ||
79 | if is_absolute is not None: | |
80 | self.is_absolute = is_absolute | |
81 | ||
82 | if uuid is not None: | |
83 | self.uuid = uuid | |
84 | ||
85 | def __eq__(self, other): | |
86 | if type(self) is not type(other): | |
87 | # not comparing apples to apples | |
88 | return False | |
89 | ||
90 | self_props = ( | |
91 | self.name, | |
92 | self.description, | |
93 | self.frequency, | |
94 | self.precision, | |
95 | self.offset, | |
96 | self.is_absolute, | |
97 | self.uuid | |
98 | ) | |
99 | other_props = ( | |
100 | other.name, | |
101 | other.description, | |
102 | other.frequency, | |
103 | other.precision, | |
104 | other.offset, | |
105 | other.is_absolute, | |
106 | other.uuid | |
107 | ) | |
108 | return self_props == other_props | |
109 | ||
110 | def __copy__(self): | |
111 | return ClockClass(name=self.name, description=self.description, | |
112 | frequency=self.frequency, precision=self.precision, | |
113 | offset=self.offset, is_absolute=self.is_absolute, | |
114 | uuid=self.uuid) | |
115 | ||
116 | def __deepcopy__(self, memo): | |
117 | cpy = self.__copy__() | |
118 | memo[id(self)] = cpy | |
119 | return cpy | |
120 | ||
f6a5e476 PP |
121 | def __hash__(self): |
122 | return hash(( | |
123 | self.name, | |
124 | self.description, | |
125 | self.frequency, | |
126 | self.precision, | |
127 | self.offset.seconds, | |
128 | self.offset.cycles, | |
129 | self.is_absolute, | |
130 | self.uuid)) | |
131 | ||
81447b5b PP |
132 | @property |
133 | def name(self): | |
134 | name = native_bt.ctf_clock_class_get_name(self._ptr) | |
f6a5e476 | 135 | assert(name is not None) |
81447b5b PP |
136 | return name |
137 | ||
138 | @name.setter | |
139 | def name(self, name): | |
140 | utils._check_str(name) | |
141 | ret = native_bt.ctf_clock_class_set_name(self._ptr, name) | |
142 | utils._handle_ret(ret, "cannot set clock class object's name") | |
143 | ||
144 | @property | |
145 | def description(self): | |
f6a5e476 | 146 | return native_bt.ctf_clock_class_get_description(self._ptr) |
81447b5b PP |
147 | |
148 | @description.setter | |
149 | def description(self, description): | |
150 | utils._check_str(description) | |
151 | ret = native_bt.ctf_clock_class_set_description(self._ptr, description) | |
152 | utils._handle_ret(ret, "cannot set clock class object's description") | |
153 | ||
154 | @property | |
155 | def frequency(self): | |
156 | frequency = native_bt.ctf_clock_class_get_frequency(self._ptr) | |
f6a5e476 | 157 | assert(frequency >= 1) |
81447b5b PP |
158 | return frequency |
159 | ||
160 | @frequency.setter | |
161 | def frequency(self, frequency): | |
162 | utils._check_uint64(frequency) | |
163 | ret = native_bt.ctf_clock_class_set_frequency(self._ptr, frequency) | |
164 | utils._handle_ret(ret, "cannot set clock class object's frequency") | |
165 | ||
166 | @property | |
167 | def precision(self): | |
168 | precision = native_bt.ctf_clock_class_get_precision(self._ptr) | |
f6a5e476 | 169 | assert(precision >= 0) |
81447b5b PP |
170 | return precision |
171 | ||
172 | @precision.setter | |
173 | def precision(self, precision): | |
174 | utils._check_uint64(precision) | |
175 | ret = native_bt.ctf_clock_class_set_precision(self._ptr, precision) | |
176 | utils._handle_ret(ret, "cannot set clock class object's precision") | |
177 | ||
178 | @property | |
179 | def offset(self): | |
180 | ret, offset_s = native_bt.ctf_clock_class_get_offset_s(self._ptr) | |
f6a5e476 | 181 | assert(ret == 0) |
81447b5b | 182 | ret, offset_cycles = native_bt.ctf_clock_class_get_offset_cycles(self._ptr) |
f6a5e476 | 183 | assert(ret == 0) |
81447b5b PP |
184 | return ClockClassOffset(offset_s, offset_cycles) |
185 | ||
186 | @offset.setter | |
187 | def offset(self, offset): | |
188 | utils._check_type(offset, ClockClassOffset) | |
189 | ret = native_bt.ctf_clock_class_set_offset_s(self._ptr, offset.seconds) | |
190 | utils._handle_ret(ret, "cannot set clock class object's offset (seconds)") | |
191 | ret = native_bt.ctf_clock_class_set_offset_cycles(self._ptr, offset.cycles) | |
192 | utils._handle_ret(ret, "cannot set clock class object's offset (cycles)") | |
193 | ||
194 | @property | |
195 | def is_absolute(self): | |
f6a5e476 PP |
196 | is_absolute = native_bt.ctf_clock_class_is_absolute(self._ptr) |
197 | assert(is_absolute >= 0) | |
81447b5b PP |
198 | return is_absolute > 0 |
199 | ||
200 | @is_absolute.setter | |
201 | def is_absolute(self, is_absolute): | |
202 | utils._check_bool(is_absolute) | |
203 | ret = native_bt.ctf_clock_class_set_is_absolute(self._ptr, int(is_absolute)) | |
204 | utils._handle_ret(ret, "cannot set clock class object's absoluteness") | |
205 | ||
206 | @property | |
207 | def uuid(self): | |
208 | uuid_bytes = native_bt.ctf_clock_class_get_uuid(self._ptr) | |
209 | ||
210 | if uuid_bytes is None: | |
f6a5e476 | 211 | return |
81447b5b PP |
212 | |
213 | return uuidp.UUID(bytes=uuid_bytes) | |
214 | ||
215 | @uuid.setter | |
216 | def uuid(self, uuid): | |
217 | utils._check_type(uuid, uuidp.UUID) | |
218 | ret = native_bt.ctf_clock_class_set_uuid(self._ptr, uuid.bytes) | |
219 | utils._handle_ret(ret, "cannot set clock class object's UUID") | |
220 | ||
f6a5e476 | 221 | def __call__(self, cycles): |
81447b5b PP |
222 | return _ClockValue(self._ptr, cycles) |
223 | ||
224 | ||
225 | def _create_clock_value_from_ptr(ptr): | |
226 | clock_value = _ClockValue._create_from_ptr(ptr) | |
227 | return clock_value | |
228 | ||
229 | ||
230 | class _ClockValue(object._Object): | |
231 | def __init__(self, clock_class_ptr, cycles): | |
232 | utils._check_uint64(cycles) | |
233 | ptr = native_bt.ctf_clock_value_create(clock_class_ptr, cycles) | |
234 | ||
235 | if ptr is None: | |
236 | raise bt2.CreationError('cannot create clock value object') | |
237 | ||
238 | super().__init__(ptr) | |
239 | ||
240 | @property | |
241 | def clock_class(self): | |
242 | ptr = native_bt.ctf_clock_value_get_class(self._ptr) | |
f6a5e476 | 243 | assert(ptr) |
81447b5b PP |
244 | return ClockClass._create_from_ptr(ptr) |
245 | ||
246 | @property | |
247 | def cycles(self): | |
248 | ret, cycles = native_bt.ctf_clock_value_get_value(self._ptr) | |
f6a5e476 | 249 | assert(ret == 0) |
81447b5b PP |
250 | return cycles |
251 | ||
252 | @property | |
253 | def ns_from_epoch(self): | |
254 | ret, ns = native_bt.ctf_clock_value_get_value_ns_from_epoch(self._ptr) | |
255 | utils._handle_ret(ret, "cannot get clock value object's nanoseconds from Epoch") | |
256 | return ns | |
257 | ||
258 | def __eq__(self, other): | |
f6a5e476 PP |
259 | if isinstance(other, numbers.Integral): |
260 | return int(other) == self.cycles | |
261 | ||
81447b5b PP |
262 | if not isinstance(other, self.__class__): |
263 | # not comparing apples to apples | |
264 | return False | |
265 | ||
266 | if self.addr == other.addr: | |
267 | return True | |
268 | ||
f6a5e476 PP |
269 | self_props = self.clock_class, self.cycles |
270 | other_props = other.clock_class, other.cycles | |
81447b5b PP |
271 | return self_props == other_props |
272 | ||
273 | def __copy__(self): | |
f6a5e476 | 274 | return self.clock_class(self.cycles) |
81447b5b PP |
275 | |
276 | def __deepcopy__(self, memo): | |
277 | cpy = self.__copy__() | |
278 | memo[id(self)] = cpy | |
279 | return cpy |