message: fixed Answer header for TXT format

This commit is contained in:
juk0de 2023-09-02 10:00:08 +02:00
parent fa292fb73a
commit 4b0f40bccd

View File

@ -96,7 +96,7 @@ class AILine(str):
def __new__(cls: Type[AILineInst], string: str) -> AILineInst: def __new__(cls: Type[AILineInst], string: str) -> AILineInst:
if not string.startswith(cls.prefix): if not string.startswith(cls.prefix):
raise TagError(f"AILine '{string}' is missing prefix '{cls.prefix}'") raise MessageError(f"AILine '{string}' is missing prefix '{cls.prefix}'")
instance = super().__new__(cls, string) instance = super().__new__(cls, string)
return instance return instance
@ -116,7 +116,7 @@ class ModelLine(str):
def __new__(cls: Type[ModelLineInst], string: str) -> ModelLineInst: def __new__(cls: Type[ModelLineInst], string: str) -> ModelLineInst:
if not string.startswith(cls.prefix): if not string.startswith(cls.prefix):
raise TagError(f"ModelLine '{string}' is missing prefix '{cls.prefix}'") raise MessageError(f"ModelLine '{string}' is missing prefix '{cls.prefix}'")
instance = super().__new__(cls, string) instance = super().__new__(cls, string)
return instance return instance
@ -133,7 +133,7 @@ class Answer(str):
A single answer with a defined header. A single answer with a defined header.
""" """
tokens: int = 0 # tokens used by this answer tokens: int = 0 # tokens used by this answer
txt_header: ClassVar[str] = '=== ANSWER ===' txt_header: ClassVar[str] = '==== ANSWER ===='
yaml_key: ClassVar[str] = 'answer' yaml_key: ClassVar[str] = 'answer'
def __new__(cls: Type[AnswerInst], string: str) -> AnswerInst: def __new__(cls: Type[AnswerInst], string: str) -> AnswerInst:
@ -355,17 +355,20 @@ class Message():
try: try:
pos = fd.tell() pos = fd.tell()
ai = AILine(fd.readline()).ai() ai = AILine(fd.readline()).ai()
except TagError: except MessageError:
fd.seek(pos) fd.seek(pos)
# ModelLine (Optional) # ModelLine (Optional)
try: try:
pos = fd.tell() pos = fd.tell()
model = ModelLine(fd.readline()).model() model = ModelLine(fd.readline()).model()
except TagError: except MessageError:
fd.seek(pos) fd.seek(pos)
# Question and Answer # Question and Answer
text = fd.read().strip().split('\n') text = fd.read().strip().split('\n')
question_idx = text.index(Question.txt_header) + 1 try:
question_idx = text.index(Question.txt_header) + 1
except ValueError:
raise MessageError(f"Question header '{Question.txt_header}' not found in '{file_path}'")
try: try:
answer_idx = text.index(Answer.txt_header) answer_idx = text.index(Answer.txt_header)
question = Question.from_list(text[question_idx:answer_idx]) question = Question.from_list(text[question_idx:answer_idx])