Skip to content

v2.4.0: Actions, Digests and Documentation Updates

Latest
Compare
Choose a tag to compare
@MatrixEditor MatrixEditor released this 30 Dec 18:13

What's Changed

Documentation Updates

  • The tutorial was reorganized into multiple files for easier navigation and a more structured learning experience. This should make it easier to find what you need and dive into the specifics of different topics.
  • Most of the common field types are now fully documented, providing clear, comprehensive examples and explanations for quick reference.

New Feature: Actions

Actions introduce the ability to execute custom logic before processing a field, allowing you to modify the IO stream or perform operations like checksum calculations or logging. There are two types of actions:

  1. Packing actions (__action_pack__): Triggered before packing the data into a struct, typically used for operations before serializing or encoding.
  2. Unpacking actions(__action_unpack__): Triggered before unpacking the data from a struct, useful for tasks before deserializing or decoding.
def checksum_action(context: _ContextLike) -> None:
    # Perform checksum or logging logic here
    pass

@struct
class MyStruct:
    some_field: Bytes(10)
    checksum: Action(checksum_action)  # Runs only when packing

New Classes

  • Aligned and Align: These help manage data alignment, ensuring your structs are correctly aligned in memory or when serialized.

  • Digest: A new class designed to handle cryptographic hashes or checksums within your structs.

    from caterpillar.py import struct, Bytes, Md5
    
    @struct
    class Format:
        # Appends a new attribute AFTER all fields within the
        # context and verifies after parsing the data
        with Md5(name="hash", verify=True):
            user_data: Bytes(100)
  • IOHook: A class to hook into the IO process, allowing further customization of data reading and writing.

Fixes and Changes

  • Class Rename: The uuid class has been renamed to Uuid for consistency and clarity.
  • Removed Class: The Pickled class has been removed in favor of more flexible serialization options.
  • Renaming: FormatField has been renamed to PyStructFormattedField to better reflect its usage.
  • Prefixed Enhancement: The Prefixed class now supports arbitrary structs, offering more flexibility when dealing with prefixed data.
  • Bug Fix: A bug in S_ADD_BYTES has been fixed, which previously caused an error when calling __bytes__.

Full Changelog: v2.2.0...v2.4.0