Commit | Line | Data |
---|---|---|
00f7eedc JG |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2013-2017 Jérémie Galarneau <jeremie.galarneau@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 | import bt2 | |
24 | import babeltrace.common as common | |
25 | ||
26 | def _create_field_declaration(field_type, name, scope): | |
27 | try: | |
28 | if type(field_type) == bt2.IntegerFieldType: | |
29 | declaration = IntegerFieldDeclaration.__new__( | |
30 | IntegerFieldDeclaration) | |
31 | elif type(field_type) == bt2.EnumerationFieldType: | |
32 | declaration = EnumerationFieldDeclaration.__new__( | |
33 | EnumerationFieldDeclaration) | |
34 | elif type(field_type) == bt2.ArrayFieldType: | |
35 | declaration = ArrayFieldDeclaration.__new__( | |
36 | ArrayFieldDeclaration) | |
37 | elif type(field_type) == bt2.SequenceFieldType: | |
38 | declaration = SequenceFieldDeclaration.__new__( | |
39 | SequenceFieldDeclaration) | |
40 | elif type(field_type) == bt2.FloatingPointNumberFieldType: | |
41 | declaration = FloatFieldDeclaration.__new__( | |
42 | FloatFieldDeclaration) | |
43 | elif type(field_type) == bt2.StructureFieldType: | |
44 | declaration = StructureFieldDeclaration.__new__( | |
45 | StructureFieldDeclaration) | |
46 | elif type(field_type) == bt2.StringFieldType: | |
47 | declaration = StringFieldDeclaration.__new__( | |
48 | StringFieldDeclaration) | |
49 | elif type(field_type) == bt2.VariantFieldType: | |
50 | declaration = VariantFieldDeclaration.__new__( | |
51 | VariantFieldDeclaration) | |
52 | else: | |
53 | return | |
54 | except bt2.Error: | |
55 | return | |
56 | ||
57 | declaration._field_type = field_type | |
58 | declaration._name = name | |
59 | declaration._scope = scope | |
60 | return declaration | |
61 | ||
62 | ||
63 | class FieldDeclaration: | |
64 | """ | |
65 | Base class for concrete field declarations. | |
66 | ||
67 | This class is not meant to be instantiated by the user. | |
68 | """ | |
69 | ||
70 | def __init__(self): | |
71 | raise NotImplementedError("FieldDeclaration cannot be instantiated") | |
72 | ||
73 | def __repr__(self): | |
74 | return "({0}) {1} {2}".format(common.CTFScope.scope_name(self.scope), | |
75 | common.CTFTypeId.type_name(self.type), | |
76 | self.name) | |
77 | ||
78 | @property | |
79 | def name(self): | |
80 | """ | |
81 | Field name, or ``None`` on error. | |
82 | """ | |
83 | ||
84 | return self._name | |
85 | ||
86 | @property | |
87 | def type(self): | |
88 | """ | |
89 | Field type (one of :class:`babeltrace.common.CTFTypeId` | |
90 | constants). | |
91 | """ | |
92 | ||
93 | return _OBJ_TO_TYPE_ID[type(self)] | |
94 | ||
95 | @property | |
96 | def scope(self): | |
97 | """ | |
98 | Field scope (one of:class:`babeltrace.common.CTFScope` | |
99 | constants). | |
100 | """ | |
101 | ||
102 | return self._scope | |
103 | ||
104 | ||
105 | class IntegerFieldDeclaration(FieldDeclaration): | |
106 | """ | |
107 | Integer field declaration. | |
108 | """ | |
109 | ||
110 | def __init__(self): | |
111 | raise NotImplementedError("IntegerFieldDeclaration cannot be instantiated") | |
112 | ||
113 | @property | |
114 | def signedness(self): | |
115 | """ | |
116 | 0 if this integer is unsigned, 1 if signed, or -1 on error. | |
117 | """ | |
118 | ||
119 | try: | |
120 | if self._field_type.is_signed: | |
121 | return 1 | |
122 | else: | |
123 | return 0 | |
124 | except bt2.Error: | |
125 | return -1 | |
126 | ||
127 | @property | |
128 | def base(self): | |
129 | """ | |
130 | Integer base (:class:`int`), or a negative value on error. | |
131 | """ | |
132 | ||
133 | try: | |
134 | return self._field_type.base | |
135 | except AssertionError: | |
136 | return -1 | |
137 | ||
138 | @property | |
139 | def byte_order(self): | |
140 | """ | |
141 | Integer byte order (one of | |
142 | :class:`babeltrace.common.ByteOrder` constants). | |
143 | """ | |
144 | ||
145 | try: | |
146 | byte_order = self._field_type.byte_order | |
147 | except AssertionError: | |
148 | return common.ByteOrder.BYTE_ORDER_UNKNOWN | |
149 | ||
150 | try: | |
151 | return _BT2_BYTE_ORDER_TO_BYTE_ORDER[byte_order] | |
152 | except KeyError: | |
153 | return common.ByteOrder.BYTE_ORDER_UNKNOWN | |
154 | ||
155 | @property | |
156 | def size(self): | |
157 | """ | |
158 | Integer size in bits, or a negative value on error. | |
159 | """ | |
160 | ||
161 | try: | |
162 | return self._field_type.size | |
163 | except AssertionError: | |
164 | return -1 | |
165 | ||
166 | @property | |
167 | def length(self): | |
168 | """ | |
169 | Integer size in bits, or a negative value on error. | |
170 | """ | |
171 | ||
172 | return self.size | |
173 | ||
174 | @property | |
175 | def encoding(self): | |
176 | """ | |
177 | Integer encoding (one of | |
178 | :class:`babeltrace.common.CTFStringEncoding` constants). | |
179 | """ | |
180 | ||
181 | try: | |
182 | encoding = self._field_type.encoding | |
183 | except bt2.Error: | |
184 | return common.CTFStringEncoding.UNKNOWN | |
185 | ||
186 | try: | |
187 | return _BT2_ENCODING_TO_ENCODING[encoding] | |
188 | except KeyError: | |
189 | return common.CTFStringEncoding.UNKNOWN | |
190 | ||
191 | ||
192 | class EnumerationFieldDeclaration(FieldDeclaration): | |
193 | """ | |
194 | Enumeration field declaration. | |
195 | ||
196 | .. note:: | |
197 | ||
198 | As of this version, this class is missing some properties. | |
199 | """ | |
200 | ||
201 | def __init__(self): | |
202 | raise NotImplementedError("EnumerationFieldDeclaration cannot be instantiated") | |
203 | ||
204 | ||
205 | class ArrayFieldDeclaration(FieldDeclaration): | |
206 | """ | |
207 | Static array field declaration. | |
208 | """ | |
209 | ||
210 | def __init__(self): | |
211 | raise NotImplementedError("ArrayFieldDeclaration cannot be instantiated") | |
212 | ||
213 | @property | |
214 | def length(self): | |
215 | """ | |
216 | Fixed length of this static array (number of contained | |
217 | elements), or a negative value on error. | |
218 | """ | |
219 | ||
220 | try: | |
221 | return self._field_type.length | |
222 | except AssertionError: | |
223 | return -1 | |
224 | ||
225 | @property | |
226 | def element_declaration(self): | |
227 | """ | |
228 | Field declaration of the underlying element. | |
229 | """ | |
230 | ||
231 | try: | |
232 | return _create_field_declaration( | |
233 | self._field_type.element_field_type, name=None, | |
234 | scope=self._scope) | |
235 | except bt2.Error: | |
236 | return | |
237 | ||
238 | ||
239 | class SequenceFieldDeclaration(FieldDeclaration): | |
240 | """ | |
241 | Sequence (dynamic array) field declaration. | |
242 | ||
243 | .. note:: | |
244 | ||
245 | As of this version, this class is missing some properties. | |
246 | """ | |
247 | ||
248 | def __init__(self): | |
249 | raise NotImplementedError("SequenceFieldDeclaration cannot be instantiated") | |
250 | ||
251 | @property | |
252 | def element_declaration(self): | |
253 | """ | |
254 | Field declaration of the underlying element. | |
255 | """ | |
256 | ||
257 | try: | |
258 | return _create_field_declaration( | |
259 | self._field_type.element_field_type, name=None, | |
260 | scope=self._scope) | |
261 | except bt2.Error: | |
262 | return | |
263 | ||
264 | ||
265 | class FloatFieldDeclaration(FieldDeclaration): | |
266 | """ | |
267 | Floating point number field declaration. | |
268 | ||
269 | .. note:: | |
270 | ||
271 | As of this version, this class is missing some properties. | |
272 | """ | |
273 | ||
274 | def __init__(self): | |
275 | raise NotImplementedError("FloatFieldDeclaration cannot be instantiated") | |
276 | ||
277 | ||
278 | class StructureFieldDeclaration(FieldDeclaration): | |
279 | """ | |
280 | Structure (ordered map of field names to field declarations) field | |
281 | declaration. | |
282 | ||
283 | .. note:: | |
284 | ||
285 | As of this version, this class is missing some properties. | |
286 | """ | |
287 | ||
288 | def __init__(self): | |
289 | raise NotImplementedError("StructureFieldDeclaration cannot be instantiated") | |
290 | ||
291 | ||
292 | class StringFieldDeclaration(FieldDeclaration): | |
293 | """ | |
294 | String (NULL-terminated array of bytes) field declaration. | |
295 | ||
296 | .. note:: | |
297 | ||
298 | As of this version, this class is missing some properties. | |
299 | """ | |
300 | ||
301 | def __init__(self): | |
302 | raise NotImplementedError("StringFieldDeclaration cannot be instantiated") | |
303 | ||
304 | ||
305 | class VariantFieldDeclaration(FieldDeclaration): | |
306 | """ | |
307 | Variant (dynamic selection between different types) field declaration. | |
308 | ||
309 | .. note:: | |
310 | ||
311 | As of this version, this class is missing some properties. | |
312 | """ | |
313 | ||
314 | def __init__(self): | |
315 | raise NotImplementedError("VariantFieldDeclaration cannot be instantiated") | |
316 | ||
317 | ||
318 | _OBJ_TO_TYPE_ID = { | |
319 | IntegerFieldDeclaration: common.CTFTypeId.INTEGER, | |
320 | FloatFieldDeclaration: common.CTFTypeId.FLOAT, | |
321 | EnumerationFieldDeclaration: common.CTFTypeId.ENUM, | |
322 | StringFieldDeclaration: common.CTFTypeId.STRING, | |
323 | StructureFieldDeclaration: common.CTFTypeId.STRUCT, | |
324 | ArrayFieldDeclaration: common.CTFTypeId.ARRAY, | |
325 | SequenceFieldDeclaration: common.CTFTypeId.SEQUENCE, | |
326 | VariantFieldDeclaration: common.CTFTypeId.VARIANT, | |
327 | } | |
328 | ||
329 | _BT2_BYTE_ORDER_TO_BYTE_ORDER = { | |
330 | bt2.ByteOrder.NATIVE: common.ByteOrder.BYTE_ORDER_NATIVE, | |
331 | bt2.ByteOrder.LITTLE_ENDIAN: common.ByteOrder.BYTE_ORDER_LITTLE_ENDIAN, | |
332 | bt2.ByteOrder.BIG_ENDIAN: common.ByteOrder.BYTE_ORDER_BIG_ENDIAN, | |
333 | bt2.ByteOrder.NETWORK: common.ByteOrder.BYTE_ORDER_NETWORK, | |
334 | } | |
335 | ||
336 | _BT2_ENCODING_TO_ENCODING = { | |
337 | bt2.Encoding.NONE: common.CTFStringEncoding.NONE, | |
338 | bt2.Encoding.ASCII: common.CTFStringEncoding.ASCII, | |
339 | bt2.Encoding.UTF8: common.CTFStringEncoding.UTF8, | |
340 | } |