defined 'ConfigType' for configuration file type hinting

This commit is contained in:
juk0de 2023-08-12 10:21:09 +02:00
parent 056bf4c6b5
commit bc5e6228a6
3 changed files with 19 additions and 17 deletions

View File

@ -7,7 +7,7 @@ import sys
import argcomplete import argcomplete
import argparse import argparse
import pathlib import pathlib
from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency from .utils import terminal_width, print_tag_args, print_chat_hist, display_source_code, print_tags_frequency, ConfigType
from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data from .storage import save_answers, create_chat_hist, get_tags, get_tags_unique, read_file, dump_data
from .api_client import ai, openai_api_key, print_models from .api_client import ai, openai_api_key, print_models
from itertools import zip_longest from itertools import zip_longest
@ -28,7 +28,7 @@ def read_config(path: str):
def create_question_with_hist(args: argparse.Namespace, def create_question_with_hist(args: argparse.Namespace,
config: dict, config: ConfigType,
) -> tuple[list[dict[str, str]], str, list[str]]: ) -> tuple[list[dict[str, str]], str, list[str]]:
""" """
Creates the "SI request", including the question and chat history as determined Creates the "SI request", including the question and chat history as determined
@ -61,7 +61,7 @@ def create_question_with_hist(args: argparse.Namespace,
return chat, full_question, tags return chat, full_question, tags
def tag_cmd(args: argparse.Namespace, config: dict) -> None: def tag_cmd(args: argparse.Namespace, config: ConfigType) -> None:
""" """
Handler for the 'tag' command. Handler for the 'tag' command.
""" """
@ -69,7 +69,7 @@ def tag_cmd(args: argparse.Namespace, config: dict) -> None:
print_tags_frequency(get_tags(config, None)) print_tags_frequency(get_tags(config, None))
def model_cmd(args: argparse.Namespace, config: dict) -> None: def model_cmd(args: argparse.Namespace, config: ConfigType) -> None:
""" """
Handler for the 'model' command. Handler for the 'model' command.
""" """
@ -77,7 +77,7 @@ def model_cmd(args: argparse.Namespace, config: dict) -> None:
print_models() print_models()
def ask_cmd(args: argparse.Namespace, config: dict) -> None: def ask_cmd(args: argparse.Namespace, config: ConfigType) -> None:
""" """
Handler for the 'ask' command. Handler for the 'ask' command.
""" """
@ -90,7 +90,7 @@ def ask_cmd(args: argparse.Namespace, config: dict) -> None:
print(f"Usage: {usage}") print(f"Usage: {usage}")
def hist_cmd(args: argparse.Namespace, config: dict) -> None: def hist_cmd(args: argparse.Namespace, config: ConfigType) -> None:
""" """
Handler for the 'hist' command. Handler for the 'hist' command.
""" """
@ -104,7 +104,7 @@ def hist_cmd(args: argparse.Namespace, config: dict) -> None:
print_chat_hist(chat, args.dump, args.only_source_code) print_chat_hist(chat, args.dump, args.only_source_code)
def print_cmd(args: argparse.Namespace, config: dict) -> None: def print_cmd(args: argparse.Namespace, config: ConfigType) -> None:
""" """
Handler for the 'print' command. Handler for the 'print' command.
""" """

View File

@ -1,7 +1,7 @@
import yaml import yaml
import io import io
import pathlib import pathlib
from .utils import terminal_width, append_message, message_to_chat from .utils import terminal_width, append_message, message_to_chat, ConfigType
from typing import List, Dict, Any, Optional from typing import List, Dict, Any, Optional
@ -41,11 +41,11 @@ def save_answers(question: str,
answers: list[str], answers: list[str],
tags: list[str], tags: list[str],
otags: Optional[list[str]], otags: Optional[list[str]],
config: Dict[str, Any] config: ConfigType
) -> None: ) -> None:
wtags = otags or tags wtags = otags or tags
num, inum = 0, 0 num, inum = 0, 0
next_fname = pathlib.Path(config['db']) / '.next' next_fname = pathlib.Path(str(config['db'])) / '.next'
try: try:
with open(next_fname, 'r') as f: with open(next_fname, 'r') as f:
num = int(f.read()) num = int(f.read())
@ -66,14 +66,14 @@ def save_answers(question: str,
def create_chat_hist(question: Optional[str], def create_chat_hist(question: Optional[str],
tags: Optional[List[str]], tags: Optional[List[str]],
extags: Optional[List[str]], extags: Optional[List[str]],
config: Dict[str, Any], config: ConfigType,
match_all_tags: bool = False, match_all_tags: bool = False,
with_tags: bool = False, with_tags: bool = False,
with_file: bool = False with_file: bool = False
) -> List[Dict[str, str]]: ) -> List[Dict[str, str]]:
chat: List[Dict[str, str]] = [] chat: List[Dict[str, str]] = []
append_message(chat, 'system', config['system'].strip()) append_message(chat, 'system', str(config['system']).strip())
for file in sorted(pathlib.Path(config['db']).iterdir()): for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
if file.suffix == '.yaml': if file.suffix == '.yaml':
with open(file, 'r') as f: with open(file, 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader) data = yaml.load(f, Loader=yaml.FullLoader)
@ -97,9 +97,9 @@ def create_chat_hist(question: Optional[str],
return chat return chat
def get_tags(config: Dict[str, Any], prefix: Optional[str]) -> List[str]: def get_tags(config: ConfigType, prefix: Optional[str]) -> List[str]:
result = [] result = []
for file in sorted(pathlib.Path(config['db']).iterdir()): for file in sorted(pathlib.Path(str(config['db'])).iterdir()):
if file.suffix == '.yaml': if file.suffix == '.yaml':
with open(file, 'r') as f: with open(file, 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader) data = yaml.load(f, Loader=yaml.FullLoader)
@ -116,5 +116,5 @@ def get_tags(config: Dict[str, Any], prefix: Optional[str]) -> List[str]:
return result return result
def get_tags_unique(config: Dict[str, Any], prefix: Optional[str]) -> List[str]: def get_tags_unique(config: ConfigType, prefix: Optional[str]) -> List[str]:
return list(set(get_tags(config, prefix))) return list(set(get_tags(config, prefix)))

View File

@ -1,6 +1,8 @@
import shutil import shutil
from pprint import PrettyPrinter from pprint import PrettyPrinter
from typing import List, Dict from typing import List, Dict, Union
ConfigType = Dict[str, Union[str, Dict[str, Union[str, int]]]]
def terminal_width() -> int: def terminal_width() -> int: