From 8031271c186e07a931577b045216404609f79b2e Mon Sep 17 00:00:00 2001 From: juk0de Date: Wed, 27 Sep 2023 08:15:35 +0200 Subject: [PATCH] chat: added message file format as ChatDB class member --- chatmastermind/chat.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/chatmastermind/chat.py b/chatmastermind/chat.py index 63a5e7f..ad1cece 100644 --- a/chatmastermind/chat.py +++ b/chatmastermind/chat.py @@ -285,6 +285,8 @@ class ChatDB(Chat): mfilter: Optional[MessageFilter] = None # the glob pattern for all messages glob: Optional[str] = None + # message format (for writing) + mformat: MessageFormat = Message.default_format def __post_init__(self) -> None: # contains the latest message ID @@ -339,9 +341,15 @@ class ChatDB(Chat): with open(self.next_path, 'w') as f: f.write(f'{fid}') + def set_msg_format(self, mformat: MessageFormat) -> None: + """ + Set message format for writing messages. + """ + self.mformat = mformat + def msg_write(self, messages: Optional[list[Message]] = None, - mformat: MessageFormat = Message.default_format) -> None: + mformat: Optional[MessageFormat] = None) -> None: """ Write either the given messages or the internal ones to their CURRENT file_path. If messages are given, they all must have a valid file_path. When writing the @@ -352,7 +360,7 @@ class ChatDB(Chat): raise ChatError("Can't write files without a valid file_path") msgs = iter(messages if messages else self.messages) while (m := next(msgs, None)): - m.to_file(mformat=mformat) + m.to_file(mformat=mformat if mformat else self.mformat) def msg_update(self, messages: list[Message], write: bool = True) -> None: """ @@ -514,7 +522,8 @@ class ChatDB(Chat): """ write_dir(self.cache_path, messages if messages else self.messages, - self.get_next_fid) + self.get_next_fid, + self.mformat) def cache_add(self, messages: list[Message], write: bool = True) -> None: """ @@ -526,7 +535,8 @@ class ChatDB(Chat): if write: write_dir(self.cache_path, messages, - self.get_next_fid) + self.get_next_fid, + self.mformat) else: for m in messages: m.file_path = make_file_path(self.cache_path, self.get_next_fid) @@ -579,7 +589,8 @@ class ChatDB(Chat): """ write_dir(self.db_path, messages if messages else self.messages, - self.get_next_fid) + self.get_next_fid, + self.mformat) def db_add(self, messages: list[Message], write: bool = True) -> None: """ @@ -591,7 +602,8 @@ class ChatDB(Chat): if write: write_dir(self.db_path, messages, - self.get_next_fid) + self.get_next_fid, + self.mformat) else: for m in messages: m.file_path = make_file_path(self.db_path, self.get_next_fid)