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