Skip to main content

A declarative byte stream mapping engine.

Project description

Doc Status Build Status PyPI License Python Status Binder

KonFoo is a Python Package for creating byte stream mappers in a declarative way with as little code as necessary to help fighting the confusion with the foo of the all too well-known memory dumps or hexadecimal views of binary data.

It comes with sensible defaults out of the box.

It aims to make the process of reading, de-serializing, viewing, serializing and writing binary data from and back to a data provider as easy as possible.

KonFoo in points:

  • declarative way to describe the mapping of binary data to Python types

  • declarative classes to read, deserialize, serialize and write binary data from and back to a data source

  • easy adjustable byte stream provider bridge to any kind of data source

  • nesting of classes

  • adaptable classes on the fly while reading/de-serializing binary data

  • easy syntax for accessing nested fields

  • view the mapped binary data as a JSON string

  • list the mapped binary data as a flatten list or dictionary

  • write the mapped binary data to a .json file

  • write the mapped binary data to a .csv file

  • save the mapped binary data to an .ini file

  • load the mapped binary data from an .ini file

  • easy creatable nested metadata dictionaries of the members of a byte stream mapper

  • metadata converter to the flare.json format to visualise the mapper with d3.js.

Example

A short example how to define a byte stream mapper.

>>> from konfoo import *
>>> class Identifier(Structure):
...     def __init__(self):
...         super().__init__()
...         self.version = Byte(align_to=4)
...         self.id = Unsigned(8, align_to=4)
...         self.length = Decimal(8, align_to=4)
...         self.module = Char(align_to=4)
...         self.index_fields()
>>> class HeaderV1(Structure)
...     def __init__(self):
...         super().__init__()
...         self.type = Identifier()
>>> class HeaderV2(HeaderV1)
...     def __init__(self):
...         super().__init__()
...         self.size = Decimal(16)
>>> header = HeaderV2()
>>> header.to_list()
[('Structure.type.version', '0x0'),
 ('Structure.type.id', '0x0'),
 ('Structure.type.length', 0),
 ('Structure.type.module', '\x00'),
 ('Structure.size', 0)]
>>> header.type.to_csv()
[{'id': 'Identifier.version', 'value': '0x0'},
 {'id': 'Identifier.id', 'value': '0x0'},
 {'id': 'Identifier.length', 'value': 0},
 {'id': 'Identifier.module', 'value': '\x00'}]
>>> header.to_json()
'{"type": {"version": "0x0", "id": "0x0", "length": 0, "module": "\u0000"},
  "size": 0}'
>>> header.deserialize(bytes.fromhex('0102094610'))
>>> header.to_json()
'{"type": {"version": "0x1", "id": "0x2", "length": 9, "module": "F"},
  "size": 16}'
>>> bytes(header).hex()
'0102094610'
>>> header = Structure(
...     type=Structure(version=Byte(4),
...                    id=Unsigned(8, 4),
...                    length=Decimal(8, 4),
...                    module=Char(4)),
...     size=Decimal(16))
>>> header.to_list()
[('Structure.type.version', '0x0'),
 ('Structure.type.id', '0x0'),
 ('Structure.type.length', 0),
 ('Structure.type.module', '\x00'),
 ('Structure.size', 0)]
>>> header.type.to_csv()
[{'id': 'Structure.version', 'value': '0x0'},
 {'id': 'Structure.id', 'value': '0x0'},
 {'id': 'Structure.length', 'value': 0},
 {'id': 'Structure.module', 'value': '\x00'}]
>>> header.to_json()
'{"type": {"version": "0x0", "id": "0x0", "length": 0, "module": "\u0000"},
  "size": 0}'
>>> header.deserialize(bytes.fromhex('0102094610'))
>>> header.to_json()
'{"type": {"version": "0x1", "id": "0x2", "length": 9, "module": "F"},
  "size": 16}'
>>> bytes(header).hex()
'0102094610'

Installing

> pip install konfoo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

KonFoo-2.0.tar.gz (94.7 kB view hashes)

Uploaded Source

Built Distribution

KonFoo-2.0-py3-none-any.whl (51.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page