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