Parse [EMITTERS]

This commit is contained in:
WQY\qiong
2022-11-14 21:21:17 +08:00
parent d190f790bb
commit 3c9192b8d9

View File

@@ -57,3 +57,36 @@ def set_emitter_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
def set_emitter(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, set_emitter_cache(name, cs))
class InpEmitter:
def __init__(self, line: str) -> None:
tokens = line.split()
num = len(tokens)
has_desc = tokens[-1].startswith(';')
num_without_desc = (num - 1) if has_desc else num
self.junction = str(tokens[0])
self.coefficient = float(tokens[1])
def inp_in_emitter(section: list[str]) -> ChangeSet:
cs = ChangeSet()
for s in section:
# skip comment
if s.startswith(';'):
continue
obj = InpEmitter(s)
cs.append({'operation': API_UPDATE, 'type': 'emitter', 'junction': obj.junction, 'coefficient': obj.coefficient})
return cs
def inp_out_emitter(name: str) -> list[str]:
lines = []
objs = read_all(name, 'select * from emitters')
for obj in objs:
junction = obj['junction']
coefficient = obj['coefficient']
lines.append(f'{junction} {coefficient}')
return lines