195 lines
4.2 KiB
Python
195 lines
4.2 KiB
Python
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
|
|
|
|
_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, None),
|
|
PUMPS : (_L, None),
|
|
VALVES : (_L, None),
|
|
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'
|
|
|
|
|
|
class SectionRange:
|
|
def __init__(self) -> None:
|
|
start = 0
|
|
end = 0
|
|
|
|
|
|
class SectionOffset:
|
|
def __init__(self, s: str) -> None:
|
|
name = s
|
|
ranges: list[SectionRange] = []
|
|
|
|
|
|
def parse_file(project: str, inp: str) -> None:
|
|
# find section position
|
|
offset_list = []
|
|
|
|
offset: dict[str, list[tuple[int, int]]] = {}
|
|
for s in section_name:
|
|
offset[s] = []
|
|
|
|
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}'):
|
|
offset[s].append(f.tell())
|
|
break
|
|
|
|
# parse the whole section rather than line
|
|
sections : dict[str, list[str]]= {}
|
|
for [s, t] in _handler.items():
|
|
if t[0] == _S:
|
|
sections[s] = []
|
|
|
|
levels = _level_1 | _level_2 | _level_3 | _level_4
|
|
|
|
with open(inp) as f:
|
|
for s in levels:
|
|
is_s = _handler[s][0] == _S
|
|
|
|
for o in offset[s]:
|
|
f.seek(o)
|
|
|
|
while True:
|
|
line = f.readline()
|
|
if not line:
|
|
break
|
|
|
|
line = line.strip()
|
|
|
|
if is_s:
|
|
sections[s].append(line)
|
|
else:
|
|
pass
|
|
|
|
f.seek(0)
|
|
|
|
|
|
def parse_inp(project: str, inp: str) -> None:
|
|
pass
|
|
|
|
|
|
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)
|
|
|
|
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
|