Kaitai Struct Python Runtime

0.11 · active · verified Thu Apr 09

Kaitai Struct provides a declarative parsing framework for binary data. This library serves as the Python runtime component, allowing users to load and interpret binary files based on `.ksy` specifications compiled into Python classes. The current version is 0.11, and releases are generally stable, focusing on compatibility with the Kaitai Struct compiler.

Warnings

Install

Imports

Quickstart

To use `kaitaistruct`, you first need to define your binary format in a `.ksy` (Kaitai Struct YAML) file. Then, use the **Kaitai Struct Compiler (ksc)** (a separate, Java-based tool available from `kaitai.io`) to generate the Python parsing code. The `ksc` command `ksc --target python your_spec.ksy` will produce `your_spec.py`. Once generated, you can import and use the classes from this file to parse your binary data. The example below *simulates* a generated class for demonstration purposes, assuming a `test.ksy` parsing a little-endian 4-byte integer.

import os

# Assuming 'test.py' was generated from 'test.ksy' by the ksc compiler.
# The 'test.ksy' and 'test.bin' files would typically be created externally.
# For this runnable example, we'll create them in a temporary manner.

# 1. Create a dummy binary file to parse
binary_data = b'\x01\x02\x03\x04'
with open('test.bin', 'wb') as f:
    f.write(binary_data)
print(f"Created test.bin with data: {binary_data.hex()}")

# 2. Simulate the presence of a generated parser class 'Test'
# In a real scenario, this would come from `ksc --target python test.ksy`
# and you'd simply `from test import Test`.
# For this example, we mock the class directly.

from kaitaistruct import KaitaiStream, KaitaiStruct

class Test(KaitaiStruct):
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._read()

    def _read(self):
        self.my_field = self._io.read_u4le()

    @property
    def get_my_field(self):
        return self.my_field

# 3. Use the generated (or mocked) parser to read the binary data
with open('test.bin', 'rb') as f_obj:
    io_stream = KaitaiStream(f_obj)
    parsed_data = Test(io_stream)
    print(f"Parsed value: {parsed_data.my_field} (0x{parsed_data.my_field:x})")

# Clean up generated/dummy files
os.remove('test.bin')

view raw JSON →