Commit f357eb81 authored by Zhukov, Alexander P's avatar Zhukov, Alexander P
Browse files

Added python parser

parents
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 22 17:25:26 2020
@author: az9
"""
from array import array
def description(file, rec):
length = read_size(file,True)
description = file.read(length).decode()
rec['description'] = description
return True
def tag(file, rec):
length = read_size(file,False)
tag = file.read(length).decode()
tags = rec.get('tags',[])
tags.append(tag)
rec['tags'] = tags
return True
def end_record(file, rec):
return False
def end_file(file, rec):
rec['EOF']=True
return False
parser = {
5: description,
4: tag,
254: end_record,
255: end_file
}
def read_size(file, long=True):
if long:
bytes = array('I',file.read(4))
bytes.byteswap()
return bytes[0]
else:
return read_byte(file)
def read_byte(file):
return int.from_bytes(file.read(1),'big')
def read_record(file):
rec_size = read_size(file)
#print(rec_size)
#buffer = file.read(rec_size)
record = {}
while True:
field_type = read_byte(file)
#print(field_type)
func = parser.get(field_type, lambda file, rec: True)
result = func(file, record)
if not result:
break
return record
file = open('/data/BinStorage/test.bin','rb')
bytes = file.read(12)
print(bytes.decode())
while True:
record = read_record(file)
if 'EOF' in record:
break
print(record)
file.close()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment