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 | from bt2 import native_bt, object, utils | |
32656995 | 24 | import bt2.field_class |
81447b5b | 25 | import collections.abc |
ae0bfae8 | 26 | import bt2.value |
81447b5b PP |
27 | import bt2.event |
28 | import copy | |
29 | import bt2 | |
30 | ||
31 | ||
f6a5e476 | 32 | class EventClassLogLevel: |
839d52a5 PP |
33 | EMERGENCY = native_bt.EVENT_CLASS_LOG_LEVEL_EMERGENCY |
34 | ALERT = native_bt.EVENT_CLASS_LOG_LEVEL_ALERT | |
35 | CRITICAL = native_bt.EVENT_CLASS_LOG_LEVEL_CRITICAL | |
36 | ERROR = native_bt.EVENT_CLASS_LOG_LEVEL_ERROR | |
37 | WARNING = native_bt.EVENT_CLASS_LOG_LEVEL_WARNING | |
38 | NOTICE = native_bt.EVENT_CLASS_LOG_LEVEL_NOTICE | |
39 | INFO = native_bt.EVENT_CLASS_LOG_LEVEL_INFO | |
40 | DEBUG_SYSTEM = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM | |
41 | DEBUG_PROGRAM = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM | |
42 | DEBUG_PROCESS = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS | |
43 | DEBUG_MODULE = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE | |
44 | DEBUG_UNIT = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT | |
45 | DEBUG_FUNCTION = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION | |
46 | DEBUG_LINE = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG_LINE | |
47 | DEBUG = native_bt.EVENT_CLASS_LOG_LEVEL_DEBUG | |
81447b5b PP |
48 | |
49 | ||
50 | class EventClass(object._Object): | |
f6a5e476 | 51 | def __init__(self, name, id=None, log_level=None, emf_uri=None, |
32656995 | 52 | context_field_class=None, payload_field_class=None): |
81447b5b | 53 | utils._check_str(name) |
839d52a5 | 54 | ptr = native_bt.event_class_create(name) |
81447b5b PP |
55 | |
56 | if ptr is None: | |
57 | raise bt2.CreationError('cannot create event class object') | |
58 | ||
59 | super().__init__(ptr) | |
60 | ||
61 | if id is not None: | |
62 | self.id = id | |
63 | ||
f6a5e476 PP |
64 | if log_level is not None: |
65 | self.log_level = log_level | |
66 | ||
67 | if emf_uri is not None: | |
68 | self.emf_uri = emf_uri | |
69 | ||
32656995 SM |
70 | if context_field_class is not None: |
71 | self.context_field_class = context_field_class | |
81447b5b | 72 | |
32656995 SM |
73 | if payload_field_class is not None: |
74 | self.payload_field_class = payload_field_class | |
81447b5b | 75 | |
81447b5b PP |
76 | @property |
77 | def stream_class(self): | |
839d52a5 | 78 | sc_ptr = native_bt.event_class_get_stream_class(self._ptr) |
81447b5b PP |
79 | |
80 | if sc_ptr is not None: | |
81 | return bt2.StreamClass._create_from_ptr(sc_ptr) | |
82 | ||
81447b5b PP |
83 | @property |
84 | def name(self): | |
839d52a5 | 85 | return native_bt.event_class_get_name(self._ptr) |
81447b5b PP |
86 | |
87 | @property | |
88 | def id(self): | |
839d52a5 | 89 | id = native_bt.event_class_get_id(self._ptr) |
f6a5e476 | 90 | return id if id >= 0 else None |
81447b5b PP |
91 | |
92 | @id.setter | |
93 | def id(self, id): | |
94 | utils._check_int64(id) | |
839d52a5 | 95 | ret = native_bt.event_class_set_id(self._ptr, id) |
81447b5b PP |
96 | utils._handle_ret(ret, "cannot set event class object's ID") |
97 | ||
f6a5e476 PP |
98 | @property |
99 | def log_level(self): | |
839d52a5 | 100 | log_level = native_bt.event_class_get_log_level(self._ptr) |
f6a5e476 PP |
101 | return log_level if log_level >= 0 else None |
102 | ||
103 | @log_level.setter | |
104 | def log_level(self, log_level): | |
105 | log_levels = ( | |
106 | EventClassLogLevel.UNSPECIFIED, | |
107 | EventClassLogLevel.EMERGENCY, | |
108 | EventClassLogLevel.ALERT, | |
109 | EventClassLogLevel.CRITICAL, | |
110 | EventClassLogLevel.ERROR, | |
111 | EventClassLogLevel.WARNING, | |
112 | EventClassLogLevel.NOTICE, | |
113 | EventClassLogLevel.INFO, | |
114 | EventClassLogLevel.DEBUG_SYSTEM, | |
115 | EventClassLogLevel.DEBUG_PROGRAM, | |
116 | EventClassLogLevel.DEBUG_PROCESS, | |
117 | EventClassLogLevel.DEBUG_MODULE, | |
118 | EventClassLogLevel.DEBUG_UNIT, | |
119 | EventClassLogLevel.DEBUG_FUNCTION, | |
120 | EventClassLogLevel.DEBUG_LINE, | |
121 | EventClassLogLevel.DEBUG, | |
122 | ) | |
123 | ||
124 | if log_level not in log_levels: | |
125 | raise ValueError("'{}' is not a valid log level".format(log_level)) | |
126 | ||
839d52a5 | 127 | ret = native_bt.event_class_set_log_level(self._ptr, log_level) |
f6a5e476 PP |
128 | utils._handle_ret(ret, "cannot set event class object's log level") |
129 | ||
130 | @property | |
131 | def emf_uri(self): | |
839d52a5 | 132 | return native_bt.event_class_get_emf_uri(self._ptr) |
f6a5e476 PP |
133 | |
134 | @emf_uri.setter | |
135 | def emf_uri(self, emf_uri): | |
136 | utils._check_str(emf_uri) | |
839d52a5 | 137 | ret = native_bt.event_class_set_emf_uri(self._ptr, emf_uri) |
f6a5e476 PP |
138 | utils._handle_ret(ret, "cannot set event class object's EMF URI") |
139 | ||
81447b5b | 140 | @property |
32656995 SM |
141 | def context_field_class(self): |
142 | fc_ptr = native_bt.event_class_get_context_type(self._ptr) | |
81447b5b | 143 | |
32656995 | 144 | if fc_ptr is None: |
81447b5b PP |
145 | return |
146 | ||
32656995 | 147 | return bt2.field_class._create_from_ptr(fc_ptr) |
81447b5b | 148 | |
32656995 SM |
149 | @context_field_class.setter |
150 | def context_field_class(self, context_field_class): | |
151 | context_field_class_ptr = None | |
81447b5b | 152 | |
32656995 SM |
153 | if context_field_class is not None: |
154 | utils._check_type(context_field_class, bt2.field_class._FieldClass) | |
155 | context_field_class_ptr = context_field_class._ptr | |
81447b5b | 156 | |
32656995 SM |
157 | ret = native_bt.event_class_set_context_type(self._ptr, context_field_class_ptr) |
158 | utils._handle_ret(ret, "cannot set event class object's context field class") | |
81447b5b PP |
159 | |
160 | @property | |
32656995 SM |
161 | def payload_field_class(self): |
162 | fc_ptr = native_bt.event_class_get_payload_type(self._ptr) | |
81447b5b | 163 | |
32656995 | 164 | if fc_ptr is None: |
81447b5b PP |
165 | return |
166 | ||
32656995 | 167 | return bt2.field_class._create_from_ptr(fc_ptr) |
81447b5b | 168 | |
32656995 SM |
169 | @payload_field_class.setter |
170 | def payload_field_class(self, payload_field_class): | |
171 | payload_field_class_ptr = None | |
81447b5b | 172 | |
32656995 SM |
173 | if payload_field_class is not None: |
174 | utils._check_type(payload_field_class, bt2.field_class._FieldClass) | |
175 | payload_field_class_ptr = payload_field_class._ptr | |
81447b5b | 176 | |
32656995 SM |
177 | ret = native_bt.event_class_set_payload_type(self._ptr, payload_field_class_ptr) |
178 | utils._handle_ret(ret, "cannot set event class object's payload field class") | |
81447b5b PP |
179 | |
180 | def __call__(self): | |
839d52a5 | 181 | event_ptr = native_bt.event_create(self._ptr) |
81447b5b PP |
182 | |
183 | if event_ptr is None: | |
184 | raise bt2.CreationError('cannot create event field object') | |
185 | ||
186 | return bt2.event._create_from_ptr(event_ptr) | |
187 | ||
188 | def __eq__(self, other): | |
189 | if type(other) is not type(self): | |
190 | return False | |
191 | ||
192 | if self.addr == other.addr: | |
193 | return True | |
194 | ||
81447b5b | 195 | self_props = ( |
81447b5b PP |
196 | self.name, |
197 | self.id, | |
f6a5e476 PP |
198 | self.log_level, |
199 | self.emf_uri, | |
32656995 SM |
200 | self.context_field_class, |
201 | self.payload_field_class | |
81447b5b PP |
202 | ) |
203 | other_props = ( | |
81447b5b PP |
204 | other.name, |
205 | other.id, | |
f6a5e476 PP |
206 | other.log_level, |
207 | other.emf_uri, | |
32656995 SM |
208 | other.context_field_class, |
209 | other.payload_field_class | |
81447b5b PP |
210 | ) |
211 | return self_props == other_props | |
212 | ||
32656995 | 213 | def _copy(self, fc_copy_func): |
81447b5b PP |
214 | cpy = EventClass(self.name) |
215 | cpy.id = self.id | |
216 | ||
f6a5e476 PP |
217 | if self.log_level is not None: |
218 | cpy.log_level = self.log_level | |
219 | ||
220 | if self.emf_uri is not None: | |
221 | cpy.emf_uri = self.emf_uri | |
81447b5b | 222 | |
32656995 SM |
223 | cpy.context_field_class = fc_copy_func(self.context_field_class) |
224 | cpy.payload_field_class = fc_copy_func(self.payload_field_class) | |
81447b5b PP |
225 | return cpy |
226 | ||
227 | def __copy__(self): | |
32656995 | 228 | return self._copy(lambda fc: fc) |
81447b5b PP |
229 | |
230 | def __deepcopy__(self, memo): | |
231 | cpy = self._copy(copy.deepcopy) | |
232 | memo[id(self)] = cpy | |
233 | return cpy |