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

Class implementation

parent 31873422
......@@ -12,51 +12,50 @@ import os
class BinaryStorage:
HEADER_SIZE=12
def read_epilog(self):
OFST_SIZE=8
self.file.seek(-4,os.SEEK_END)
raw = array('I',self.file.read(4))
raw.byteswap()
num_records = raw[0]
print(num_records)
self.file.seek(-4-OFST_SIZE*num_records,os.SEEK_END)
raw = array('L',self.file.read(OFST_SIZE*num_records))
raw.byteswap()
print(raw.itemsize)
print(raw)
return raw
def __init__(self, path):
self.file = open('/data/BinStorage/test.bin','rb')
bytes = self.file.read(12)
print(bytes.decode())
def close(self):
self.file.close()
def read(self):
return BinaryStorage.read_record(self.file)
def read_byte(self):
return int.from_bytes(self.file.read(1),'big')
def read_size(self, long=True):
def read_size(file, long=True):
if long:
bytes = array('I',self.file.read(4))
bytes = array('I',file.read(4))
bytes.byteswap()
return bytes[0]
else:
return self.read_byte()
def short_str(self):
length = self.read_size(self.file,False)
return self.file.read(length).decode()
def name(self, rec):
length = self.read_size(False)
raw = self.file.read(length)
return BinaryStorage.read_byte(file)
def read_byte(file):
return int.from_bytes(file.read(1),'big')
def short_str(file):
length = BinaryStorage.read_size(file,False)
return file.read(length).decode()
def name(file, rec):
length = BinaryStorage.read_size(file, False)
raw = file.read(length)
# print(raw)
name = raw.decode()
rec['name'] = name
return True
def tst(self,rec):
raw = self.file.read(8)
def tst(file,rec):
raw = file.read(8)
ar = array('I',raw)
ar.byteswap()
t=dt.fromtimestamp(ar[0]+ar[1]*1E-9)
......@@ -64,28 +63,27 @@ class BinaryStorage:
return True
def param_value(self, param=False,wf=False, vtype='I'):
def param_value(param=False,wf=False, vtype='I'):
type_size = {
'I': 4,
'B': 1,
'd': 8,
'f': 4
}
def read(obj, rec):
def read(file, rec):
if param:
key = obj.short_str()
key = BinaryStorage.short_str(file)
# print('key', key)
else:
key = 'value'
if wf:
length = obj.read_size()
raw = obj.file.read(length)
length = BinaryStorage.read_size(file)
raw = file.read(length)
ar = array(vtype,raw)
ar.byteswap()
value = ar
else:
raw = obj.file.read(type_size[vtype])
raw = file.read(type_size[vtype])
ar = array(vtype,raw)
ar.byteswap()
value = ar[0]
......@@ -99,84 +97,88 @@ class BinaryStorage:
return read
def description(self, rec):
length = self.read_size(True)
description = self.file.read(length).decode()
def description(file, rec):
length = BinaryStorage.read_size(file,True)
description = file.read(length).decode()
rec['description'] = description
return True
def tag(self, rec):
length = self.read_size(False)
tag = self.file.read(length).decode()
def tag(file, rec):
length = BinaryStorage.read_size(file,False)
tag = file.read(length).decode()
tags = rec.get('tags',[])
tags.append(tag)
rec['tags'] = tags
return True
def end_record(self, rec):
def end_record(file, rec):
return False
def end_file(self, rec):
def end_file(file, rec):
rec['EOF']=True
return False
def __init__(self, path):
self.file = open(path,'rb')
# self.record_table = self.read_epilog(self.file)
bytes = self.file.read(12)
print(bytes.decode())
self.parser = {
5: self.description,
4: self.tag,
1: self.name,
2: self.tst,
6: self.param_value(param=True,wf=False,vtype='I'),
16: self.param_value(param=False,wf=False,vtype='I'),
36: self.param_value(param=False,wf=True,vtype='I'),
26: self.param_value(param=True,wf=True,vtype='I'),
7: self.param_value(param=True,wf=False,vtype='d'),
17: self.param_value(param=False,wf=False,vtype='d'),
37: self.param_value(param=False,wf=True,vtype='d'),
27: self.param_value(param=True,wf=True,vtype='d'),
8: self.param_value(param=True,wf=False,vtype='B'),
18: self.param_value(param=False,wf=False,vtype='B'),
38: self.param_value(param=False,wf=True,vtype='B'),
28: self.param_value(param=True,wf=True,vtype='B'),
9: self.param_value(param=True,wf=False,vtype='f'),
19: self.param_value(param=False,wf=False,vtype='f'),
39: self.param_value(param=False,wf=True,vtype='f'),
29: self.param_value(param=True,wf=True,vtype='f'),
254: self.end_record,
255: self.end_file
}
parser = {
5: description,
4: tag,
1: name,
2: tst,
6: param_value(param=True,wf=False,vtype='I'),
16: param_value(param=False,wf=False,vtype='I'),
36: param_value(param=False,wf=True,vtype='I'),
26: param_value(param=True,wf=True,vtype='I'),
7: param_value(param=True,wf=False,vtype='d'),
17: param_value(param=False,wf=False,vtype='d'),
37: param_value(param=False,wf=True,vtype='d'),
27: param_value(param=True,wf=True,vtype='d'),
8: param_value(param=True,wf=False,vtype='B'),
18: param_value(param=False,wf=False,vtype='B'),
38: param_value(param=False,wf=True,vtype='B'),
28: param_value(param=True,wf=True,vtype='B'),
9: param_value(param=True,wf=False,vtype='f'),
19: param_value(param=False,wf=False,vtype='f'),
39: param_value(param=False,wf=True,vtype='f'),
29: param_value(param=True,wf=True,vtype='f'),
254: end_record,
255: end_file
}
def read_record(self):
rec_size = self.read_size()
def read_record(file):
rec_size = BinaryStorage.read_size(file)
#print(rec_size)
#buffer = file.read(rec_size)
record = {}
while True:
field_type = self.read_byte()
field_type = BinaryStorage.read_byte(file)
# print('Field type', field_type, hex(field_type))
func = self.parser.get(field_type, lambda self, rec: True)
result = func(self, record)
func = BinaryStorage.parser.get(field_type, lambda file, rec: True)
result = func(file, record)
if not result:
break
return record
def close(self):
self.file.close()
def read_epilog(file):
OFST_SIZE=8
file.seek(-4,os.SEEK_END)
raw = array('I',file.read(4))
raw.byteswap()
num_records = raw[0]
print(num_records)
file.seek(-4-OFST_SIZE*num_records,os.SEEK_END)
raw = array('L',file.read(OFST_SIZE*num_records))
raw.byteswap()
print(raw.itemsize)
print(raw)
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 16:11:06 2020
@author: az9
"""
from read_storage import BinaryStorage
# file = open('/data/BinStorage/test.bin','rb')
# bytes = file.read(12)
# print(bytes.decode())
file = BinaryStorage('/data/BinStorage/test.bin')
while True:
record = file.read()
if 'EOF' in record:
break
print(record)
file.close()
\ No newline at end of file
Markdown is supported
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