refactor: renamed (almost) all Chat/ChatDB functions
This commit is contained in:
+119
-120
@@ -118,14 +118,14 @@ class Chat:
|
||||
|
||||
messages: list[Message]
|
||||
|
||||
def filter(self, mfilter: MessageFilter) -> None:
|
||||
def msg_filter(self, mfilter: MessageFilter) -> None:
|
||||
"""
|
||||
Use 'Message.match(mfilter) to remove all messages that
|
||||
don't fulfill the filter requirements.
|
||||
"""
|
||||
self.messages = [m for m in self.messages if m.match(mfilter)]
|
||||
|
||||
def sort(self, reverse: bool = False) -> None:
|
||||
def msg_sort(self, reverse: bool = False) -> None:
|
||||
"""
|
||||
Sort the messages according to 'Message.msg_id()'.
|
||||
"""
|
||||
@@ -135,33 +135,33 @@ class Chat:
|
||||
except MessageError:
|
||||
pass
|
||||
|
||||
def clear(self) -> None:
|
||||
def msg_clear(self) -> None:
|
||||
"""
|
||||
Delete all messages.
|
||||
"""
|
||||
self.messages = []
|
||||
|
||||
def add_messages(self, messages: list[Message]) -> None:
|
||||
def msg_add(self, messages: list[Message]) -> None:
|
||||
"""
|
||||
Add new messages and sort them if possible.
|
||||
"""
|
||||
self.messages += messages
|
||||
self.sort()
|
||||
self.msg_sort()
|
||||
|
||||
def latest_message(self, mfilter: Optional[MessageFilter] = None) -> Optional[Message]:
|
||||
def msg_latest(self, mfilter: Optional[MessageFilter] = None) -> Optional[Message]:
|
||||
"""
|
||||
Return the last added message (according to the file ID) that matches the given filter.
|
||||
When containing messages without a valid file_path, it returns the latest message in
|
||||
the internal list.
|
||||
"""
|
||||
if len(self.messages) > 0:
|
||||
self.sort()
|
||||
self.msg_sort()
|
||||
for m in reversed(self.messages):
|
||||
if mfilter is None or m.match(mfilter):
|
||||
return m
|
||||
return None
|
||||
|
||||
def find_messages(self, msg_names: list[str]) -> list[Message]:
|
||||
def msg_find(self, msg_names: list[str]) -> list[Message]:
|
||||
"""
|
||||
Search and return the messages with the given names. Names can either be filenames
|
||||
(incl. suffixes) or full paths. Messages that can't be found are ignored (i. e. the
|
||||
@@ -170,16 +170,16 @@ class Chat:
|
||||
return [m for m in self.messages
|
||||
if any((m.file_path and (m.file_path == Path(mn) or m.file_path.name == mn)) for mn in msg_names)]
|
||||
|
||||
def remove_messages(self, msg_names: list[str]) -> None:
|
||||
def msg_remove(self, msg_names: list[str]) -> None:
|
||||
"""
|
||||
Remove the messages with the given names. Names can either be filenames
|
||||
(incl. the suffix) or full paths.
|
||||
"""
|
||||
self.messages = [m for m in self.messages
|
||||
if not any((m.file_path and (m.file_path == Path(mn) or m.file_path.name == mn)) for mn in msg_names)]
|
||||
self.sort()
|
||||
self.msg_sort()
|
||||
|
||||
def tags(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> set[Tag]:
|
||||
def msg_tags(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> set[Tag]:
|
||||
"""
|
||||
Get the tags of all messages, optionally filtered by prefix or substring.
|
||||
"""
|
||||
@@ -188,7 +188,7 @@ class Chat:
|
||||
tags |= m.filter_tags(prefix, contain)
|
||||
return set(sorted(tags))
|
||||
|
||||
def tags_frequency(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> dict[Tag, int]:
|
||||
def msg_tags_frequency(self, prefix: Optional[str] = None, contain: Optional[str] = None) -> dict[Tag, int]:
|
||||
"""
|
||||
Get the frequency of all tags of all messages, optionally filtered by prefix or substring.
|
||||
"""
|
||||
@@ -292,44 +292,78 @@ class ChatDB(Chat):
|
||||
with open(self.next_path, 'w') as f:
|
||||
f.write(f'{fid}')
|
||||
|
||||
def read_db(self) -> None:
|
||||
def msg_write(self, messages: Optional[list[Message]] = None) -> None:
|
||||
"""
|
||||
Reads new messages from the DB directory. New ones are added to the internal list,
|
||||
existing ones are replaced. A message is determined as 'existing' if a message with
|
||||
the same base filename (i. e. 'file_path.name') is already in the list.
|
||||
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
|
||||
internal messages, the ones with a valid file_path are written, the others
|
||||
are ignored.
|
||||
"""
|
||||
new_messages = read_dir(self.db_path, self.glob, self.mfilter)
|
||||
# remove all messages from self.messages that are in the new list
|
||||
self.messages = [m for m in self.messages if not message_in(m, new_messages)]
|
||||
# copy the messages from the temporary list to self.messages and sort them
|
||||
self.messages += new_messages
|
||||
self.sort()
|
||||
if messages and any(m.file_path is None for m in messages):
|
||||
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()
|
||||
|
||||
def read_cache(self) -> None:
|
||||
def msg_update(self, messages: list[Message], write: bool = True) -> None:
|
||||
"""
|
||||
Reads new messages from the cache directory. New ones are added to the internal list,
|
||||
existing ones are replaced. A message is determined as 'existing' if a message with
|
||||
the same base filename (i. e. 'file_path.name') is already in the list.
|
||||
Update EXISTING messages. A message is determined as 'existing' if a message with
|
||||
the same base filename (i. e. 'file_path.name') is already in the list. Only accepts
|
||||
existing messages.
|
||||
"""
|
||||
if any(not message_in(m, self.messages) for m in messages):
|
||||
raise ChatError("Can't update messages that are not in the internal list")
|
||||
# remove old versions and add new ones
|
||||
self.messages = [m for m in self.messages if not message_in(m, messages)]
|
||||
self.messages += messages
|
||||
self.msg_sort()
|
||||
# write the UPDATED messages if requested
|
||||
if write:
|
||||
self.msg_write(messages)
|
||||
|
||||
def msg_latest(self,
|
||||
mfilter: Optional[MessageFilter] = None,
|
||||
source: Literal['mem', 'disk', 'cache', 'db', 'all'] = '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:
|
||||
* 'mem' : only search messages currently in memory
|
||||
* 'disk' : search messages on disk (cache + DB directory), but not in memory
|
||||
* 'cache': only search messages in the cache directory
|
||||
* 'db' : only search messages in the DB directory
|
||||
* 'all' : search all messages ('mem' + 'disk')
|
||||
"""
|
||||
source_messages: list[Message] = []
|
||||
if source == 'mem':
|
||||
return super().msg_latest(mfilter)
|
||||
if source in ['cache', 'disk', 'all']:
|
||||
source_messages += read_dir(self.cache_path, mfilter=mfilter)
|
||||
if source in ['db', 'disk', 'all']:
|
||||
source_messages += read_dir(self.db_path, mfilter=mfilter)
|
||||
if source in ['all']:
|
||||
# only consider messages with a valid file_path so they can be sorted
|
||||
source_messages += [m for m in self.messages if (m.file_path is not None and (mfilter is None or m.match(mfilter)))]
|
||||
source_messages.sort(key=lambda m: m.msg_id(), reverse=True)
|
||||
for m in source_messages:
|
||||
if mfilter is None or m.match(mfilter):
|
||||
return m
|
||||
return None
|
||||
|
||||
def cache_read(self) -> None:
|
||||
"""
|
||||
Read messages from the cache directory. New ones are added to the internal list,
|
||||
existing ones are replaced. A message is determined as 'existing' if a message
|
||||
with the same base filename (i. e. 'file_path.name') is already in the list.
|
||||
"""
|
||||
new_messages = read_dir(self.cache_path, self.glob, self.mfilter)
|
||||
# remove all messages from self.messages that are in the new list
|
||||
self.messages = [m for m in self.messages if not message_in(m, new_messages)]
|
||||
# copy the messages from the temporary list to self.messages and sort them
|
||||
self.messages += new_messages
|
||||
self.sort()
|
||||
self.msg_sort()
|
||||
|
||||
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,
|
||||
messages if messages else self.messages,
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
|
||||
def write_cache(self, messages: Optional[list[Message]] = None) -> None:
|
||||
def cache_write(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
|
||||
@@ -340,36 +374,9 @@ class ChatDB(Chat):
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
|
||||
def clear_cache(self) -> None:
|
||||
def cache_add(self, messages: list[Message], write: bool = True) -> None:
|
||||
"""
|
||||
Deletes all Message files from the cache dir and removes those messages from
|
||||
the internal list.
|
||||
"""
|
||||
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], write: bool = True) -> None:
|
||||
"""
|
||||
Add the given new messages and set the file_path to the DB directory.
|
||||
Only accepts messages without a file_path.
|
||||
"""
|
||||
if any(m.file_path is not None for m in messages):
|
||||
raise ChatError("Can't add new messages with existing file_path")
|
||||
if 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], write: bool = True) -> None:
|
||||
"""
|
||||
Add the given new messages and set the file_path to the cache directory.
|
||||
Add NEW messages and set the file_path to the cache directory.
|
||||
Only accepts messages without a file_path.
|
||||
"""
|
||||
if any(m.file_path is not None for m in messages):
|
||||
@@ -383,62 +390,54 @@ class ChatDB(Chat):
|
||||
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()
|
||||
self.msg_sort()
|
||||
|
||||
def write_messages(self, messages: Optional[list[Message]] = None) -> None:
|
||||
def cache_clear(self) -> 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
|
||||
internal messages, the ones with a valid file_path are written, the others
|
||||
are ignored.
|
||||
Delete all message files from the cache dir and remove them from the internal list.
|
||||
"""
|
||||
if messages and any(m.file_path is None for m in messages):
|
||||
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()
|
||||
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 update_messages(self, messages: list[Message], write: bool = True) -> None:
|
||||
def db_read(self) -> None:
|
||||
"""
|
||||
Update existing messages. A message is determined as 'existing' if a message with
|
||||
the same base filename (i. e. 'file_path.name') is already in the list. Only accepts
|
||||
existing messages.
|
||||
Read messages from the DB directory. New ones are added to the internal list,
|
||||
existing ones are replaced. A message is determined as 'existing' if a message
|
||||
with the same base filename (i. e. 'file_path.name') is already in the list.
|
||||
"""
|
||||
if any(not message_in(m, self.messages) for m in messages):
|
||||
raise ChatError("Can't update messages that are not in the internal list")
|
||||
# remove old versions and add new ones
|
||||
self.messages = [m for m in self.messages if not message_in(m, messages)]
|
||||
self.messages += messages
|
||||
self.sort()
|
||||
# write the UPDATED messages if requested
|
||||
new_messages = read_dir(self.db_path, self.glob, self.mfilter)
|
||||
# remove all messages from self.messages that are in the new list
|
||||
self.messages = [m for m in self.messages if not message_in(m, new_messages)]
|
||||
# copy the messages from the temporary list to self.messages and sort them
|
||||
self.messages += new_messages
|
||||
self.msg_sort()
|
||||
|
||||
def db_write(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,
|
||||
messages if messages else self.messages,
|
||||
self.file_suffix,
|
||||
self.get_next_fid)
|
||||
|
||||
def db_add(self, messages: list[Message], write: bool = True) -> None:
|
||||
"""
|
||||
Add NEW messages and set the file_path to the DB directory.
|
||||
Only accepts messages without a file_path.
|
||||
"""
|
||||
if any(m.file_path is not None for m in messages):
|
||||
raise ChatError("Can't add new messages with existing file_path")
|
||||
if write:
|
||||
self.write_messages(messages)
|
||||
|
||||
def latest_message(self,
|
||||
mfilter: Optional[MessageFilter] = None,
|
||||
source: Literal['mem', 'disk', 'cache', 'db', 'all'] = '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:
|
||||
* 'mem' : only search messages currently in memory
|
||||
* 'disk' : search messages on disk (cache + DB directory), but not in memory
|
||||
* 'cache': only search messages in the cache directory
|
||||
* 'db' : only search messages in the DB directory
|
||||
* 'all' : search all messages ('mem' + 'disk')
|
||||
"""
|
||||
source_messages: list[Message] = []
|
||||
if source == 'mem':
|
||||
return super().latest_message(mfilter)
|
||||
if source in ['cache', 'disk', 'all']:
|
||||
source_messages += read_dir(self.cache_path, mfilter=mfilter)
|
||||
if source in ['db', 'disk', 'all']:
|
||||
source_messages += read_dir(self.db_path, mfilter=mfilter)
|
||||
if source in ['all']:
|
||||
# only consider messages with a valid file_path so they can be sorted
|
||||
source_messages += [m for m in self.messages if (m.file_path is not None and (mfilter is None or m.match(mfilter)))]
|
||||
source_messages.sort(key=lambda m: m.msg_id(), reverse=True)
|
||||
for m in source_messages:
|
||||
if mfilter is None or m.match(mfilter):
|
||||
return m
|
||||
return None
|
||||
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.msg_sort()
|
||||
|
||||
Reference in New Issue
Block a user