Last active
August 29, 2015 13:58
-
-
Save benwilber/9986083 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import struct | |
from io import BytesIO | |
from pycoin.block import Block | |
def read_uint32(fd): | |
return struct.unpack(b'<I', fd.read(4))[0] | |
def parse_file(block_file): | |
basename = os.path.basename(block_file) | |
with open(block_file, 'rb') as fd: | |
fd.seek(-1, os.SEEK_END) | |
file_end = fd.tell() | |
fd.seek(0) | |
while fd.tell() < file_end: | |
# Skip the magic bytes | |
fd.seek(4, os.SEEK_CUR) | |
position = fd.tell() | |
block_length = read_uint32(fd) | |
bio = BytesIO(fd.read(block_length)) | |
yield basename, position, block_length, Block.parse(bio) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment