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