From f79a9cdae89b44ce3b17a3279c3d8c1d77bec053 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 15 Mar 2023 22:34:38 +0800 Subject: [PATCH] Support inp in tag, demand, status --- api/inp_in_new.py | 23 ++++++++++++++++++----- api/s10_status.py | 15 +++++++++++++++ api/s8_tags.py | 17 +++++++++++++++++ api/s9_demands.py | 17 +++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/api/inp_in_new.py b/api/inp_in_new.py index 4c4ede6..7160a8f 100644 --- a/api/inp_in_new.py +++ b/api/inp_in_new.py @@ -1,5 +1,5 @@ from .project import * -from .database import ChangeSet, write +from .database import ChangeSet, write, try_read from .sections import * from .s1_title import inp_in_title_new from .s2_junctions import inp_in_junction_new @@ -8,6 +8,9 @@ from .s4_tanks import inp_in_tank_new from .s5_pipes import inp_in_pipe_new from .s6_pumps import inp_in_pump_new from .s7_valves import inp_in_valve_new +from .s8_tags import inp_in_tag_new +from .s9_demands import inp_in_demand_new +from .s10_status import inp_in_status_new from .s11_patterns import inp_in_pattern_new from .s12_curves import inp_in_curve_new from .s13_controls import inp_in_control_new @@ -28,9 +31,9 @@ _handler = { PIPES : (_L, inp_in_pipe_new), PUMPS : (_L, inp_in_pump_new), VALVES : (_L, inp_in_valve_new), - TAGS : (_L, None), - DEMANDS : (_L, None), - STATUS : (_L, None), + TAGS : (_L, inp_in_tag_new), + DEMANDS : (_L, inp_in_demand_new), + STATUS : (_L, inp_in_status_new), PATTERNS : (_L, inp_in_pattern_new), CURVES : (_L, inp_in_curve_new), CONTROLS : (_L, inp_in_control_new), @@ -128,6 +131,7 @@ def parse_file(project: str, inp: str) -> None: pattern_desc_line = None curve_type_desc_line = None + demand_junction = None with open(inp) as f: for s in levels: @@ -146,8 +150,10 @@ def parse_file(project: str, inp: str) -> None: break line = line.strip() - if line == '' or line.startswith('['): + if line.startswith('['): break + elif line == '': + continue if is_s: sections[s].append(line) @@ -171,6 +177,13 @@ def parse_file(project: str, inp: str) -> None: tokens = line.split() write(project, f"insert into _curve (id, type) values ('{tokens[0]}', '{type_and_desc[0].strip()}');") curve_type_desc_line = None + elif s == DEMANDS: + tokens = line.split() + junction = str(tokens[0]) + if demand_junction != junction: + if try_read(project, f"select * from demands where junction = '{junction}'") != None: + write(project, f"delete from demands where junction = '{junction}';") + demand_junction = junction if handler != None: handler(project, line) diff --git a/api/s10_status.py b/api/s10_status.py index 07d1f50..2aa1cf5 100644 --- a/api/s10_status.py +++ b/api/s10_status.py @@ -113,6 +113,21 @@ def inp_in_status(section: list[str]) -> ChangeSet: return cs +def inp_in_status_new(name: str, line: str) -> None: + tokens = line.split() + + num = len(tokens) + has_desc = tokens[-1].startswith(';') + num_without_desc = (num - 1) if has_desc else num + + link = str(tokens[0]) + value = tokens[1].upper() + if value == LINK_STATUS_OPEN or value == LINK_STATUS_CLOSED or value == LINK_STATUS_ACTIVE: + write(name, f"insert into status (link, status, setting) values ('{link}', '{value}', null);") + else: + write(name, f"insert into status (link, status, setting) values ('{link}', null, {float(value)});") + + def inp_out_status(name: str) -> list[str]: lines = [] objs = read_all(name, 'select * from status') diff --git a/api/s8_tags.py b/api/s8_tags.py index 9dd8a32..9ce8fd9 100644 --- a/api/s8_tags.py +++ b/api/s8_tags.py @@ -103,6 +103,23 @@ def inp_in_tag(section: list[str]) -> ChangeSet: return cs +def inp_in_tag_new(name: str, line: str) -> None: + tokens = line.split() + + num = len(tokens) + has_desc = tokens[-1].startswith(';') + num_without_desc = (num - 1) if has_desc else num + + t_type = str(tokens[0].upper()) + id = str(tokens[1]) + tag = str(tokens[2]) + + if t_type == TAG_TYPE_NODE: + write(name, f"insert into tags_node (id, tag) values ('{id}', '{tag}');") + elif t_type == TAG_TYPE_LINK: + write(name, f"insert into tags_link (id, tag) values ('{id}', '{tag}');") + + def inp_out_tag(name: str) -> list[str]: lines = [] objs = read_all(name, 'select * from tags_node') diff --git a/api/s9_demands.py b/api/s9_demands.py index 2fedb1e..d482900 100644 --- a/api/s9_demands.py +++ b/api/s9_demands.py @@ -97,6 +97,23 @@ def inp_in_demand(section: list[str]) -> ChangeSet: return cs +def inp_in_demand_new(name: str, line: str) -> None: + tokens = line.split() + + num = len(tokens) + has_desc = tokens[-1].startswith(';') + num_without_desc = (num - 1) if has_desc else num + + junction = str(tokens[0]) + demand = float(tokens[1]) + pattern = str(tokens[2]) if num_without_desc >= 3 else None + pattern = f"'{pattern}'" if pattern != None else 'null' + category = str(tokens[3]) if num_without_desc >= 4 else None + category = f"'{category}'" if category != None else 'null' + + write(name, f"\ninsert into demands (junction, demand, pattern, category) values ('{junction}', {demand}, {pattern}, {category});") + + def inp_out_demand(name: str) -> list[str]: lines = [] objs = read_all(name, f"select * from demands order by _order")