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

WIP

parent 6e56f708
......@@ -9,24 +9,62 @@ from array import array
from datetime import datetime as dt
import os
HEADER = 'SNS-BIET2020'
HEADER_SZ = 12
ENDRCRD = 254
ENDFILE = 255
NAME = 1
UNIXTST = 2
EPICSTST = 3
TAG = 4
DESCR = 5
I32PARM = 6
I32 = 16
I32WFPARM = 26
I32WF = 36
DBLPARM = 7
DBL = 17
DBLWFPARM = 27
DBLWF = 37
U8PARM = 8
U8 = 18
U8WFPARM = 28
U8WF = 38
FLTPARM = 9
FLT = 19
FLTWFPARM = 29
FLT8WF = 39
class Writer:
def __init__(self, path):
self.file = open(path, 'wb')
self.file.write(HEADER)
# print(bytes.decode())
def close(self):
self.file.close()
HEADER_SZ=12
class BinaryStorage:
def __init__(self, path):
self.file = open(path,'rb')
bytes = self.file.read(HEADER_SZ)
# print(bytes.decode())
self.rec_table = read_epilog(self.file)
self.file.seek(HEADER_SZ)
def close(self):
self.file.close()
def read(self):
return read_record(self.file)
......@@ -34,20 +72,23 @@ class BinaryStorage:
offset = self.rec_table[key]
self.file.seek(offset)
return read_record(self.file)
def __getitem__(self, key):
if isinstance(key,slice):
return [self.readAt(i) for i in range(key.start,key.stop,key.step)]
else:
return self.readAt(key)
def __len__(self):
return len(self.rec_table)
"""
Internal implementation
"""
def read_size(file, long=True):
if long:
bytes = array('I',file.read(4))
......@@ -56,11 +97,11 @@ def read_size(file, long=True):
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()
......@@ -120,10 +161,11 @@ def param_value(param=False,wf=False, vtype='I'):
def description(file, rec):
length = read_size(file,True)
description = file.read(length).decode()
rec['description'] = description
desc = file.read(length).decode()
rec['description'] = desc
return True
def tag(file, rec):
length = read_size(file,False)
tag = file.read(length).decode()
......@@ -132,49 +174,51 @@ def tag(file, rec):
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
DESCR: description,
TAG: tag,
NAME: name,
UNIXTST: tst,
I32PARM: param_value(param=True,wf=False,vtype='I'),
I32: param_value(param=False,wf=False,vtype='I'),
I32WF: param_value(param=False,wf=True,vtype='I'),
I32WFPARM: param_value(param=True,wf=True,vtype='I'),
DBLPARM: param_value(param=True,wf=False,vtype='d'),
DBL: param_value(param=False,wf=False,vtype='d'),
DBLWF: param_value(param=False,wf=True,vtype='d'),
DBLWFPARM: param_value(param=True,wf=True,vtype='d'),
U8PARM: param_value(param=True,wf=False,vtype='B'),
U8: param_value(param=False,wf=False,vtype='B'),
U8WF: param_value(param=False,wf=True,vtype='B'),
U8WFPARM: param_value(param=True,wf=True,vtype='B'),
FLTPARM: param_value(param=True,wf=False,vtype='f'),
FLT: param_value(param=False,wf=False,vtype='f'),
FLT8WF: param_value(param=False,wf=True,vtype='f'),
FLTWFPARM: param_value(param=True,wf=True,vtype='f'),
ENDRCRD: end_record,
ENDFILE: end_file
}
def read_record(file):
rec_size = read_size(file)
#print(rec_size)
#buffer = file.read(rec_size)
# print(rec_size)
# buffer = file.read(rec_size)
record = {}
while True:
......
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