chat: added message file format as ChatDB class member

This commit is contained in:
juk0de 2023-09-27 08:15:35 +02:00
parent 5e392e782e
commit 8031271c18

View File

@ -285,6 +285,8 @@ class ChatDB(Chat):
mfilter: Optional[MessageFilter] = None mfilter: Optional[MessageFilter] = None
# the glob pattern for all messages # the glob pattern for all messages
glob: Optional[str] = None glob: Optional[str] = None
# message format (for writing)
mformat: MessageFormat = Message.default_format
def __post_init__(self) -> None: def __post_init__(self) -> None:
# contains the latest message ID # contains the latest message ID
@ -339,9 +341,15 @@ class ChatDB(Chat):
with open(self.next_path, 'w') as f: with open(self.next_path, 'w') as f:
f.write(f'{fid}') 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, def msg_write(self,
messages: Optional[list[Message]] = None, 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. 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 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") raise ChatError("Can't write files without a valid file_path")
msgs = iter(messages if messages else self.messages) msgs = iter(messages if messages else self.messages)
while (m := next(msgs, None)): 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: def msg_update(self, messages: list[Message], write: bool = True) -> None:
""" """
@ -514,7 +522,8 @@ class ChatDB(Chat):
""" """
write_dir(self.cache_path, write_dir(self.cache_path,
messages if messages else self.messages, 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: def cache_add(self, messages: list[Message], write: bool = True) -> None:
""" """
@ -526,7 +535,8 @@ class ChatDB(Chat):
if write: if write:
write_dir(self.cache_path, write_dir(self.cache_path,
messages, messages,
self.get_next_fid) self.get_next_fid,
self.mformat)
else: else:
for m in messages: for m in messages:
m.file_path = make_file_path(self.cache_path, self.get_next_fid) 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, write_dir(self.db_path,
messages if messages else self.messages, 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: def db_add(self, messages: list[Message], write: bool = True) -> None:
""" """
@ -591,7 +602,8 @@ class ChatDB(Chat):
if write: if write:
write_dir(self.db_path, write_dir(self.db_path,
messages, messages,
self.get_next_fid) self.get_next_fid,
self.mformat)
else: else:
for m in messages: for m in messages:
m.file_path = make_file_path(self.db_path, self.get_next_fid) m.file_path = make_file_path(self.db_path, self.get_next_fid)