Inspection
Where every field of a file is, and why one does not decode
Format.inspect decodes as much of some data as it can and reports
where each field was read. It does not raise for data that does not
decode: the error is part of the result.
from binal import Format
recording = Format("""
struct Recording {
byte_order little,
magic: bytes size 4 = b"REC1",
count: u8,
flags: Flags,
samples: ref(u32) Samples(count),
}
struct Flags { compressed: u1, level: u7 }
struct Samples(n: int) { points: [Point; n], crc: u32 = crc32(bytesof(points)) }
struct Point { x: i16, y: i16 }
""")
data = recording.encode(
"Recording",
{
"count": 2,
"flags": {"compressed": 1, "level": 5},
"samples": {"points": [{"x": 1, "y": 2}, {"x": -3, "y": 4}]},
},
)
print(recording.inspect("Recording", data + b"tail").layout) offset size field type value
00000000 4 magic bytes size 4 b"REC1"
00000004 1 count u8 2
00000005 1 flags Flags
00000005.0 1b flags.compressed u1 1
00000005.1 7b flags.level u7 5
00000006 4 samples ref(u32) Samples(count) -> <Samples at 10>
0000000a 8 <Samples at 10>.points [Point; n]
0000000a 2 <Samples at 10>.points[0].x i16 1
0000000c 2 <Samples at 10>.points[0].y i16 2
0000000e 2 <Samples at 10>.points[1].x i16 -3
00000010 2 <Samples at 10>.points[1].y i16 4
00000012 4 <Samples at 10>.crc u32 1253589359
00000016 4 (not described) Objects behind references are listed at their addresses, data in another address space (decompressed data, say) under its own heading, and bytes no field describes in their place. Offsets are hexadecimal, with the bit after a point for a field that does not fill whole bytes.
The same is available as values:
found = recording.inspect("Recording", data + b"tail")
found.gaps # ((0, 22, 26),): (space, start, end)
magic = found.fields[0]
magic.path, magic.type, magic.start_bit, magic.end_bit, magic.value
# ('magic', 'bytes size 4', 0, 32, b'REC1') At most Limits.max_collection_size fields are listed. Past that the
layout says that more follow, and gaps is empty.
Why data does not decode
explanation shows the field decoding stopped on, as the schema
declares it, and the bytes it was read from:
print(recording.inspect("Recording", data[:-1] + b"\xff").explanation) error: cannot decode <Samples at 10>.crc at byte 18: expected 1253589359, found 4290265455
--> <Samples at 10>.crc
bytes 18..22
|
| struct Samples {
| crc: u32 = crc32(bytesof(points)),
| }
|
| 00000010 04 00 6f 41 b8 ff
| ^^ ^^ ^^ ^^ error is the exception decode raises, and fields has everything
that was read before it.
A model’s format inspects the same way:
export_schema(Model).format().inspect(...).
The command line
| Command | Does |
|---|---|
binal inspect schema.binal Type file | Prints the layout, and the explanation if the file does not decode |
binal decode schema.binal Type file | Prints the value as JSON; --document keeps references |
binal encode schema.binal Type value.json -o file | Encodes JSON; --over original keeps bytes the schema does not describe |
binal check schema.binal | Reports schema errors, types, and portability |
binal fmt schema.binal | Prints the schema in canonical form |
binal json schema.binal | Prints the schema as portable JSON |
A type’s parameters are given with --arg, in order, as JSON. The
command does not need Python;
Installation
says how to get it.
Editors
editors/vscode
is an extension for Visual Studio Code: syntax highlighting for .binal
files, and their errors as you type. Other editors that read TextMate
grammars can use its grammar.