diff --git a/chatmastermind/chat.py b/chatmastermind/chat.py index f3637de..8d64f86 100644 --- a/chatmastermind/chat.py +++ b/chatmastermind/chat.py @@ -7,12 +7,16 @@ from pprint import PrettyPrinter from pydoc import pager from dataclasses import dataclass from typing import TypeVar, Type, Optional, ClassVar, Any, Callable, Literal +from .configuration import default_config_file from .message import Message, MessageFilter, MessageError, message_in from .tags import Tag ChatInst = TypeVar('ChatInst', bound='Chat') ChatDBInst = TypeVar('ChatDBInst', bound='ChatDB') +db_next_file = '.next' +ignored_files = [db_next_file, default_config_file] + class ChatError(Exception): pass @@ -45,7 +49,9 @@ def read_dir(dir_path: Path, messages: list[Message] = [] file_iter = dir_path.glob(glob) if glob else dir_path.iterdir() for file_path in sorted(file_iter): - if file_path.is_file() and file_path.suffix in Message.file_suffixes: + if (file_path.is_file() + and file_path.name not in ignored_files # noqa: W503 + and file_path.suffix in Message.file_suffixes): # noqa: W503 try: message = Message.from_file(file_path, mfilter) if message: @@ -235,7 +241,7 @@ class ChatDB(Chat): def __post_init__(self) -> None: # contains the latest message ID - self.next_fname = self.db_path / '.next' + self.next_path = self.db_path / db_next_file # make all paths absolute self.cache_path = self.cache_path.absolute() self.db_path = self.db_path.absolute() @@ -274,7 +280,7 @@ class ChatDB(Chat): def get_next_fid(self) -> int: try: - with open(self.next_fname, 'r') as f: + with open(self.next_path, 'r') as f: next_fid = int(f.read()) + 1 self.set_next_fid(next_fid) return next_fid @@ -283,7 +289,7 @@ class ChatDB(Chat): return 1 def set_next_fid(self, fid: int) -> None: - with open(self.next_fname, 'w') as f: + with open(self.next_path, 'w') as f: f.write(f'{fid}') def read_db(self) -> None: diff --git a/chatmastermind/configuration.py b/chatmastermind/configuration.py index 1415eb2..d1f9601 100644 --- a/chatmastermind/configuration.py +++ b/chatmastermind/configuration.py @@ -9,7 +9,7 @@ OpenAIConfigInst = TypeVar('OpenAIConfigInst', bound='OpenAIConfig') supported_ais: list[str] = ['openai'] -default_config_path = '.config.yaml' +default_config_file = '.config.yaml' class ConfigError(Exception): diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 7e18185..fcd1b2f 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -7,7 +7,7 @@ import argcomplete import argparse from pathlib import Path from typing import Any -from .configuration import Config, default_config_path +from .configuration import Config, default_config_file from .message import Message from .commands.question import question_cmd from .commands.tags import tags_cmd @@ -24,7 +24,7 @@ def tags_completer(prefix: str, parsed_args: Any, **kwargs: Any) -> list[str]: def create_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( description="ChatMastermind is a Python application that automates conversation with AI") - parser.add_argument('-C', '--config', help='Config file name.', default=default_config_path) + parser.add_argument('-C', '--config', help='Config file name.', default=default_config_file) # subcommand-parser cmdparser = parser.add_subparsers(dest='command', diff --git a/tests/test_chat.py b/tests/test_chat.py index ca74725..ff44cda 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -241,7 +241,7 @@ class TestChatDB(unittest.TestCase): self.assertEqual(chat_db.get_next_fid(), 5) self.assertEqual(chat_db.get_next_fid(), 6) self.assertEqual(chat_db.get_next_fid(), 7) - with open(chat_db.next_fname, 'r') as f: + with open(chat_db.next_path, 'r') as f: self.assertEqual(f.read(), '7') def test_chat_db_write(self) -> None: