Skip to content

Python module for creating intuitive and efficient file parsers. Originally created because I got tired of parsing binary files manually while trying to parse Skyrim save files

Notifications You must be signed in to change notification settings

CameronChurchwell/MothPriest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MothPriest

Python module for creating intuitive file parsers.

Imagine you have a file with the following structure:

Field Size (Bytes) Value
Magic 6 "STRING"
StringSize 4 uint32 (little endian) size String in bytes
String StringSize string data for this file

MothPriest makes parsing and editing this file simple:

from io import BytesIO
from mothpriest.parsers import *

parser = BlockParser(
    "root",
    [
        MagicParser("STRING", "Magic"),
        IntegerParser("StringSize", size=4, little_endian=True, signed=False),
        StringParser("String", size="StringSize") # Note the reference back to StringSize
    ]
)

data = b"STRING\x1A\x00\x00\x00Lorem ipsum dolor sit amet"
with BytesIO(data) as buffer:
    parser(buffer)

assert parser['String'] == "Lorem ipsum dolor sit amet"

parser['String'] = "The quick brown fox jumps over the lazy dog"
# StringSize is now inconsistent with String, but there is no need to manually update

with BytesIO() as buffer:
    parser.unparse(buffer)
    buffer.seek(0)
    output_data = buffer.read()

# Note how StringSize is updated automatically
assert output_data == b"STRING\x2B\x00\x00\x00The quick brown fox jumps over the lazy dog"

About

Python module for creating intuitive and efficient file parsers. Originally created because I got tired of parsing binary files manually while trying to parse Skyrim save files

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages