hist_cmd: implemented '--convert' option
This commit is contained in:
@@ -1,13 +1,53 @@
|
||||
import sys
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from ..configuration import Config
|
||||
from ..chat import ChatDB
|
||||
from ..message import MessageFilter
|
||||
from ..message import MessageFilter, Message
|
||||
|
||||
|
||||
def hist_cmd(args: argparse.Namespace, config: Config) -> None:
|
||||
msg_suffix = Message.file_suffix_write # currently '.msg'
|
||||
|
||||
|
||||
def convert_messages(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Handler for the 'hist' command.
|
||||
Convert messages to a new format. Also used to change old suffixes
|
||||
('.txt', '.yaml') to the latest default message file suffix ('.msg').
|
||||
"""
|
||||
chat = ChatDB.from_dir(Path(config.cache),
|
||||
Path(config.db))
|
||||
# read all known message files
|
||||
msgs = chat.msg_gather(loc='disk', glob='*.*')
|
||||
# make a set of all message IDs
|
||||
msg_ids = set([m.msg_id() for m in msgs])
|
||||
# set requested format and write all messages
|
||||
chat.set_msg_format(args.convert)
|
||||
# delete the current suffix
|
||||
# -> a new one will automatically be created
|
||||
for m in msgs:
|
||||
if m.file_path:
|
||||
m.file_path = m.file_path.with_suffix('')
|
||||
chat.msg_write(msgs)
|
||||
# read all messages with the current default suffix
|
||||
msgs = chat.msg_gather(loc='disk', glob='*{msg_suffix}')
|
||||
# make sure we converted all of the original messages
|
||||
for mid in msg_ids:
|
||||
if not any(mid == m.msg_id() for m in msgs):
|
||||
print(f"Message '{mid}' has not been found after conversion. Aborting.")
|
||||
sys.exit(1)
|
||||
# delete messages with old suffixes
|
||||
msgs = chat.msg_gather(loc='disk', glob='*.*')
|
||||
for m in msgs:
|
||||
if m.file_path and m.file_path.suffix != msg_suffix:
|
||||
m.rm_file()
|
||||
print(f"Successfully converted {len(msg_ids)} messages.")
|
||||
if len(msgs):
|
||||
print(f"Deleted {len(msgs)} messages with deprecated suffixes.")
|
||||
|
||||
|
||||
def print_chat(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Print the DB chat history.
|
||||
"""
|
||||
|
||||
mfilter = MessageFilter(tags_or=args.or_tags,
|
||||
@@ -21,3 +61,13 @@ def hist_cmd(args: argparse.Namespace, config: Config) -> None:
|
||||
chat.print(args.source_code_only,
|
||||
args.with_tags,
|
||||
args.with_files)
|
||||
|
||||
|
||||
def hist_cmd(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Handler for the 'hist' command.
|
||||
"""
|
||||
if args.print:
|
||||
print_chat(args, config)
|
||||
elif args.convert:
|
||||
convert_messages(args, config)
|
||||
|
||||
Reference in New Issue
Block a user