# -*- coding: utf-8 -*-
from statemachine import StateMachine, State
from glob import glob
import re
# regular
# https://www.cnblogs.com/wyh0923/p/13937138.html
current_supported_code = ('repeat', 'stv',)
class P750PatternDecode(StateMachine):
state_start = State('start decode', initial=True)
state_decode_import = State('decode import')
state_decode_vector = State('decode vector')
state_start_decode_content = State('start decode content')
state_end_decode_content = State('end decode content')
decode_import = state_start.to(state_decode_import)
decode_vector = state_decode_import.to(state_decode_vector)
start_decode_content = state_decode_vector.to(state_start_decode_content)
end_decode = state_start_decode_content.to(state_end_decode_content)
@staticmethod
def read_fileline_generator(file, size=4096):
while 1:
# data = file.read(size)
data = file.readline()
if not data:
break
yield data
def __init__(self, filename):
super().__init__()
self.filename = filename
self.change = {}
# iter(list), next
def analysis(self):
with open(self.filename, 'r+', encoding='utf-8') as f:
for line in self.read_fileline_generator(f):
newline = line.strip()
if 0 == len(newline):
continue
if newline.startswith('import'):
self.decode_import(newline)
continue
if newline.startswith('{'):
self.start_decode_content(newline)
continue
if newline.startswith('}'):
self.end_decode(newline)
continue
if newline.startswith('vector'):
self.decode_vector(line)
continue
if newline.startswith('//'):
continue
if newline.find(';') == -1:
for newline2 in self.read_fileline_generator(f):
break
newline = newline + newline2.strip()
self.decode_vectors(newline)
def on_decode_import(self, line):
rimport = re.match('^import[\s]+(\w+)[\s]+(\w+);$', line)
if rimport is not None:
tset = rimport.group(1)
data = rimport.group(2)
self[tset] = data
# print(tset, data)
def on_decode_vector(self, line):
rimport = re.match('^vector[\s]+\((.+)\)[\n]$', line)
if rimport is not None:
rlist = rimport.group(1)
result = [item.strip() for item in rlist.split(',')]
self['pins'] = result[1:]
def on_start_decode_content(self, line):
print('received {')
def on_end_decode(self, line):
print('received }')
def decode_vectors(self, line):
if line.startswith('start_label'):
self.decode_startlabel(line)
return
if line.startswith('>'):
self.decode_one_vector(line)
return
if line.startswith(current_supported_code):
print(line)
pass
# handle jump-label
def decode_vector_with_code(self, line):
pass
def __getitem__(self, item):
return self.change[item]
def __setitem__(self, key, value):
self.change[key] = value
def decode_startlabel(self, line):
rss = re.match('^start_label[\s]+(.*)[:](.*)', line)
if rss is not None:
value = rss.group(1)
self['start_label'] = value
value = rss.group(2)
self.decode_one_vector(value)
def decode_one_vector(self, line):
rss = re.match('^>(.*);$', line)
if rss is not None:
result = rss.group(1).split()
print(str(result))
def __str__(self):
# return self.change.__str__()
return str(self.change)
# file_path = "craneL_idcode_20211109_module_0_750.atp"
file_path = "2.atp"
# multiprocess handling files
if __name__ == '__main__':
# get all the files under a directory
roots = glob('*.py')
print(roots)
decode = P750PatternDecode(file_path)
# print(decode.current_state)
# print(decode.current_state_value)
if decode.current_state_value == decode.state_start.value:
# print(decode.current_state_value)
pass
if decode.current_state == decode.state_start:
print(decode.current_state_value)
pass
decode.analysis()
print(decode)
python : statemachine
最新推荐文章于 2024-08-26 07:55:05 发布