chat: new possibilites for adding messages
This commit is contained in:
+47
-9
@@ -55,6 +55,16 @@ def read_dir(dir_path: pathlib.Path,
|
||||
return messages
|
||||
|
||||
|
||||
def make_file_path(dir_path: pathlib.Path,
|
||||
file_suffix: str,
|
||||
next_fid: Callable[[], int]) -> pathlib.Path:
|
||||
"""
|
||||
Create a file_path for the given directory using the
|
||||
given file_suffix and ID generator function.
|
||||
"""
|
||||
return dir_path / f"{next_fid():04d}{file_suffix}"
|
||||
|
||||
|
||||
def write_dir(dir_path: pathlib.Path,
|
||||
messages: list[Message],
|
||||
file_suffix: str,
|
||||
@@ -73,9 +83,7 @@ def write_dir(dir_path: pathlib.Path,
|
||||
file_path = message.file_path
|
||||
# message has no file_path: create one
|
||||
if not file_path:
|
||||
fid = next_fid()
|
||||
fname = f"{fid:04d}{file_suffix}"
|
||||
file_path = dir_path / fname
|
||||
file_path = make_file_path(dir_path, file_suffix, next_fid)
|
||||
# file_path does not point to given directory: modify it
|
||||
elif not file_path.parent.samefile(dir_path):
|
||||
file_path = dir_path / file_path.name
|
||||
@@ -124,11 +132,11 @@ class Chat:
|
||||
"""
|
||||
self.messages = []
|
||||
|
||||
def add_msgs(self, msgs: list[Message]) -> None:
|
||||
def add_messages(self, messages: list[Message]) -> None:
|
||||
"""
|
||||
Add new messages and sort them if possible.
|
||||
"""
|
||||
self.messages += msgs
|
||||
self.messages += messages
|
||||
self.sort()
|
||||
|
||||
def tags(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> set[Tag]:
|
||||
@@ -279,25 +287,25 @@ class ChatDB(Chat):
|
||||
self.messages += new_messages
|
||||
self.sort()
|
||||
|
||||
def write_db(self, msgs: Optional[list[Message]] = None) -> None:
|
||||
def write_db(self, messages: Optional[list[Message]] = None) -> None:
|
||||
"""
|
||||
Write messages to the DB directory. If a message has no file_path, a new one
|
||||
will be created. If message.file_path exists, it will be modified to point
|
||||
to the DB directory.
|
||||
"""
|
||||
write_dir(self.db_path,
|
||||
msgs if msgs else self.messages,
|
||||
messages if messages else self.messages,
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
|
||||
def write_cache(self, msgs: Optional[list[Message]] = None) -> None:
|
||||
def write_cache(self, messages: Optional[list[Message]] = None) -> None:
|
||||
"""
|
||||
Write messages to the cache directory. If a message has no file_path, a new one
|
||||
will be created. If message.file_path exists, it will be modified to point to
|
||||
the cache directory.
|
||||
"""
|
||||
write_dir(self.cache_path,
|
||||
msgs if msgs else self.messages,
|
||||
messages if messages else self.messages,
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
|
||||
@@ -309,3 +317,33 @@ class ChatDB(Chat):
|
||||
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)]
|
||||
|
||||
def add_to_db(self, messages: list[Message], do_write: bool = True) -> None:
|
||||
"""
|
||||
Adds the given messages and sets the file_path to the DB directory.
|
||||
"""
|
||||
if do_write:
|
||||
write_dir(self.db_path,
|
||||
messages,
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
else:
|
||||
for m in messages:
|
||||
m.file_path = make_file_path(self.db_path, self.default_file_suffix, self.get_next_fid)
|
||||
self.messages += messages
|
||||
self.sort()
|
||||
|
||||
def add_to_cache(self, messages: list[Message], do_write: bool = True) -> None:
|
||||
"""
|
||||
Adds the given messages and sets the file_path to the cache directory.
|
||||
"""
|
||||
if do_write:
|
||||
write_dir(self.cache_path,
|
||||
messages,
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
else:
|
||||
for m in messages:
|
||||
m.file_path = make_file_path(self.cache_path, self.default_file_suffix, self.get_next_fid)
|
||||
self.messages += messages
|
||||
self.sort()
|
||||
|
||||
Reference in New Issue
Block a user