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

Replaced bunch of parsing function with currying.

parent a10e286a
*.aliases
*.lvlps
/test.bin
No preview for this file type
......@@ -22,17 +22,19 @@ def read_byte(file):
def short_str(file):
length = read_size(file,False)
return file.read(length).decode()
def name(file, rec):
length = read_size(file,False)
description = file.read(length).decode()
length = read_size(file, False)
raw = file.read(length)
description = raw.decode()
rec['name'] = description
return True
def tst(file,rec):
raw = file.read(8)
ar = array('I',raw)
......@@ -41,46 +43,33 @@ def tst(file,rec):
rec['timestamp']=t
return True
def param_value(file,rec,param=False,wf=False, vtype='I'):
if param:
key = short_str(file)
else:
key = 'value'
if wf:
length = read_size(file)
raw = file.read(length)
ar = array(vtype,raw)
ar.byteswap()
value = ar
else:
raw = file.read(4)
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
def i32val(file, rec):
param_value(file,rec, False, False,'I')
return True
def i32par(file, rec):
param_value(file, rec, True, False,'I')
return True
def i32wfval(file, rec):
param_value(file,rec, False, True,'I')
return True
def i32wfpar(file, rec):
param_value(file, rec, True, True,'I')
return True
def param_value(param=False,wf=False, vtype='I'):
def read(file, rec):
if param:
key = short_str(file)
else:
key = 'value'
if wf:
length = read_size(file)
raw = file.read(length)
ar = array(vtype,raw)
ar.byteswap()
value = ar
else:
raw = file.read(4)
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 = read_size(file,True)
......@@ -104,21 +93,18 @@ def end_file(file, rec):
return False
parser = {
5: description,
4: tag,
1: name,
2: tst,
6: i32par,
16: i32val,
36: i32wfval,
26: i32wfpar,
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'),
254: end_record,
255: end_file
}
}
def read_record(file):
......@@ -129,16 +115,12 @@ def read_record(file):
while True:
field_type = read_byte(file)
#print(field_type)
# print('Field type', 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')
......
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