Files
TJWaterServer/api/s23_options_v3.py
2023-03-08 22:56:29 +08:00

68 lines
2.0 KiB
Python

from .database import *
from .s23_options_util import get_option_schema, get_option_v3_schema, generate_v2, generate_v3
from .batch_cmd import execute_batch_command
def set_option_v3(name: str, cs: ChangeSet) -> ChangeSet:
new_cs = cs
new_cs.merge(generate_v2(cs))
return execute_batch_command(name, new_cs)
def _parse_v2(v2_lines: list[str]) -> dict[str, str]:
cs_v2 = g_update_prefix | { 'type' : 'option' }
for s in v2_lines:
tokens = s.split()
if tokens[0].upper() == 'PATTERN': # can not upper id
cs_v2 |= { '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_v2 |= { 'QUALITY' : value }
else:
line = s.upper().strip()
for key in get_option_schema('').keys():
if line.startswith(key):
value = line.removeprefix(key).strip()
cs_v2 |= { key : value }
return cs_v2
def inp_in_option_v3(section: list[str]) -> ChangeSet:
if len(section) <= 0:
return ChangeSet()
cs_v3 = g_update_prefix | { 'type' : 'option_v3' }
v2_lines = []
for s in section:
if s.startswith(';'):
continue
tokens = s.strip().split()
key = tokens[0]
if key in get_option_v3_schema('').keys():
value = tokens[1] if len(tokens) >= 2 else ''
cs_v3 |= { key : value }
else:
v2_lines.append(s.strip())
# unlikely...
cs_v2 = _parse_v2(v2_lines)
result = ChangeSet(cs_v3)
result.merge(generate_v3(ChangeSet(cs_v2)))
result.merge(generate_v2(result))
return result
def inp_out_option_v3(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from options_v3")
for obj in objs:
key = obj['key']
value = obj['value']
if value != '':
lines.append(f'{key} {value}')
return lines