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