Parse [REACTIONS]

This commit is contained in:
WQY\qiong
2022-11-17 18:33:04 +08:00
parent 4837d4ee45
commit 6e84aef52a

View File

@@ -60,27 +60,6 @@ def set_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_reaction_cache(name, cs))
# def inp_in_reaction(section: list[str]) -> ChangeSet:
# cs = g_update_prefix | { 'type' : 'reaction' }
# for s in section:
# line = s.upper().strip()
# for key in get_reaction_schema('').keys():
# if line.startswith(key):
# value = line.removeprefix(key).strip()
# cs |= { key : value }
# return ChangeSet(cs)
def inp_out_reaction(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from reactions")
for obj in objs:
key = obj['key']
value = obj['value']
lines.append(f'{key} {value}')
return lines
def get_pipe_reaction_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'pipe' : {'type': 'str' , 'optional': False , 'readonly': True },
'bulk' : {'type': 'float' , 'optional': True , 'readonly': False},
@@ -200,3 +179,60 @@ def set_tank_reaction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
def set_tank_reaction(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_tank_reaction_cache(name, cs))
def inp_in_reaction(section: list[str]) -> ChangeSet:
cs = ChangeSet()
for s in section:
tokens = s.strip().split()
token0 = tokens[0].upper()
if token0 == 'BULK' or token0 == 'WALL':
pipe = tokens[1]
key = token0.lower()
value = tokens[2]
cs.append(g_update_prefix | { 'type' : 'pipe_reaction', 'pipe' : pipe, key: value })
elif token0 == 'TANK':
tank = tokens[1]
value = tokens[2]
cs.append(g_update_prefix | { 'type' : 'pipe_reaction', 'tank' : tank, 'value': value })
else:
line = s.upper().strip()
for key in get_reaction_schema('').keys():
if line.startswith(key):
value = line.removeprefix(key).strip()
cs.append(g_update_prefix | { 'type' : 'reaction', key : value })
return cs
def inp_out_reaction(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from reactions")
for obj in objs:
key = obj['key']
value = obj['value']
lines.append(f'{key} {value}')
objs = read_all(name, f"select * from reactions_pipe_bulk")
for obj in objs:
pipe = obj['pipe']
bulk = obj['bulk']
lines.append(f'BULK {pipe} {bulk}')
objs = read_all(name, f"select * from reactions_pipe_wall")
for obj in objs:
pipe = obj['pipe']
wall = obj['wall']
lines.append(f'WALL {pipe} {wall}')
objs = read_all(name, f"select * from reactions_tank")
for obj in objs:
tank = obj['tank']
value = obj['value']
lines.append(f'TANK {tank} {value}')
return lines