Layout

source: bluebase/core/layout.py


dataclassclass LayoutField(name: str, format: str)[source]

Bases: object

A field in a byte-level layout.

Variables:
  • name (str) – the name of the field

  • format (str) – the format of the field used by the struct module, excluding the endian prefix

  • size (int) – the size of the field, automatically calculated from its format

class LayoutMixin(buffer: bytearray)[source]

Bases: ABC

An abstract base class for defining byte-level layouts.

It holds a buffer internally and manages its layout according to the configured fields.

Variables:

buffer (bytearray) – the underlying buffer organized according to the layout

Raises:

TypeError – if fields is not properly defined

ENDIAN: ClassVar[str] = '<'

The endian format used by the struct module.

size: ClassVar[int]

The total size of the layout, automatically calculated from fields.

lookup: ClassVar[dict[str, tuple[int, LayoutField]]]

A mapping from each field name to its offset and field, generated automatically.

fields: ClassVar[tuple[LayoutField, ...]]

The fields that define the layout.

get_format(field: LayoutField) → str[source]

Return the format of a field, including the endian prefix.

get(name: str) → Any[source]

Return the value of the field with the given name from the buffer.

set(name: str, value: Any) → None[source]

Set the value of the field with the given name in the buffer.