1 # The MIT License (MIT)
3 # Copyright (c) 2013-2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
24 import babeltrace
.common
as common
25 import babeltrace
.reader_field_declaration
as reader_field_declaration
27 class FieldError(Exception):
29 Field error, raised when the value of a field cannot be accessed.
32 def __init__(self
, value
):
36 return repr(self
.value
)
39 def __init__(self
, scope_id
, field
, name
):
40 self
._scope
_id
= scope_id
46 """Return the name of a field or None on error."""
52 """Return the type of a field or -1 if unknown."""
55 return _OBJ_TO_TYPE_ID
[type(self
._field
)]
60 def declaration(self
):
61 """Return the associated Definition object."""
63 return reader_field_declaration
._create
_field
_declaration
(
64 self
._field
.field_type
, self
._name
, self
.scope
)
69 Return the value associated with the field according to its type.
74 if type(self
._field
) in (bt2
._ArrayField
, bt2
._SequenceField
):
75 elem_ft
= self
._field
.field_type
.element_field_type
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()
81 return self
._field
._value
87 """Return the scope of a field or None on error."""
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
,