From 46f898a000d2d3d14d6745ebedc6c3c9c5acf2f6 Mon Sep 17 00:00:00 2001 From: xinzish <32566857+xinzish@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:41:36 +0800 Subject: [PATCH] refine --- api/inp_in.py | 18 +++++++++++++----- api/inp_out.py | 2 +- api/s0_base.py | 8 ++++++++ api/s32_region.py | 8 ++++++++ api/sections.py | 4 +++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/api/inp_in.py b/api/inp_in.py index ea2e4c0..9b5e73b 100644 --- a/api/inp_in.py +++ b/api/inp_in.py @@ -31,6 +31,7 @@ from .s24_coordinates import inp_in_coord from .s25_vertices import inp_in_vertex from .s26_labels import inp_in_label from .s27_backdrop import inp_in_backdrop +from .s32_region import inp_in_region,inp_in_bound _S = 'S' _L = 'L' @@ -64,6 +65,8 @@ _handler = { OPTIONS : (_S, _inp_in_option), # line, version COORDINATES : (_L, inp_in_coord), VERTICES : (_L, inp_in_vertex), + REGION : (_L, inp_in_region), + BOUND : (_L, inp_in_bound), LABELS : (_L, inp_in_label), BACKDROP : (_S, inp_in_backdrop), #END : 'END', @@ -106,6 +109,8 @@ _level_4 = [ ENERGY, REACTIONS, VERTICES, + REGION, + BOUND, ] @@ -156,7 +161,7 @@ def _get_file_offset(inp: str) -> tuple[dict[str, list[int]], bool]: elif line != '' and line.startswith(';') == False: if current == DEMANDS: demand_outside = True - + return (offset, demand_outside) @@ -180,7 +185,7 @@ def parse_file(project: str, inp: str, version: str = '3') -> None: curve_type_desc_line = None sql_batch = SQLBatch(project) - + print("aaa") _print_time("Second scan...") with open(inp) as f: for s in levels: @@ -250,11 +255,14 @@ def parse_file(project: str, inp: str, version: str = '3') -> None: sql_batch.add(f"insert into _curve (id, type) values ('{tokens[0]}', '{type}');") current_curve = tokens[0] curve_type_desc_line = None - + elif s == BOUND: + tokens = line.split() if s == JUNCTIONS: sql_batch.add(handler(line, demand_outside)) elif s == PATTERNS: sql_batch.add(handler(line, current_pattern not in variable_patterns)) + elif s==BOUND: + continue else: sql_batch.add(handler(line)) @@ -310,12 +318,12 @@ def import_inp(project: str, cs: ChangeSet, version: str = '3') -> bool: os.remove(filename) _print_time(f'Start writing temp file "{filename}"...') - with open(filename, 'w') as f: + with open(filename, 'w',encoding="GBK") as f: f.write(str(cs.operations[0]['inp'])) _print_time(f'End writing temp file "{filename}"...') result = read_inp(project, filename, version) - os.remove(filename) + #os.remove(filename) return result diff --git a/api/inp_out.py b/api/inp_out.py index 38cbff2..52b0aa5 100644 --- a/api/inp_out.py +++ b/api/inp_out.py @@ -51,7 +51,7 @@ def dump_inp(project: str, inp: str, version: str = '3'): if os.path.exists(path): os.remove(path) - file = open(path, mode='w') + file = open(path, mode='w',encoding="UTF-8") for name in section_name: if name == TITLE: diff --git a/api/s0_base.py b/api/s0_base.py index bace826..6f42f98 100644 --- a/api/s0_base.py +++ b/api/s0_base.py @@ -7,6 +7,7 @@ _NODE = '_node' _LINK = '_link' _CURVE = '_curve' _PATTERN = '_pattern' +_REGION = '_region' JUNCTION = 'junction' RESERVOIR = 'reservoir' @@ -18,6 +19,8 @@ VALVE = 'valve' PATTERN = 'pattern' CURVE = 'curve' +REGION = 'region' + def _get_from(name: str, id: str, base_type: str) -> Row | None: with conn[name].cursor(row_factory=dict_row) as cur: @@ -70,6 +73,9 @@ def is_curve(name: str, id: str) -> bool: def is_pattern(name: str, id: str) -> bool: return _get_from(name, id, _PATTERN) != None +def is_region(name: str, id: str) -> bool: + return _get_from(name, id, _REGION) != None + def _get_all(name: str, base_type: str) -> list[str]: ids : list[str] = [] @@ -95,6 +101,8 @@ def get_curves(name: str) -> list[str]: def get_patterns(name: str) -> list[str]: return _get_all(name, _PATTERN) +def get_regions(name: str) -> list[str]: + return _get_all(name, _REGION) def get_node_links(name: str, id: str) -> list[str]: with conn[name].cursor(row_factory=dict_row) as cur: diff --git a/api/s32_region.py b/api/s32_region.py index 9d90e20..355e81d 100644 --- a/api/s32_region.py +++ b/api/s32_region.py @@ -81,3 +81,11 @@ def delete_region(name: str, cs: ChangeSet) -> ChangeSet: if get_region(name, cs.operations[0]['id']) == {}: return ChangeSet() return execute_command(name, _delete_region(name, cs)) + +def inp_in_region(line: str) -> str: + tokens = line.split() + return str(f"insert into _region (id, type) values ('{tokens[0]}', '{tokens[1]}');") + +def inp_in_bound(line: str) -> str: + tokens = line.split() + return tokens[0] \ No newline at end of file diff --git a/api/sections.py b/api/sections.py index 7cd74f6..0a2c0e4 100644 --- a/api/sections.py +++ b/api/sections.py @@ -63,6 +63,8 @@ REPORT = 'REPORT' OPTIONS = 'OPTIONS' COORDINATES = 'COORDINATES' VERTICES = 'VERTICES' +REGION='REGION' +BOUND='BOUND' LABELS = 'LABELS' BACKDROP = 'BACKDROP' END = 'END' @@ -72,4 +74,4 @@ section_name = [TITLE, JUNCTIONS, RESERVOIRS, TANKS, PIPES, PATTERNS, CURVES, CONTROLS, RULES, ENERGY, EMITTERS, QUALITY, SOURCES, REACTIONS, MIXING, TIMES, REPORT, OPTIONS, COORDINATES, VERTICES, - LABELS, BACKDROP, END] + REGION, BOUND, LABELS, BACKDROP, END]