Allow to customize glob and location in command question and hist, convert location type to enum.
This commit is contained in:
+45
-31
@@ -6,7 +6,8 @@ from pathlib import Path
|
||||
from pprint import PrettyPrinter
|
||||
from pydoc import pager
|
||||
from dataclasses import dataclass
|
||||
from typing import TypeVar, Type, Optional, Any, Callable, Literal, Union
|
||||
from enum import Enum
|
||||
from typing import TypeVar, Type, Optional, Any, Callable, Union
|
||||
from .configuration import default_config_file
|
||||
from .message import Message, MessageFilter, MessageError, MessageFormat, message_in, message_valid_formats
|
||||
from .tags import Tag
|
||||
@@ -16,10 +17,17 @@ ChatDBInst = TypeVar('ChatDBInst', bound='ChatDB')
|
||||
|
||||
db_next_file = '.next'
|
||||
ignored_files = [db_next_file, default_config_file]
|
||||
msg_location = Literal['mem', 'disk', 'cache', 'db', 'all']
|
||||
msg_suffix = Message.file_suffix_write
|
||||
|
||||
|
||||
class msg_location(Enum):
|
||||
MEM = 'mem'
|
||||
DISK = 'disk'
|
||||
CACHE = 'cache'
|
||||
DB = 'db'
|
||||
ALL = 'all'
|
||||
|
||||
|
||||
class ChatError(Exception):
|
||||
pass
|
||||
|
||||
@@ -44,12 +52,12 @@ def read_dir(dir_path: Path,
|
||||
Parameters:
|
||||
* 'dir_path': source directory
|
||||
* 'glob': if specified, files will be filtered using 'path.glob()',
|
||||
otherwise it uses 'path.iterdir()'.
|
||||
otherwise it uses '*{msg_suffix}'.
|
||||
* 'mfilter': use with 'Message.from_file()' to filter messages
|
||||
when reading them.
|
||||
"""
|
||||
messages: list[Message] = []
|
||||
file_iter = dir_path.glob(glob) if glob else dir_path.iterdir()
|
||||
file_iter = dir_path.glob(glob) if glob else dir_path.glob(f'*{msg_suffix}')
|
||||
for file_path in sorted(file_iter):
|
||||
if (file_path.is_file()
|
||||
and file_path.name not in ignored_files # noqa: W503
|
||||
@@ -304,7 +312,8 @@ class ChatDB(Chat):
|
||||
cache_path: Path,
|
||||
db_path: Path,
|
||||
glob: Optional[str] = None,
|
||||
mfilter: Optional[MessageFilter] = None) -> ChatDBInst:
|
||||
mfilter: Optional[MessageFilter] = None,
|
||||
loc: msg_location = msg_location.DB) -> ChatDBInst:
|
||||
"""
|
||||
Create a 'ChatDB' instance from the given directory structure.
|
||||
Reads all messages from 'db_path' into the local message list.
|
||||
@@ -316,7 +325,12 @@ class ChatDB(Chat):
|
||||
* 'mfilter': use with 'Message.from_file()' to filter messages
|
||||
when reading them.
|
||||
"""
|
||||
messages = read_dir(db_path, glob, mfilter)
|
||||
messages: list[Message] = []
|
||||
if loc in [msg_location.DB, msg_location.DISK, msg_location.ALL]:
|
||||
messages.extend(read_dir(db_path, glob, mfilter))
|
||||
if loc in [msg_location.CACHE, msg_location.DISK, msg_location.ALL]:
|
||||
messages.extend(read_dir(cache_path, glob, mfilter))
|
||||
messages.sort(key=lambda x: x.msg_id())
|
||||
return cls(messages, cache_path, db_path, mfilter, glob)
|
||||
|
||||
@classmethod
|
||||
@@ -399,14 +413,14 @@ class ChatDB(Chat):
|
||||
If 'require_file_path' is True, return only files with a valid file_path.
|
||||
"""
|
||||
loc_messages: list[Message] = []
|
||||
if loc in ['mem', 'all']:
|
||||
if loc in [msg_location.MEM, msg_location.ALL]:
|
||||
if require_file_path:
|
||||
loc_messages += [m for m in self.messages if (m.file_path is not None and (mfilter is None or m.match(mfilter)))]
|
||||
else:
|
||||
loc_messages += [m for m in self.messages if (mfilter is None or m.match(mfilter))]
|
||||
if loc in ['cache', 'disk', 'all']:
|
||||
if loc in [msg_location.CACHE, msg_location.DISK, msg_location.ALL]:
|
||||
loc_messages += read_dir(self.cache_path, glob=glob, mfilter=mfilter)
|
||||
if loc in ['db', 'disk', 'all']:
|
||||
if loc in [msg_location.DB, msg_location.DISK, msg_location.ALL]:
|
||||
loc_messages += read_dir(self.db_path, glob=glob, mfilter=mfilter)
|
||||
# remove_duplicates and sort the list
|
||||
unique_messages: list[Message] = []
|
||||
@@ -415,14 +429,14 @@ class ChatDB(Chat):
|
||||
unique_messages.append(m)
|
||||
try:
|
||||
unique_messages.sort(key=lambda m: m.msg_id())
|
||||
# messages in 'mem' can have an empty file_path
|
||||
# messages in msg_location.MEM can have an empty file_path
|
||||
except MessageError:
|
||||
pass
|
||||
return unique_messages
|
||||
|
||||
def msg_find(self,
|
||||
msg_names: list[str],
|
||||
loc: msg_location = 'mem',
|
||||
loc: msg_location = msg_location.MEM,
|
||||
) -> list[Message]:
|
||||
"""
|
||||
Search and return the messages with the given names. Names can either be filenames
|
||||
@@ -430,29 +444,29 @@ class ChatDB(Chat):
|
||||
found are ignored (i. e. the caller should check the result if they require all
|
||||
messages).
|
||||
Searches one of the following locations:
|
||||
* 'mem' : messages currently in memory
|
||||
* 'disk' : messages on disk (cache + DB directory), but not in memory
|
||||
* msg_location.MEM : messages currently in memory
|
||||
* msg_location.DISK : messages on disk (cache + DB directory), but not in memory
|
||||
* 'cache': messages in the cache directory
|
||||
* 'db' : messages in the DB directory
|
||||
* 'all' : all messages ('mem' + 'disk')
|
||||
* msg_location.DB : messages in the DB directory
|
||||
* msg_location.ALL : all messages (msg_location.MEM + msg_location.DISK)
|
||||
"""
|
||||
loc_messages = self.msg_gather(loc, require_file_path=True)
|
||||
return [m for m in loc_messages
|
||||
if any((m.file_path and self.msg_name_matches(m.file_path, mn)) for mn in msg_names)]
|
||||
|
||||
def msg_remove(self, msg_names: list[str], loc: msg_location = 'mem') -> None:
|
||||
def msg_remove(self, msg_names: list[str], loc: msg_location = msg_location.MEM) -> None:
|
||||
"""
|
||||
Remove the messages with the given names. Names can either be filenames
|
||||
(with or without suffix), full paths or Message.msg_id(). Also deletes the
|
||||
files of all given messages with a valid file_path.
|
||||
Delete files from one of the following locations:
|
||||
* 'mem' : messages currently in memory
|
||||
* 'disk' : messages on disk (cache + DB directory), but not in memory
|
||||
* msg_location.MEM : messages currently in memory
|
||||
* msg_location.DISK : messages on disk (cache + DB directory), but not in memory
|
||||
* 'cache': messages in the cache directory
|
||||
* 'db' : messages in the DB directory
|
||||
* 'all' : all messages ('mem' + 'disk')
|
||||
* msg_location.DB : messages in the DB directory
|
||||
* msg_location.ALL : all messages (msg_location.MEM + msg_location.DISK)
|
||||
"""
|
||||
if loc != 'mem':
|
||||
if loc != msg_location.MEM:
|
||||
# delete the message files first
|
||||
rm_messages = self.msg_find(msg_names, loc=loc)
|
||||
for m in rm_messages:
|
||||
@@ -463,16 +477,16 @@ class ChatDB(Chat):
|
||||
|
||||
def msg_latest(self,
|
||||
mfilter: Optional[MessageFilter] = None,
|
||||
loc: msg_location = 'mem') -> Optional[Message]:
|
||||
loc: msg_location = msg_location.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 loc is 'mem').
|
||||
Only consider messages with a valid file_path (except if loc is msg_location.MEM).
|
||||
Searches one of the following locations:
|
||||
* 'mem' : messages currently in memory
|
||||
* 'disk' : messages on disk (cache + DB directory), but not in memory
|
||||
* msg_location.MEM : messages currently in memory
|
||||
* msg_location.DISK : messages on disk (cache + DB directory), but not in memory
|
||||
* 'cache': messages in the cache directory
|
||||
* 'db' : messages in the DB directory
|
||||
* 'all' : all messages ('mem' + 'disk')
|
||||
* msg_location.DB : messages in the DB directory
|
||||
* msg_location.ALL : all messages (msg_location.MEM + msg_location.DISK)
|
||||
"""
|
||||
# only consider messages with a valid file_path so they can be sorted
|
||||
loc_messages = self.msg_gather(loc, require_file_path=True)
|
||||
@@ -492,7 +506,7 @@ class ChatDB(Chat):
|
||||
and message.file_path.parent.samefile(self.cache_path) # noqa: W503
|
||||
and message.file_path.exists()) # noqa: W503
|
||||
else:
|
||||
return len(self.msg_find([message], loc='cache')) > 0
|
||||
return len(self.msg_find([message], loc=msg_location.CACHE)) > 0
|
||||
|
||||
def msg_in_db(self, message: Union[Message, str]) -> bool:
|
||||
"""
|
||||
@@ -504,7 +518,7 @@ class ChatDB(Chat):
|
||||
and message.file_path.parent.samefile(self.db_path) # noqa: W503
|
||||
and message.file_path.exists()) # noqa: W503
|
||||
else:
|
||||
return len(self.msg_find([message], loc='db')) > 0
|
||||
return len(self.msg_find([message], loc=msg_location.DB)) > 0
|
||||
|
||||
def cache_read(self, glob: Optional[str] = None, mfilter: Optional[MessageFilter] = None) -> None:
|
||||
"""
|
||||
@@ -569,7 +583,7 @@ class ChatDB(Chat):
|
||||
self.cache_write([message])
|
||||
# remove the old one (if any)
|
||||
if old_path:
|
||||
self.msg_remove([str(old_path)], loc='db')
|
||||
self.msg_remove([str(old_path)], loc=msg_location.DB)
|
||||
# (re)add it to the internal list
|
||||
self.msg_add([message])
|
||||
|
||||
@@ -628,6 +642,6 @@ class ChatDB(Chat):
|
||||
self.db_write([message])
|
||||
# remove the old one (if any)
|
||||
if old_path:
|
||||
self.msg_remove([str(old_path)], loc='cache')
|
||||
self.msg_remove([str(old_path)], loc=msg_location.CACHE)
|
||||
# (re)add it to the internal list
|
||||
self.msg_add([message])
|
||||
|
||||
Reference in New Issue
Block a user