from .project import * from .database import ChangeSet from .sections import * from .s1_title import inp_in_title_new from .s2_junctions import inp_in_junction_new from .s3_reservoirs import inp_in_reservoir_new from .s4_tanks import inp_in_tank_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 from .s14_rules import inp_in_rule_new from .s21_times import inp_in_time_new from .s22_report import inp_in_report_new from .s23_options import inp_in_option_new from .s27_backdrop import inp_in_backdrop_new _UNKNOWN = 'UNKNOWN' _level_1 = { TITLE, PATTERNS, CURVES, CONTROLS, RULES, TIMES, REPORT, OPTIONS, BACKDROP, } _level_2 = { JUNCTIONS, RESERVOIRS, TANKS, } _level_3 = { PIPES, PUMPS, VALVES, DEMANDS, EMITTERS, QUALITY, SOURCES, MIXING, COORDINATES, LABELS, } _level_4 = { TAGS, STATUS, ENERGY, REACTIONS, VERTICES, } def scan1(project: str, inp: str) -> list[str]: handlers = { TITLE: inp_in_title_new, #1 PATTERNS: inp_in_pattern_new, #11 CURVES: inp_in_curve_new, #12 CONTROLS: inp_in_control_new, #13 RULES: inp_in_rule_new, #14 TIMES: inp_in_time_new, #21 REPORT: inp_in_report_new, #22 OPTIONS: inp_in_option_new, #23 BACKDROP: inp_in_backdrop_new,#27 } curr_section = _UNKNOWN prev_section = _UNKNOWN inp_sections: list[str] = [] sections : dict[str, list[str]]= {} for c in handlers.keys(): sections[c] = [] for line in open(inp): line = line.strip() if line.startswith('['): the_section = _UNKNOWN for s in section_name: if line.startswith(f'[{s}'): the_section = s inp_sections.append(s) break prev_section = curr_section if prev_section in handlers.keys(): handlers[prev_section](project, sections[prev_section]) sections[prev_section].clear() curr_section = the_section continue elif line == '': continue if curr_section in handlers.keys(): sections[curr_section].append(line) return inp_sections def scan2(project: str, inp: str) -> None: handlers = { JUNCTIONS: inp_in_junction_new, #2 RESERVOIRS: inp_in_reservoir_new, #3 TANKS: inp_in_tank_new, #4 } curr_section = _UNKNOWN sections : dict[str, list[str]]= {} for c in handlers.keys(): sections[c] = [] for line in open(inp): line = line.strip() if line.startswith('['): is_candidate = False for s in handlers.keys(): if line.startswith(f'[{s}'): curr_section = s is_candidate = True break if is_candidate: continue else: curr_section = _UNKNOWN continue elif line == '': continue if curr_section in handlers.keys(): handlers[curr_section](project, line) def scan3(project: str, inp: str) -> None: handlers = { } curr_section = _UNKNOWN sections : dict[str, list[str]]= {} for c in handlers.keys(): sections[c] = [] for line in open(inp): line = line.strip() if line.startswith('['): is_candidate = False for s in handlers.keys(): if line.startswith(f'[{s}'): curr_section = s is_candidate = True break if is_candidate: continue else: curr_section = _UNKNOWN continue elif line == '': continue if curr_section in handlers.keys(): handlers[curr_section](project, line) def scan4(project: str, inp: str) -> None: handlers = { } curr_section = _UNKNOWN sections : dict[str, list[str]]= {} for c in handlers.keys(): sections[c] = [] for line in open(inp): line = line.strip() if line.startswith('['): is_candidate = False for s in handlers.keys(): if line.startswith(f'[{s}'): curr_section = s is_candidate = True break if is_candidate: continue else: curr_section = _UNKNOWN continue elif line == '': continue if curr_section in handlers.keys(): handlers[curr_section](project, line) def parse_inp(project: str, inp: str) -> None: scan1(project, inp) scan2(project, inp) scan3(project, inp) scan4(project, inp) def read_inp_new(project: str, inp: str) -> bool: if is_project_open(project): close_project(project) if have_project(project): delete_project(project) create_project(project) open_project(project) parse_inp(project, inp) close_project(project) return True def import_inp_new(project: str, cs: ChangeSet) -> bool: if is_project_open(project): close_project(project) if have_project(project): delete_project(project) create_project(project) open_project(project) close_project(project) return True