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