From 968d858580818f4650724c72c3f49ec53b8ee290 Mon Sep 17 00:00:00 2001 From: juk0de Date: Wed, 20 Sep 2023 09:04:40 +0200 Subject: [PATCH] print_cmd: added option to print latest message --- chatmastermind/commands/print.py | 50 ++++++++++++++++++++++---------- chatmastermind/main.py | 4 ++- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/chatmastermind/commands/print.py b/chatmastermind/commands/print.py index 3d2b990..d2a66a1 100644 --- a/chatmastermind/commands/print.py +++ b/chatmastermind/commands/print.py @@ -3,25 +3,43 @@ import argparse from pathlib import Path from ..configuration import Config from ..message import Message, MessageError +from ..chat import ChatDB + + +def print_message(message: Message, args: argparse.Namespace) -> None: + """ + Print given message according to give arguments. + """ + if args.question: + print(message.question) + elif args.answer: + print(message.answer) + elif message.answer and args.only_source_code: + for code in message.answer.source_code(): + print(code) + else: + print(message.to_str()) def print_cmd(args: argparse.Namespace, config: Config) -> None: """ Handler for the 'print' command. """ - fname = Path(args.file) - try: - message = Message.from_file(fname) - if message: - if args.question: - print(message.question) - elif args.answer: - print(message.answer) - elif message.answer and args.only_source_code: - for code in message.answer.source_code(): - print(code) - else: - print(message.to_str()) - except MessageError: - print(f"File is not a valid message: {args.file}") - sys.exit(1) + # print given file + if args.file is not None: + fname = Path(args.file) + try: + message = Message.from_file(fname) + if message: + print_message(message, args) + except MessageError: + print(f"File is not a valid message: {args.file}") + sys.exit(1) + # print latest message + elif args.latest: + chat = ChatDB.from_dir(Path(config.cache), Path(config.db)) + latest = chat.msg_latest(loc='disk') + if not latest: + print("No message found!") + sys.exit(1) + print_message(latest, args) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 418dafc..62c4539 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -114,7 +114,9 @@ def create_parser() -> argparse.ArgumentParser: help="Print message files.", aliases=['p']) print_cmd_parser.set_defaults(func=print_cmd) - print_cmd_parser.add_argument('-f', '--file', help='Print given message file', required=True, metavar='FILE') + print_group = print_cmd_parser.add_mutually_exclusive_group(required=True) + print_group.add_argument('-f', '--file', help='Print given message file', metavar='FILE') + print_group.add_argument('-l', '--latest', help='Print latest message', action='store_true') print_cmd_modes = print_cmd_parser.add_mutually_exclusive_group() print_cmd_modes.add_argument('-q', '--question', help='Only print the question', action='store_true') print_cmd_modes.add_argument('-a', '--answer', help='Only print the answer', action='store_true')