Move to kernel style SPDX license identifiers
[babeltrace.git] / src / bindings / python / bt2 / bt2 / event_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 field_class as bt2_field_class
7 from bt2 import value as bt2_value
8
9
10 def _bt2_stream_class():
11 from bt2 import stream_class as bt2_stream_class
12
13 return bt2_stream_class
14
15
16 class EventClassLogLevel:
17 EMERGENCY = native_bt.EVENT_CLASS_LOG_LEVEL_EMERGENCY
18 ALERT = native_bt.EVENT_CLASS_LOG_LEVEL_ALERT
19 CRITICAL = native_bt.EVENT_CLASS_LOG_LEVEL_CRITICAL
20 ERROR = native_bt.EVENT_CLASS_LOG_LEVEL_ERROR
21 WARNING = native_bt.EVENT_CLASS_LOG_LEVEL_WARNING
22 NOTICE = native_bt.EVENT_CLASS_LOG_LEVEL_NOTICE
23 INFO = native_bt.EVENT_CLASS_LOG_LEVEL_INFO
24 DEBUG_SYSTEM = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
25 DEBUG_PROGRAM = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
26 DEBUG_PROCESS = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
27 DEBUG_MODULE = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
28 DEBUG_UNIT = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
29 DEBUG_FUNCTION = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
30 DEBUG_LINE = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
31 DEBUG = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG
32
33
34 class _EventClassConst(object._SharedObject):
35 _get_ref = staticmethod(native_bt.event_class_get_ref)
36 _put_ref = staticmethod(native_bt.event_class_put_ref)
37 _borrow_stream_class_ptr = staticmethod(
38 native_bt.event_class_borrow_stream_class_const
39 )
40 _borrow_specific_context_field_class_ptr = staticmethod(
41 native_bt.event_class_borrow_specific_context_field_class_const
42 )
43 _borrow_payload_field_class_ptr = staticmethod(
44 native_bt.event_class_borrow_payload_field_class_const
45 )
46 _borrow_user_attributes_ptr = staticmethod(
47 native_bt.event_class_borrow_user_attributes_const
48 )
49 _create_field_class_from_ptr_and_get_ref = staticmethod(
50 bt2_field_class._create_field_class_from_const_ptr_and_get_ref
51 )
52 _create_value_from_ptr_and_get_ref = staticmethod(
53 bt2_value._create_from_const_ptr_and_get_ref
54 )
55 _stream_class_pycls = property(lambda s: _bt2_stream_class()._StreamClassConst)
56
57 @property
58 def stream_class(self):
59 sc_ptr = self._borrow_stream_class_ptr(self._ptr)
60
61 if sc_ptr is not None:
62 return self._stream_class_pycls._create_from_ptr_and_get_ref(sc_ptr)
63
64 @property
65 def user_attributes(self):
66 ptr = self._borrow_user_attributes_ptr(self._ptr)
67 assert ptr is not None
68 return self._create_value_from_ptr_and_get_ref(ptr)
69
70 @property
71 def name(self):
72 return native_bt.event_class_get_name(self._ptr)
73
74 @property
75 def id(self):
76 id = native_bt.event_class_get_id(self._ptr)
77 return id if id >= 0 else None
78
79 @property
80 def log_level(self):
81 is_available, log_level = native_bt.event_class_get_log_level(self._ptr)
82
83 if is_available != native_bt.PROPERTY_AVAILABILITY_AVAILABLE:
84 return None
85
86 return _EVENT_CLASS_LOG_LEVEL_TO_OBJ[log_level]
87
88 @property
89 def emf_uri(self):
90 return native_bt.event_class_get_emf_uri(self._ptr)
91
92 @property
93 def specific_context_field_class(self):
94 fc_ptr = self._borrow_specific_context_field_class_ptr(self._ptr)
95
96 if fc_ptr is None:
97 return
98
99 return self._create_field_class_from_ptr_and_get_ref(fc_ptr)
100
101 @property
102 def payload_field_class(self):
103 fc_ptr = self._borrow_payload_field_class_ptr(self._ptr)
104
105 if fc_ptr is None:
106 return
107
108 return self._create_field_class_from_ptr_and_get_ref(fc_ptr)
109
110
111 class _EventClass(_EventClassConst):
112 _borrow_stream_class_ptr = staticmethod(native_bt.event_class_borrow_stream_class)
113 _borrow_specific_context_field_class_ptr = staticmethod(
114 native_bt.event_class_borrow_specific_context_field_class
115 )
116 _borrow_payload_field_class_ptr = staticmethod(
117 native_bt.event_class_borrow_payload_field_class
118 )
119 _borrow_user_attributes_ptr = staticmethod(
120 native_bt.event_class_borrow_user_attributes
121 )
122 _create_field_class_from_ptr_and_get_ref = staticmethod(
123 bt2_field_class._create_field_class_from_ptr_and_get_ref
124 )
125 _create_value_from_ptr_and_get_ref = staticmethod(
126 bt2_value._create_from_ptr_and_get_ref
127 )
128 _stream_class_pycls = property(lambda s: _bt2_stream_class()._StreamClass)
129
130 def _user_attributes(self, user_attributes):
131 value = bt2_value.create_value(user_attributes)
132 native_bt.event_class_set_user_attributes(self._ptr, value._ptr)
133
134 _user_attributes = property(fset=_user_attributes)
135
136 def _name(self, name):
137 return native_bt.event_class_set_name(self._ptr, name)
138
139 _name = property(fset=_name)
140
141 def _log_level(self, log_level):
142 native_bt.event_class_set_log_level(self._ptr, log_level)
143
144 _log_level = property(fset=_log_level)
145
146 def _emf_uri(self, emf_uri):
147 status = native_bt.event_class_set_emf_uri(self._ptr, emf_uri)
148 utils._handle_func_status(status, "cannot set event class object's EMF URI")
149
150 _emf_uri = property(fset=_emf_uri)
151
152 def _specific_context_field_class(self, context_field_class):
153 status = native_bt.event_class_set_specific_context_field_class(
154 self._ptr, context_field_class._ptr
155 )
156 utils._handle_func_status(
157 status, "cannot set event class object's context field class"
158 )
159
160 _specific_context_field_class = property(fset=_specific_context_field_class)
161
162 def _payload_field_class(self, payload_field_class):
163 status = native_bt.event_class_set_payload_field_class(
164 self._ptr, payload_field_class._ptr
165 )
166 utils._handle_func_status(
167 status, "cannot set event class object's payload field class"
168 )
169
170 _payload_field_class = property(fset=_payload_field_class)
171
172 @staticmethod
173 def _validate_create_params(
174 name,
175 user_attributes,
176 log_level,
177 emf_uri,
178 specific_context_field_class,
179 payload_field_class,
180 ):
181 if name is not None:
182 utils._check_str(name)
183
184 if user_attributes is not None:
185 value = bt2_value.create_value(user_attributes)
186 utils._check_type(value, bt2_value.MapValue)
187
188 if log_level is not None:
189 log_levels = (
190 EventClassLogLevel.EMERGENCY,
191 EventClassLogLevel.ALERT,
192 EventClassLogLevel.CRITICAL,
193 EventClassLogLevel.ERROR,
194 EventClassLogLevel.WARNING,
195 EventClassLogLevel.NOTICE,
196 EventClassLogLevel.INFO,
197 EventClassLogLevel.DEBUG_SYSTEM,
198 EventClassLogLevel.DEBUG_PROGRAM,
199 EventClassLogLevel.DEBUG_PROCESS,
200 EventClassLogLevel.DEBUG_MODULE,
201 EventClassLogLevel.DEBUG_UNIT,
202 EventClassLogLevel.DEBUG_FUNCTION,
203 EventClassLogLevel.DEBUG_LINE,
204 EventClassLogLevel.DEBUG,
205 )
206
207 if log_level not in log_levels:
208 raise ValueError("'{}' is not a valid log level".format(log_level))
209
210 if emf_uri is not None:
211 utils._check_str(emf_uri)
212
213 if specific_context_field_class is not None:
214 utils._check_type(
215 specific_context_field_class, bt2_field_class._StructureFieldClass
216 )
217
218 if payload_field_class is not None:
219 utils._check_type(payload_field_class, bt2_field_class._StructureFieldClass)
220
221
222 _EVENT_CLASS_LOG_LEVEL_TO_OBJ = {
223 native_bt.EVENT_CLASS_LOG_LEVEL_EMERGENCY: EventClassLogLevel.EMERGENCY,
224 native_bt.EVENT_CLASS_LOG_LEVEL_ALERT: EventClassLogLevel.ALERT,
225 native_bt.EVENT_CLASS_LOG_LEVEL_CRITICAL: EventClassLogLevel.CRITICAL,
226 native_bt.EVENT_CLASS_LOG_LEVEL_ERROR: EventClassLogLevel.ERROR,
227 native_bt.EVENT_CLASS_LOG_LEVEL_WARNING: EventClassLogLevel.WARNING,
228 native_bt.EVENT_CLASS_LOG_LEVEL_NOTICE: EventClassLogLevel.NOTICE,
229 native_bt.EVENT_CLASS_LOG_LEVEL_INFO: EventClassLogLevel.INFO,
230 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: EventClassLogLevel.DEBUG_SYSTEM,
231 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: EventClassLogLevel.DEBUG_PROGRAM,
232 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: EventClassLogLevel.DEBUG_PROCESS,
233 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: EventClassLogLevel.DEBUG_MODULE,
234 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: EventClassLogLevel.DEBUG_UNIT,
235 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: EventClassLogLevel.DEBUG_FUNCTION,
236 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: EventClassLogLevel.DEBUG_LINE,
237 native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG: EventClassLogLevel.DEBUG,
238 }
This page took 0.037427 seconds and 4 git commands to generate.