gtirb.serialization module¶
- class gtirb.serialization.BoolCodec[source]¶
Bases:
Codec
A Codec for bool.
- static decode(raw_bytes, *, serialization=None, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
bool
- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, val, *, serialization=None, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.Codec[source]¶
Bases:
object
The base class for codecs.
- static decode(raw_bytes, *, serialization, subtypes, get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
gtirb.serialization.Serialization
) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
object
- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, item, *, serialization, subtypes)[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item (
object
) – The arbitrary Python object to encode.serialization (
gtirb.serialization.Serialization
) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- exception gtirb.serialization.DecodeError[source]¶
Bases:
CodecError
An exception during decoding.
- exception gtirb.serialization.EncodeError[source]¶
Bases:
CodecError
An exception during encoding.
- class gtirb.serialization.Float32Codec[source]¶
Bases:
FloatCodec
-
bytesize:
int
= 4¶
-
struct_format:
str
= '<f'¶
-
typname:
str
= 'float'¶
-
bytesize:
- class gtirb.serialization.Float64Codec[source]¶
Bases:
FloatCodec
-
bytesize:
int
= 8¶
-
struct_format:
str
= '<d'¶
-
typname:
str
= 'double'¶
-
bytesize:
- class gtirb.serialization.FloatCodec[source]¶
Bases:
Codec
Generic base class for float-based Codecs
-
bytesize:
int
¶
- classmethod decode(raw_bytes, *, serialization=None, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
float
- Returns:
A new Python object, as decoded from
raw_bytes
.
- classmethod encode(out, val, *, serialization=None, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
-
struct_format:
str
¶
-
typname:
str
¶
-
bytesize:
- class gtirb.serialization.Int16Codec[source]¶
Bases:
IntegerCodec
A Codec for 16-bit signed integers.
-
bytesize:
int
= 2¶
-
signed:
bool
= True¶
-
typname:
str
= 'int16_t'¶
-
bytesize:
- class gtirb.serialization.Int32Codec[source]¶
Bases:
IntegerCodec
A Codec for 32-bit signed integers.
-
bytesize:
int
= 4¶
-
signed:
bool
= True¶
-
typname:
str
= 'int32_t'¶
-
bytesize:
- class gtirb.serialization.Int64Codec[source]¶
Bases:
IntegerCodec
A Codec for 64-bit signed integers.
-
bytesize:
int
= 8¶
-
signed:
bool
= True¶
-
typname:
str
= 'int64_t'¶
-
bytesize:
- class gtirb.serialization.Int8Codec[source]¶
Bases:
IntegerCodec
A Codec for 8-bit signed integers.
-
bytesize:
int
= 1¶
-
signed:
bool
= True¶
-
typname:
str
= 'int8_t'¶
-
bytesize:
- class gtirb.serialization.IntegerCodec[source]¶
Bases:
Codec
Generic base class for integer-based Codecs
-
bytesize:
int
¶
- classmethod decode(raw_bytes, *, serialization=None, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
int
- Returns:
A new Python object, as decoded from
raw_bytes
.
- classmethod encode(out, val, *, serialization=None, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
-
signed:
bool
¶
-
typname:
str
¶
-
bytesize:
- class gtirb.serialization.MappingCodec[source]¶
Bases:
Codec
A Codec for mapping<K,V> entries. Implemented via
dict
.- static decode(raw_bytes, *, serialization, subtypes, get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
gtirb.serialization.Serialization
) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
typing.Mapping
[object
,object
]- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, mapping, *, serialization, subtypes)[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
gtirb.serialization.Serialization
) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.OffsetCodec[source]¶
Bases:
Codec
A Codec for
gtirb.Offset
objects, containing a UUID and a displacement.- static decode(raw_bytes, *, serialization=None, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, val, *, serialization=None, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.SequenceCodec[source]¶
Bases:
Codec
A Codec for sequence<T> entries. Implemented via
list
.- static decode(raw_bytes, *, serialization, subtypes, get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
gtirb.serialization.Serialization
) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
typing.Sequence
[object
]- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, sequence, *, serialization, subtypes)[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
gtirb.serialization.Serialization
) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.Serialization[source]¶
Bases:
object
Manages codecs used to serialize and deserialize GTIRB objects.
The
gtirb.Serialization.decode()
method ofgtirb.AuxData.serializer
is called when GTIRB AuxData is loaded viagtirb.IR.load_protobuf()
, and thegtirb.Serialization.encode()
method ofgtirb.AuxData.serializer
is called when GTIRB AuxData is saved to file viagtirb.IR.save_protobuf()
. You can alter the encoding and decoding of AuxData values viagtirb.Serialization.codecs
. To do this, create a new subclass ofgtirb.serialization.Codec
and add it togtirb.Serialization.codecs
:>>> gtirb.AuxData.serializer.codecs['my_custom_type'] = MyCustomCodec
This example registers a new type name,
my_custom_type
, and associate it with a new codec,MyCustomCodec
.- Variables:
~.codecs – A mapping of type names to codecs. Codecs can be added or overridden using this dictionary.
- decode(raw_bytes, type_name, get_by_uuid=None)[source]¶
Decode a
gtirb.AuxData
of the specified type from the specified byte stream.- Parameters:
raw_bytes (
typing.Union
[bytes
,bytearray
,memoryview
,typing.BinaryIO
]) – The byte stream from which to read the encoded value.type_name (
str
) – The type name of the object encoded byraw_bytes
.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
object
- Returns:
The object encoded by
raw_bytes
.
- encode(out, val, type_name)[source]¶
Encodes the value of an AuxData value to bytes.
- Parameters:
out (
typing.BinaryIO
) – A binary stream to write bytes to.val (
object
) – Thegtirb.AuxData
to encode.type_name (
str
) – The type name of the value encapsulated by thegtirb.AuxData
.
- Return type:
None
- class gtirb.serialization.SetCodec[source]¶
Bases:
Codec
A Codec for set<T> entries. Implemented via
set
.- static decode(raw_bytes, *, serialization, subtypes, get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
gtirb.serialization.Serialization
) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
typing.Set
[object
]- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, items, *, serialization, subtypes)[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
gtirb.serialization.Serialization
) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.StringCodec[source]¶
Bases:
Codec
A Codec for strings.
- static decode(raw_bytes, *, serialization=None, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
str
- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, val, *, serialization=None, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.SubtypeTree(name, subtypes)[source]¶
Bases:
object
A type hint representing a parsed serialization type name. A
SubtypeTree
is has two items: Astr
giving the name of the type and a Sequence of type parameters (which are alsoSubtypeTree
s). For example, the following are all validSubtypeTree
s:>>> SubtypeTree('string', ()) >>> SubtypeTree('sequence', (SubtypeTree('UUID',()),)) >>> SubtypeTree( 'mapping', ( SubtypeTree('string', ()), SubtypeTree('set', (SubtypeTree('UUID', ()),)) ) )
- __hash__ = None¶
- class gtirb.serialization.TupleCodec[source]¶
Bases:
Codec
A Codec for tuple<…> entries. Implemented via
tuple
.- static decode(raw_bytes, *, serialization, subtypes, get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
gtirb.serialization.Serialization
) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
typing.Tuple
[object
,...
]- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, items, *, serialization, subtypes)[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
gtirb.serialization.Serialization
) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- exception gtirb.serialization.TypeNameError(hint)[source]¶
Bases:
EncodeError
A type name is malformed.
- class gtirb.serialization.UUIDCodec[source]¶
Bases:
Codec
A Codec for raw UUIDs or Nodes.
Decoding a UUID first checks the Node cache for an object with the corresponding UUID, and either returns the object it hits or a new raw UUID.
- static decode(raw_bytes, *, serialization=None, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
typing.Union
[uuid.UUID
,gtirb.node.Node
]- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, val, *, serialization=None, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
typing.Optional
[gtirb.serialization.Serialization
]) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None
- class gtirb.serialization.Uint16Codec[source]¶
Bases:
IntegerCodec
A Codec for 16-bit unsigned integers.
-
bytesize:
int
= 2¶
-
signed:
bool
= False¶
-
typname:
str
= 'uint16_t'¶
-
bytesize:
- class gtirb.serialization.Uint32Codec[source]¶
Bases:
IntegerCodec
A Codec for 32-bit unsigned integers.
-
bytesize:
int
= 4¶
-
signed:
bool
= False¶
-
typname:
str
= 'uint32_t'¶
-
bytesize:
- class gtirb.serialization.Uint64Codec[source]¶
Bases:
IntegerCodec
A Codec for 64-bit unsigned integers.
-
bytesize:
int
= 8¶
-
signed:
bool
= False¶
-
typname:
str
= 'uint64_t'¶
-
bytesize:
- class gtirb.serialization.Uint8Codec[source]¶
Bases:
IntegerCodec
A Codec for 8-bit unsigned integers.
-
bytesize:
int
= 1¶
-
signed:
bool
= False¶
-
typname:
str
= 'uint8_t'¶
-
bytesize:
- exception gtirb.serialization.UnknownCodecError(name)[source]¶
Bases:
CodecError
An unknown codec name is encountered. Caught and handled by the top-level codec methods.
- Parameters:
name (
str
) – the name of the unknown codec
- class gtirb.serialization.UnknownData[source]¶
Bases:
bytes
This class is a blob of bytes representing data with an unknown type. Generated by
gtirb.Serialization.decode()
when it encounters the name of an unknown codec. Use only at the top level of an auxdata.
- class gtirb.serialization.VariantCodec[source]¶
Bases:
Codec
A Codec for variant<Ts…> entries.
An encoded record containg two part: index - position of member of variant’s list value - encoded values of selected member
- static decode(raw_bytes, *, serialization, subtypes=(), get_by_uuid=None)[source]¶
Decode the specified raw data into a Python object.
- Parameters:
raw_bytes (
typing.BinaryIO
) – The BytesIO object to be decoded.serialization (
gtirb.serialization.Serialization
) – A Serialization instance used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.get_by_uuid (
typing.Optional
[typing.Callable
[[uuid.UUID
],typing.Optional
[gtirb.node.Node
]]]) – A function to look up nodes by UUID.
- Return type:
- Returns:
A new Python object, as decoded from
raw_bytes
.
- static encode(out, variant, *, serialization, subtypes=())[source]¶
Encode an item, writing the serialized object to
out
.- Parameters:
out (
typing.BinaryIO
) – A binary stream to serialize to.item – The arbitrary Python object to encode.
serialization (
gtirb.serialization.Serialization
) – A Serialization instance, used to invoke other codecs if needed.subtypes (
typing.Sequence
[gtirb.serialization.SubtypeTree
]) – The parsed type of this object.
- Return type:
None