Files
TJWaterServerBinary/app/native/wndb/model/options_v3.py
T
jiang 90b02057bc
Generic Container CI/CD / test-build-publish (push) Successful in 1m13s
Server CI/CD v2 / build-test-publish-and-deploy (push) Successful in 1m13s
feat(projects): automate project infrastructure provisioning
2026-09-11 10:57:51 +08:00

103 lines
3.7 KiB
Python

from ..core.database import ChangeSet, g_update_prefix, read_all, sql_literal
from .options import get_option_schema, get_option_v3_schema, generate_v2, generate_v3
def _parse_v2(v2_lines: list[str]) -> dict[str, str]:
cs_v2 = g_update_prefix | { 'type' : 'option' }
for s in v2_lines:
stripped = s.strip()
if not stripped or stripped.startswith(';'):
continue
tokens = stripped.split()
if tokens[0].upper() == 'PATTERN': # can not upper id
value = tokens[1] if len(tokens) > 1 else ''
cs_v2 |= { 'PATTERN' : value }
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 _option_changes(option_type: str, *change_sets: ChangeSet) -> ChangeSet:
values: dict[str, str] = {}
for change_set in change_sets:
for operation in change_set.operations:
values.update(
{
key: str(value)
for key, value in operation.items()
if key not in {'operation', 'type'}
}
)
if not values:
return ChangeSet()
return ChangeSet(g_update_prefix | {'type': option_type} | values)
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:
stripped = s.strip()
if not stripped or stripped.startswith(';'):
continue
tokens = stripped.split()
key = tokens[0]
if key in get_option_v3_schema('').keys():
value = ''
if len(tokens) == 2:
value = tokens[1]
elif len(tokens) > 2:
value = ' '.join(tokens[1:])
cs_v3 |= { key : value }
else:
v2_lines.append(stripped)
cs_v2 = _parse_v2(v2_lines)
direct_v2 = _option_changes('option', ChangeSet(cs_v2))
direct_v3 = _option_changes('option_v3', ChangeSet(cs_v3))
generated_v2 = generate_v2(direct_v3) if direct_v3.operations else ChangeSet()
generated_v3 = generate_v3(direct_v2) if direct_v2.operations else ChangeSet()
result = ChangeSet()
result.merge(_option_changes('option', generated_v2, direct_v2))
result.merge(_option_changes('option_v3', generated_v3, direct_v3))
return result
def inp_in_option_v3(section: list[str]) -> str:
sql = ''
result = _inp_in_option_v3(section)
for op in result.operations:
for key in op.keys():
if key == 'operation' or key == 'type':
continue
if op['type'] == 'option_v3':
sql += f"update network.simulation_settings set value = {sql_literal(op[key])} where engine_version = 'v3' and key = {sql_literal(key)};"
else:
sql += f"update network.simulation_settings set value = {sql_literal(op[key])} where engine_version = 'legacy' and key = {sql_literal(key)};"
return sql
def inp_out_option_v3(name: str) -> list[str]:
lines = []
objs = read_all(name, "select key, value from network.simulation_settings where engine_version = 'v3' order by key")
for obj in objs:
key = obj['key']
value = obj['value']
if str(value).strip() != '':
lines.append(f'{key} {value}')
return lines