chat: ChatDB now correctly ignores files that contain no valid messages
This commit is contained in:
parent
071871f929
commit
6ad404899e
@ -57,7 +57,7 @@ def read_dir(dir_path: Path,
|
|||||||
if message:
|
if message:
|
||||||
messages.append(message)
|
messages.append(message)
|
||||||
except MessageError as e:
|
except MessageError as e:
|
||||||
print(f"Error processing message in '{file_path}': {str(e)}")
|
print(f"WARNING: Skipping message in '{file_path}': {str(e)}")
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -370,7 +370,7 @@ class Message():
|
|||||||
try:
|
try:
|
||||||
question_idx = text.index(Question.txt_header) + 1
|
question_idx = text.index(Question.txt_header) + 1
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise MessageError(f"Question header '{Question.txt_header}' not found in '{file_path}'")
|
raise MessageError(f"'{file_path}' does not contain a valid message")
|
||||||
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])
|
||||||
@ -390,8 +390,11 @@ class Message():
|
|||||||
* Message.model_yaml_key: str [Optional]
|
* Message.model_yaml_key: str [Optional]
|
||||||
"""
|
"""
|
||||||
with open(file_path, "r") as fd:
|
with open(file_path, "r") as fd:
|
||||||
data = yaml.load(fd, Loader=yaml.FullLoader)
|
try:
|
||||||
data[cls.file_yaml_key] = file_path
|
data = yaml.load(fd, Loader=yaml.FullLoader)
|
||||||
|
data[cls.file_yaml_key] = file_path
|
||||||
|
except Exception:
|
||||||
|
raise MessageError(f"'{file_path}' does not contain a valid message")
|
||||||
return cls.from_dict(data)
|
return cls.from_dict(data)
|
||||||
|
|
||||||
def to_str(self, with_tags: bool = False, with_file: bool = False, source_code_only: bool = False) -> str:
|
def to_str(self, with_tags: bool = False, with_file: bool = False, source_code_only: bool = False) -> str:
|
||||||
|
|||||||
@ -156,13 +156,18 @@ class TestChatDB(unittest.TestCase):
|
|||||||
next_fname = pathlib.Path(self.db_path.name) / '.next'
|
next_fname = pathlib.Path(self.db_path.name) / '.next'
|
||||||
with open(next_fname, 'w') as f:
|
with open(next_fname, 'w') as f:
|
||||||
f.write('4')
|
f.write('4')
|
||||||
|
# add some "trash" in order to test if it's correctly handled / ignored
|
||||||
|
self.trash_files = ['.config.yaml', 'foo.yaml', 'bla.txt']
|
||||||
|
for file in self.trash_files:
|
||||||
|
with open(pathlib.Path(self.db_path.name) / file, 'w') as f:
|
||||||
|
f.write('test trash')
|
||||||
|
|
||||||
def message_list(self, tmp_dir: tempfile.TemporaryDirectory) -> list[pathlib.Path]:
|
def message_list(self, tmp_dir: tempfile.TemporaryDirectory) -> list[pathlib.Path]:
|
||||||
"""
|
"""
|
||||||
List all Message files in the given TemporaryDirectory.
|
List all Message files in the given TemporaryDirectory.
|
||||||
"""
|
"""
|
||||||
# exclude '.next'
|
# exclude '.next'
|
||||||
return list(pathlib.Path(tmp_dir.name).glob('*.[ty]*'))
|
return [f for f in pathlib.Path(tmp_dir.name).glob('*.[ty]*') if f.name not in self.trash_files]
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
self.db_path.cleanup()
|
self.db_path.cleanup()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user