243 lines
6.9 KiB
Python
243 lines
6.9 KiB
Python
from .project import *
|
|
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
|
|
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 .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
|
|
from .s14_rules import inp_in_rule_new
|
|
from .s15_energy import inp_in_energy_new
|
|
from .s16_emitters import inp_in_emitter_new
|
|
from .s17_quality import inp_in_quality_new
|
|
from .s18_sources import inp_in_source_new
|
|
from .s19_reactions import inp_in_reaction_new
|
|
from .s20_mixing import inp_in_mixing_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 .s24_coordinates import inp_in_coord_new
|
|
from .s25_vertices import inp_in_vertex_new
|
|
from .s26_labels import inp_in_label_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, 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),
|
|
RULES : (_L, inp_in_rule_new),
|
|
ENERGY : (_L, inp_in_energy_new),
|
|
EMITTERS : (_L, inp_in_emitter_new),
|
|
QUALITY : (_L, inp_in_quality_new),
|
|
SOURCES : (_L, inp_in_source_new),
|
|
REACTIONS : (_L, inp_in_reaction_new),
|
|
MIXING : (_L, inp_in_mixing_new),
|
|
TIMES : (_S, inp_in_time_new),
|
|
REPORT : (_S, inp_in_report_new),
|
|
OPTIONS : (_S, inp_in_option_new),
|
|
COORDINATES : (_L, inp_in_coord_new),
|
|
VERTICES : (_L, inp_in_vertex_new),
|
|
LABELS : (_L, inp_in_label_new),
|
|
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,
|
|
]
|
|
|
|
|
|
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
|
|
demand_junction = 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.startswith('['):
|
|
break
|
|
elif line == '':
|
|
continue
|
|
|
|
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
|
|
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
|
|
|
|
handler(project, line)
|
|
|
|
|
|
f.seek(0)
|
|
|
|
if is_s:
|
|
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
|