From d8890220212e76e7a80665272476f78886e89585 Mon Sep 17 00:00:00 2001 From: juk0de Date: Fri, 1 Sep 2023 12:46:23 +0200 Subject: [PATCH] cmm: the 'tags' command now uses the new 'ChatDB' --- chatmastermind/main.py | 34 +++++++++++++++++++++------------- chatmastermind/utils.py | 5 ----- tests/test_main.py | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 7866179..3f31aee 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -7,10 +7,11 @@ import sys import argcomplete import argparse import pathlib -from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ChatType -from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data +from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, ChatType +from .storage import save_answers, create_chat_hist, get_tags_unique, read_file, dump_data from .api_client import ai, openai_api_key, print_models from .configuration import Config +from .chat import ChatDB from itertools import zip_longest from typing import Any @@ -57,12 +58,17 @@ def create_question_with_hist(args: argparse.Namespace, return chat, full_question, tags -def tag_cmd(args: argparse.Namespace, config: Config) -> None: +def tags_cmd(args: argparse.Namespace, config: Config) -> None: """ - Handler for the 'tag' command. + Handler for the 'tags' command. """ + chat = ChatDB.from_dir(cache_path=pathlib.Path('.'), + db_path=pathlib.Path(config.db)) if args.list: - print_tags_frequency(get_tags(config, None)) + tags_freq = chat.tags_frequency(args.prefix, args.contain) + for tag, freq in tags_freq.items(): + print(f"- {tag}: {freq}") + # TODO: add renaming def config_cmd(args: argparse.Namespace, config: Config) -> None: @@ -187,14 +193,16 @@ def create_parser() -> argparse.ArgumentParser: hist_cmd_parser.add_argument('-S', '--only-source-code', help='Print only source code', action='store_true') - # 'tag' command parser - tag_cmd_parser = cmdparser.add_parser('tag', - help="Manage tags.", - aliases=['t']) - tag_cmd_parser.set_defaults(func=tag_cmd) - tag_group = tag_cmd_parser.add_mutually_exclusive_group(required=True) - tag_group.add_argument('-l', '--list', help="List all tags and their frequency", - action='store_true') + # 'tags' command parser + tags_cmd_parser = cmdparser.add_parser('tags', + help="Manage tags.", + aliases=['t']) + tags_cmd_parser.set_defaults(func=tags_cmd) + tags_group = tags_cmd_parser.add_mutually_exclusive_group(required=True) + tags_group.add_argument('-l', '--list', help="List all tags and their frequency", + action='store_true') + tags_cmd_parser.add_argument('-p', '--prefix', help="Filter tags by prefix") + tags_cmd_parser.add_argument('-c', '--contain', help="Filter tags by contained substring") # 'config' command parser config_cmd_parser = cmdparser.add_parser('config', diff --git a/chatmastermind/utils.py b/chatmastermind/utils.py index bd80e4f..e6eeb97 100644 --- a/chatmastermind/utils.py +++ b/chatmastermind/utils.py @@ -78,8 +78,3 @@ def print_chat_hist(chat: ChatType, dump: bool = False, source_code: bool = Fals print(message['content']) else: print(f"{message['role'].upper()}: {message['content']}") - - -def print_tags_frequency(tags: list[str]) -> None: - for tag in sorted(set(tags)): - print(f"- {tag}: {tags.count(tag)}") diff --git a/tests/test_main.py b/tests/test_main.py index db5fcdb..23c3d00 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -227,7 +227,7 @@ class TestCreateParser(CmmTestCase): mock_add_subparsers.assert_called_once_with(dest='command', title='commands', description='supported commands', required=True) mock_cmdparser.add_parser.assert_any_call('ask', parents=ANY, help=ANY, aliases=ANY) mock_cmdparser.add_parser.assert_any_call('hist', parents=ANY, help=ANY, aliases=ANY) - mock_cmdparser.add_parser.assert_any_call('tag', help=ANY, aliases=ANY) + mock_cmdparser.add_parser.assert_any_call('tags', help=ANY, aliases=ANY) mock_cmdparser.add_parser.assert_any_call('config', help=ANY, aliases=ANY) mock_cmdparser.add_parser.assert_any_call('print', help=ANY, aliases=ANY) self.assertTrue('.config.yaml' in parser.get_default('config'))