cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / bindings / python / bt2 / bt2 / clock_class.py
1 # SPDX-License-Identifier: MIT
2 #
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
4
5 import uuid as uuidp
6
7 from bt2 import utils as bt2_utils
8 from bt2 import value as bt2_value
9 from bt2 import object as bt2_object
10 from bt2 import native_bt
11
12
13 class ClockClassOffset:
14 def __init__(self, seconds=0, cycles=0):
15 bt2_utils._check_int64(seconds)
16 bt2_utils._check_int64(cycles)
17 self._seconds = seconds
18 self._cycles = cycles
19
20 @property
21 def seconds(self):
22 return self._seconds
23
24 @property
25 def cycles(self):
26 return self._cycles
27
28 def __eq__(self, other):
29 if not isinstance(other, self.__class__):
30 # not comparing apples to apples
31 return False
32
33 return (self.seconds, self.cycles) == (other.seconds, other.cycles)
34
35
36 class _ClockClassConst(bt2_object._SharedObject):
37 @staticmethod
38 def _get_ref(ptr):
39 native_bt.clock_class_get_ref(ptr)
40
41 @staticmethod
42 def _put_ref(ptr):
43 native_bt.clock_class_put_ref(ptr)
44
45 _create_value_from_ptr_and_get_ref = staticmethod(
46 bt2_value._create_from_const_ptr_and_get_ref
47 )
48 _borrow_user_attributes_ptr = staticmethod(
49 native_bt.clock_class_borrow_user_attributes_const
50 )
51
52 @property
53 def user_attributes(self):
54 ptr = self._borrow_user_attributes_ptr(self._ptr)
55 assert ptr is not None
56 return self._create_value_from_ptr_and_get_ref(ptr)
57
58 @property
59 def name(self):
60 return native_bt.clock_class_get_name(self._ptr)
61
62 @property
63 def description(self):
64 return native_bt.clock_class_get_description(self._ptr)
65
66 @property
67 def frequency(self):
68 return native_bt.clock_class_get_frequency(self._ptr)
69
70 @property
71 def precision(self):
72 precision = native_bt.clock_class_get_precision(self._ptr)
73 return precision
74
75 @property
76 def offset(self):
77 offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr)
78 return ClockClassOffset(offset_s, offset_cycles)
79
80 @property
81 def origin_is_unix_epoch(self):
82 return native_bt.clock_class_origin_is_unix_epoch(self._ptr)
83
84 @property
85 def uuid(self):
86 uuid_bytes = native_bt.clock_class_get_uuid(self._ptr)
87
88 if uuid_bytes is None:
89 return
90
91 return uuidp.UUID(bytes=uuid_bytes)
92
93 def cycles_to_ns_from_origin(self, cycles):
94 bt2_utils._check_uint64(cycles)
95 status, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles)
96 error_msg = "cannot convert clock value to nanoseconds from origin for given clock class"
97 bt2_utils._handle_func_status(status, error_msg)
98 return ns
99
100
101 class _ClockClass(_ClockClassConst):
102 _create_value_from_ptr_and_get_ref = staticmethod(
103 bt2_value._create_from_ptr_and_get_ref
104 )
105 _borrow_user_attributes_ptr = staticmethod(
106 native_bt.clock_class_borrow_user_attributes
107 )
108
109 def _user_attributes(self, user_attributes):
110 value = bt2_value.create_value(user_attributes)
111 bt2_utils._check_type(value, bt2_value.MapValue)
112 native_bt.clock_class_set_user_attributes(self._ptr, value._ptr)
113
114 _user_attributes = property(fset=_user_attributes)
115
116 def _name(self, name):
117 bt2_utils._check_str(name)
118 status = native_bt.clock_class_set_name(self._ptr, name)
119 bt2_utils._handle_func_status(status, "cannot set clock class object's name")
120
121 _name = property(fset=_name)
122
123 def _description(self, description):
124 bt2_utils._check_str(description)
125 status = native_bt.clock_class_set_description(self._ptr, description)
126 bt2_utils._handle_func_status(
127 status, "cannot set clock class object's description"
128 )
129
130 _description = property(fset=_description)
131
132 def _frequency(self, frequency):
133 bt2_utils._check_uint64(frequency)
134 native_bt.clock_class_set_frequency(self._ptr, frequency)
135
136 _frequency = property(fset=_frequency)
137
138 def _precision(self, precision):
139 bt2_utils._check_uint64(precision)
140 native_bt.clock_class_set_precision(self._ptr, precision)
141
142 _precision = property(fset=_precision)
143
144 def _offset(self, offset):
145 bt2_utils._check_type(offset, ClockClassOffset)
146 native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles)
147
148 _offset = property(fset=_offset)
149
150 def _origin_is_unix_epoch(self, origin_is_unix_epoch):
151 bt2_utils._check_bool(origin_is_unix_epoch)
152 native_bt.clock_class_set_origin_is_unix_epoch(
153 self._ptr, int(origin_is_unix_epoch)
154 )
155
156 _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch)
157
158 def _uuid(self, uuid):
159 bt2_utils._check_type(uuid, uuidp.UUID)
160 native_bt.clock_class_set_uuid(self._ptr, uuid.bytes)
161
162 _uuid = property(fset=_uuid)
This page took 0.032152 seconds and 4 git commands to generate.