message / chat: 'msg_id()' now returns 'file_path.stem' (removed suffix)

This commit is contained in:
2023-09-15 12:20:41 +02:00
parent fc82f85b7c
commit 525cdb92a1
4 changed files with 24 additions and 17 deletions
+15 -13
View File
@@ -16,7 +16,7 @@ ChatDBInst = TypeVar('ChatDBInst', bound='ChatDB')
db_next_file = '.next'
ignored_files = [db_next_file, default_config_file]
valid_sources = Literal['mem', 'disk', 'cache', 'db', 'all']
msg_place = Literal['mem', 'disk', 'cache', 'db', 'all']
class ChatError(Exception):
@@ -194,8 +194,9 @@ class Chat:
def msg_find(self, msg_names: list[str]) -> list[Message]:
"""
Search and return the messages with the given names. Names can either be filenames
(with or without suffix) or full paths. Messages that can't be found are ignored
(i. e. the caller should check the result if they require all messages).
(with or without suffix), full paths or Message.msg_id(). Messages that can't be
found are ignored (i. e. the caller should check the result if they require all
messages).
"""
return [m for m in self.messages
if any((m.file_path and self.msg_name_matches(m.file_path, mn)) for mn in msg_names)]
@@ -203,7 +204,7 @@ class Chat:
def msg_remove(self, msg_names: list[str]) -> None:
"""
Remove the messages with the given names. Names can either be filenames
(with or without suffix) or full paths.
(with or without suffix), full paths or Message.msg_id().
"""
self.messages = [m for m in self.messages
if not any((m.file_path and self.msg_name_matches(m.file_path, mn)) for mn in msg_names)]
@@ -352,7 +353,7 @@ class ChatDB(Chat):
self.msg_write(messages)
def msg_gather(self,
source: valid_sources,
source: msg_place,
require_file_path: bool = False,
mfilter: Optional[MessageFilter] = None) -> list[Message]:
"""
@@ -385,13 +386,14 @@ class ChatDB(Chat):
def msg_find(self,
msg_names: list[str],
source: valid_sources = 'mem',
source: msg_place = 'mem',
) -> list[Message]:
"""
Search and return the messages with the given names. Names can either be filenames
(with or without suffix) or full paths. Messages that can't be found are ignored
(i. e. the caller should check the result if they require all messages).
Searches one of the following sources:
(with or without suffix), full paths or Message.msg_id(). Messages that can't be
found are ignored (i. e. the caller should check the result if they require all
messages).
Searches one of the following places:
* 'mem' : messages currently in memory
* 'disk' : messages on disk (cache + DB directory), but not in memory
* 'cache': messages in the cache directory
@@ -405,8 +407,8 @@ class ChatDB(Chat):
def msg_remove(self, msg_names: list[str]) -> None:
"""
Remove the messages with the given names. Names can either be filenames
(with or without suffix) or full paths. Also deletes the files of all given
messages with a valid file_path.
(with or without suffix), full paths or Message.msg_id(). Also deletes the
files of all given messages with a valid file_path.
"""
# delete the message files first
rm_messages = self.msg_find(msg_names, source='all')
@@ -418,11 +420,11 @@ class ChatDB(Chat):
def msg_latest(self,
mfilter: Optional[MessageFilter] = None,
source: valid_sources = 'mem') -> Optional[Message]:
source: msg_place = 'mem') -> Optional[Message]:
"""
Return the last added message (according to the file ID) that matches the given filter.
Only consider messages with a valid file_path (except if source is 'mem').
Searches one of the following sources:
Searches one of the following places:
* 'mem' : messages currently in memory
* 'disk' : messages on disk (cache + DB directory), but not in memory
* 'cache': messages in the cache directory
+3 -2
View File
@@ -543,10 +543,11 @@ class Message():
def msg_id(self) -> str:
"""
Returns an ID that is unique throughout all messages in the same (DB) directory.
Currently this is the file name. The ID is also used for sorting messages.
Currently this is the file name without suffix. The ID is also used for sorting
messages.
"""
if self.file_path:
return self.file_path.name
return self.file_path.stem
else:
raise MessageError("Can't create file ID without a file path")