Implemented the new '.msg' suffix for Message files and a conversion command #16

Merged
ok merged 16 commits from msg_suffix into main 2023-10-07 12:41:53 +02:00
Showing only changes of commit aecfd1088d - Show all commits
+18 -6
View File
@@ -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)