Refine inp in
This commit is contained in:
@@ -14,8 +14,39 @@ from .s22_report import inp_in_report_new
|
|||||||
from .s23_options import inp_in_option_new
|
from .s23_options import inp_in_option_new
|
||||||
from .s27_backdrop import inp_in_backdrop_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 = {
|
_level_1 = {
|
||||||
TITLE,
|
TITLE,
|
||||||
@@ -56,174 +87,75 @@ _level_4 = {
|
|||||||
VERTICES,
|
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
|
_UNKNOWN = 'UNKNOWN'
|
||||||
prev_section = _UNKNOWN
|
|
||||||
|
|
||||||
inp_sections: list[str] = []
|
|
||||||
|
|
||||||
|
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]]= {}
|
sections : dict[str, list[str]]= {}
|
||||||
for c in handlers.keys():
|
for [s, t] in _handler.items():
|
||||||
sections[c] = []
|
if t[0] == _S:
|
||||||
|
sections[s] = []
|
||||||
|
|
||||||
for line in open(inp):
|
levels = _level_1 | _level_2 | _level_3 | _level_4
|
||||||
line = line.strip()
|
|
||||||
|
|
||||||
if line.startswith('['):
|
with open(inp) as f:
|
||||||
the_section = _UNKNOWN
|
for s in levels:
|
||||||
|
is_s = _handler[s][0] == _S
|
||||||
|
|
||||||
for s in section_name:
|
for o in offset[s]:
|
||||||
if line.startswith(f'[{s}'):
|
f.seek(o)
|
||||||
the_section = s
|
|
||||||
inp_sections.append(s)
|
|
||||||
break
|
|
||||||
|
|
||||||
prev_section = curr_section
|
while True:
|
||||||
if prev_section in handlers.keys():
|
line = f.readline()
|
||||||
handlers[prev_section](project, sections[prev_section])
|
if not line:
|
||||||
sections[prev_section].clear()
|
break
|
||||||
|
|
||||||
curr_section = the_section
|
line = line.strip()
|
||||||
|
|
||||||
continue
|
if is_s:
|
||||||
|
sections[s].append(line)
|
||||||
elif line == '':
|
else:
|
||||||
continue
|
pass
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
f.seek(0)
|
||||||
|
|
||||||
|
|
||||||
def parse_inp(project: str, inp: str) -> None:
|
def parse_inp(project: str, inp: str) -> None:
|
||||||
scan1(project, inp)
|
pass
|
||||||
scan2(project, inp)
|
|
||||||
scan3(project, inp)
|
|
||||||
scan4(project, inp)
|
|
||||||
|
|
||||||
|
|
||||||
def read_inp_new(project: str, inp: str) -> bool:
|
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)
|
create_project(project)
|
||||||
open_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)
|
close_project(project)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user