from .project import * from .database import ChangeSet, write 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 .s5_pipes import inp_in_pipe_new from .s6_pumps import inp_in_pump_new from .s7_valves import inp_in_valve_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 _S = 'S' _L = 'L' _handler = { TITLE : (_S, inp_in_title_new), JUNCTIONS : (_L, inp_in_junction_new), RESERVOIRS : (_L, inp_in_reservoir_new), TANKS : (_L, inp_in_tank_new), 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), PATTERNS : (_L, inp_in_pattern_new), CURVES : (_L, inp_in_curve_new), CONTROLS : (_L, inp_in_control_new), RULES : (_L, inp_in_rule_new), ENERGY : (_L, None), EMITTERS : (_L, None), QUALITY : (_L, None), SOURCES : (_L, None), REACTIONS : (_L, None), MIXING : (_L, None), TIMES : (_S, inp_in_time_new), REPORT : (_S, inp_in_report_new), OPTIONS : (_S, inp_in_option_new), COORDINATES : (_L, None), VERTICES : (_L, None), LABELS : (_L, None), BACKDROP : (_S, inp_in_backdrop_new), #END : 'END', } _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, ] _UNKNOWN = 'UNKNOWN' def _get_offset(inp: str) -> dict[str, list[int]]: offset: dict[str, list[int]] = {} with open(inp) as f: while True: line = f.readline() if not line: break line = line.strip() if line.startswith('['): for s in section_name: if line.startswith(f'[{s}'): if s not in offset: offset[s] = [] offset[s].append(f.tell()) break return offset def parse_file(project: str, inp: str) -> None: offset = _get_offset(inp) levels = _level_1 + _level_2 + _level_3 + _level_4 # parse the whole section rather than line sections : dict[str, list[str]]= {} for [s, t] in _handler.items(): if t[0] == _S: sections[s] = [] pattern_desc_line = None curve_type_desc_line = None with open(inp) as f: for s in levels: if s not in offset: continue is_s = _handler[s][0] == _S handler = _handler[s][1] for ptr in offset[s]: f.seek(ptr) while True: line = f.readline() if not line: break line = line.strip() if line == '' or line.startswith('['): break if is_s: sections[s].append(line) else: if line.startswith(';'): line = line.removeprefix(';') if s == PATTERNS: # ;desc pattern_desc_line = line elif s == CURVES: # ;type: desc curve_type_desc_line = line continue if s == PATTERNS: if pattern_desc_line != None: tokens = line.split() write(project, f"insert into _pattern (id) values ('{tokens[0]}');") pattern_desc_line = None elif s == CURVES: if curve_type_desc_line != None: type_and_desc = curve_type_desc_line.split(':') tokens = line.split() write(project, f"insert into _curve (id, type) values ('{tokens[0]}', '{type_and_desc[0].strip()}');") curve_type_desc_line = None if handler != None: handler(project, line) f.seek(0) if is_s and handler != None: handler(project, sections[s]) def parse_inp(project: str, inp: str) -> None: parse_file(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) '''try: parse_inp(project, inp) except: close_project(project) delete_project(project) return False''' 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