Compare commits
2 Commits
500f90fdf1
...
5c287ce3a1
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c287ce3a1 | |||
| d8e58313f3 |
@ -107,7 +107,9 @@ def clear_dir(dir_path: Path,
|
|||||||
"""
|
"""
|
||||||
file_iter = dir_path.glob(glob) if glob else dir_path.iterdir()
|
file_iter = dir_path.glob(glob) if glob else dir_path.iterdir()
|
||||||
for file_path in file_iter:
|
for file_path in file_iter:
|
||||||
if file_path.is_file() and file_path.suffix in Message.file_suffixes:
|
if (file_path.is_file()
|
||||||
|
and file_path.name not in ignored_files # noqa: W503
|
||||||
|
and file_path.suffix in Message.file_suffixes): # noqa: W503
|
||||||
file_path.unlink(missing_ok=True)
|
file_path.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
|
||||||
@ -401,7 +403,11 @@ class ChatDB(Chat):
|
|||||||
for m in loc_messages:
|
for m in loc_messages:
|
||||||
if not message_in(m, unique_messages):
|
if not message_in(m, unique_messages):
|
||||||
unique_messages.append(m)
|
unique_messages.append(m)
|
||||||
|
try:
|
||||||
unique_messages.sort(key=lambda m: m.msg_id())
|
unique_messages.sort(key=lambda m: m.msg_id())
|
||||||
|
# messages in 'mem' can have an empty file_path
|
||||||
|
except MessageError:
|
||||||
|
pass
|
||||||
return unique_messages
|
return unique_messages
|
||||||
|
|
||||||
def msg_find(self,
|
def msg_find(self,
|
||||||
@ -490,13 +496,13 @@ class ChatDB(Chat):
|
|||||||
else:
|
else:
|
||||||
return len(self.msg_find([message], loc='db')) > 0
|
return len(self.msg_find([message], loc='db')) > 0
|
||||||
|
|
||||||
def cache_read(self) -> None:
|
def cache_read(self, glob: Optional[str] = None, mfilter: Optional[MessageFilter] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Read messages from the cache directory. New ones are added to the internal list,
|
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
|
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.
|
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)
|
new_messages = read_dir(self.cache_path, glob, mfilter)
|
||||||
# remove all messages from self.messages that are in the new list
|
# 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)]
|
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
|
# copy the messages from the temporary list to self.messages and sort them
|
||||||
@ -533,15 +539,31 @@ class ChatDB(Chat):
|
|||||||
self.messages += messages
|
self.messages += messages
|
||||||
self.msg_sort()
|
self.msg_sort()
|
||||||
|
|
||||||
def cache_clear(self) -> None:
|
def cache_clear(self, glob: Optional[str] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Delete all message files from the cache dir and remove them from the internal list.
|
Delete all message files from the cache dir and remove them from the internal list.
|
||||||
"""
|
"""
|
||||||
clear_dir(self.cache_path, self.glob)
|
clear_dir(self.cache_path, glob)
|
||||||
# only keep messages from DB dir (or those that have not yet been written)
|
# 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)]
|
self.messages = [m for m in self.messages if not m.file_path or m.file_path.parent.samefile(self.db_path)]
|
||||||
|
|
||||||
def db_read(self) -> None:
|
def cache_move(self, message: Message) -> None:
|
||||||
|
"""
|
||||||
|
Moves the given messages to the cache directory.
|
||||||
|
"""
|
||||||
|
# remember the old path (if any)
|
||||||
|
old_path: Optional[Path] = None
|
||||||
|
if message.file_path:
|
||||||
|
old_path = message.file_path
|
||||||
|
# write message to the new destination
|
||||||
|
self.cache_write([message])
|
||||||
|
# remove the old one (if any)
|
||||||
|
if old_path:
|
||||||
|
self.msg_remove([str(old_path)], loc='db')
|
||||||
|
# (re)add it to the internal list
|
||||||
|
self.msg_add([message])
|
||||||
|
|
||||||
|
def db_read(self, glob: Optional[str] = None, mfilter: Optional[MessageFilter] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Read messages from the DB directory. New ones are added to the internal list,
|
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
|
existing ones are replaced. A message is determined as 'existing' if a message
|
||||||
@ -583,3 +605,19 @@ class ChatDB(Chat):
|
|||||||
m.file_path = make_file_path(self.db_path, self.default_file_suffix, self.get_next_fid)
|
m.file_path = make_file_path(self.db_path, self.default_file_suffix, self.get_next_fid)
|
||||||
self.messages += messages
|
self.messages += messages
|
||||||
self.msg_sort()
|
self.msg_sort()
|
||||||
|
|
||||||
|
def db_move(self, message: Message) -> None:
|
||||||
|
"""
|
||||||
|
Moves the given messages to the db directory.
|
||||||
|
"""
|
||||||
|
# remember the old path (if any)
|
||||||
|
old_path: Optional[Path] = None
|
||||||
|
if message.file_path:
|
||||||
|
old_path = message.file_path
|
||||||
|
# write message to the new destination
|
||||||
|
self.db_write([message])
|
||||||
|
# remove the old one (if any)
|
||||||
|
if old_path:
|
||||||
|
self.msg_remove([str(old_path)], loc='cache')
|
||||||
|
# (re)add it to the internal list
|
||||||
|
self.msg_add([message])
|
||||||
|
|||||||
@ -585,3 +585,52 @@ class TestChatDB(unittest.TestCase):
|
|||||||
self.assertEqual(chat_db.msg_latest(loc='all'), new_message)
|
self.assertEqual(chat_db.msg_latest(loc='all'), new_message)
|
||||||
# the DB does not contain the new message
|
# the DB does not contain the new message
|
||||||
self.assertEqual(chat_db.msg_latest(loc='db'), self.message4)
|
self.assertEqual(chat_db.msg_latest(loc='db'), self.message4)
|
||||||
|
|
||||||
|
def test_msg_gather(self) -> None:
|
||||||
|
chat_db = ChatDB.from_dir(pathlib.Path(self.cache_path.name),
|
||||||
|
pathlib.Path(self.db_path.name))
|
||||||
|
all_messages = [self.message1, self.message2, self.message3, self.message4]
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='all'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='db'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='mem'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='disk'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='cache'), [])
|
||||||
|
# add a new message, but only to the internal list
|
||||||
|
new_message = Message(Question("What?"))
|
||||||
|
all_messages_mem = all_messages + [new_message]
|
||||||
|
chat_db.msg_add([new_message])
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='mem'), all_messages_mem)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='all'), all_messages_mem)
|
||||||
|
# the nr. of messages on disk did not change -> expect old result
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='db'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='disk'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='cache'), [])
|
||||||
|
# test with MessageFilter
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='all', mfilter=MessageFilter(tags_or={Tag('tag1')})),
|
||||||
|
[self.message1])
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='disk', mfilter=MessageFilter(tags_or={Tag('tag2')})),
|
||||||
|
[self.message2])
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='cache', mfilter=MessageFilter(tags_or={Tag('tag3')})),
|
||||||
|
[])
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='mem', mfilter=MessageFilter(question_contains="What")),
|
||||||
|
[new_message])
|
||||||
|
|
||||||
|
def test_msg_move_and_gather(self) -> None:
|
||||||
|
chat_db = ChatDB.from_dir(pathlib.Path(self.cache_path.name),
|
||||||
|
pathlib.Path(self.db_path.name))
|
||||||
|
all_messages = [self.message1, self.message2, self.message3, self.message4]
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='db'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='cache'), [])
|
||||||
|
# move first message to the cache
|
||||||
|
chat_db.cache_move(self.message1)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='cache'), [self.message1])
|
||||||
|
self.assertEqual(self.message1.file_path.parent, pathlib.Path(self.cache_path.name)) # type: ignore [union-attr]
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='db'), [self.message2, self.message3, self.message4])
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='all'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='disk'), all_messages)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='mem'), all_messages)
|
||||||
|
# now move first message back to the DB
|
||||||
|
chat_db.db_move(self.message1)
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='cache'), [])
|
||||||
|
self.assertEqual(self.message1.file_path.parent, pathlib.Path(self.db_path.name)) # type: ignore [union-attr]
|
||||||
|
self.assertSequenceEqual(chat_db.msg_gather(loc='db'), all_messages)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user