Specialize pattern and curve
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from .project import *
|
from .project import *
|
||||||
from .database import ChangeSet
|
from .database import ChangeSet, write
|
||||||
from .sections import *
|
from .sections import *
|
||||||
from .s1_title import inp_in_title_new
|
from .s1_title import inp_in_title_new
|
||||||
from .s2_junctions import inp_in_junction_new
|
from .s2_junctions import inp_in_junction_new
|
||||||
@@ -91,25 +91,8 @@ _level_4 = {
|
|||||||
_UNKNOWN = 'UNKNOWN'
|
_UNKNOWN = 'UNKNOWN'
|
||||||
|
|
||||||
|
|
||||||
class SectionRange:
|
def _get_offset(inp: str) -> dict[str, list[int]]:
|
||||||
def __init__(self) -> None:
|
offset: dict[str, list[int]] = {}
|
||||||
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:
|
with open(inp) as f:
|
||||||
while True:
|
while True:
|
||||||
@@ -121,23 +104,39 @@ def parse_file(project: str, inp: str) -> None:
|
|||||||
if line.startswith('['):
|
if line.startswith('['):
|
||||||
for s in section_name:
|
for s in section_name:
|
||||||
if line.startswith(f'[{s}'):
|
if line.startswith(f'[{s}'):
|
||||||
|
if s not in offset:
|
||||||
|
offset[s] = []
|
||||||
offset[s].append(f.tell())
|
offset[s].append(f.tell())
|
||||||
break
|
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
|
# parse the whole section rather than line
|
||||||
sections : dict[str, list[str]]= {}
|
sections : dict[str, list[str]]= {}
|
||||||
for [s, t] in _handler.items():
|
for [s, t] in _handler.items():
|
||||||
if t[0] == _S:
|
if t[0] == _S:
|
||||||
sections[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:
|
with open(inp) as f:
|
||||||
for s in levels:
|
for s in levels:
|
||||||
is_s = _handler[s][0] == _S
|
if s not in offset:
|
||||||
|
continue
|
||||||
|
|
||||||
for o in offset[s]:
|
is_s = _handler[s][0] == _S
|
||||||
f.seek(o)
|
handler = _handler[s][1]
|
||||||
|
|
||||||
|
for ptr in offset[s]:
|
||||||
|
f.seek(ptr)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line = f.readline()
|
line = f.readline()
|
||||||
@@ -145,17 +144,44 @@ def parse_file(project: str, inp: str) -> None:
|
|||||||
break
|
break
|
||||||
|
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
if line == '' or line.startswith('['):
|
||||||
|
break
|
||||||
|
|
||||||
if is_s:
|
if is_s:
|
||||||
sections[s].append(line)
|
sections[s].append(line)
|
||||||
else:
|
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)
|
f.seek(0)
|
||||||
|
|
||||||
|
if is_s and handler != None:
|
||||||
|
handler(project, sections[s])
|
||||||
|
|
||||||
|
|
||||||
def parse_inp(project: str, inp: str) -> None:
|
def parse_inp(project: str, inp: str) -> None:
|
||||||
pass
|
parse_file(project, inp)
|
||||||
|
|
||||||
|
|
||||||
def read_inp_new(project: str, inp: str) -> bool:
|
def read_inp_new(project: str, inp: str) -> bool:
|
||||||
|
|||||||
@@ -126,26 +126,10 @@ def inp_in_pattern(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_pattern_new(name: str, section: list[str]) -> None:
|
def inp_in_pattern_new(name: str, line: str) -> None:
|
||||||
descs = {}
|
tokens = line.split()
|
||||||
patterns: list[str] = []
|
for token in tokens[1:]:
|
||||||
|
write(name, f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});")
|
||||||
count = len(section)
|
|
||||||
for i in range(0, count):
|
|
||||||
if section[i].startswith(';'):
|
|
||||||
# this is description
|
|
||||||
next = i + 1
|
|
||||||
if next < count and section[next].startswith(';') == False:
|
|
||||||
next_tokens = section[next].split()
|
|
||||||
descs[next_tokens[0]] = section[i].removeprefix(';')
|
|
||||||
continue
|
|
||||||
|
|
||||||
tokens = section[i].split()
|
|
||||||
if tokens[0] not in patterns:
|
|
||||||
patterns.append(tokens[0])
|
|
||||||
write(name, f"insert into _pattern (id) values ('{tokens[0]}');")
|
|
||||||
for token in tokens[1:]:
|
|
||||||
write(name, f"insert into patterns (id, factor) values ('{tokens[0]}', {float(token)});")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_pattern(name: str) -> list[str]:
|
def inp_out_pattern(name: str) -> list[str]:
|
||||||
|
|||||||
@@ -159,30 +159,9 @@ def inp_in_curve(section: list[str]) -> ChangeSet:
|
|||||||
return cs
|
return cs
|
||||||
|
|
||||||
|
|
||||||
def inp_in_curve_new(name: str, section: list[str]) -> None:
|
def inp_in_curve_new(name: str, line: str) -> None:
|
||||||
types = {}
|
tokens = line.split()
|
||||||
descs = {}
|
write(name, f"insert into curves (id, x, y) values ('{tokens[0]}', {float(tokens[1])}, {float(tokens[2])});")
|
||||||
curves: list[str] = []
|
|
||||||
|
|
||||||
count = len(section)
|
|
||||||
for i in range(0, count):
|
|
||||||
if section[i].startswith(';'):
|
|
||||||
# ;type: desc
|
|
||||||
type_plus_desc = section[i].removeprefix(';')
|
|
||||||
type_plus_desc_tokens = type_plus_desc.split(':')
|
|
||||||
next = i + 1
|
|
||||||
if next < count and section[next].startswith(';') == False:
|
|
||||||
next_tokens = section[next].split()
|
|
||||||
types[next_tokens[0]] = type_plus_desc_tokens[0].strip().upper()
|
|
||||||
if len(type_plus_desc_tokens) > 1:
|
|
||||||
descs[next_tokens[0]] = type_plus_desc_tokens[1].strip()
|
|
||||||
continue
|
|
||||||
|
|
||||||
tokens = section[i].split()
|
|
||||||
if tokens[0] not in curves:
|
|
||||||
curves.append(tokens[0])
|
|
||||||
write(name, f"insert into _curve (id, type) values ('{tokens[0]}', '{types[tokens[0]]}');")
|
|
||||||
write(name, f"insert into curves (id, x, y) values ('{tokens[0]}', {float(tokens[1])}, {float(tokens[2])});")
|
|
||||||
|
|
||||||
|
|
||||||
def inp_out_curve(name: str) -> list[str]:
|
def inp_out_curve(name: str) -> list[str]:
|
||||||
|
|||||||
Reference in New Issue
Block a user