Parse [SOURCES]

This commit is contained in:
WQY\qiong
2022-11-14 21:36:41 +08:00
parent 2002b1f246
commit 2f8f7c4918

View File

@@ -98,3 +98,40 @@ def delete_source_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
def delete_source(name: str, cs: ChangeSet) -> ChangeSet:
return execute_command(name, delete_source_cache(name, cs))
class InpSource:
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.node = str(tokens[0])
self.s_type = float(tokens[1])
self.strength = float(tokens[2])
self.pattern = str(tokens[3]) if num_without_desc >= 4 else None
def inp_in_source(section: list[str]) -> ChangeSet:
cs = ChangeSet()
for s in section:
# skip comment
if s.startswith(';'):
continue
obj = InpSource(s)
cs.append({'operation': API_ADD, 'type': 'source', 'node': obj.node, 's_type': obj.s_type, 'strength': obj.strength, 'pattern': obj.pattern})
return cs
def inp_out_source(name: str) -> list[str]:
lines = []
objs = read_all(name, 'select * from sources')
for obj in objs:
node = obj['node']
s_type = obj['s_type']
strength = obj['strength']
pattern = obj['pattern'] if obj['pattern'] != None else ''
lines.append(f'{node} {s_type} {strength} {pattern}')
return lines