| 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 | import babeltrace.reader_field_declaration as reader_field_declaration |
| 26 | |
| 27 | |
| 28 | class FieldError(Exception): |
| 29 | """ |
| 30 | Field error, raised when the value of a field cannot be accessed. |
| 31 | """ |
| 32 | |
| 33 | def __init__(self, value): |
| 34 | self.value = value |
| 35 | |
| 36 | def __str__(self): |
| 37 | return repr(self.value) |
| 38 | |
| 39 | |
| 40 | class _Definition: |
| 41 | def __init__(self, scope_id, field, name): |
| 42 | self._scope_id = scope_id |
| 43 | self._field = field |
| 44 | self._name = name |
| 45 | |
| 46 | @property |
| 47 | def name(self): |
| 48 | """Return the name of a field or None on error.""" |
| 49 | |
| 50 | return self._name |
| 51 | |
| 52 | @property |
| 53 | def type(self): |
| 54 | """Return the type of a field or -1 if unknown.""" |
| 55 | |
| 56 | try: |
| 57 | return _OBJ_TO_TYPE_ID[type(self._field)] |
| 58 | except KeyError: |
| 59 | return -1 |
| 60 | |
| 61 | @property |
| 62 | def declaration(self): |
| 63 | """Return the associated Definition object.""" |
| 64 | |
| 65 | return reader_field_declaration._create_field_declaration( |
| 66 | self._field.field_type, self._name, self.scope) |
| 67 | |
| 68 | @property |
| 69 | def value(self): |
| 70 | """ |
| 71 | Return the value associated with the field according to its type. |
| 72 | Return None on error. |
| 73 | """ |
| 74 | |
| 75 | try: |
| 76 | if type(self._field) in (bt2._ArrayField, bt2._SequenceField): |
| 77 | elem_ft = self._field.field_type.element_field_type |
| 78 | |
| 79 | if type(elem_ft) is bt2.IntegerFieldType: |
| 80 | if elem_ft.size == 8 and elem_ft.encoding != bt2.Encoding.NONE: |
| 81 | return bytes(x for x in self._field._value if x != 0).decode() |
| 82 | |
| 83 | return self._field._value |
| 84 | except bt2.Error: |
| 85 | return |
| 86 | |
| 87 | @property |
| 88 | def scope(self): |
| 89 | """Return the scope of a field or None on error.""" |
| 90 | |
| 91 | return self._scope_id |
| 92 | |
| 93 | |
| 94 | _OBJ_TO_TYPE_ID = { |
| 95 | bt2._IntegerField: common.CTFTypeId.INTEGER, |
| 96 | bt2._FloatingPointNumberField: common.CTFTypeId.FLOAT, |
| 97 | bt2._EnumerationField: common.CTFTypeId.ENUM, |
| 98 | bt2._StringField: common.CTFTypeId.STRING, |
| 99 | bt2._StructureField: common.CTFTypeId.STRUCT, |
| 100 | bt2._ArrayField: common.CTFTypeId.ARRAY, |
| 101 | bt2._SequenceField: common.CTFTypeId.SEQUENCE, |
| 102 | bt2._VariantField: common.CTFTypeId.VARIANT, |
| 103 | } |