Specialize pattern and curve
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from .project import *
|
||||
from .database import ChangeSet
|
||||
from .database import ChangeSet, write
|
||||
from .sections import *
|
||||
from .s1_title import inp_in_title_new
|
||||
from .s2_junctions import inp_in_junction_new
|
||||
@@ -91,25 +91,8 @@ _level_4 = {
|
||||
_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] = []
|
||||
def _get_offset(inp: str) -> dict[str, list[int]]:
|
||||
offset: dict[str, list[int]] = {}
|
||||
|
||||
with open(inp) as f:
|
||||
while True:
|
||||
@@ -121,23 +104,39 @@ def parse_file(project: str, inp: str) -> None:
|
||||
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)
|
||||
print(offset)
|
||||
|
||||
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] = []
|
||||
|
||||
levels = _level_1 | _level_2 | _level_3 | _level_4
|
||||
pattern_desc_line = None
|
||||
curve_type_desc_line = None
|
||||
|
||||
with open(inp) as f:
|
||||
for s in levels:
|
||||
is_s = _handler[s][0] == _S
|
||||
if s not in offset:
|
||||
continue
|
||||
|
||||
for o in offset[s]:
|
||||
f.seek(o)
|
||||
is_s = _handler[s][0] == _S
|
||||
handler = _handler[s][1]
|
||||
|
||||
for ptr in offset[s]:
|
||||
f.seek(ptr)
|
||||
|
||||
while True:
|
||||
line = f.readline()
|
||||
@@ -145,17 +144,44 @@ def parse_file(project: str, inp: str) -> None:
|
||||
break
|
||||
|
||||
line = line.strip()
|
||||
if line == '' or line.startswith('['):
|
||||
break
|
||||
|
||||
if is_s:
|
||||
sections[s].append(line)
|
||||
else:
|
||||
pass
|
||||
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:
|
||||
pass
|
||||
parse_file(project, inp)
|
||||
|
||||
|
||||
def read_inp_new(project: str, inp: str) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user