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