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