From f6109949c816d350244d396a3d13ea302f8c38b8 Mon Sep 17 00:00:00 2001 From: juk0de Date: Thu, 14 Sep 2023 16:05:18 +0200 Subject: [PATCH] chat: ChatDB now correctly ignores files that contain no valid messages --- chatmastermind/chat.py | 2 +- chatmastermind/message.py | 11 +++++++---- tests/test_chat.py | 14 +++++++++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/chatmastermind/chat.py b/chatmastermind/chat.py index 8d64f86..c1464c3 100644 --- a/chatmastermind/chat.py +++ b/chatmastermind/chat.py @@ -57,7 +57,7 @@ def read_dir(dir_path: Path, if message: messages.append(message) 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 diff --git a/chatmastermind/message.py b/chatmastermind/message.py index 64929a3..402a6d1 100644 --- a/chatmastermind/message.py +++ b/chatmastermind/message.py @@ -370,7 +370,7 @@ class Message(): try: question_idx = text.index(Question.txt_header) + 1 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: answer_idx = text.index(Answer.txt_header) question = Question.from_list(text[question_idx:answer_idx]) @@ -390,9 +390,12 @@ class Message(): * Message.model_yaml_key: str [Optional] """ with open(file_path, "r") as fd: - data = yaml.load(fd, Loader=yaml.FullLoader) - data[cls.file_yaml_key] = file_path - return cls.from_dict(data) + try: + data = yaml.load(fd, Loader=yaml.FullLoader) + data[cls.file_yaml_key] = file_path + return cls.from_dict(data) + except Exception: + raise MessageError(f"'{file_path}' does not contain a valid message") def to_str(self, with_tags: bool = False, with_file: bool = False, source_code_only: bool = False) -> str: """ diff --git a/tests/test_chat.py b/tests/test_chat.py index ff44cda..bca3a9f 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -2,6 +2,7 @@ import unittest import pathlib import tempfile import time +import yaml from io import StringIO from unittest.mock import patch from chatmastermind.tags import TagLine @@ -156,13 +157,24 @@ class TestChatDB(unittest.TestCase): next_fname = pathlib.Path(self.db_path.name) / '.next' with open(next_fname, 'w') as f: 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') + # also create a file with actual yaml content + with open(pathlib.Path(self.db_path.name) / 'content.yaml', 'w') as f: + yaml.dump({'key': 'value'}, f) + self.trash_files.append('content.yaml') + + self.maxDiff = None def message_list(self, tmp_dir: tempfile.TemporaryDirectory) -> list[pathlib.Path]: """ List all Message files in the given TemporaryDirectory. """ # 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: self.db_path.cleanup()