Package peppy :: Package lib :: Module structrecord
[frames] | no frames]

Module structrecord

StructRecord -- a binary record packer/unpacker

This module unpacks binary records into objects, and packs objects
into binary records.  Similar in spirit to Construct (on the web at
construct.wikispaces.com), but assembles parsed data into objects
instead of construct's unnamed data structures.

Allows conditionals, recursive definition of structures, and custom
pack/unpack classes.  A class to store unparsed binary data needs a
class attribute named 'typedef' that is a tuple that defines the
structure of the binary record.  The tuple contains a description of
the entities in the binary record, in order that they occur in the
file.

Simple example for parsing 3 32-bit integers of varying endianness:

    class TestParse:
        typedef=(
            FormatField('testi1','<i'),
            SLInt32('testi2'),
            SBInt32('testi3'),
            )

More complicated examples include conditionals, cases, lists, and nested
structures.  For more examples, see the unit tests.

Classes
  FieldError
  FieldTypedefError
  FieldLengthError
  FieldNameError
  FieldAbortError
  Field
  NoOp
  Abort
  MetaField
  Skip
  CString
  FormatField
  Wrapper
  Switch
  UnpackOnly
  PackOnly
  Modify
  ModifyUnpack
  ModifyPack
  ComputeUnpack
  ComputePack
  Anchor
  UBInt32Checksum
  ULInt32Checksum
  Pointer
  ReadAhead
  IfElse
  If
  Adapter
  OffsetStringIO
Wrapper around StringIO to report the correct offset within the file
  MetaSizeList
This structure is for when you know the length of the field, but you don't know how many elements are in the field.
  CookedInt
  CookedFloat
  List
  MetaList
  Record
baseclass for binary records
  RecordList
Functions
 
repr1(name, value, indent, printall=False)
 
SLInt8(name, default=0)
 
SBInt8(name, default=0)
 
ULInt8(name, default=0)
 
UBInt8(name, default=0)
 
SLInt16(name, default=0)
 
SBInt16(name, default=0)
 
ULInt16(name, default=0)
 
UBInt16(name, default=0)
 
SLInt32(name, default=0)
 
SBInt32(name, default=0)
 
ULInt32(name, default=0)
 
UBInt32(name, default=0)
 
LFloat32(name, default=0)
 
BFloat32(name, default=0)
 
LFloat64(name, default=0)
 
BFloat64(name, default=0)
 
Tuple(field, num)
 
String(name, length)
Variables
  debug = True
  base_indent = ' '