Files
TJWaterServer/api/s23_options.py
2023-03-08 22:43:21 +08:00

54 lines
1.6 KiB
Python

from .database import *
from .s23_options_util import get_option_schema, set_option_cmd, set_option_v3_cmd, generate_v3
def set_option(name: str, cs: ChangeSet) -> ChangeSet:
v2_cmd = set_option_cmd(name, cs)
result = execute_command(name, v2_cmd)
v3 = generate_v3(ChangeSet(v2_cmd.redo_cs[0]))
v3_cmd = set_option_v3_cmd(name, v3)
result.merge(execute_command(name, v3_cmd))
return result
def inp_in_option(section: list[str]) -> ChangeSet:
if len(section) <= 0:
return ChangeSet()
cs = g_update_prefix | { 'type' : 'option' }
for s in section:
if s.startswith(';'):
continue
tokens = s.strip().split()
if tokens[0].upper() == 'PATTERN': # can not upper id
cs |= { 'PATTERN' : tokens[1] }
elif tokens[0].upper() == 'QUALITY': # can not upper trace node
value = tokens[1]
if len(tokens) > 2:
value += f' {tokens[2]}'
cs |= { 'QUALITY' : value }
else:
line = s.upper().strip()
for key in get_option_schema('').keys():
if line.startswith(key):
value = line.removeprefix(key).strip()
cs |= { key : value }
result = ChangeSet(cs)
result.merge(generate_v3(result))
return result
def inp_out_option(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from options")
for obj in objs:
key = obj['key']
value = obj['value']
if value != '':
lines.append(f'{key} {value}')
return lines