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