chat: added clear_cache() function and test

This commit is contained in:
2023-09-02 09:19:47 +02:00
committed by Oleksandr Kozachuk
parent c318b99671
commit 8e63831701
2 changed files with 65 additions and 0 deletions
+20
View File
@@ -82,6 +82,17 @@ def write_dir(dir_path: pathlib.Path,
message.to_file(file_path)
def clear_dir(dir_path: pathlib.Path,
glob: Optional[str] = None) -> None:
"""
Deletes all Message files in the given directory.
"""
file_iter = dir_path.glob(glob) if glob else dir_path.iterdir()
for file_path in file_iter:
if file_path.is_file() and file_path.suffix in Message.file_suffixes:
file_path.unlink(missing_ok=True)
@dataclass
class Chat:
"""
@@ -289,3 +300,12 @@ class ChatDB(Chat):
msgs if msgs else self.messages,
self.file_suffix,
self.get_next_fid)
def clear_cache(self) -> None:
"""
Deletes all Message files from the cache dir and removes those messages from
the internal list.
"""
clear_dir(self.cache_path, self.glob)
# only keep messages from DB dir (or those that have not yet been written)
self.messages = [m for m in self.messages if not m.file_path or m.file_path.parent.samefile(self.db_path)]