From 6e84aef52a2454acf0d547bf95c63d273d70af8b Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Thu, 17 Nov 2022 18:33:04 +0800 Subject: [PATCH] Parse [REACTIONS] --- api/s19_reactions.py | 78 ++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/api/s19_reactions.py b/api/s19_reactions.py index 1222cc8..7ef4b1c 100644 --- a/api/s19_reactions.py +++ b/api/s19_reactions.py @@ -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 \ No newline at end of file