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