Refine inp in

This commit is contained in:
WQY\qiong
2023-03-15 20:03:55 +08:00
parent 24a39aeca6
commit ed6f6daca0

View File

@@ -14,8 +14,39 @@ 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'
_UNKNOWN = 'UNKNOWN'
_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,
@@ -56,174 +87,75 @@ _level_4 = {
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
_UNKNOWN = 'UNKNOWN'
inp_sections: list[str] = []
sections : dict[str, list[str]]= {}
for c in handlers.keys():
sections[c] = []
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
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)
offset[s].append(f.tell())
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
# parse the whole section rather than line
sections : dict[str, list[str]]= {}
for c in handlers.keys():
sections[c] = []
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
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
if is_s:
sections[s].append(line)
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)
pass
f.seek(0)
def parse_inp(project: str, inp: str) -> None:
scan1(project, inp)
scan2(project, inp)
scan3(project, inp)
scan4(project, inp)
pass
def read_inp_new(project: str, inp: str) -> bool:
@@ -236,10 +168,14 @@ def read_inp_new(project: str, inp: str) -> bool:
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