From 749361fd47ea2f8a95ea07f613a1917b446891b8 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sat, 12 Nov 2022 14:44:31 +0800 Subject: [PATCH] Parse [JUNCTIONS] --- api/s1_title.py | 1 + api/s2_junctions.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/api/s1_title.py b/api/s1_title.py index e30a08e..f49b889 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -37,6 +37,7 @@ def inp_in_title(section: list[str]) -> ChangeSet: cs = ChangeSet({'operation' : API_ADD, 'type': 'title', 'value' : title.value}) return cs + def inp_out_title(name: str) -> list[str]: title = str(get_title(name)['value']) return title.split('\n') diff --git a/api/s2_junctions.py b/api/s2_junctions.py index cbcef1e..88ff1a0 100644 --- a/api/s2_junctions.py +++ b/api/s2_junctions.py @@ -118,3 +118,39 @@ def delete_junction_cache(name: str, cs: ChangeSet) -> SqlChangeSet: def delete_junction(name: str, cs: ChangeSet) -> ChangeSet: return execute_command(name, delete_junction_cache(name, cs)) + + +class InpJunction: + 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.id = str(tokens[0]) + self.elevation = float(tokens[1]) + self.demand = float(tokens[2]) if num_without_desc >= 3 else None + self.pattern = str(tokens[3]) if num_without_desc >= 4 else None + self.desc = str(tokens[-1]) if has_desc else None + + +def inp_in_junction(section: list[str]) -> ChangeSet: + cs = ChangeSet() + for s in section: + junction = InpJunction(s) + cs.append({'operation': API_ADD, 'type': 'junction', 'id': junction.id, 'elevation': junction.elevation, 'demand': junction.demand, 'pattern': junction.pattern}) + return cs + + +def inp_out_junction(name: str) -> list[str]: + lines = [] + junctions = read_all(name, 'select * from junctions') + for j in junctions: + id = j['id'] + elev = j['elevation'] + demand = j['demand'] if j['demand'] != None else '' + pattern = j['pattern'] if j['pattern'] != None else '' + desc = ';' + lines.append(f'{id} {elev} {demand} {pattern} {desc}') + return lines