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

Minor refactoring

parent f909fc20
*.aliases
*.lvlps
/test.bin
/BPMDataRaw
/.idea
/venv
*.zip
*.pyc
/py/__pycache__/
/LV17
......@@ -7,9 +7,9 @@ Created on Mon Jan 27 16:14:52 2020
"""
from read_storage import BinaryStorage
from read_storage import BinaryStorage
file = BinaryStorage('/data/BinStorage/test.bin')
print(file[0:10:2])
file.close()
\ No newline at end of file
file.close()
......@@ -7,9 +7,6 @@ Created on Mon Jan 27 16:11:06 2020
"""
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')
......@@ -19,4 +16,4 @@ while True:
break
print(record)
file.close()
\ No newline at end of file
file.close()
......@@ -21,7 +21,7 @@ class BinaryStorage:
self.file = open('/data/BinStorage/test.bin','rb')
bytes = self.file.read(HEADER_SZ)
# print(bytes.decode())
self.rec_table = BinaryStorage.read_epilog(self.file)
self.rec_table = read_epilog(self.file)
self.file.seek(HEADER_SZ)
......@@ -30,12 +30,12 @@ class BinaryStorage:
def read(self):
return BinaryStorage.read_record(self.file)
return read_record(self.file)
def readAt(self, key):
offset = self.rec_table[key]
self.file.seek(offset)
return BinaryStorage.read_record(self.file)
return read_record(self.file)
def __getitem__(self, key):
......@@ -52,160 +52,160 @@ class BinaryStorage:
return len(self.rec_table)
def read_size(file, long=True):
if long:
bytes = array('I',file.read(4))
bytes.byteswap()
return bytes[0]
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 short_str(file):
length = read_size(file,False)
return file.read(length).decode()
def name(file, rec):
length = read_size(file, False)
raw = file.read(length)
# print(raw)
name = raw.decode()
rec['name'] = name
return True
def tst(file,rec):
raw = file.read(8)
ar = array('I',raw)
ar.byteswap()
t=dt.fromtimestamp(ar[0]+ar[1]*1E-9)
rec['timestamp']=t
return True
def param_value(param=False,wf=False, vtype='I'):
type_size = {
'I': 4,
'B': 1,
'd': 8,
'f': 4
}
def read(file, rec):
if param:
key = short_str(file)
# print('key', key)
else:
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(file,rec):
raw = file.read(8)
ar = array('I',raw)
ar.byteswap()
t=dt.fromtimestamp(ar[0]+ar[1]*1E-9)
rec['timestamp']=t
return True
def param_value(param=False,wf=False, vtype='I'):
type_size = {
'I': 4,
'B': 1,
'd': 8,
'f': 4
}
def read(file, rec):
if param:
key = BinaryStorage.short_str(file)
# print('key', key)
else:
key = 'value'
if wf:
length = BinaryStorage.read_size(file)
raw = file.read(length)
ar = array(vtype,raw)
ar.byteswap()
value = ar
else:
raw = file.read(type_size[vtype])
ar = array(vtype,raw)
ar.byteswap()
value = ar[0]
if param:
pdict = rec.get('parameters',{})
pdict[key]=value
rec['parameters'] = pdict
else:
rec[key]=value
return True
return read
def description(file, rec):
length = BinaryStorage.read_size(file,True)
description = file.read(length).decode()
rec['description'] = description
return True
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
key = 'value'
if wf:
length = read_size(file)
raw = file.read(length)
ar = array(vtype,raw)
ar.byteswap()
value = ar
else:
raw = file.read(type_size[vtype])
ar = array(vtype,raw)
ar.byteswap()
value = ar[0]
if param:
pdict = rec.get('parameters',{})
pdict[key]=value
rec['parameters'] = pdict
else:
rec[key]=value
return True
def end_record(file, rec):
return False
def end_file(file, rec):
rec['EOF']=True
return False
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(file):
rec_size = BinaryStorage.read_size(file)
#print(rec_size)
#buffer = file.read(rec_size)
record = {}
while True:
field_type = BinaryStorage.read_byte(file)
# print('Field type', field_type, hex(field_type))
func = BinaryStorage.parser.get(field_type, lambda file, rec: True)
result = func(file, record)
if not result:
break
return record
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)
# the last record is EOF, and the very last offset has nothing
raw = array('L',file.read(OFST_SIZE*(num_records-2)))
raw.byteswap()
# print(raw.itemsize)
# print(raw)
return raw
return read
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,
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(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', field_type, hex(field_type))
func = parser.get(field_type, lambda file, rec: True)
result = func(file, record)
if not result:
break
return record
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)
# the last record is EOF, and the very last offset has nothing
raw = array('L',file.read(OFST_SIZE*(num_records-2)))
raw.byteswap()
# print(raw.itemsize)
# print(raw)
return raw
......
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