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 | import bt2 | |
cc81b5ab | 24 | import bt2.logging |
fb25b9e3 | 25 | from bt2 import native_bt |
81447b5b PP |
26 | |
27 | ||
28 | def _check_bool(o): | |
29 | if not isinstance(o, bool): | |
30 | raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__)) | |
31 | ||
32 | ||
33 | def _check_int(o): | |
34 | if not isinstance(o, int): | |
35 | raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__)) | |
36 | ||
37 | ||
38 | def _check_float(o): | |
39 | if not isinstance(o, float): | |
40 | raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__)) | |
41 | ||
42 | ||
43 | def _check_str(o): | |
44 | if not isinstance(o, str): | |
45 | raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__)) | |
46 | ||
47 | ||
48 | def _check_type(o, expected_type): | |
49 | if not isinstance(o, expected_type): | |
61d96b89 FD |
50 | raise TypeError( |
51 | "'{}' is not a '{}' object".format(o.__class__.__name__, expected_type) | |
52 | ) | |
81447b5b PP |
53 | |
54 | ||
dcd94213 | 55 | def _is_in_int64_range(v): |
61d96b89 FD |
56 | assert isinstance(v, int) |
57 | return v >= -(2 ** 63) and v <= (2 ** 63 - 1) | |
81447b5b PP |
58 | |
59 | ||
dcd94213 PP |
60 | def _is_int64(v): |
61 | if not isinstance(v, int): | |
62 | return False | |
63 | ||
64 | return _is_in_int64_range(v) | |
65 | ||
66 | ||
67 | def _is_in_uint64_range(v): | |
61d96b89 FD |
68 | assert isinstance(v, int) |
69 | return v >= 0 and v <= (2 ** 64 - 1) | |
81447b5b PP |
70 | |
71 | ||
dcd94213 PP |
72 | def _is_uint64(v): |
73 | if not isinstance(v, int): | |
74 | return False | |
75 | ||
76 | return _is_in_uint64_range(v) | |
77 | ||
78 | ||
81447b5b | 79 | def _check_int64(v, msg=None): |
dcd94213 PP |
80 | _check_int(v) |
81 | ||
82 | if not _is_in_int64_range(v): | |
81447b5b PP |
83 | if msg is None: |
84 | msg = 'expecting a signed 64-bit integral value' | |
85 | ||
86 | msg += ' (got {})'.format(v) | |
87 | raise ValueError(msg) | |
88 | ||
89 | ||
90 | def _check_uint64(v, msg=None): | |
dcd94213 PP |
91 | _check_int(v) |
92 | ||
93 | if not _is_in_uint64_range(v): | |
81447b5b PP |
94 | if msg is None: |
95 | msg = 'expecting an unsigned 64-bit integral value' | |
96 | ||
97 | msg += ' (got {})'.format(v) | |
98 | raise ValueError(msg) | |
99 | ||
100 | ||
101 | def _is_m1ull(v): | |
102 | return v == 18446744073709551615 | |
103 | ||
104 | ||
105 | def _is_pow2(v): | |
106 | return v != 0 and ((v & (v - 1)) == 0) | |
107 | ||
108 | ||
109 | def _check_alignment(a): | |
110 | _check_uint64(a) | |
111 | ||
81447b5b PP |
112 | if not _is_pow2(a): |
113 | raise ValueError('{} is not a power of two'.format(a)) | |
114 | ||
115 | ||
cc81b5ab PP |
116 | def _check_log_level(log_level): |
117 | _check_int(log_level) | |
118 | ||
119 | log_levels = ( | |
c9ecaa78 | 120 | bt2.logging.LoggingLevel.TRACE, |
cc81b5ab PP |
121 | bt2.logging.LoggingLevel.DEBUG, |
122 | bt2.logging.LoggingLevel.INFO, | |
e9d0e821 | 123 | bt2.logging.LoggingLevel.WARNING, |
cc81b5ab PP |
124 | bt2.logging.LoggingLevel.ERROR, |
125 | bt2.logging.LoggingLevel.FATAL, | |
126 | bt2.logging.LoggingLevel.NONE, | |
127 | ) | |
128 | ||
129 | if log_level not in log_levels: | |
130 | raise ValueError("'{}' is not a valid logging level".format(log_level)) | |
fb25b9e3 PP |
131 | |
132 | ||
133 | def _handle_func_status(status, msg=None): | |
134 | if status == native_bt.__BT_FUNC_STATUS_OK: | |
135 | # no error | |
136 | return | |
137 | ||
493428a0 | 138 | if status == native_bt.__BT_FUNC_STATUS_ERROR: |
3b2be708 | 139 | assert msg is not None |
614743a5 | 140 | raise bt2._Error(msg) |
493428a0 SM |
141 | elif status == native_bt.__BT_FUNC_STATUS_MEMORY_ERROR: |
142 | assert msg is not None | |
614743a5 | 143 | raise bt2._MemoryError(msg) |
fb25b9e3 PP |
144 | elif status == native_bt.__BT_FUNC_STATUS_END: |
145 | if msg is None: | |
146 | raise bt2.Stop | |
147 | else: | |
148 | raise bt2.Stop(msg) | |
149 | elif status == native_bt.__BT_FUNC_STATUS_AGAIN: | |
150 | if msg is None: | |
151 | raise bt2.TryAgain | |
152 | else: | |
153 | raise bt2.TryAgain(msg) | |
154 | elif status == native_bt.__BT_FUNC_STATUS_CANCELED: | |
155 | if msg is None: | |
156 | raise bt2.Canceled | |
157 | else: | |
158 | raise bt2.Canceled(msg) | |
fb25b9e3 PP |
159 | elif status == native_bt.__BT_FUNC_STATUS_OVERFLOW: |
160 | if msg is None: | |
161 | raise bt2.OverflowError | |
162 | else: | |
163 | raise bt2.OverflowError(msg) | |
164 | elif status == native_bt.__BT_FUNC_STATUS_INVALID_OBJECT: | |
165 | if msg is None: | |
166 | raise bt2.InvalidObject | |
167 | else: | |
168 | raise bt2.InvalidObject(msg) | |
169 | elif status == native_bt.__BT_FUNC_STATUS_INVALID_PARAMS: | |
170 | if msg is None: | |
171 | raise bt2.InvalidParams | |
172 | else: | |
173 | raise bt2.InvalidParams(msg) | |
174 | elif status == native_bt.__BT_FUNC_STATUS_UNSUPPORTED: | |
175 | if msg is None: | |
176 | raise bt2.Unsupported | |
177 | else: | |
178 | raise bt2.Unsupported(msg) | |
179 | else: | |
180 | assert False |